本文语句大部分SQL语句来自《数据库系统概论》(第四版)王珊&萨师煊 ,是我们上课用的教材,感觉很不错,总结了其中第三章的SQL语句,比较实用,希望对大家有帮助。总结如下,可以用来学习基础的SQL语句。
建立数据库
CREATE DATABASE DB_Student
建立表
CREATE TABLE Student
(Sno CHAR(9) PRIMARY KEY,--主码
Sname CHAR(20) UNIQUE,--唯一值
Ssex CHAR(2),
Sage SMALLINT,
Sdept CHAR(20)
);
CREATE TABLE Course
(Cno CHAR(4) PRIMARY KEY,
Cname char(40),
Cpno CHAR(4),
Ccredit SMALLINT,
FOREIGN KEY (Cpno) REFERENCES Course(Cno)
);
CREATE TABLE SC
(Sno CHAR(9),
Cno CHAR(4),
Grade SMALLINT,
PRIMARY KEY (Sno,Cno),
FOREIGN KEY (Sno) REFERENCES Student(Sno),--外码
FOREIGN KEY (Cno) REFERENCES Course(Cno)
);
数据
表操作
alter table Student add S_entrance date--增加列
alter table student alter column Sage int--修改字段类型
alter table course add unique (Cname)--增加唯一性约束
drop table Student--删除基本表
drop table student cascade--删除基本表及相关依赖对象
创建索引
drop index stusname
查询数据
select sno,sname from student
select sname,sno,sdept from student
select sname,2004-sage from student
select sname,'Year of Birth:',2004-sage, lower(sdept) from student--查询结果第二列是一个算数表达式
select sname name,'Year of Birth:' BIRTH,2004-sage birthday,LOWER(sdept) department from student--LOWER()小写字母
select sno from sc
select distinct sno from sc--消除重复行
select sno from sc
select all sno from sc
select sname from student where sqept='CS'
--=、>、<、>=、<=、!=、<>、!>、!< 比较的运算符
select sname,sage from student where sage<20
select distinct sno from sc where sage<20
select sname,sdept,sage from student where sage between 20 and 23
select sname,sdept,sage from student where sage not between 20 and 23
select sname,ssex from student where sdept in ('CS','MA','IS')
select sname,sage from student where sdept not in('CS','MA','IS')
select * from student where sno like '200215121'
select * from student where sno='200215121'
--字符匹配
--% 任意长度字符串,_ 任意单个字符,ESCAPE 转义字符
select sname,sno,ssex from student where sname like '刘%'
select sname from student where sname like '欧阳__'
select sname,sno from student where sname like '__阳%'
select sname,sno,ssex from student where sname not like '刘%'
select cno,ccredit from course where cname like 'DB/_design' escape '/'
select * from course where cname like 'DB/_%i__' escape '/'
select sno,cno from sc where grade is null --null 空值
select sno,cno from sc where grade is not null
select sname from student where sdept='CS' and sage<20
select sname,sage from studnet where sdept='CS' or sdept='MA' or sdept='IS'
select sno,grade from sc where cno='3' order by grade desc -- order by 排序
select * from student order by sdept,sage desc --空值最大
--聚集函数
select count(*) from student -- count() 行数
select count(distinct sno) from sc
select avg(grade) from sc where cno='1' -- avg() 平均数
select max(grade) from sc where cno='1' -- max() 最大值
select sum(Ccredit) from sc,course where sno='200215012' and sc.cno=course.cno -- sum() 总数
--分组
select cno,count(sno) from sc group by cno
select sno from sc group by sno having count(*) >3 --having 给出选择组的条件
--连接查询
select student. *,SC.* FROM STUDENT,SC where student.sno=sc.sno
select student.sno,sname,ssex,sage,sdept,cno,grade from student,sc where student.sno =sc.sno
select first.cno,second.cpno from course first,course second fwhere first.cpno=second.cno -- 自身连接
select student.sno,sname,ssex,sage,sdept,cno,grade from student left out join sc in (student.sno=sc.sno)--外连接
--from student left out join sc using (sno)
select student.sno,sname from student,sc where student.sno=sc.sno and sc.cno='2' and sc.grade>90
select student.sno,sname,cname,grade from student,sc,course where student.sno=sc.sno and sc.cno=course.cno
select sname from student where sno in (select sno from sc shere con='2')
select sdept from student where sname='刘晨'
select sno.sname,sdept from student where sdept='CS'
--嵌套查询
select sno,sname,sdept from student where sdept in (select sdept from studnet where sname='刘晨')
select sno,sname,sdept from student where sdept in ('CS')
select s1.sno,s1.sname,s1.sdept from student s1,student s2 where s1.sdept =s2.sdept and s2.sname='刘晨'
select sno,sname from student where sno in (select sno from sc where cno in(select cno from course where cname='信息系统'))
select student.sno,sname from student ,sc,course where student.sno=sc.sno and sc.cno =course.cno and course.cname='信息系统'
--内查询的结果是一个值,因此可以用=代替in
select sno,sname,sdept from student where sdpet=(se3lect sdept from studnet where sname='刘晨')
select sno,sname,sdept from student where(select sdept from student where sname='刘晨')=sdept
select sno,cno from sc x where grade >=(select avg(grade) from sc y where y.sno=x.sno)
select avg(grade) from sc y where y.sno='200215121'
select sno,cno from sc x where grade>=88
select sname,sage from student where sage <ANY (SELECT sage from student where sdept='CS') and sdept <>'CS'
select sname,sage from student where sage<(select max(sage) from student where sdept='CS') and sdept <> 'CS'
select sname,sage from student where sage < all (select sage from student where sdept ='CS')
select sname,sage from student where sage<(select min(sage) from student where sdept='CS') and sdept <>'CS'
select sname from student where exists(select * from sc where sno=student.sno and cno='1')
select sname from student where not exists (select * from sc where sno=student.sno and cno='1')
select sno.sname,sdept from student s1 where exists(select * from studetn s2 where s2.sdept=s1.sdept and s2.sname='刘晨')
select sname from student where not exists (select * from course where not exists(select * from sc where sno=student.sno and cno=course.cno))
select distinct sno frome sc scx where not exists (select * from sc scy where scy.sno='200215122' and not exists(select * from sc scz where scz.sno=scx.sno and scz.cno=scy.cno))
http://zxlovenet.cnblogs.com
集合查询
select * from student where sdept ='CS' union select * from student where sage<=19 --union并操作
se
新闻热点
疑难解答