小学生学习Java数据库2

“`
– 创建外键的第二种方式
create table student1 (sid int primary key,sname varchar(20));
create table score (sid int,score int);
– 给score表添加外键
alter table score add constraint fk_student1_score_sid foreign key (sid) references student1 (sid;
– 删除(要使用约束的别名来删除)
– 一个表可以有多个外键
– 注意 : 约束名(外键名字)不能重复
alter table score drop foreign key fk_student1_score_sid;

– 表和表之间的关系
– 多对多的关系(利用第三张表来表示关系的)
– 并且第三张表作为从表 拥有其他两个主表的外键
– 学生表 老师表 中间表 并建立多对多关系
create table student(sid int primary key,sname varchar(20));
create table teacher(tid int primary key,sname varchar(20));
create table teacher_student (sid int,tid int);

alter table teacher_student add constraint fk_teacher_tid foreign key (tid) references teacher (tid);
alter table teacher_student add constraint fk_student_sid foreign key (sid) references student (sid);
– 一对一关系(不常用 完成可以写成一张表)
– 如果考虑查询效率问题 可以对表进行拆分 拆分有度

– 合并查询
– union 取两个表的并集 必须字段名和类型相同
– union all 把两个表的数据合并到一起
select * from A union select * from B;
select * from A union all select * from B;

– 多表查询
– 这样查询会产生笛卡尔积 (会产生大量的无用数据)
select * from A,B;
– 学生表 和 分数表 一起查询
select * from student,score;
– 去除错误数据(利用两张表的编号相同去除)

– 99查询法(利用表中的字段相同 去除重复数据)
select * from student,score where student.stuid = score.stuid;

– 查询学生的编号和学生的分数
select student.stuid,score.score from student,score where student.stuid = score.stuid;

– 别名
select s.stuid,c.score from student s,score c where s.stuid=c.stuid;

– 创建科目表
create table course (courseid int,cname varchar(20));

– 3张表查询
select * from student,score,course;

– 去除重复数据(通过字段的联系)

扫描二维码关注公众号,回复: 1757367 查看本文章

select s.stuname,c.score,o.cname from student s,score c,course o where s.stuid=c.stuid and c.courseid=o.courseid;

– 连接查询(多表查询)
– 内连接(inner 可以省略)
– on 后面是去除重复数据的条件
select * from student s inner join score c on s.stuid=c.stuid;

– 3表内连接
select * from student s inner join score c on s.stuid=c.stuid join course o on c.courseid=o.courseid;

– 查询表中80分以上的学生的 姓名 分数 科目信息
select s.stuname,c.score,o.cname from student s inner join score c on s.stuid=c.stuid join course o on c.courseid=o.courseid where c.score >80;

– 外连接 左外连接 右外连接
– 关键词 outer 可以省略
– 左外连接 就是以左边的表为主 会查询出左边的表为主
– 右外连接 反之
– 学生和分数表
select * from score left join student on student.stuid=score.stuid;

– 自然连接 关键词 natural join
– 可以自动匹配两个表中 相同字段的值
– 字段名和类型相同
select * from student NATURAL join score;

– 子查询 (嵌套查询)
– 员工表和部门表来测试
– 查询工资高于jones的员工信息
select * from emp where sal>(select sal from emp where ename =’JONES’);

– 查询与SCOTT同一个部门的员工。
select * from emp where deptno = (
select deptno from emp where ename =’SCOTT’);

– 工资高于30号部门所有人的员工信息
select * from emp where sal> (
select max(sal) from emp where deptno=30);

– 查询工作和工资与MARTIN(马丁)完全相同的员工信息
select * from emp where (job,sal) in (
select job,sal from emp where ename = ‘MARTIN’);

– 有2个以上直接下属的员工信息
select * from emp where empno in (
select mgr from emp group by mgr having count(mgr)>=2);

– 查询员工编号为7788的员工名称、员工工资、部门名称、部门地址
select e.ename,e.sal,d.dname,d.loc from emp e,dept d where e.deptno= d.deptno and e.empno = 7788;

– 求各个部门薪水最高的员工所有信息
– 把查询出来的结果当做一张表 来连接查询
select deptno,max(sal) msal from emp group by deptno;
select * from emp e1,(select deptno,max(sal) msal from emp group by deptno) e2 where e1.sal = e2.msal and e1.deptno = e2.deptno;

– 自连接查询
– 要查找的信息在一张表中 但一次查不出来 把自己这个表当做 两个表 来连接查询
– 求7369员工编号、姓名、经理编号和经理姓名
select e1.empno,e1.ename,e2.ename from emp e1,emp e2 where e1.empno=e2.mgr and e2.empno=7369;

猜你喜欢

转载自blog.csdn.net/zmw1224/article/details/80697623