码迷,mamicode.com
首页 > 数据库 > 详细

2. SQL -- 查询表,创建表,插入数据到表

时间:2015-03-18 06:37:20      阅读:205      评论:0      收藏:0      [点我收藏+]

标签:数据库   create   where   成绩表   

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)    

本文出自 “Ricky's Blog” 博客,转载请与作者联系!

2. SQL -- 查询表,创建表,插入数据到表

标签:数据库   create   where   成绩表   

原文地址:http://57388.blog.51cto.com/47388/1621602

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!