数据库学习笔记(SQL server语句)

select * from Course
select * from SC
select * from Student


drop table Course
drop table SC
drop table Student


create table Student--学生表
( Sno bigint primary key,--列级限制
Sname varchar(20)  not null,--不能为空
Ssex char(2),--中文字符占2个char
Sage smallint  check(Sage between 15 and 30),--限制年龄使用check语句
Sdept varchar(20) 
)
create table Course--课程表
( Cno int primary key,--课程代码
Cname varchar(20) unique,--去唯一值,不能重复
Cpno varchar(20),
)
create table SC--选课表
( Sno bigint ,
Cno int,
Grade smallint check(Grade between 0 and 100 ),--限制成绩
primary key(Sno,Cno)--表级限制
)


insert into Student values(20161101,'张一','女',20,'计科')
insert into Student values(20161102,'张二','女',21,'物电')
insert into Student values(20161103,'张三','男',19,'计科')
insert into Student values(20161104,'张四','男',20,'计科')
insert into Student values(20161105,'张五','男',21,'物电')
--可以用这种形式insert into Student(Sno,Sname,Ssex,Sage,Sdept) values(20161101,'张一','女',20,'计科')
insert into Course values(1,'高等数学','')--也可以(1,'高等数学',null)
insert into Course values(2,'模拟电路','高等数学')
insert into Course values(3,'数字电路','模拟电路')
insert into Course values(4,'计算机组成原理','数字电路')


insert into SC values(20161101,1,null)
insert into SC values(20161102,1,null)
insert into SC values(20161103,1,null)
insert into SC values(20161104,1,null)
insert into SC values(20161105,1,null)
insert into SC values(20161101,2,null)
insert into SC values(20161102,2,null)
insert into SC values(20161103,2,null)
insert into SC values(20161104,2,null)
insert into SC values(20161101,3,null)
insert into SC values(20161102,3,null)
insert into SC values(20161103,3,null)
insert into SC values(20161101,4,null)
insert into SC values(20161102,4,null)


delete from SC where Sno=20161102 and Cno=4--删除学号为20161102,选修4号课程的选课记录
delete from SC--删除所有选课记录
delete from SC where Sno in(select Sno from Student where Sage=19)--嵌套子查询:记录唯一可以用=,否则用in


select * from SC where Grade is null--空值判断用is null和is not null


update Student set Sage=23 where Sage=19--修改年龄为19岁的学生的年龄为23
update Student set Sage=Sage+1--所有人年龄加1
update SC set Grade=60 where Sno in(select Sno from Student where Sdept='计科')--嵌套子查询,设置所有计科同学分数为60


create table Avr_age
( Sdept varchar(20),
age int,
)
insert into Avr_age select Sdept ,avg(Sage) from Student group by Sdept 
select * from Avr_age
--插入子查询结果
--函数大小写没有影响


alter table Student add Sentrance date--向表增加一列“入学时间”
alter table Student alter column Sentrance varchar(10)--修改列的数据类型
alter table Student add unique(Sname)--向某列增加约束条件
alter table Student drop column Sentrance--删除表中某列
--column:列


create unique index SCno on SC(Sno asc,Cno desc)--创建索引,此处条件为学号升序,再课程号降序
alter index SCno RENAME to SCCC--此处有问题
EXEC sp_rename 'SC.SCno','SCSno','INDEX'--这才是正确的
drop index SC.SCSno--索引必须写入表名


select Sno,Sname from Student--查询指定内容
select Sage-1 from Student--查询中进行运算
select Sno,'hhh',Sname from Student--无中生有
select distinct Sno from SC --限制重复项多次出现
select * from Student where Sage<23--运算符
select COUNT(*) from Student 
select SUM(Sage) from Student 
select AVG(Sage) from Student 
select MAX(Sage) from Student 
select MIN(Sage) from Student 
select Sno from Student group by Sno,Sage having AVG(Sage)>0--使用group by就不能使用where ,只能使用having
--凡是在group by后面出现的字段,必须同时在select后面出现;凡是在select后面出现的、
--同时未在聚合函数中出现的字段,必须同时出现在group by后面sel,检查sql是否符合上述法则。
select SC.*,Student.* from SC,Student --笛卡尔积,没有意义
select SC.*,Student.* from SC,Student where SC.Sno=Student.Sno--等值连接
select FIRSTT.Cname,SECONDD.Cpno from Course FIRSTT,Course SECONDD where FIRSTT.Cpno=SECONDD.Cname--自身连接
select Student.Sno,Sname,Ssex,Sdept,Cno,Grade from Student left outer join SC on (Student.Sno=SC.Sno)--外连接
select a.sno,Sname,avg1 from Student,
(select Sno,AVG(Sage) avg1 from Student Group by Sno) a 
where Student.Sno=a.Sno--重命名一个虚表
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 exists 
(select * from SC where Sno=Student.Sno and Cno=1)--exists用法,常用于显示表格列名(not exists)
select Sname,Sage from Student where Sage<all
(select Sage from Student where Sdept='物电')--all的用法
select Sname,Sage from Student where Sage<any
(select Sage from Student where Sdept='物电')--any的用法
select Sname,Sage from Student where Sage<some
(select Sage from Student where Sdept='物电')--some的用法


create view JK_Student
as
select Sno,Sname,Sage from Student where Sdept='计科'--创建视图


select * from JK_Student
--视图是一系列数据库语句操作


select * from student where sage between 19 and 20--between的用法
select * from student where sdept in ('语文')--in/not in 的用法 


select * from student where sname like '李%'  
select * from student where sname like '李_'  
select * from student where sdept like '%电'
select * from student where sno like '201611%'
--字符串替代及like的用法


create table SC1
(
Sno char(9) not null,
Cno char(4) not null,
Grade smallint,
primary key(Sno,Cno),
foreign key(Sno) references Student(Sno),
foreign key(Cno) references Course(Cno)
)
--表级定义参照完整性


grant select
on table Student
to U1
grant insert,update(Sno)--具体权限限制
on table SC
to U2
grant all privileges--全部权限授予
ob table SC
to U3
with grant option--允许再授予给别人
--授权操作


revoke insert
on table SC
from U2 cascade--级联权限收回
revoke insert
on table SC
from public--全部查询权限收回


create role xxx--创建角色


create table Student
(
Sno numeric(6)
constraint c1 check(Sno Between 90000 and 99999),
Sname char(20)
constraint c2 not null,
Sage numeric
constraint c3 check(Sage<30),
Ssex char(2)
constraint c4 check(Ssex in('男','女')),
constraint StudentKey primary key(Sno)
)
alter table Student
drop constraint c1
alter table Student
add constraint C3 check(Sage<40)
--完整性约束命名子句(方便删除和修改)


create trigger Student_Count
after insert on Student
referencing
new table as Delta
for each statement
insert into StudentInsertLog(Numbers)
select COUNT(*) from Delta
--触发器

猜你喜欢

转载自blog.csdn.net/caoyang_he/article/details/79943462