关于sql server语法的部分总结

来源:互联网 发布:sql2005数据库安装教程 编辑:程序博客网 时间:2024/05/16 07:17

 

 

Insert   插入数据行

 Insert  [into]  <表名>  [列名] values  <值列表>

Eg: insert into student (name,address,grade,email)

    Values(‘张三,‘上海’,6,‘ZQC@sohu)

Insert 语句不能为标识列指定值,自增长的,不能插入

 

一次插入多行数据的三种方法:

1.    预先创建好表

Insertinto 新表 (列名)select 列名from 原始表

  Eg: insert into Tongxuelu (姓名,地址,电子邮件)

      Select  name,address,email  from  student

2.    运行过程中直接生成新表

Select 表名.列名 into新表 from原始表

  Eg:select name,  address, email into tongxuelu from student

插入标题列的语法:

Select 表名.列名 identity (数据类型,标识种子,标识增长量AS 列名 into新表 from 原始表  

    Eg:select name, address, email, identity (int,1,1)  as studentID  into tongxuelu  from  student

3.    Union语句用于将两个不同的数据或查询结果组合成一个新的结果集。

      Eg:insert  student (name,grade ,sex)

         Select  ‘张可7union

         Select ‘李杨’,40 union

         Select  ‘杨晓’,20 union

         Select  ‘汤美’,3,0 union

         Select  ‘陈刚’,4,1 union

这样的结果与insert。。。select的结果是一样的。

 

 

1.delete

delete  from  <表名>  [where<删除条件>]

  eg:delete  from  student  where  name=‘张三’

 delete  from 连用,中间不能插入内容。

2Truncate table   

   Truncate  table  <表名>

     Eg:truncate  table  student

   注:此语法用于删除表中的所有行,且不能用于有外键约束应用的表,否则需要使用delete语句。

 

Update

Update  <表名> set  <列名=更新值>  [where<更新条件>](多个更新条件用and/or连接)

   Eg:update  student  set  score = score + 5  where score <=95 and /or score >=70

 

 

*查*

select <列名>from <表名> [where<查询条件表达式>] [order by <排序的列名>[ascdesc]]

 

1.查询所有的数据行和列

Select * from student(表名)

 2.查询部分行列——条件查询

      Eg: select  code,name,address   from   student  where address=‘河南新乡’

  3.在查询中使用列名

   AS子句可以用来改变结果集列的名称,也可以为组合或者计算出的列指定名称,还有一种情况是让标题列的信息更易懂。

      Eg: select code as 学员编号,name as学员姓名,address as学员地址   from  student  where address =‘河南新乡’

4.查询空行

采用“is null”或者“is not null

   Eg: select name from student where email is null/is not null

5.在查询中使用常量列

   Eg: select 姓名=name,地址=address,‘河北新龙’ as学校名称  from  student

      查询输出多了一列“学校名称”,该列的所有数据都是“河北新龙”。

6.查询返回限制的行数

 Eg:  select   top 5/20 percent   name,address/*  from student  where sex=1

7.查询排序

    Eg: Select  studentID  as 学员编号,(score*0.9+5 as综合成绩 from score where (score*0.9+5)>60  order by score  asc/desc

8.在查询中使用函数

  函数分为:

a.     字符串函数: charindexlenltrimrtrimreplacestuff

b.    日期函数:     getdate  dateadd  datediff  datename  datepart

c.     数学函数:    ceiling  floor  round

d.    系统函数:    convert

9.模糊查询

 a. like

   eg: select * from card where id like ‘00[^8]%[a,c]’

      [^] 不在括号中所指定范围内的任意一个字符

      [ ] 括号中所指定范围内的一个字符

 b. between…and

   eg:select * from score where score between 60 and 80

      包括6080

c. in/not in 在列举的条件内进行查询

  eg:select name as 学员姓名 from student where address in/not in  (‘北京上海广州’)

10.聚合函数

   a.sum()

     eg:select sum (sales) as 销售业绩 from titles where type = ‘business’

   b.avg(平均数)

     eg:select avg(score) as 平均成绩 from score where score >=60

   c.max(最大值),min(最小值)

      eg:select  avg(score) as 平均成绩,max(score) as 最高分,minscoreas最低分  from score  where score >=60

 d.count非空值的计数,最后得到的是一个数值

    select  count(列名/*) from表名  where 查询条件

   eg:select count (*)  as  及格人数 from score where score>=60

 

 

 

0 0
原创粉丝点击