您的位置:首页 > 数据库

2. SQL -- 查询表,创建表,插入数据到表,更新数据,删除数据

2015-03-17 21:04 666 查看
(1):查询一个数据库中是否存在某个表(两种方式):假设表名为table_name

if exists(select * from sysobjects where name='table_name')
drop table table_name

if object_id('table_name') is not null
drop table table_name

同样的操作也可用来判断数据库是否存在!

(2):对表的一些实例操作:
创建一个表的实例:(学生成绩表:grade_table)
if exists(select * from sysobjects where name = 'grade_table')
drop table grade_table go
create table grade_table
(
stuID varchar(20),
courseID varchar(20),
grade int
) (3):插入表中数据:(学生成绩表:grade_table) insert into grade_table values('10001','001','85') insert into grade_table values('10002','001','95')(4): 更新表中数据: update grade_table set grade=70 where stuID='10001' and courseID='001'(5): 删除表中数据: delete grade_table where stuID='10001' and courseID='001'(6): 新建一个与student_table相同的表student然后插入student_table中查询的数据,一般此方法可用来导一些数据 create table student_table
(
stuID varchar(20),
courseID varchar(20),
grade int
)

create table student
(
stuID varchar(20),
courseID varchar(20),
grade int
)

insert into student_table values('10001','001','85')
insert into student_table values('10002','001','95')

insert into student
select * from student_table as s1
where s1.stuID not in (select stuID from student)
(7) : 更新数据

Update 语句

Update 语句用于修改表中的数据。

语法:

UPDATE 表名称 SET 列名称 = 新值 WHERE 列名称 = 某值(8):delete

DELETE 语句

DELETE 语句用于删除表中的行。

语法

DELETE FROM 表名称 WHERE 列名称 = 值
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
相关文章推荐