基本SQL语句学习1.0

来源:互联网 发布:python ascii转utf8 编辑:程序博客网 时间:2024/04/26 20:10

基本SQL学习1.0

运行环境:SQL Server 2005

基本数据类型:

数据类型

含义

char(n)

长度为n的定长字符串

varchar(n)

最大长度为n的定长字符串

int

长整数

smallint

短整数

numeric(p,d)

定点数,由p位数字组成(不含符号,小数点),小数点为d位

real

取决于机器精度的浮点数

double precision

取决于机器精度的双精度浮点数

float(n)

浮点数,精度至少为n位数字

datetime

日期时间

 

学生表Student(Sno,Sname,Ssex,Sage,Sdept)

课程表Course(Cno,Cname,Cpno,Ccredit) Cpno为先行课(外键)

学生选课表SC(SnoCno,Grade)下划线的为主键

1.表

1.1.建表

格式:

    create table 表名(....)

学生表:

create table Student

(

    Sno varchar(9)primarykey,

    Sname varchar(20)unique,

    Ssex varchar(2),

    Sage smallint,

    Sdept varchar(20)

)

 

课程表:

create table Course

(

    Cno varchar(4)primarykey,

    Cname varchar(40),

    Cpno varchar(4),

    Ccredit smallint,

foreign key(Cpno) references Course(Cno)

)

 

学生课程表:

create table SC

(

    Sno varchar(9),

    Cno varchar(4),

    Grade smallint,

    primary key(Sno,Cno),

    foreign key(Sno) references Student(Sno),

    foreign key(Cno) references Course(Cno)

)

1.2.修改表结构

格式:

    alter table <表名>

    [add <新列名> <数据类型> [完整性约束]]

    [drop <完整性约束名>]

    [alter column <列名> <数据类型>]

增加列

alter table Studentadd Sentrancedatetime

修改列数据类型

alter table Studentaltercolumn Sageint

增加约束

alter table Courseaddunique(Cname)

1.3.删表

格式:

       drop table <表名> [restrict | cascade]

       restrict(默认值):有限制的删除。删除的表没有被引用,不能有视图,触发器,存储过程等。

       cascade:级联删除。删除表的同时删除了依赖该表的对象。

       SQL Server中无此restrict | cascade选项

限制性的删除

drop table student

无法删除对象'student',因为该对象正由一个FOREIGN KEY约束引用。

级联删除

drop table student cascade

报错!

1.4查表

格式:

       select [all | distinct] <目标列表达式> [, <目标列表达式>].....

       from <表名或视图名>[, <表名或视图名>]

       [where <条件表达式>]

       [group by <列名1> [having <条件表达式>]]

       [order by <列名2> [asc | desc]]

常用查询条件:

比较

>,<.=,>=,<=,!=,<>,!>,!<,not+

确定范围

between and , not between and

确定集合

in , not in

字符匹配

like , or like

%:代表任意长度字符串(包括0)

_:代表任意单个字符

escape:转义字符,使得%,_不是上述意思。

空值

is null , is not null

多重条件(逻辑运算)

and ,or , not

聚集函数列表:

count([distinct | all ] *)

统计元组个数

count([distinct | all] <列名>)

统计一列中值的个数

sum([distinct | all] <列名>)

统计一列中值的总和

avg([distinct | all] <列名>)

统计一列中值得平均值

max([distinct | all] <列名>)

求一列中最大值

min([distinct | all] <列名>)

求一列中最小值

连接查询:

表R:                                    

A

B

C

a1

b1

5

a1

b2

6

a2

b3

8

a2

b4

12

表S:

B

E

b1

3

b2

7

b3

10

b3

2

b5

2

一般连接:(R.C<S.E)

A

R.B

C

S.B

E

a1

b1

5

b2

7

a1

b1

5

b3

10

a1

b1

6

b2

7

a1

b1

6

b3

10

a2

b3

8

b3

10

等值连接:R.B=S.B

A

R.B

C

S.B

E

a1

b1

5

b1

3

a1

b2

6

b2

7

a2

b3

8

b3

10

a2

b3

8

b3

2

自然连接:(去掉等值连接中的重复列)

A

B

C

E

a1

b1

5

3

a1

b2

6

7

a2

b3

8

10

a2

b3

8

2

       在做自然连接时,选择两个关系在公共属性上值相等的元组构成新的关系。此事,关系R中某些元组可能在S中不存在公共属性上值相等的元组,从而造成这些元组在操作过程中舍弃了,同样,关系S中的某些元组也被舍弃了。

       如果把舍弃的元组保存在结果关系中,而在其他属性上填NULL值,则这种连接成为外连接。如果保留左边关系中要舍弃的元组,则称为左外连接,反之,为右外连接。

嵌套查询:不相关子查询,子查询的查询条件不依赖父查询。否则为相关子查询。

       集合查询:并操作union,交操作intersect,差操作except

查询实例:

无条件查询

查询指定列

select Sno,Snamefrom student

查询全部列

select * from student

查询经过计算的值

select Sname,2014-Sageas Sbirthdayfrom Student

取消取值重复的列

select distinct Snofrom SC

条件查询

比较

select * from Student where Sname = '张顺'

select * from Student where Sage < 26

确定范围

select * from Student where Sage between 10 and 30

确定集合

select * from Student where Sname in('张顺','李科')

字符匹配

select * from Student where Sname like '%'

select * from Student where Sname like '_'

空值

select * from Student where Sname is not null

多重条件

select * from Student where Sage > 21 and Sname isnotnull

order by子句(排序)

升序

select * from Student where Sage > 10 order by Snoasc

多个排序

select * from Student where Sage > 10 order by Snoasc,Sagedesc

聚集函数(默认all

学生总人数

select count(*)as totalfrom Student

平均成绩

select avg(Grade)from SCwhere Cno= '1'

最高分

select max(Grade)from SC

总学分

select sum(Ccredit)from SC,Coursewhere Sno = '1'and SC.Cno= Course.Cno

group by子句(分组) having子句(选择)

分组并选择

select Sno from SCgroupby Sno having count(*)> 1

连接查询

等值连接(用=)

select Student.*,SC.*from Student,SCwhere Student.Sno= Sc.Sno

自然连接(去掉等值连接中的重复列)

select Student.Sno,Sname,Ssex,Sdept,Cno,Gradefrom Student,SC where Student.Sno= Sc.Sno

非等值连接(不用=)

 

自身连接

select c1.Cname,c2.Cnamefrom Course c1,Course c2where c1.Cno = c2.Cpno

左外连接

select Sname,Sage,Ssex,Sdept,Cno,Gradefrom Student leftjoin SCon (Student.Sno= SC.Sno)

多表连接

select Sname,Cname,Gradefrom Student,Course,SCwhere Student.Sno= SC.Snoand SC.Cno = Course.Cno

嵌套查询

不相关子查询

select Sname from Studentwhere Sdeptin (select Sdeptfrom Studentwhere Sname='张顺')

 

 

select Sname from Studentwhere Snoin(select Snofrom SCwhere Cnoin(select Cnofrom Course where Cname='OS'))

相关子查询

select Sno,Cnofrom SC sc1where Grade> = (select avg(Grade)from SC sc2where sc1.Sno= sc2.Sno)

带any(任意的一个)

select Sname,Sagefrom Studentwhere Sage< any (select Sagefrom Studentwhere Sdept='计算机学院')and Sdept != '计算机学院'

带all(所有)

select Sname,Sagefrom Studentwhere Sage< all (select Sagefrom Studentwhere Sdept='计算机学院')and Sdept != '计算机学院'

集合查询

union(会去掉重复,若不去掉,用union all)

select Sname from Studentwhere Sdept= '计算机学院'union select Snamefrom Student where Sage> 20

intersect

select Sname from Studentwhere Sdept= '计算机学院'intersect select Snamefrom Student where Sage> 20

except

select Sname from Studentwhere Sdept= '计算机学院'except select Snamefrom Student where Sage> 20

    

1.5表中插入数据

直接插入

insert into Student(Sno,Sname)values(4,'电费')

插入子查询结果

insert into Student2(sno,sname)select Sno,Snamefrom Student

1.6表中数据更新

修改一个元组的值

update Student set Sage= 10where Sname = '张顺'

修改多个元组的值

update Student set Sage= 10

带子查询的更新语句

update SC set Grade= 0where '计算机学院'in (select Sdeptfrom SC,Studentwhere SC.Sno = Student.Sno)

1.7删除表中数据

删除一个元组

delete from Studentwhere Snamelike '_'

删除多个元组

delete from Student2

带子查询的删除语句

delete from Studentwhere'计算机学院'in(select Sdeptfrom Studentwhere Sdept= '计算机学院')

 

2.索引

2.1建立索引(建立的目的是加快查询速度。)

格式:

       create [unique] [cluster] index <索引名> on <表名>(<列名> [次序]...)

       unqiue:唯一索引。说明此索引的每一个索引值只对应唯一的数据记录。

       cluster:聚簇索引。指索引项的顺序与表中记录的物理顺序一致。一个表中最多只能建立一个聚簇索引。

创建聚簇索引

create clusteredindex index_studenton Student(Sname)

创建唯一索引

create unique index index_course on Course(Cnoasc,Cnamedesc)

2.2删除索引

格式:

       drop index <索引名> on <表名>

删除索引

drop index index_courseon Course

 

3.视图

3.1创建视图

       视图是从一个或多个基本表(或视图)中导出的表。它与基本表不同,是一个虚表。数据库中只存放视图的定义,但不存放视图中的数据,其数据仍来源于基本表,所以基本表中数据变化了则视图中的数据也会变化。

格式:

       create view <视图名> [(<列名>[,<列名>].....)]

       as <子查询>

       [with check option]

       该子查询中不应该有order子句和distinct。

       with check option:对视图进行查新,更新,删除时都要满足子查询中的条件

举例:

create view v_Courseasselect Cno,Cnamefrom Coursewith check option

create view S22asselect sno,snamefrom Student2where sno> 1 with check option

3.2删除视图

格式:

    delete view <视图名> [cascade]

    cascade:删除该视图和由该视图导出的视图。

举例

drop view v_Course

3.3查询视图

举例:

select * from S22 where sno < 3

等价于

select * from Student2 where Sno > 1 and Sno <3

3.4更新视图

举例:

update S22 set sno= 5where sno = 2

 

4数据库完整性

4.1实体完整性(主键)

4.2参照完整性(外键)

    删除,修改时3种策略:1.noaction(默认):不允许执行;2.cascade:级联;3.set null:置空。

举例

create Table SC2

(

    Sno varchar(9),

    Cno varchar(4),

    Grade smallint,

    primary key(Sno,Cno),

    foreign key(Sno) references Student(Sno)

    on updatecascade

    on deletecascade,

    foreign key(Cno) references Course(Cno)

    on updatecascade

    on deletenoaction

)

4.3用户自定义完整性

       3中实现方式:1.notnull;2.unique;3.check(布尔表达式)

举例

create table Student22

(

    Sno varchar(9)primarykey,

    Sname varchar(20)unique,

    Sage smallint not null,

    Ssex varchar(2)check(Ssexin ('',''))

)

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

0 0
原创粉丝点击