表关系

  1. 1.主键 primary key<br /> 特性: 非空,去重的<br /> 作用: 主键是一行数据的唯一标识,不管后面的值是否相等,通过主键可以完全区分出来<br /> 2.外键 foreign key<br /> 1.建立表与表之间的关系,必须使用外键<br /> 2.外键的数据类型,要与其他表的主键相同<br /> 3.外键的取值范围必须在主键的范围之内<br /> 4.外键可以为空<br /> 外键语法: foreign key(外键字段名) references 主表名(主键字段名)<br /> 注意:创建表的时候,先建立父表 再创建子表<br /> 删除表的时候,先删除子表,再删除父表
  2. 插入数据的时候先插入主表的数据,再插入从表的数据<br /> 删除数据的时候先删从表的数据,再删主表的数据<br /> <br /> ![image.png](https://cdn.nlark.com/yuque/0/2021/png/21682952/1635395962406-18d73374-d050-41ed-b93f-f6da106acc4a.png#clientId=u385ac4fc-a352-4&crop=0&crop=0&crop=1&crop=1&from=paste&height=268&id=u2ff301dd&margin=%5Bobject%20Object%5D&name=image.png&originHeight=427&originWidth=778&originalType=binary&ratio=1&rotation=0&showTitle=false&size=37973&status=done&style=none&taskId=udf55e1bb-e6c7-4223-8ff8-888d5615bb2&title=&width=487.99737548828125)<br /> ![image.png](https://cdn.nlark.com/yuque/0/2021/png/21682952/1635346810045-bb022a6e-a2d4-41be-8b14-24ffad95394d.png#clientId=u2106fda6-d3d1-4&crop=0&crop=0&crop=1&crop=1&from=paste&height=215&id=u4b5b5d3c&margin=%5Bobject%20Object%5D&name=image.png&originHeight=416&originWidth=907&originalType=binary&ratio=1&rotation=0&showTitle=false&size=131902&status=done&style=none&taskId=u9efbbbfc-30c1-4815-a2b1-b56f4a1b653&title=&width=468.48956298828125)<br />假设:<br /> create table course(<br /> cid int primary key,<br /> cname varchar(255)<br /> );<br /> <br /> create table student(<br /> sid int,<br /> sname varchar(255),<br /> cid int,<br /> foreign key(cid) references course(cid)<br /> );<br />

.主键:是表中的唯一标示键。
作用:保证实体的完整性;加快数据库的操作速度;增加新的表记录时,数据库会自动检索新记录的主键值,不允许该值与其他表中记录的主键重复;数据库会按主键值的顺序显示记录,如果没有设定主键,则按输入的顺序显示记录。

外键:是主键的从属,表示了两个表之间的联系。
作用:使用外键可以避免冗余。

  1. ![image.png](https://cdn.nlark.com/yuque/0/2021/png/21682952/1635334881059-b3c52848-067e-40ec-a4da-e81c1911f191.png#clientId=u38f03f79-822a-4&crop=0&crop=0&crop=1&crop=1&from=paste&height=557&id=u97e7cb99&margin=%5Bobject%20Object%5D&name=image.png&originHeight=786&originWidth=841&originalType=binary&ratio=1&rotation=0&showTitle=false&size=284666&status=done&style=none&taskId=ubc452f8b-0fe8-41f1-9565-4535cfd445f&title=&width=596.4947814941406)

练习(主键外键)

CREATE table starinfo(
sid int PRIMARY KEY,
star VARCHAR(255)
);

CREATE table bloodtype(
bid int PRIMARY KEY,
bloodtype VARCHAR(255)
);

CREATE TABLE user_h(
id INT,
loginpwd VARCHAR(255),
nickname VARCHAR(255),
sex char(1),
sid int,
FOREIGN KEY(sid) REFERENCES starinfo(sid),
bid int,
FOREIGN KEY(bid) REFERENCES bloodtype(bid)
);
image.png
INSERT into starinfo VALUES (1,’白羊座’),(2,’金牛座’),(3,’双子座’),(4,’巨蟹座’),
(5,’狮子座’),(6,’处女座’),(7,’天秤座’),(8,’天蝎座’),(9,’射手座’),(10,’摩羯座’),
(11,’水瓶座’),(12,’双鱼座’);

INSERT into bloodtype VALUES (1,’A型’),(2,’B型’);
INSERT into bloodtype VALUES (3,’O型’),(4,’AB型’);
INSERT into user_h VALUES
(1,’0000’,’豆豆’,’男’,5,1),
(2,’00A0’,’小强’,’男’,4,4),
(3,’0000’,’静静’,’男’,3,2),
(4,’00B0’,’.NET’,’男’,6,3),
(5,’0CC0’,’bobo’,’男’,2,1),
(6,’0BB0’,’我爱C#’,’男’,4,2),
(7,’0AA0’,’风筝’,’女’,1,1),
(8,’A000’,’benben’,’男’,1,1),
(9,’000C’,’吕洞宾’,’男’,11,1),
(10,’A000’,’清凉一夏’,’女’,6,2);
image.png
查询练习:
查询出血型为“O型”的用户姓名、性别;
select u.nickname,u.sex
from user_h u
join bloodtype b
on u.bid=b.bid
where b.bloodtype =’O型’;

使用内链接连接查询,查询出血型为“A型”并且星座为“白羊座”的用用户姓名、性别;
select u.nickname,u.sex
from user_h u
join bloodtype b
on u.bid = b.bid
join starinfo s
on u.sid = s.sid
where b.bloodtype=’A型’and s.star=’白羊座’;

用户“.NET”更新自己的名字为“天外飞仙”,请编写SQL语句进行更新;
update user_h set nickname =’天外飞仙’ where nickname=’.NET’;

查询输出用户的详细信息,输出结果如下图所示:
select u.nickname,u.sex,s.star,b.bloodtype
from user_h u
join bloodtype b
on u.bid = b.bid
join starinfo s
on u.sid = s.sid;

查询用户的密码中包含字符“A”的用户姓名、性别
select nickname,sex
from user_h
where LoginPWD like ‘%A%’;

连接查询

  1. <br />**1.什么是连接查询?**<br /> 所有的公司业务一般都不是一张表查询的,基本都是多张表连接查询<br />**2.连接查询的分类**<br /> 按照连接方式来划分<br /> A.内连接(只匹配连接表中字段内容相同的部分数据)<br /> inner join<br /> B.外连接(这个使用频率不高)<br /> 左外连接:(left join) <br /> 说明:是以左表数据为基准,左表与之右表不匹配的时候显示为null<br /> <br /> 右外连接:(right join)<br /> 说明:是以右表数据为基准,右表与之左表不匹配的时候显示为null <br /> <br /> <br />**3.内连接 两张表是平等的,并且顺序可以调换**<br /> <br />有两张表<br />课程表和老师表。<br />两个表之间的关系,是通过s_id关联在一起的

内连接

  1. ![image.png](https://cdn.nlark.com/yuque/0/2021/png/21682952/1635255993419-e4787a63-bdec-4b00-b86a-958236c49b26.png#clientId=u59df5ac3-0e2a-4&crop=0&crop=0&crop=1&crop=1&from=paste&height=395&id=u67179dc8&margin=%5Bobject%20Object%5D&name=image.png&originHeight=551&originWidth=746&originalType=binary&ratio=1&rotation=0&showTitle=false&size=37455&status=done&style=none&taskId=u71de3f6a-ed30-44b6-843e-aa2c26555ba&title=&width=534.9947814941406)<br /> ![image.png](https://cdn.nlark.com/yuque/0/2021/png/21682952/1635254710250-6c3edc43-c200-49fa-a668-0d218872ccee.png#clientId=u5261bc39-766f-4&crop=0&crop=0&crop=1&crop=1&from=paste&height=434&id=aBB3Y&margin=%5Bobject%20Object%5D&name=image.png&originHeight=619&originWidth=982&originalType=binary&ratio=1&rotation=0&showTitle=false&size=193978&status=done&style=none&taskId=u982f0e35-9f53-44ee-a10d-85bcd3db817&title=&width=688.9869384765625)<br /> 语法:select * from 表 1<br /> inner join 表 2<br /> on 表1.字段名=表2.字段名<br />**student表 ** ![image.png](https://cdn.nlark.com/yuque/0/2021/png/21682952/1635312270689-c2dea992-2b07-4a89-8713-2e05c49a82c7.png#clientId=uad2b66b2-a68d-4&crop=0&crop=0&crop=1&crop=1&from=paste&height=224&id=ub150f005&margin=%5Bobject%20Object%5D&name=image.png&originHeight=316&originWidth=454&originalType=binary&ratio=1&rotation=0&showTitle=false&size=54623&status=done&style=none&taskId=ub230c8a6-13c4-433e-af27-ee4b4c7155e&title=&width=321.98175048828125) ** score表** ![image.png](https://cdn.nlark.com/yuque/0/2021/png/21682952/1635312282183-cff5fc2b-db2a-4794-9703-2e02d03f42fe.png#clientId=uad2b66b2-a68d-4&crop=0&crop=0&crop=1&crop=1&from=paste&height=221&id=ud0feacfb&margin=%5Bobject%20Object%5D&name=image.png&originHeight=310&originWidth=322&originalType=binary&ratio=1&rotation=0&showTitle=false&size=27378&status=done&style=none&taskId=uaa34d0b6-4b12-49b4-9522-efe4bee8a81&title=&width=229.97915649414062)

例:查询孙风的考试成绩
image.png
1.查询出赵雷的2号学科考试成绩
select stu.s_name,sc.s_score
from student stu
join score sc
on stu.s_id=sc.s_id
where stu.s_name =’赵雷’ and sc.c_id=02;
2.查询出三号学科考了80分的学生生日
select stu.s_birth
from student stu
join score sc
on stu.s_id=sc.s_id
where sc.s_score =80 and sc.c_id=3;

左连接

  1. ![image.png](https://cdn.nlark.com/yuque/0/2021/png/21682952/1635256057344-72d43d25-900a-4f39-aeee-9460ae939966.png#clientId=u59df5ac3-0e2a-4&crop=0&crop=0&crop=1&crop=1&from=paste&height=399&id=ua9bde65f&margin=%5Bobject%20Object%5D&name=image.png&originHeight=583&originWidth=689&originalType=binary&ratio=1&rotation=0&showTitle=false&size=39920&status=done&style=none&taskId=u53bdfdef-61cd-41d0-92e6-feb407a3c9d&title=&width=471.4921875)<br /> 语法:select * from 表1<br /> left join 表 2<br /> on 表1.字段名=表2.字段名

image.png

右连接

语法:select * from 表1
right join 表 2
on 表1.字段名=表2.字段名
image.png

练习:

33、查询出名字叫周梅每门课的成绩
34、查询出李四教的所有学生
35、查询出李四老师教的课程的平均成绩
36、查询出每个老师教的课程的平均成绩
37、查询出每个老师教的学生人数
38、查询出选课人数不低于2人的课程和老师姓名
39、查询赵雷的语文成绩分数是多少
40、查询 钱电的数学成绩是 多少
41、查询 李云的英语成绩是 多少
42、查询 周梅的 语文老师姓名是什么 多少ID
43、查询 郑竹的任课老师是 多少id 叫什么名字
44、查询 孙风的 语文老师姓名是 多少
45、查询 孙风哪几门科目参加了考试
答案:
— 33、查询出名字叫周梅每门课的成绩
SELECT s.s_name,c.c_name,sc.s_score
FROM student s
JOIN score sc JOIN course c ON s.s_id = sc.s_id AND sc.c_id = c.c_id
WHERE s.s_name = ‘周梅’ ;
— 34、查询出李四教的所有学生
SELECT t.t_name,s.s_name
FROM student s JOIN score sc JOIN course c JOIN teacher t
ON s.s_id = sc.s_id AND sc.c_id = c.c_id AND t.t_id=c.t_id
WHERE t.t_name=’李四’;
— 35、查询出李四老师教的课程的平均成绩
SELECT AVG(sc.s_score)
FROM score sc JOIN course c JOIN teacher t
ON sc.c_id = c.c_id AND t.t_id=c.t_id
WHERE t.t_name=’李四’;
— 36、查询出每个老师教的课程的平均成绩
SELECT t.t_name,c.c_name,AVG(sc.s_score)
FROM score sc JOIN course c JOIN teacher t
ON sc.c_id = c.c_id AND t.t_id=c.t_id
GROUP BY t.t_name/c.c_name;
— 37、查询出每个老师教的学生人数
SELECT t.t_name,COUNT()
FROM score sc JOIN course c JOIN teacher t
ON sc.c_id = c.c_id AND t.t_id=c.t_id
GROUP BY t.t_name;
— 38、查询出选课人数不低于2人的课程和老师姓名
SELECT t.t_name,c.c_name
FROM score sc JOIN course c JOIN teacher t
ON sc.c_id = c.c_id AND t.t_id=c.t_id
GROUP BY t.t_name/ c.c_name
HAVING COUNT(
) >=2;
— 39、查询赵雷的语文成绩分数是多少
SELECT s_score,s_name,c_name
from student st
join score sc on st.s_id=sc.s_id
join course co on sc.c_id=co.c_id
where s_name=”赵雷” and c_name=”语文”;
— 40、查询 钱电的数学成绩是 多少
SELECT s_score,s_name,c_name
from student st
join score sc on st.s_id=sc.s_id
join course co on sc.c_id=co.c_id
where s_name=”钱电” and c_name=”数学”;
— 41、查询 李云的英语成绩是 多少
SELECT s_score,s_name,c_name
from student st
join score sc on st.s_id=sc.s_id
join course co on sc.c_id=co.c_id
where s_name=”李云” and c_name=”英语”;
— 42、查询 周梅的 语文老师姓名是什么 多少ID
SELECT te.t_id,t_name
from student st
join score sc on st.s_id=sc.s_id
join course co on sc.c_id=co.c_id
join teacher te on co.t_id=te.t_id
where s_name=”周梅” and c_name=”语文”;
— 43、查询 郑竹的任课老师是 多少id 叫什么名字
SELECT te.t_id,t_name
from student st
join score sc on st.s_id=sc.s_id
join course co on sc.c_id=co.c_id
join teacher te on co.t_id=te.t_id
where s_name=”郑竹”;
— 44、查询 孙风的 语文老师姓名是 多少
SELECT te.t_id,t_name
from student st
join score sc on st.s_id=sc.s_id
join course co on sc.c_id=co.c_id
join teacher te on co.t_id=te.t_id
where s_name=”孙风” and c_name=”语文”;
— 45、查询 孙风哪几门科目参加了考试
SELECT s_name,c_name,s_score
from student st
join score sc on st.s_id=sc.s_id
join course co on sc.c_id=co.c_id
WHERE s_name=”孙风”;

练习:

1)查询“计算机”专业学生在“2007-12-15”至“2008-1-8”时间段内借书的
学生编号、学生名称、图书编号、图书名称、借出日期;
2)查询借过图书的学生编号、学生名称、专业;
3)查询借过作者为“霍达”的图书的学生姓名、图书名称、借出日期、归还日期;
book表 image.png
borrow表image.png
student70表image.png
答案:
1)查询“计算机”专业学生在“2007-12-15”至“2008-1-8”时间段内借书的
学生编号、学生名称、图书编号、图书名称、借出日期;
select stu.stuid,stu.stuname,b.bid,b.title,bo.t_time
from student70 stu
inner join borrow bo
on stu.stuid = bo.stuid
inner join book b
on b.bid = bo.bid
where stu.major = ‘计算机’ and bo.t_time between ‘2007-12-15’ and ‘2008-1-8’;
2)查询借过图书的学生编号、学生名称、专业;
select stu.stuid,stu.stuname,stu.major
from student70 stu
inner join borrow bo
on stu.stuid = bo.stuid
where bo.t_time is not null;
3)查询借过作者为“霍达”的图书的学生姓名、图书名称、借出日期、归还日期;
select stu.stuname,b.title,bo.t_time,bo.b_time
from student70 stu
inner join borrow bo