SQL语句实例

1. 创建一个新的数据库
Create  database  newdatabase
1) 新创建一个数据库:

CREATE DATABASE newdb
ON
(
  NAME = newdb_dat,                           --文件的逻辑名
  FILENAME = 'c: \newdb.mdf',           --文件存放的物理位置
  SIZE = 5MB,                                       --文件的初始大小
  MAXSIZE = 100MB,                           --文件最大存储空间
  FILEGROWTH = 5MB                       --文件增长大小
)
LOG ON
(
  NAME = newdb_log, 
  FILENAME = 'c: \newdb.ldf',
  SIZE = 10MB,
  MAXSIZE = 100MB,
  FILEGROWTH = 10MB
)
GO

2) 创建一个新的表

Create  table  newtable
(
 id int primary key not null,      --设定主键不为空
 name nvarchar(10) not null,    --设定不为空
 birthday smalldatetime  
   
)

2. 插入数据

Insert  into  table      --在表中插入一条记录 
Values ('001','张三','1980-9-8')
3. 修改数据

Update  table                --把原来姓名为'李四'修改成了'张三'
Set  name = '张三'
Where  name = '李四'

Update  student            --将所有学生的年龄都增加1岁
Set  age = age+1

4. 删除数据(注意:语句DELETE和DROP TABLE之间有一个非常重要的区别。DELETE删除一个表中的一部分或全部的内容。而DROP TABLE则将表的内容和表的大纲全都一起删除。这样,在执行了DELETE语句之后,表依然存在于数据库中,尽管表中或许已没有行了,但在执行DROP TABLE语句后,表将不复存在)

Delete  from  student     --删除学生表中所有性别为’女’的学员信息
Where  sex = '女'

Delete  from student      --删除学生表中学号为’002’的学员信息
Where  stid = ’002’

5. 修改表

Alter  table student
Add  age char(20) null          --在表中添加了age列
Drop  column name              --在表中删除了name列
Alter  column age smallint   
--在表中修改了age列的数据类型为半字长整数

6. 修改列名

EXEC sp_rename '要修改的表名.[要修改的字段名]','修改后的字段名','COLUMN'

7. 删除表

Drop table student    --在数据库中删除了student表


简单查询

1. 查询学生表中的所有数据

Select  *                               --‘*’代表所有全部列
from student

2. 查询学生表中的name列

Select  name
from student

3. 查询学生表中的name列和stid列

Select stid, name     --查询多列时,用’,’隔开
from student

4. 查询学生表中姓名为'张三'的学号

Select  id
From  student
Where name ='张三'  
 --查询条件使用Where子句实现

5. 从学生表中查询出前5列的学员信息

Select  TOP 5 *      --top5代表前5行数据
From  student     

6. 查询学生表中姓名不为'张三'的学号

Select  id
From  student
Where name <> '张三'    
--where name != '张三'

7. 查询学生表中年龄大于24岁的学员姓名

Select  name
From  student
Where age > 24

8. 查询学生表中年龄在22至24岁之间的学员姓名

Select  name
From  student
Where age >= 22 and age <= 24  
--where age between 22 and 24

9. 查询学生表中学号为'001'和'008'的学员姓名

Select name
From  student
Where id=001 or id=008   
--where id in(001,008)

10. 查询学生表中年龄小于22岁,而大于24岁的学员姓名

Select  name
From  student
Where age < 22 or age > 24   
--或 where age not between 22 and 24

11. 查询学生表中班级编号为’001’,而年龄大于24岁的学员姓名

Select  name
From  student
Where classid =’001’ and age > 24 

12. 查询学生表中的所有的班级编号(不要重复)

Select  distinct  classid   -- distinct关键字从select语句的结果中除去重复的行
From  student

13. NOT操作的优先级最高,AND其次,OR最低
比较以下两个语句的不同:

Selec t  *
From  student
Where  age > 22 and age < 24
or classid = '001' and sex = '男'
 

Select  *
From  student
Where  ((age>22 and age<24)
or  classid='001') and sex='男'

14. 查询不属于班级编号为'S1T01'的所有学员姓名

Select  name
From  student
Where  not  classid ='S1T01'

15. 查询学生表中班级编号为空的学员姓名

Select  name
From  student
Where  classid  is  null

16. 查询学生表中班级编号不为空的学员姓名

Select  name
From  student
Where  classid  is  not  null

17. 查询学生总人数

Select  count(*)
From  student

18. 计算学员的平均年龄

Select  avg(age)
From  student

19. 查询学员的最大年龄

Select  max(age)
From  student

20. 查询学生表中的所有数据,并以年龄降序排列

Select  *
From  student
Order  by age desc  
 --asc为升序

21. 计算每个地址中不同城市的数目

Select  address as 城市, count(address)
From  student
Group  by address

22. HAVING子句定义了用于行组的条件判断。HAVING子句对行组来说所具有的意义,与WHERE子句对于每一单独的行所具有的意义是相同的。

1) 计算每个班级中学员的最大年龄,并按班号从大到小排列,使用下面的语句:

SELECT classid, MAX(age) as 最大年龄
FROM student
GROUP BY classid
ORDER BY classid desc

2) 要返回平均年龄在22到24之间的班级编号,使用下面的查询语句:

SELECT classid as 班号,AVG(age) as 平均年龄
FROM student
GROUP BY classid
HAVING AVG(age) between 22 and 24

23. LIKE操作符用于将列的值与某个特定的模式做比较。列的数据类型可以是任何字符或者日期型数据。
通配符 %:any 代表零个或多个任意字符  _:single 代表某一个任意字符

1) 查询所有姓名以李开头的学员的姓名和编号

Select  id,name
From  student
Where name like '李%'

2) 查询所有姓名中第二个是'国'的学员的姓名和编号

Select  id,name
From  student
Where name like '_国%'

3) 查询所在地名称是以C到F的字符打头的所有部门的详细资料

Select  *
From  department
Where address like '[C-F]%'

4) 查询姓名开头不是'李'开头的学员的姓名和编号

Select  stid,name
From  student
Where name not like '李%'

5) 查询姓(firstname)的打头字母不是J,K,L,M,N,O,并且名(lastname)的打头字母不是E或者Z的所有学生的编号和名字

Select  stid,name
From  student
Where  firstname like '[^J-O]%'
and lastname like '[^EZ]%'

24. 子查询

查询班级名称为'S1T01'的学员姓名

Select  name
From  student
Where  classid
in   /=
(
 Select  classid
 From  class
 Where  name='S1T01'
)

25. 多表查询
1) 合并union

USE NORTHWIND
 SELECT ContactName,city,postalcode
 FROM customers
 UNION
 SELECT lastname + ' ' + firstname ,city,postalcode
 FROM employees
2) 内联接

Select  s.name,c.name
From  student as s inner join class as c
On  s.classid=c.classid

/* Select  s.name,c.name
From  student as s,class as c
Where 
s.classid=c.classid */

3) 外联接

a) 左外联接

Select  s.name,c.name
From  student as s left join class as c
On  s.classid=c.classid

b) 右外联接

Select  s.name,c.name
From  student as s right join class as c
On  s.classid=c.classid

c) 完全联接

Select  s.name,c.name
From  student as s full join class as c
On  s.classid=c.classid

d) 交叉联接

Select  s.name,c.name
From  student as s cross join class as c

或者select s.name,c.name
from student as s ,class as c
4) 自联接
查询学生表中的所有学生的上级领导姓名

Select  学生表.name as 学生姓名, 领导表.name as 领导姓名
From  student as 学生表 left join student as 领导表
On  学生表.leaderid=领导表.stid