标签:code 常见 tween and 关系 带来 关联关系 场景 uniq
老师 - 学生(has) 父母 - 子女(has)
职员 - 项目经理,程序员(继承)
NoSQL != no sql === NOT Only SQL
NoSQL 看重存储效率(存储是否快捷)
关系型数据 看重的数据之间的关系
稍微复杂点的系统:关系型数据(关系) + NoSQL(快速的存取数据)
1代身份证:15位 2代身份证:18位
关联关系:
1对1 例如:中国的夫妻关系
1对n 例如:某一个老师跟学生 血缘关系下的父母与子女
n对n 例如:系统中的角色和权限
这些关系中,最常见的:1对n 比较少的:1对1 n对n
create database studentdb
use studentdb
go
create login zhou1 with password=‘mypassword123’
go
create user zhou1 for login zhou1
create schema sch1 authorization zhou1
drop schema sch1
Create Table Users
{
vUserName varchar(18) Not NULL,
vPassword varchar(20) Not NULL
}
Create Table Student
{
Sno char(10) NOT NULL Constraint PK_stu_NO Primary Key,
Sname char(20) NOT NULL,
Ssex cahr(2) NOT NULL Constraint CK_Stu_Sex Check(Ssex in(‘男‘,‘女‘)),
Sage tinyint Constraint CK_Stu_Age Check(Sage between 1 and 80),
Tel char(15) NOT NULL
}
Alter Table Student Drop Constraint CK_Stu_Age
Alter Table Student Drop Column Sage
Alter Table Student Add dBirth datetime
Alter Table Student Alter Column Smajor varchar(20)
drop table student
SELECT 表上哪些列显示
*表示所有列
//投影运算 指的是选择部分列数据
//友好列标题 SELECT Sno as 学号
//top关键字 SELECT Top 3 * from Student 只显示前3条数据
FROM 这个表来源
WHERE 这个表的情况,进行筛选
select * from student where Sage=20
select * from student where ssex=‘女‘and sage>21
select sno,sname,ssex from student whrer sno between ‘2‘ and ‘4‘
select * from teacher where tropt in(‘教授‘,‘副教授‘)
select distinct tropt from teacher
select sname,sdept from student where sdept like‘%学%‘
select sname,sdept from student where emall like ‘__@%‘
select *from student where low is null
select *from student where high is not null
select xklb as 类别,count(cname) as 数量 from course group by xklb
HAVING
ORDER BY ASC||DESC
ASC表示升序,DESC表示降序
select *from student order by sage desc
select *from student order by sage desc,sno asc在sage相同情况下按snow升序对比排列
COUNT
count函数返回匹配行数
select count(*) from teacher where tropt=‘教授’
显示教师的最大最小平均年龄
select max(tage),min(tage),avg(tage) from teacher
指定条件求和
select sum(credit) from course where xklb=‘必修‘ 对course表中的必修课的学分进行求和
select smajor,ssex,count(sno) from student group by Smajor ,sex order by count(sno) desc
对student表,按照专业和性别进行分组,显示每个专业,每种性别的学生数量,按照学生数量的降序显示结果
select tropt,count(tropt) from teacher group by tropt having count(tropt)>=5
对teacher表,显示职称和对应的认识,要求只有统计人数大于等于5人才显示
语法1:
select student.sno,student.sname,sc.sno,sc.grage from sc join student on sc.sno=student.sno
语法2:
select student.sno,student.sname,sc.sno,sc.grage from sc,student where sc.sno=student.sno
显示学生的学号,姓名,课程名,考试分数
语法1:
select student.sno,student.sname,course.cname,sc.grage from sc join student on sc.sno=student.sno join course on sc.sno=course.sno
语法2:
select student.sno,student.sname,course.cname,sc.grage from sc,student,course where sc.sno=student.sno and sc.sno=course.sno
让student表和sc表进行左外连接,即不管是学生是否有选修课程,该学生的信息的都会显示出来
select student.sno,student.sname,sc.sno,sc.grade from student left outer join sc on student.sno=sc.sno
让sc表和teacher表进行右外连接,显示教师编号,教师姓名,讲师教授的课程号
select teacher.tno,teacher.tname,sc.cno from sc right outer join teacher on sc.tno=teachaer.tno
让sc表和teacher表进行全外连接,显示教师编号,教师姓名,讲师教授的课程号。
teacher.tno,teacher.tname,sc.cno from sc full outer join teacher on sc.tno=teacher.tno
让学生和课程两张表进行交叉连接
select *from student cross join course
过in引入的子查询结果是包含零个值或多个值得列表,子查询返回结果之后,外部查询将利用这些结果
可以用all或者any修改引入子查询的比较运算符。some是与any等效的ISO标准,
以>比较运算符为例,>all表示大于每一个值,表示大于最大值。例如,>all(1,2,3)表示大于3
>any表示至少大于一个值,即大于最小值,因此>any(1,2,3)表示大于1
使用exists关键字引入子查询后,子查询的作用就相当于进行存在测试
外部查询的where子句测试子查询返回的行是否存在
子查询实际上不产生任何数据,它只返回TRUE或flase值
select *from student where sno=(select sno from sc where grade=48)
select *from student where sno in (select distinct sno from sc)
子查询得到学生的学号,外部查询根据学号找到学生
select * from student where sno not in (select distinct sno from sc)not in表示字段的值不在后面的子查询返回到结果中
select *from teacher where tsex=‘男‘ and tage>all(select tage from teacher where tsex=‘女‘)子查询得到每一位女教师的年龄,外层查询使用“>all”的语法,即比集合中最大值还大
select *from student where exists (select *from sc where sno=student.sno and cno=‘B004‘)
select *from student where not exists (select *from sc where sno=student.sno and cno=‘X001‘)
select sno,sname,smajor from student s1 where exists (select *from student s2 where s1.smajor=s2.smajor and s2.name=‘王国‘)
将学生的学号,姓名,与教师的教工号,姓名,在一个检索结果中显示出来
select Sno,Sname from Student
union
select Tno,Tname from Teacher
//union 将多个查询结果合并起来时系统自动去掉重复元组
//union all 将多个查询结果合并起来时,保留重复元组
对专业名以计算机开头的学生,及年龄为21的学生,用交运算求二者的交集
select Sno,Sname,Sage,Smajor from Student
where Smajor like‘计算机%‘
intersect
select Sno,Sname,Smajor from Student
where Sage=21
查询专业名以计算机开头的学生,但不包括年龄是21的学生
select Sno,Sname,Sage,Smajor from Student
where Smajor like ‘计算机%‘
except
select Sno,Sname,Sage,Smajor from Student
where Sage=21
insert into Course(Cno,Cname,Ccredit,XKLB)
values(‘X004‘,‘计算机前沿‘,2,‘选修‘)
insert into Teacher(Tno,Tname,Tsex)
select Sno,Sname,Ssex,‘讲师‘ from Studnet
select * into experts from teacher
where Tprot=‘教授‘
update course set ccredit=3
where cno=‘B002‘
update student
set smajor
(select top 1 cno from sc where sc.sno=student.sno)
delete from course
where cno=‘B009‘
delete from course where cno not in
(select cno from sc)
create view vwscs
as
select sno,sname,ssex,tel,emall from student
where sdept=‘计算机科学学院‘
update vwscs
set tel=‘13966667777‘
where sname=‘杨华‘
select *from vwscore where score grade<85
create index
drop index
alter table course2
add constraint pk_course2_cno primary key clustered(cno)
create unique index idxcoursename
on course2(name)
drop index idxcoursename on course2
drop index course2.idxcoursename
标签:code 常见 tween and 关系 带来 关联关系 场景 uniq
原文地址:https://www.cnblogs.com/dongxuelove/p/12933504.html