1.数据查询:
SELECT:用于选择需要查询的列和行。
FROM:用于指定要查询的表。
WHERE:用于指定查询条件。
GROUP BY:用于按照指定的列对结果进行分组。
HAVING:用于指定分组条件。
ORDER BY:用于指定查询结果的排序方式。
2.数据操纵:
INSERT INTO:用于将数据插入表中。
UPDATE:用于更新表中的数据。
DELETE FROM:用于删除表中的数据。
3.数据定义:
CREATE TABLE:用于创建新表。
ALTER TABLE:用于修改现有表的结构。
DROP TABLE:用于删除表。
4.数据控制:
GRANT:用于向用户或用户组授予特定的数据库权限。
REVOKE:用于撤销已授予的数据库权限。
SQL语句特点:
建立三个表
1. 建立学生表Student
create table Student
(
Sno char(9) primary key,
Sname char(20) unique,
Ssex char(2),
Sage smallint,
Sdept char(20)
);
2. 建立一个课程表Course
create table Course
(
Cno char(4) primary key,
Cname char(40) not null,
Cpno char(4),
Ccredit smallint,
foreign key (Cpno) references Course (Cno)
);
3. 创建一个SC表
create table SC
(
Sno char(9),
Cno char(9),
Grade smallint,
primary key (Sno,Cno),
foreign key (Sno) references Student(Sno),
foreign key (Cno) references Course(Cno)
);
一般格式:select [all或distinct]<目标列表达式>[,<目标列表达式>]...
from<表名字或视图名>[,<表名或视图名...>]或(
[where<条件表达式>]
[group by<列名1>[having <条件表达式>]]
[order by<列名2>[asc或desc]];
select Sno,Sname
from student;
select *
from student;
select Sname,2023-Sage as Birthyear,
lower(Sdept)as Department
from student;
/*注意列名的丢失*/
select distinct Sno/*毕竟在SC表中 学号是重复出现的元素*/
from SC
where Grade>60;
select Sname,Sdept,Sage
from student
where Sage between 20 and 30;
select Sname,Sage
from student
where Sdept in('CS','MA','IS');
/*in 查找属性值属于指定集合的元组*/
select Sno
from Student
where Sname like '_立%';/*一个汉字这里是俩个斜杠*/
/*但是在sql server中一个汉字代表的是一个斜杠*/
select Cno,Ccredit
from Course
where Cname like 'DB\_Design' escape '\';
/*由于_是占位符 但是在这里_是具体符号,所以得用escape关键字*/
以上为错误示范
select *
from Student
order by Sdept , Sage desc;/*默认升序*/
select count(distinct Sno)
from sc;
select avg(Grade)
from SC;
where Cno='1';
select sum(Ccreidt)
from SC,Course
where Sno='202102810224' and SC.Cno=Course.Cno;/*必须连接俩表 否则关系无法建立起来*/
往往表头元素和数字有关系的时候 就会用group by语句 比如:查询每个学生的选课门数,选修三门以上学生的名字等等
目的:细化聚集函数的作用对象,分组后,聚集函数将作用于每一个组,每一个组都有一个函数值
将表头元素和聚集函数值生成表建立联系
不可以用where 只可以用having
group by 后面加聚集函数之前的所有列表属性
having后面只可以加聚集函数
select Cno,count(Sno)
from SC
group by Cno;
select Sno,count(Cno)
from SC
group by Sno;
select Sno
from SC
group by Sno
having count(Cno)>3;
SC的Sno投影-Sno的投影(选择Grade>70(SC)) 所有成绩都<=70的
并(union)上
有且仅有一门是70分的(和数量有关 得用到group by)
where后面不可以加聚集函数
/*所有成绩<=70的*/
select Sno
from SC
where Sno not in
(select Sno from SC where Grade >70)
union
/*有且仅有一门是70分的*/
select Sno
from SC
where Grade>70
group by Sno
having count(Cno)=1
/*对于课本的表 这个代码是没有数据的*/
为什么这段代码没有数据??
分组之后 Cno已经没有=1的了
select Sno,avg(Grade)
from SC
group by Sno
having avg(Grade)>=90;
select Student.*,SC.*
from Student,SC
where SC.Sno=Student.Sno;
select Student.Sno,Sname
from Student,SC
where SC.Sno=Student.Sno and SC.Cno='2' and SC.Grade>90;
select C1.Cno,C2.Cpno
from Course as C1 ,Course as C2
where C1.Cpno=C2.Cno;
select Sno,Sname,Cno,Grade
from SC,Course,Student
where Student.Sno=SC.Sno and SC.Cno=Course.Cno /*虽然是三表连接 但是用一个and就可以解决问题*/
注意,SC连接的时候用的是Sno,不是Cno
在 SQL 语言中, 一个select-from-where 语句称为一个查询块,将一个查询块嵌套在另一个和查询块的 where 子句或者 having 短语的条件中的查询称为 嵌套查询
select Sno,Sname,Sdept
from Student
where Sdept in(select Sdept from Student where Sname='刘晨');
select Sno
from Student
where Cno='1' and Sno in
(select Sno from Student where Cno='2');
select distinct Student.Sno,Sname
from SC,Student,Course
where SC.Sno=Student.Sno and Course.Cno=SC.Cno and Course.Cname='信息系统';
/*查询所选课程成绩全部大于70分的学生的学号*/
select distinct Sno
from SC
where Grade not in(select Grade from SC where Grade<=70);
select Sno,Cno
from SC as X
where Grade>=(select avg(grade) from SC Y where Y.Sno=X.Sno);
// 1. 从外层查询中取出 SC 的一个元组 x, 将元组 x 的 Sno 值(201215121) 传送给内层查询
select avg(Grade)
form SC y
where y.Sno='201215121'
// 2. 执行内层查询,得到值 88(平均成绩的近似值), 用该值代替内层查询, 得到外层查询
select Sno,Cno
from SC x
where Grade>=88
// 3. 执行这个查询,得到
select Sno,Cno
from SC x
where Grade>=88;
然后从外层查询取出下一个元组重复上述 1 ~ 3 步骤的处理, 知道外层的 SC 元组全部处理完毕,结果为
// 学号 和 课程号
(201215121,1)
(201215121,3)
(201215122,2)
select Sname,Sage
from student
where Sage'CS';
select Sname,Sage
from Student
where Sage'CS';
select Sname
from Student
where exists(select * from SC where SC.Sno=Student.Sno and Cno='1');
相关子查询的过程分析:
1.取外层查询中Student表的第一个元组,根据与内层查询相关的属性值处理内层查询
2.与SC.Sno判断条件,如果满足,则记录下来
3.取Student表的下一个元组,重复这一过程
select Sname
from Student
where not exists
(select* from Course where not exists
(select* from SC where Sno=Student.Sno and Cno=Course.Cno));
理解:如果同时满足俩个条件,那么双重否定表肯定就是想要的结果
只要有一个结果不满足,那么就返回真,之后还有一个NOT EXISTS,就是假
select distinct Sno
from SC X
where not exists
(select * from SC Y where y.Sno='20215122' and not exists
(select * from SC Z where Z.Sno=X.Sno and Z.Sno=Y.Cno));
select *
from Student
where Sdept='CS'
union
select *
from Student
where Sage<=19;
select Sno
from SC
where Cno='1'
intersect
select Sno
from SC
where Cno='2';
select *
from Student
where Sdept='CS'
except--除去的意思
select *
from Student
where Sage<=19;
意义:可以将一些相关子查询改写为便于理解的非相关子查询
前情背景: 我们的子查询语句一般都是在where语句中,当然也可以出现在from语句中,这时候可以利用子查询生成的临时派生表成为主查询的查询对象
select Sno,Cno
from SC x
where Grade>=(select avg(Grade) from SC y where y.Sno=x.Sno);
改写:
select Sno,Cno
from SC,(select Sno,avg(Grade) from SC group by Sno) as avgsc(avgsno,avggrade);
where SC.Sno=avgsc.avgsno and SC.Cno>avgsc.avggrade;
注:如果子查询没有聚集函数,派生类可以不指定属性列
select Sname
from Student,(select Sno from SC where Cno='1') as SC1
where Student.Sno=SC1.Sno;
insert
into 表名 (表头名1,表头名2...)
values (常量1,常量2)
注意:如果into中没有指明任何属性列名,则新插入的元组必须在每个属性列上均有值
INSERT
INTO Student(Sno,Sname,Ssex,Sdept,Sage)
VALUES ('201215128 陈冬,男,IS18);
INSERT
INTO student
VALUES('201215126','张成民','男',18,'CS');
CREATE TABLE dept_age(
Sdept char(15),
Age SMALLINT
);
INSERT
INTO dept_age(Sdept,Age)
SELECT Sdept,AVG(Sage)
FROM student
GROUP BY Sdept;
UPDATE student
set Sage=Sage+1;
UPDATE sc
SET Grade=0
WHERE Sno IN
(
SELECT Sno FROM student WHERE Sdept='CS'
);
DELETE FROM sc
WHERE Sno IN
(SELECT Sno FROM student WHERE Sdept='CS');
插入:inserted表中有,deleted无
删除:deleted有,inserted无
CREATE TRIGGER trigger_name
ON table_name
INSTEAD OF DELETE--前触发器
AS
BEGIN
-- 触发器内容
END
--则将此次操作记录到另一个表中sc_u(sno,cno,oldgrade,newgrade)
create trigger sb
on sc
after update
as
begin
set nocount on;
if (1.1<=(select inserted.grade/deleted.grade
from inserted,deleted
where inserted.sno=deleted.sno and inserted.cno=deleted.cno))
begin
insert into sc_u
select deleted.* ,inserted.grade
from inserted,deleted
where inserted.sno=deleted.sno and inserted.cno=deleted.cno
end
end
create TRIGGER scl_grade
on SC
instead of INSERT
as
BEGIN
SET NOCOUNT ON;
if 60> (select grade--50 --70
from inserted)
begin
insert into SC
select Sno,Cno,60
from inserted
end
if 60<= (select grade from inserted)
begin
insert into SC
select Sno,Cno,inserted.Grade
from inserted
end
END;
insert into SC values('001','10',50);
ADD CONSTRAINT FK_CustomerOrder
FOREIGN KEY (CustomerID)
REFERENCES Customers(CustomerID)
ON DELETE CASCADE;
表和into的位置写反了
第二个表是course 写成了SC 因为要的是全部课程 所以就得从Course中选择
更改: