操作表:
Student (S#,Sname, Ssex,Sage,D#,Sclass)
SC(S#,C#,Score)
Course(C#,Cname,Chours,Credit,T#)
Teacher(T#,Tname,D#,Salary)
Dept(D#,Dname,Dean)
create:创建数据库或表
create database SCT --创建数据库
create table SC( --创建表
S# char(8),
C# char(3),
Score int
)
select:选取数据
select Sname from Student
select * from Student -- 显示Student表中所有列的信息
distinct:去重
select distinct Sname from Student
where:按条件选择
select Sname from Student
where Ssex = '女'
and 和 or
select Sname from Student
where Ssex = '女' and Sage < 20
select Sname from Student
where Ssex = '女' or Sage < 20
odrder by:对结果进行排序
select S#, Sname from Student
order by Sage asc -- 按升序进行排序,降序desc
insert:插入
insert into Student values('98030101','张三','男',20,'03','980301')
insert into Student(S#, Sname, Ssex) values('98030102','张四','男')
updata:更新
update Student
set D# = '05',Ssex = '女' where Sname = '赵四'
delete:删除
delete from Student
where Sname = '张三'
delete * from Student -- 删除Student中所有行
like:模糊查询
select S#, Sname from Student
where Sname like '张%' -- 寻找叫张某某的学生,'%'匹配零个或多个字符
select S#, Sname from Student
where Sname like '张_' -- 寻找叫张某的同学,'_'匹配单个字符
select S#, Sname from Student
where Sname like '张\%' -- 寻找叫张%的同学,'\'表转义字符
select S#, Sname from Student
where Sname like '[张]%' -- 寻找以张开头的同学
select S#, Sname from Student
where Sname like '[^张]%' -- 寻找不是以张开头的同学
in:允许在 where 子句中规定多个值
select distinct S# from SC -- 列举学过001号课程和002号课程同学的学号
where C# in ('001','002');
as:别名
select Sname as 姓名 from Student
inner join:内连接
select * from Student
inner join SC on Student.S# = SC.S#
left join:左连接
select * from Student
left join SC on Student.S# = SC.S#
right join:右连接
select * from Student
right join SC on Student.S# = SC.S#
full join:全连接
select * from Student
full join SC on Student.S# = SC.S#
Union:并
select Sname from Student, SC where Student.S# = SC.S# and C# = '001'
union
select Sname from Student, SC where Student.S# = SC.S# and C# = '002'
-- Union后面加all 列出所有值(包括重复)
select into:批量复制
select Student.S#, Sname, Score into StudentBackup
from Student,SC
where Ssex = '女' and SC.S# = Student.S#
constraint:完整性约束
-- 列约束
create table tablename(colname datatype, -- colname列的数据类型datatype
not null -- 列值非空,表示若不向该字段添加值,则无法插入或更新新纪录
unique -- 列值唯一,每个表可以有多个
primary key -- 列为主键,每个表只能有一个,包含非空唯一特性
constraint constraintname -- 约束命名为constraintname
check(search_cond) -- 列值满足条件search_cond,条件只能使用列当前值
references tablename (colname) on delete cascade | set null) -- 引用tablename的列colname的值,若引用表的某项被删除,对应本表的对应项被删除或者更新为null
-- 表约束
create table tablename(colname,colname,...),
constraint constraintname -- 为约束名
unique(colname, colname,...) -- 几列值组合在一起是唯一
primary key(colname, colname, ...) -- 几列联合为主键
check(search_condition) -- 元组多列值共同满足的条件,条件中只能使用同一元组的不同列当前值
foreign key(colname, colname,...)
reference tablename(colname, colname, ...)
on delete cascade | set null -- 引用另一个表tablename的若干列的值作为外键
drop:删除
drop table Student -- 删除表
drop database SCT -- 删除数据库
truncate table Student -- 只删除表的内容
alter:修改
alter table Student -- 增加列
add Sheight float
alter table Student -- 删除列
drop column Sheight
alter table Student
alter column Sheight int -- 修改列的属性
view:视图
create view test as -- 创建视图
select S#, Sname from Student
select * from test -- 查询视图
select Sname from test -- 在视图的基础上添加条件
drop view test -- 撤销视图
group by:
select sum(Score) from SC -- 每个学生的总成绩
group by S#
select sum(Score) from SC -- 每门课程的总成绩
group by C#
having:
Select S#, Avg(Score) from SC -- 有一门以上及格的同学的及格科目的平均成绩
where Score > 60
group by S# having Count(*) > 1
select S#, Avg(Score) from SC -- 有一门以上及格的同学的所有科目的平均成绩
where S# in ( select S# from SC
where Score > 60
group by S# having Count(*) > 1)
group by S#;