第三章:关系数据库标准语言SQL

模式数据定义语言(Schema Data Definition lanuage DDL )

外模式数据定义语言(Subschema Data Definition lanuage 外模式DDL)

数据存储有关的描述语言(Data storage Description Language,DSDL)

数据操纵语言(Data Manipulation Lanuage DML)

DQL 查询语言 DCL 控制语言

操作对象操作方式
创建删除修改
模式create schemaDrop schema
create tableDrop tablealter Table
视图create viewDrop view
索引create indexDrop indexalter index

模式

1create schema <模式名> authorization 用户名 没用模式名,则模式名隐含为用户名

2Drop schema<模式><casade|restrict>cascade 再删除模式时,其下的所有对象与视图都删掉

3restrict:模式下若有定义的对象与视图,则不能执行删除模式

create <表名>(列名<数据类型>  [列级完整性约束条件]

,[表级完整性约束条件])

关于表的名词性解释。

/*完整性约束条件 1:实体完整性:主要规范主键,即主属性不为空等

                             2:参照完整性:主要规范外键,即外码(外码不需要与主码同名,外码必须等于所参照主码的值,或者为空)

                             3:用户完整性:其他属性的域、取值范围、条件等*/

/*候选码:一个属性组可以唯一地标识元组

主码:候选码里选一个;

主属性:候选码的所有属性称为主属性,

非码属性:除了主属性以外的属性

全码

*/

举例:

create table student

(

sno char(9) primary key,

snmae char(20) unique /*取唯一值*/

ssex char(2),

sage smallint,

sdept char(20)

)

create table coures(

cno char(4) primary key,

cname char(40) not null,

ccredit  smallint,

foreign key(cpno) references coures(cno)

)

/*

char和varchar都是用来存储字符串的,但是他们保持和检索的方式不同。

char是属于固定长度的字符类型,而varchar是属于可变长度的字符类型。

由于char是固定长度的所以它的处理速度比varchar快很多。但是缺点是浪费存储空间,读取char类型数据时候时如果尾部有空格会丢失空格,所以对于那种长度变化不大的并且对查询速度有较高要求的数据可以考虑使用char类型来存储。

另外随着MySQL版本的不断升级,varchar数据类型的性能也在不断改进并提高,所以在许多的应用中,varchar类型被更多的使用

*/

外模式-对应视图(多个视图) 

模式-对应数据库(包含多个表)

内模式 对应物理存储文件

三种方法表名 表所属的模式

1:在表名显示的给出 ;create table "s-t".student(.....);S-T 为表名

2:创建模式是同时创建表;

3:在所属模式中,直接创建表

修改表

alter table<表名>

[add[column]<列名><数据类型>[约束]]

[add[表级约束]]

[drop [column]<列名>[cascade|restrict]]

[drop constraint[column]<完整性约束>[Restrict|cascade]]

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

删除表

drop table<表名> [restrict|cascade];

以上restrict都是一个问题,就是不能被其他表所引用作为外键,视图,触发器,函数,存储过程等,若有则不能删除

查询

select [all|distinct]<目标列> 

from[表或视图]  as[别名]

[where<条件表达式>]

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

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

每个关键字的作用演示

创建3个表如下:

课程表

学生选课表

 

  学生表

名词理解

传统集合运算:并,差,交,笛卡尔积

专门的关系运算:选择,投影,连接,除

单表查询:

1select Sno,sname form student  这里select对应的是投影

2select sname,2014-sname from student /*2014-sage这里是个运算,表中是2014-sage的结果*/

3复杂版:select sname NAME ,'year of birth:' birth,2014-sage birthday,lower(sdept) department from student;

4消除查询列重复元组:select sno from distinct sc

distinct 消除重复元组

可以返现 “,”分隔符 前面是查询记过,后面便是查询列的命名

查询满足条件  其实这里相当于关系条件的 选择

关键词:

查询条件谓词
比较比较大小、!=,<>,NOT+上述比较符
确定集合

IN,NOT IN

字符匹配

Like,not like

空值is null, is not null
多重条件and,or,not
确定范围between and ,not between and

1select sname, sage from student where sage<20;

2select sname,sdept,sage from student  where sage between 20 and 23'

3 select sname,ssex from student where sdept in('cs','ma','is');

4字符匹配

[not] like '<匹配串>' [escape'<换码字符>']

其中匹配串的通配符有:“%”:a%b以a开头,以b结尾的任意长度字符

                                        “_” :a_b表示 以a开头,b结尾的长度为3的任意字符

select *from student where sno ='201215121';

select*from student where sname like '刘%';

/*注意当用户要查询的字符串本身就有通配符%或_,就使用"escape"换字符进行转义*/

select sno,ccredit  from student where cname like 'DB\_design'    escape'\'

此时“\”不在具有通配符意义。

1多重查询条件:select sname from student where sdept='cs' and sage<20;

可以用and和or

2order by L对查询结果按照属性组进行升序(asc)或降序排列(desc)

3group by 字句:将具有相同cno值的元组为一组;

select cno,count(sno) from sc group by cno;

select sno from sc group by sno having count(*)>3;

where 与 croup短语的区别在于作用对象不同,where作用于基本表或者视图,而having作用于组,从中选择满足条件的组

注意 where 语句中不能用聚集函数作为表达式。

4 聚集函数;

count(*)统计元组个数
count([distinct|all]<列名>)统计一列中值的个数
sum([distinct|all]<列名>)计算一列值的总和
avg([distinct|all]<列名>)计算一列值的平均值
max([distinct|all]<列名>)求一列值中的最大值
min([distinct|all]<列名>)最小值

例:select count(distinct sno) from sc;

连接查询

各连接字段类型必须是可比的,但名字不必相同

格式:表名.列名  比较运算符 表名.列名

select from student.* sc.* from student sc where student.sno=sc.sno;

/*自然连接会把目标列中重复的属性列去掉*/

自身连接()

列:查询一门先修课的先修课

select first.cno,second.cpno

form course first ,course second where first.cpno=second.cno;

外连接

select student from student left outer join sc on(student.sno=sc.sno);

/*可以使用using来去掉结果中的重复值*/

左边连接列出左边关系的所有元组,右边连接列出右边关系的所有元组

多表连接

select student.sno,sname,cname,grade  from student ,sc,coures 

where student.sno=sc.sno and sc.cno=course.cno;

嵌套查询

即将一个查询块套在另一个查询块里面:

一个select-from-where 是一个查询块,如将一个查询块套在where子句或having短语中的查询

为嵌套查询

select sname from where sno in(select sno from sc where cno='2');

in这个谓词用来查找属性值属于集合的元组,in是嵌套查询中最常使用的谓词

*注意:select语句不能使用order by语句order by只能对最终结果排序。

不相关查询;子查询不依赖父查询。

查询选修了课程名为“信息系统”的学生学号和姓名

select sno,sname from student 

where sno in(select sno from sc  where cno IN(

select from course where  cname ='信息系统'))

select sno,cno from sc x where grade >=(select avg(grade from sc y where y.sno=x.sno))

相关谓词解释

/*  '<>'相当于 '!="       */

ANY 为任意

如:select from sname,sage from student where sage <any( select sage from student where sdept='cs') and sdept<>'cs'

可以发现谓词可以与聚集函数相互替换

EXISTS 谓词的子查询

exist 不返回任何数据,只产生逻辑真值:true或false;

exist 只关心内层查询是否有返回值,所以比较高效

select sname from student where not exists (select*from sc where sno=student.sno and con='1');

集合查询

并:UNION 交:INTERSECT 差:EXCEPT

select * from where sdept='cs '  UNION select * from student where sage <=19;

派生表的查询:

select sno,cno from sc ,(select sno,avg(grade) from sc group by sno as avg_sc(avg_sno,avg_grade));

数据更新

插入数据 

insert into student(sno,sname) values('201215128','城东');

插入子查询结果

create table dept_sage(sdept char(15)  avg_avg samllint);

insert into dept_age(sdept,avg_age)   select sdept,avg(sage)

from student group by sdept; 

修改数据

update student set sage=22 where sno='201215121';

update student set sage=sage+1;

update studen set grade=0 where sno in (select sno from student where sdept='cs');

删除数据

delete from student where.........

空值处理

判断是否空值 用 IS NULL 或 IS not NULL 来处理、

空值的算数运算,比较运算,和逻辑运算

空值与另一个值的比较运算结果为UNKNOWN;

找出不及格与缺考的学生

select sno from sc where grade<60 and cno='1'  UNION select sno from sc  where grade IS null AND con='1';

视图

create  view  IS_studen as select sno,sname,sage from student where sdept ='5';

执行create view 语句时只是把视图的定义存入数据字典,并不执行其中的select语句

删除视图:

drop view  student_s/*如拒绝执行,很可能在这个视图之上又建立了视图,所以非要删除

则 drop view  student_s  cascade/*此时删除其本省和其导出的全部视图*/

更新视图:

对视图的更新最终要转化为对基本表的更新

update IS_student set sname='刘晨' where sno='20125122';

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值