SQL 语句总结

表名:Table3    包含列:ID,name,old,city

 

新建数据库:create database 数据库名

新建表:create table 表名(列1 数据类型   not null [非空] PRIMARY KEY IDENTITY[自增主键],....)

删除数据库:drop database 数据库名

删除表:drop table 表名

 

 

表中插入列   :alter  table  表名  add  列名  列的数据类型

表中删除列   :alter  table  表名  drop  column  列名

表中修改列的数据类型  :alter  table  表名  alter  column  列名  新的数据类型

 

选取  :select 列名 from 表名               

      select * from 表名    *代表选取所有列 

  1:排序输出

      select * from 表名 order by old desc,name desc      按照old逆序排列,若相等按照name逆序排列

  2:限定输出数量

      select  top  number  |  percent  列名1,列名2..  from  表名

      (1)选取前5条记录

        select top 5 * from 表名

      (2)选取50%的记录

        select top 50 percent * from 表名

  3:不重复输出一列

      select distinct 列名 from 表名

  4:计算输出

      select  编号,数量,单价,总价=(isnull(数量,0)*单价) from price

        (isnull()函数,判定是否为null,是的话返回默认值)

  5.分组条件输出

      select 单价 from price group by 单价 having sum(单价)>50

      select 分组关键字 from 数据库名称 group by  分组关键字 having 条件

  6.嵌套查询

      select * from 网站职员表 where 毕业院校 in (select 毕业院校 from 网站职员表 group by 毕业院校 having sum(工资)>5000)

      --返回总工资大于5000的职员信息

      select * from 网站职员表 where exists(select * from 网站职员表 where 毕业院校=‘哈工大’)

      --exisct()返回值为true 则查询全部,括号中查询出数据则exisct()返回true  意思是  只要后面的查询语句查到数据,那么就查询全部

      --本句意思:有哈工大的职员 则查询所有的

      select * from 网站职员表 where 工资>any(select 工资 from 网站职员表 where 毕业院校=‘哈工大’)

      --查询工资大于哈工大的工资的资料(大于其中一个就可以)  any(查询语句) :任意一个

      select * from 网站职员表 where 工资>all(select 工资 from 网站职员表 where 毕业院校=‘哈工大’)

      --查询工资大于哈工大所有人的工资的资料(必须大于所有值)  all(查询语句) :所有值

  7.查询数据放入另一个表中(没有时创建)

    select * into 第二网站职员表 from 网站职员表 where 工资>2800

    --把网站职员表中工资大于2800的员工插入第二网站职员表中

  

 

更新  :update  表名  set  列名 = 值,列名 = 值  where  条件

  1:更新一列

      update  表名  set  列名 = 值  where  条件

  2:更新多列

      update  表名  set  列名 = 值,列名 = 值....  where  条件

  3:内联更新

      update Table2 set table2.城市=table3.city from Table2 inner join Table3 on Table2.学号=Table3.ID

      (表2和表3中 学号 和 id 相等时 表2的 城市 等于表三的 city )

 

 

插入  :

  1:插入一行

      insert into 表名 values (值1,值2,值3....)

  2:插入指定列

      insert into Table3 (列名1,列名2....)  values  (值1,值2....)

  3:插入和选取的组合

      insert into Table1 select * from Table2

      (将表2查找到的数据插入到表1中)

 

删除

  1:删除某行或者某些行

      delete from 表名 where 条件

  2:删除所有行

      delete from 表名

 

where  条件  : 

  1:条件的  and  和  or

      select * from 表名 where old=23 and city='henan' or old=21

  2:条件的  like

      select * from 表名 where 列名 like 值

      select * from 表名 where 列名 not like 值

      (1): 通配符      注:SQL 通配符必须与 LIKE 运算符一起使用。

 

                 

        

        where city like 'ne%'

        where city like ‘ne_’

        where city like '[n,e,r]%'

  3:条件的 in    注:IN 操作符允许我们在 WHERE 子句中规定多个值。

      select * from 表名 where 列名 in (值1,值2,值3....)

  4:条件的 between ... and ...    注:选取两个值之间的数据

      where 列名 between 值1 and 值2

  or   where 列名 not between 值1 and 值2      --范围之外

  5:判定null

      where 列名 is null

      where 列名 is not null

别名  (Alias)

  1:表的别名

      select 别名.列名 from 表名 as 别名

  2:列的别名

      select 列名 as 别名 from 表名            (输出的列名会是别名)

 

内联  join

  1:普通引用

    select 表1.列,表二.列  from 表1,表2   where 表1.主键=表2.主键

  2:join 内联

    select 表1.列,表二.列 from 表1  inner join 表2  on  表1.主键=表2.主键

  3:不同的join

      

UNION 操作符:用于合并两个或多个 SELECT 语句的结果集,不允许重复。

    select 列名 from 表1

    union

    select 列名 from 表2

UNION ALL:允许重复。

select into 语句:表的备份   从一个表中查找数据,放入另一个表中

    select * into HN from Table2 where Table2.城市='河南'      (into 后的表  会新建)

    内联复制

    select * into CY from table2 inner join table3 on table2.学号=table.ID

    

    

    

 

    

 

posted @ 2014-06-04 14:36  nylg-haozi  阅读(155)  评论(0编辑  收藏  举报