您的位置:首页 > 数据库

SQL语句之语法汇总

2012-06-18 17:09 316 查看
一.创建表、修改表与删除表

  1.1代码



1.创建表:
create table Person5(Id int not null,Name nvarchar(50),Age int null)
2.修改表:
alter table T_Employee add FSubCompany varchar(20);   --表中添加分公司
alter table T_Employee add FDepartment varchar(20);   --表中添加部门
3.删除表:
drop table person4;




1.2分析

  1)create实现在代码中直接生成表,表的字段可以再表名(Person5)后面逐一定义,格式为:字段名 类型 为空与否;

  2)alter是在表中添加新的字段,定义其名称和类型;

  3)drop将整个表删掉。

二.数据的添加、更新与删除

  2.1代码



1.添加数据:
--1)一般添加:
insert into Person1(Id,Name,Age)values(3,'小王',20);
--2)id设置主键后,自动生成,id项不必insert操作:
insert into Person3(Name,Age)values('lily',20);   --正确
insert into Person3 values('john',30);           --正确
--3)id设置主键后,自动生成,若使用guid类型,需要程序员干预生成:
create table Person5(Id uniqueidentifier not null,Name nvarchar(50),Age int null);
insert into Person4(Id,Name,Age)values(newid(),'tom',30);     --正确
insert into Person4(Name,Age)values('tom',30);            --错误






2.更新数据
--1)无条件更新:
update Person1 set Age=20;                 --更新一个列/字段
update Person1 set Age=30,Name='xiaoxiao'  --更新多个列/字段
update Person1 set Age=Age+1;
--2)条件更新
update Person1 set NickName=N'青年人' where Age>=20;
update Person1 set NickName=N'二十岁' where Age=20;                --等于 只有一个等号
update Person1 set NickName=N'非二十岁' where Age<>20;                --不等于
update Person1 set NickName=N'非二十岁' where (Age>20 and Age<30)or Age=20;   --不等于 and or not




3.删除表中某些内容
--1)删除表中的全部数据
delete from Person1;
--2)删除表中符合条件的数据
delete from Person1 where Age>=30;


 2.2分析

  1)name等字段可在属性中设置默认值,当不添加时会使用默认值;

   guid做主键,数据插入的顺序与实际排列的顺序不同;Id项需要添加进去后,点击sql执行,才能将guid添加进去!

    在数据添加第二种情况下,两种写法都正确!即多项insert时后面可省略字段名称,但不建议如此书写!

    有些人喜欢讲into去掉,直接用inset Person(...);也不会报错,但加上into更规范一些!

  2)执行update时,如果没有where条件,则将表中所有相应字段的所有值都更新;另外,update可以执行计算语句,如:update Person1 set Age=Age+1;

   注意在sql语句中逻辑运算符的使用:等于号为=,不等于号为<>,并且为and,或者为or!

    当sql中出现中文时,在中文字符串前加上N,如 NickName=N'青年人'。

  3)与前文中drop方法不同,drop直接将表删除;而delete实现对表中满足特定条件(where Age>=30)数据的删除操作。

三.select查询

  3.1代码



1.简单select
select FName from T_Employee;                   --单字段查询 
select FNumber,FName,FAge,FSalary from T_Employee;        --多字段查询
select * from T_Employee;                       --所有字段查询
2.条件select
select FName,FSalary from T_Employee where FSalary<5000;     --按工资条件查询
select * from T_Employee where FSalary<5000;
3.可以给列加“别名”,运算操作也可以执行:
select FName as 姓名 ,FSalary+10000 as 月薪 from T_Employee  where FSalary<5000;
4.范围
a)多个单值的等价方式
select * from T_Employee where FAge=23 or FAge=26 or FAge=28;  --or条件选择,等价于下方
select * from T_Employee where FAge in (23,26,28);
b)范围值的等价方式
select * from T_Employee where FAge>=20 and FAge<=25;          --范围,等价于下方
select * from T_Employee where FAge between 20 and 25;      --包含边界点!,不好用,当条件为FAge>20 and FAge<25时即不可用!




  3.2分析:

  select就是从某表中,获取到满足条件的结果,涉及到单条件及多条件语句,查询结果显示也可利用别名等进行设定。

四.SQL聚合函数

  4.1代码:

select MAX(FSalary) from T_Employee ;
select SUM(FSalary) from T_Employee where FAge>25;


  4.2分析:

  1)SQL聚合函数:MAX(最大值)、MIN(最小值)、AVG(平均值)、SUM(和)、COUNT(数量)。

  3)聚合函数不是实际意义上对表的聚合;而是对查询结果的聚合。

  2)代码为:查询最高工资、25岁以上工资之和。

一.数据分组group by(聚合函数)

  1.1代码:



1.group by简单语句
--统计每个年龄段的人数
1). select FAge,count(*)as 人数 from T_Employee group by FAge;   --right
2). select FAge,FSalary,count(*) from T_Employee group by FAge;   --error
--程序报错:选择列表中的列 'T_Employee.FSalary' 无效,因为该列没有包含在聚合函数或 GROUP BY 子句中。
--分析:按照age分组,同一个age可能有好几个工资水平,计算机无法确定显示哪个而报错!
3). select FAge,max(FSalary)as 最高工资,count(*) as 人数 from T_Employee group by FAge; --right!
--分析:显示出age每组的最高工资即可




2.group by与where、having1). select FAge,count(*) from T_Employee where count(*)>1 group by FAge;--error:
2). select FAge,count(*) from T_Employee group by FAge having count(*)>1   --right:


  1.2分析:

  1)group by是按照某一个字段来对table中信息进行整合分组,在代码"group by简单语句"中,1、3正确,而2错误,可以做如下分析:

  我们以FAge进行分组,即将每个年龄段的信息进行整合,获取到的结果可能是23岁的有abc三个人,工资为100、200、300,那么只能取其中某一个工资水平显示,如

max(FSalary),而不能直接写FSalary,否则计算机必然无法确定显示哪个而报错!参考下图,为“3)”的显示结果:




  2)聚合语句不能出现在 WHERE 子句中,除非有having ,因而语句"1)"错误,而语句"2)"出现having,正确,分析:having对获取到的每组信息,再进行过滤,取得人数超过1的组(结果显示:25岁 3人等)
  3)既然存在上述情况,那是不是having可以取代where语句呢?答案是否定的,参考代码:

a). select FAge, count(*) from T_Employee where FSalary>2000 group by FAge;   --right
b). select FAge, count(*) from T_Employee group by FAge having FSalary>2000;  --error:  
c). select FAge, count(*) from T_Employee group by FAge having FAge>25;       --right


  分析:"a)"中使用where,可以实现对分组前的数据进行过滤;即如果25岁组中有3人(3000,2000,4000),那么使用where语句后,结果为25 2人。
  而having只能对获得的组信息进行过滤,即对"select FAge, count(*) from T_Employee group by FAge"的结果进行过滤,此时无法再对FSalary去筛选,无法实现where实现的功能,因而"b)"错误;
  但如果对年龄刷选having FAge>25就正确,因为select结果中包含FAge字段,可以进行简单where对年龄的筛选,即"c)"正确!  

二.数据排序

  2.1代码:  

1). 单字段排序:
select * from T_Employee order by FAge ASC;            --对年龄进行排序
2). 多字段排序
select * from T_Employee order by FAge ASC ,Fsalary ASC;    --进一步排序(年龄相同的,按照工资排序)!前面的优先级高!
3). 对过滤后结果进行排序:
select * from T_Employee where FAge>24 order by FAge DESC;


  2.2 分析

  1)排序条件可以有多个,但越前优先级越高;

  2)ASC升序(default)、 DESC降序,平时使用排序时,ASC方式虽为默认,但最好不要省略,增强代码可读性

三.通配符

  3.1代码

1). 通配符_,单个字符:
select * from T_Employee where FName LIKE '_erry';
2). 通配符%,零或多个字符:
select * from T_Employee where FName like '%n%';      --FName中含有字母n的
select * from T_Employee where FNumber like 'DEV%';   --FNumber中以DEV开始的


  3.2分析

  注意区分Like中 单字符 不定字符用法的区别!

四. 空值处理

  4.1代码:



1)空值查询
select 'abc'+'123'     --结果:abc123
select ''+'123'        --结果:123
select null+'123'      --结果:NULL
select * from T_Employee where FName=null;        --error  没有结果,可以尝试<null  >null <>null均无结果!
select * from T_Employee where FName is null;     --right  查询到name为null的结果!
select * from T_Employee where FName is not null; --error  查询到所有name不为null的结果! 
2)空值的处理
update T_Employee set FSubCompany=null,FDepartment='Sales'where FNumber='SALE004';  --设定某项为null
select isnull(FSubCompany,'未知子公司') from T_Employee                   --字段为null的则显示默认值'未知子公司'




4.2分析:
  1)数据库中,一个列如果没有指定值,那么值就是null,此处的null与c#中的null不同,数据库中表示“不知道”,而不是“没有”
  2)可以尝试FName!=null、FName=<null、FName=>null、FName=<>null均无结果!
五.限制结果集的行数

  5.1 代码:

1). select top 5* from T_Employee                --显示表中前五个
2). select top 5* from T_Employee order by FSalary Desc  --按照工资倒序排列,取前五个(最高的五个)


  5.2 分析

  top 5* 表示表中前五条信息,由升序或降序来决定顺序。

  top 5 FSarray表示工资的前五个,由升序或降序来决定顺序。

六.子查询

  6.1代码:

select top 5* from T_Employee
where FNumber not in (select top 5 FNumber from T_Employee order by FSalary Desc)
order by FSalary Desc;


  6.2 分析

  select语句嵌套,实现子查询,即在"select得到的表"中进行二次查询! 

 select top 5 FNumber from T_Employee order by FSalary Desc得到前五个数字;在此条件再用where查询!
七. 去掉数据重复distinct
  7.1代码


1). select FDepartment from T_Employee;                    --会有重复,完全显示查询结果
2). select distinct FDepartment from T_Employee;           --没有重复
3). select FDepartment,FSubCompany from T_Employee;        --会有重复,完全显示查询结果
4). select distinct FDepartment,FSubCompany from T_Employee;   --仅消除完全重复的行!


  7.2 分析

  参考,当table为下表时  

  


执行"1)",完全显示出table的所有FDepartment 内容,字段会有重复,如红线标注区域;

执行"2)",显示FDepartment所有分类,没有重复

执行"3)",完全显示出table的所有FDepartment,FSubCompany 内容,会有重复,完全显示查询结果

执行"4)",仅消除完全重复的行。当FDepartment,FSubCompany数据完全相同时,数据合并,如的二个红色框中,会合并成一条数据,而第一个框中数据有差异不会合并,

八. union与union all
  8.1代码:




1). union会将重复数据合并9+6->14条!"表1中5条数据"+"表2中6条数据",结果可能小于11,因为将重复的FName字段合并
select FName from TempEmployee  !
union
select FName from T_Employee;
2). union all不考虑重复数据,完全合并5+6->11条!!
select FName from TempEmployee
union all
select FName from T_Employee;
3).其他关于union的示例(只要对应字段类型一致(不严谨)即可)
select FName,FAge from TempEmployee
union
select FName,FAge from T_Employee;

select FName,FAge ,0 from TempEmployee
union
select FName,FAge ,1 from T_Employee;

select FName,FAge ,FIdCardNumber from TempEmployee
union
select FName,FAge,FsubCompany from T_Employee;

--可用字段'临时工,无部门'补齐,实现union操作

select FIdCardNumber,FName,FAge ,'临时工,无部门' fromTempEmployee
union
select FNumber,FName,FAge,FDepartment from T_Employee;
4).错误:
select FName,FAge ,0 from TempEmployee --"0和FDepartment "类型不能转换
union
select FName,FAge,FDepartment from T_Employee;




  8.2分析

  1)union合并会消除重复项,而union all不考虑重复,会将数据完全合并累加

  2) union时,需要两个table中要union的结果集中,具有相同的列数,且列类型要相容才可以union

  3)union需要进行重复值扫描,效率比较低,所以如果不是确定要进行重复行合并,那么就使用union all

  4)例子

    a.在T_Employee中,(报表)查询员工最低年龄、最高年龄?参考:  



select '正式员工最高年龄', max(FAge) from T_Employee
union all
select '正式员工最低年龄', min(FAge) from T_Employee
union all
select '临时工最高年龄', max(FAge) from TempEmployee
union all
select '临时工最低年龄', min(FAge) from TempEmployee;




    b.查询正式工信息:工号、工资,在最后一行加上员工工资额合计,参考:

select FNumber,FSalary from T_Employee
union
select '员工工资合计:',sum(FSalary) from T_Employee


(待续...)


附:创建的几个table



Table  T_Employee

--创建表

drop table T_Employee
create table T_Employee(FNumber varchar(20),FName varchar(20),FAge int,FSalary numeric(10,2),PRIMARY KEY(FNumber));
insert into T_Employee(FNumber,FName,FAge,FSalary)values('DEV001','Tom',25,8300);
insert into T_Employee(FNumber,FName,FAge,FSalary)values('DEV002','Jerry',28,4300);
insert into T_Employee(FNumber,FName,FAge,FSalary)values('HR001','Jane',25,5300);
insert into T_Employee(FNumber,FName,FAge,FSalary)values('HR002','Tina',26,6000);
insert into T_Employee(FNumber,FName,FAge,FSalary)values('IT001','Smith',23,3300);
insert into T_Employee(FNumber,FName,FAge,FSalary)values('IT002','Tom',27,9300);
insert into T_Employee(FNumber,FName,FAge,FSalary)values('SALE001','John',24,3300);
insert into T_Employee(FNumber,FName,FAge,FSalary)values('SALE002','Kerry',22,5500);
insert into T_Employee(FNumber,FName,FAge,FSalary)values('SALE003','Stone',25,4500);
insert into T_Employee(FNumber,FName,FAge,FSalary)values('SALE004','john',23,5500);

--修改表:

alter table T_Employee add FSubCompany varchar(20); --分公司
alter table T_Employee add FDepartment varchar(20); --部门
alter table T_Employee add FInDate datetime; --入职日期
update T_Employee set FInDate='2010-09-07 12:00:00', FSubCompany='Beijing',FDepartment='development'where FNumber='DEV001';
update T_Employee set FInDate='2011-09-07 12:00:00', FSubCompany='Shenzhen',FDepartment='development'where FNumber='DEV002';
update T_Employee set FInDate='2012-09-07 12:00:00', FSubCompany='Beijing',FDepartment='HumanResource'where FNumber='HR001';
update T_Employee set FInDate='2009-09-07 12:00:00', FSubCompany='Beijing',FDepartment='HumanResource'where FNumber='HR002';
update T_Employee set FInDate='2010-09-07 12:00:00', FSubCompany='Shenzhen',FDepartment='InfoTech'where FNumber='IT001';
update T_Employee set FInDate='2011-09-07 12:00:00', FSubCompany='Beijing',FDepartment='InfoTech'where FNumber='IT002';
update T_Employee set FInDate='2011-09-07 12:00:00', FSubCompany='Beijing',FDepartment='Sales'where FNumber='SALE001';
update T_Employee set FInDate='2012-09-07 12:00:00', FSubCompany='Shenzhen',FDepartment='Sales'where FNumber='SALE002';
update T_Employee set FInDate='2009-09-07 12:00:00', FSubCompany='Beijing',FDepartment='Sales'where FNumber='SALE003';
update T_Employee set FInDate='2009-09-07 12:00:00', FSubCompany='Shenzhen',FDepartment='Sales'where FNumber='SALE004';

--创建Table TempEmployee

create table TempEmployee(FIdCardNumber varchar(20),FName varchar(20),FAge int not null);

insert into TempEmployee(FIdCardNumber,FName,FAge)values('12345601','张',20);

insert into TempEmployee(FIdCardNumber,FName,FAge)values('12345602','Tom',24);

insert into TempEmployee(FIdCardNumber,FName,FAge)values('12345603','李',18);

insert into TempEmployee(FIdCardNumber,FName,FAge)values('12345604','孙',26);

insert into TempEmployee(FIdCardNumber,FName,FAge)values('12345605','周',30);

insert into TempEmployee(FIdCardNumber,FName,FAge)values('12345606','武',27);


内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: