SQL常用语法汇总

  今天开发任务稍微松缓一点,所以有时间在园里面看了几篇博客,看到有人写了sql语法汇总,开发过程中用数据库比较平凡,所以决定花点时间总结一下平时所学所用,也以便日后工作中不记得时查阅。T-sql也是一门查询语言,内容比较多,一时间无法全部总结全面,之后我会随着日后工作中常用的抽时间来一一总结。

一、创建表与删除表

  1.1代码

1.创建表:
create table Person5(Id int not null,Name nvarchar(50),Age int null)
2.删除表:
drop table person4;

  1.2分析

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

  2)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(数量)。

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

posted @ 2012-06-15 16:55  tangrongyue  Views(274)  Comments(0Edit  收藏  举报