(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)