文档版本 开发工具 测试平台 工程名字 日期 作者 备注 V1.0 2016.05.10 lutianfei none SQL多表操作 外键约束 作用:保证数据的完整性。 定义外键约束 可以直接在create语句中定义外键 foreign key 当前表名 (字段名) references 目标表名 (目标表的主键) 创
文档版本 | 开发工具 | 测试平台 | 工程名字 | 日期 | 作者 | 备注 |
---|---|---|---|---|---|---|
V1.0 | 2016.05.10 | lutianfei | none |
定义外键约束
创建完语句后,可以直接使用修改语句定义
alter table 表名 add foreign key 当前表名 (字段名) references 目标表名 (目标表的主键);
作用:保证数据的完整性。
例子
有一个部门的表,还有一个员工表,
<code class=" hljs sql"> <span class="hljs-operator"><span class="hljs-keyword">create</span> <span class="hljs-keyword">database</span> day16;</span> use day16; <span class="hljs-operator"><span class="hljs-keyword">create</span> <span class="hljs-keyword">table</span> dept( did <span class="hljs-keyword">int</span> <span class="hljs-keyword">primary</span> <span class="hljs-keyword">key</span> auto_increment, dname <span class="hljs-keyword">varchar</span>(<span class="hljs-number">30</span>) );</span> <span class="hljs-operator"><span class="hljs-keyword">create</span> <span class="hljs-keyword">table</span> emp( eid <span class="hljs-keyword">int</span> <span class="hljs-keyword">primary</span> <span class="hljs-keyword">key</span> auto_increment, ename <span class="hljs-keyword">varchar</span>(<span class="hljs-number">20</span>), salaly <span class="hljs-keyword">double</span>, dno <span class="hljs-keyword">int</span> );</span> <span class="hljs-operator"><span class="hljs-keyword">insert</span> <span class="hljs-keyword">into</span> dept <span class="hljs-keyword">values</span>(<span class="hljs-keyword">null</span>,<span class="hljs-string">'研发部'</span>);</span> <span class="hljs-operator"><span class="hljs-keyword">insert</span> <span class="hljs-keyword">into</span> dept <span class="hljs-keyword">values</span>(<span class="hljs-keyword">null</span>,<span class="hljs-string">'销售部'</span>);</span> <span class="hljs-operator"><span class="hljs-keyword">insert</span> <span class="hljs-keyword">into</span> dept <span class="hljs-keyword">values</span>(<span class="hljs-keyword">null</span>,<span class="hljs-string">'人事部'</span>);</span> <span class="hljs-operator"><span class="hljs-keyword">insert</span> <span class="hljs-keyword">into</span> dept <span class="hljs-keyword">values</span>(<span class="hljs-keyword">null</span>,<span class="hljs-string">'扯淡部'</span>);</span> <span class="hljs-operator"><span class="hljs-keyword">insert</span> <span class="hljs-keyword">into</span> dept <span class="hljs-keyword">values</span>(<span class="hljs-keyword">null</span>,<span class="hljs-string">'牛宝宝部'</span>);</span> <span class="hljs-operator"><span class="hljs-keyword">insert</span> <span class="hljs-keyword">into</span> emp <span class="hljs-keyword">values</span>(<span class="hljs-keyword">null</span>,<span class="hljs-string">'班长'</span>,<span class="hljs-number">10000</span>,<span class="hljs-number">1</span>);</span> <span class="hljs-operator"><span class="hljs-keyword">insert</span> <span class="hljs-keyword">into</span> emp <span class="hljs-keyword">values</span>(<span class="hljs-keyword">null</span>,<span class="hljs-string">'美美'</span>,<span class="hljs-number">10000</span>,<span class="hljs-number">2</span>);</span> <span class="hljs-operator"><span class="hljs-keyword">insert</span> <span class="hljs-keyword">into</span> emp <span class="hljs-keyword">values</span>(<span class="hljs-keyword">null</span>,<span class="hljs-string">'小凤'</span>,<span class="hljs-number">12000</span>,<span class="hljs-number">3</span>);</span> <span class="hljs-operator"><span class="hljs-keyword">insert</span> <span class="hljs-keyword">into</span> emp <span class="hljs-keyword">values</span>(<span class="hljs-keyword">null</span>,<span class="hljs-string">'如花'</span>,<span class="hljs-number">14000</span>,<span class="hljs-number">2</span>);</span> <span class="hljs-operator"><span class="hljs-keyword">insert</span> <span class="hljs-keyword">into</span> emp <span class="hljs-keyword">values</span>(<span class="hljs-keyword">null</span>,<span class="hljs-string">'芙蓉'</span>,<span class="hljs-number">11000</span>,<span class="hljs-number">1</span>);</span> <span class="hljs-operator"><span class="hljs-keyword">insert</span> <span class="hljs-keyword">into</span> emp <span class="hljs-keyword">values</span>(<span class="hljs-keyword">null</span>,<span class="hljs-string">'东东'</span>,<span class="hljs-number">800</span>,<span class="hljs-keyword">null</span>);</span> <span class="hljs-operator"><span class="hljs-keyword">insert</span> <span class="hljs-keyword">into</span> emp <span class="hljs-keyword">values</span>(<span class="hljs-keyword">null</span>,<span class="hljs-string">'波波'</span>,<span class="hljs-number">1000</span>,<span class="hljs-keyword">null</span>);</span> <span class="hljs-operator"><span class="hljs-keyword">update</span> emp <span class="hljs-keyword">set</span> salaly=<span class="hljs-number">2500</span> <span class="hljs-keyword">where</span> eid = <span class="hljs-number">5</span>;</span> </code>
一对多:在多方
需要添加一个字段,并且和一方
主键的类型必须是相同的。
建表原则(都是在多方表中维护):
画图举例部门与员工的关系
在生活中一个学生可以选择多门课程,每一门课程也可以被多个学生所选择。这些例子在数据库设计中可以使用多对多来完成。
建表原则:
外键
指向两个多对多关系表的主键
。 主键对应
唯一外键对应
笛卡尔积的概念:(了解)
查询的语法
select * from 表A,表B; 返回的结果就是笛卡尔积。
多表查询时,如果不指定表之间的连接条件,则连接变成笛卡尔乘积操作,笛卡尔积的效果如本页所示。
进行笛卡尔积后,查询结果中存在大量无意义的数据,示例中只有A表中的A_ID和B表中A_ID一致的行才有意义,这样我们通过加上WHERE过滤条件得到想要的数据。这种横跨多表的查询操作一般用连接完成。
如果一张表的记录为m,另一张表的记录是n,两个表做交叉连接后,查询结果的数量为m*n
条。
左外链接
(看左表,把左表所有的数据全部查询出来)
右外链接
(看右表,把右表所有的数据全部查询出来)
外链接与内连接的关系
嵌套查询
,也称为子查询
。外层的查询块称为父查询
,内层的查询块称为子查询
。
语法:select * from table where 条件 > (select * from table where 条件)
>any
大于结果的最小值>all
大于结果的最大值<code class=" hljs sql"><span class="hljs-operator"><span class="hljs-keyword">create</span> <span class="hljs-keyword">table</span> dept( did <span class="hljs-keyword">int</span> <span class="hljs-keyword">primary</span> <span class="hljs-keyword">key</span> auto_increment, dname <span class="hljs-keyword">varchar</span>(<span class="hljs-number">30</span>) );</span> <span class="hljs-operator"><span class="hljs-keyword">create</span> <span class="hljs-keyword">table</span> emp( eid <span class="hljs-keyword">int</span> <span class="hljs-keyword">primary</span> <span class="hljs-keyword">key</span> auto_increment, ename <span class="hljs-keyword">varchar</span>(<span class="hljs-number">20</span>), salaly <span class="hljs-keyword">double</span>, dno <span class="hljs-keyword">int</span> );</span> //查看所有人所属的部门名称和员工名称? //方法一: <span class="hljs-operator"><span class="hljs-keyword">select</span> dept.dname,emp.ename <span class="hljs-keyword">from</span> dept,emp <span class="hljs-keyword">where</span> dept.did = emp.dno;</span> //方法二: <span class="hljs-operator"><span class="hljs-keyword">select</span> d.dname,e.ename <span class="hljs-keyword">from</span> dept d,emp e <span class="hljs-keyword">where</span> d.did = e.dno;</span> //统计每个部门的人数(按照部门名称统计,分组group by count) <span class="hljs-operator"><span class="hljs-keyword">select</span> d.dname,<span class="hljs-aggregate">count</span>(*) <span class="hljs-keyword">from</span> dept d,emp e <span class="hljs-keyword">where</span> d.did = e.dno <span class="hljs-keyword">group</span> <span class="hljs-keyword">by</span> d.dname;</span> //统计部门的平均工资(按部门名称统计 ,分组group by avg) <span class="hljs-operator"><span class="hljs-keyword">select</span> d.dname,<span class="hljs-aggregate">avg</span>(salaly) <span class="hljs-keyword">from</span> dept d,emp e <span class="hljs-keyword">where</span> d.did = e.dno <span class="hljs-keyword">group</span> <span class="hljs-keyword">by</span> d.dname;</span> //统计部门的平均工资大于公司平均工资的部门(子查询) * 公司的平均工资 <span class="hljs-operator"><span class="hljs-keyword">select</span> <span class="hljs-aggregate">avg</span>(salaly) <span class="hljs-keyword">from</span> emp;</span> * 部门的平均工资 <span class="hljs-operator"><span class="hljs-keyword">select</span> d.dname,<span class="hljs-aggregate">avg</span>(e.salaly) <span class="hljs-keyword">as</span> sa <span class="hljs-keyword">from</span> dept d,emp e <span class="hljs-keyword">where</span> d.did = e.dno <span class="hljs-keyword">group</span> <span class="hljs-keyword">by</span> d.dname <span class="hljs-keyword">having</span> sa > (<span class="hljs-keyword">select</span> <span class="hljs-aggregate">avg</span>(salaly) <span class="hljs-keyword">from</span> emp);</span></code>
<code class=" hljs sql"><span class="hljs-operator"><span class="hljs-keyword">DROP</span> <span class="hljs-keyword">TABLE</span> dept;</span> <span class="hljs-comment">--部门表</span> <span class="hljs-operator"><span class="hljs-keyword">CREATE</span> <span class="hljs-keyword">TABLE</span> dept( deptno <span class="hljs-keyword">int</span> <span class="hljs-keyword">PRIMARY</span> <span class="hljs-keyword">KEY</span>, dname <span class="hljs-keyword">VARCHAR</span>(<span class="hljs-number">14</span>) , --部门名称 loc <span class="hljs-keyword">VARCHAR</span>(<span class="hljs-number">13</span>) ---部门地址 ) ;</span> <span class="hljs-operator"><span class="hljs-keyword">CREATE</span> <span class="hljs-keyword">TABLE</span> dept( deptno <span class="hljs-keyword">int</span> <span class="hljs-keyword">PRIMARY</span> <span class="hljs-keyword">KEY</span>, dname <span class="hljs-keyword">VARCHAR</span>(<span class="hljs-number">14</span>) , loc <span class="hljs-keyword">VARCHAR</span>(<span class="hljs-number">13</span>) ) ;</span> <span class="hljs-operator"><span class="hljs-keyword">INSERT</span> <span class="hljs-keyword">INTO</span> DEPT <span class="hljs-keyword">VALUES</span> (<span class="hljs-number">10</span>,<span class="hljs-string">'ACCOUNTING'</span>,<span class="hljs-string">'NEW YORK'</span>);</span> <span class="hljs-operator"><span class="hljs-keyword">INSERT</span> <span class="hljs-keyword">INTO</span> DEPT <span class="hljs-keyword">VALUES</span> (<span class="hljs-number">20</span>,<span class="hljs-string">'RESEARCH'</span>,<span class="hljs-string">'DALLAS'</span>);</span> <span class="hljs-operator"><span class="hljs-keyword">INSERT</span> <span class="hljs-keyword">INTO</span> DEPT <span class="hljs-keyword">VALUES</span> (<span class="hljs-number">30</span>,<span class="hljs-string">'SALES'</span>,<span class="hljs-string">'CHICAGO'</span>);</span> <span class="hljs-operator"><span class="hljs-keyword">INSERT</span> <span class="hljs-keyword">INTO</span> DEPT <span class="hljs-keyword">VALUES</span> (<span class="hljs-number">40</span>,<span class="hljs-string">'OPERATIONS'</span>,<span class="hljs-string">'BOSTON'</span>);</span> <span class="hljs-operator"><span class="hljs-keyword">drop</span> <span class="hljs-keyword">table</span> emp;</span> <span class="hljs-comment">--员工表</span> <span class="hljs-operator"><span class="hljs-keyword">CREATE</span> <span class="hljs-keyword">TABLE</span> emp( empno <span class="hljs-keyword">int</span> <span class="hljs-keyword">PRIMARY</span> <span class="hljs-keyword">KEY</span>, --员工编号 ename <span class="hljs-keyword">VARCHAR</span>(<span class="hljs-number">10</span>), ---员工姓名 job <span class="hljs-keyword">VARCHAR</span>(<span class="hljs-number">9</span>), --员工工作 mgr <span class="hljs-keyword">int</span>, ----员工直属领导编号 hiredate <span class="hljs-keyword">DATE</span>, ----入职时间 sal <span class="hljs-keyword">double</span>, ---工资 comm <span class="hljs-keyword">double</span>, --奖金 deptno <span class="hljs-keyword">int</span> <span class="hljs-keyword">REFERENCES</span> dept);</span> <span class="hljs-comment">--关联dept表</span> <span class="hljs-operator"><span class="hljs-keyword">CREATE</span> <span class="hljs-keyword">TABLE</span> emp( empno <span class="hljs-keyword">int</span> <span class="hljs-keyword">PRIMARY</span> <span class="hljs-keyword">KEY</span>, ename <span class="hljs-keyword">VARCHAR</span>(<span class="hljs-number">10</span>), job <span class="hljs-keyword">VARCHAR</span>(<span class="hljs-number">9</span>), mgr <span class="hljs-keyword">int</span>, hiredate <span class="hljs-keyword">DATE</span>, sal <span class="hljs-keyword">double</span>, comm <span class="hljs-keyword">double</span>, deptno <span class="hljs-keyword">int</span> , <span class="hljs-keyword">foreign</span> <span class="hljs-keyword">key</span> emp(deptno) <span class="hljs-keyword">REFERENCES</span> dept(deptno) );</span> <span class="hljs-operator"><span class="hljs-keyword">INSERT</span> <span class="hljs-keyword">INTO</span> EMP <span class="hljs-keyword">VALUES</span>(<span class="hljs-number">7369</span>,<span class="hljs-string">'SMITH'</span>,<span class="hljs-string">'CLERK'</span>,<span class="hljs-number">7902</span>,<span class="hljs-string">"1980-12-17"</span>,<span class="hljs-number">800</span>,<span class="hljs-keyword">NULL</span>,<span class="hljs-number">20</span>);</span> <span class="hljs-operator"><span class="hljs-keyword">INSERT</span> <span class="hljs-keyword">INTO</span> EMP <span class="hljs-keyword">VALUES</span>(<span class="hljs-number">7499</span>,<span class="hljs-string">'ALLEN'</span>,<span class="hljs-string">'SALESMAN'</span>,<span class="hljs-number">7698</span>,<span class="hljs-string">'1981-02-20'</span>,<span class="hljs-number">1600</span>,<span class="hljs-number">300</span>,<span class="hljs-number">30</span>);</span> <span class="hljs-operator"><span class="hljs-keyword">INSERT</span> <span class="hljs-keyword">INTO</span> EMP <span class="hljs-keyword">VALUES</span>(<span class="hljs-number">7521</span>,<span class="hljs-string">'WARD'</span>,<span class="hljs-string">'SALESMAN'</span>,<span class="hljs-number">7698</span>,<span class="hljs-string">'1981-02-22'</span>,<span class="hljs-number">1250</span>,<span class="hljs-number">500</span>,<span class="hljs-number">30</span>);</span> <span class="hljs-operator"><span class="hljs-keyword">INSERT</span> <span class="hljs-keyword">INTO</span> EMP <span class="hljs-keyword">VALUES</span>(<span class="hljs-number">7566</span>,<span class="hljs-string">'JONES'</span>,<span class="hljs-string">'MANAGER'</span>,<span class="hljs-number">7839</span>,<span class="hljs-string">'1981-04-02'</span>,<span class="hljs-number">2975</span>,<span class="hljs-keyword">NULL</span>,<span class="hljs-number">20</span>);</span> <span class="hljs-operator"><span class="hljs-keyword">INSERT</span> <span class="hljs-keyword">INTO</span> EMP <span class="hljs-keyword">VALUES</span>(<span class="hljs-number">7654</span>,<span class="hljs-string">'MARTIN'</span>,<span class="hljs-string">'SALESMAN'</span>,<span class="hljs-number">7698</span>,<span class="hljs-string">'1981-09-28'</span>,<span class="hljs-number">1250</span>,<span class="hljs-number">1400</span>,<span class="hljs-number">30</span>);</span> <span class="hljs-operator"><span class="hljs-keyword">INSERT</span> <span class="hljs-keyword">INTO</span> EMP <span class="hljs-keyword">VALUES</span>(<span class="hljs-number">7698</span>,<span class="hljs-string">'BLAKE'</span>,<span class="hljs-string">'MANAGER'</span>,<span class="hljs-number">7839</span>,<span class="hljs-string">'1981-05-01'</span>,<span class="hljs-number">2850</span>,<span class="hljs-keyword">NULL</span>,<span class="hljs-number">30</span>);</span> <span class="hljs-operator"><span class="hljs-keyword">INSERT</span> <span class="hljs-keyword">INTO</span> EMP <span class="hljs-keyword">VALUES</span>(<span class="hljs-number">7782</span>,<span class="hljs-string">'CLARK'</span>,<span class="hljs-string">'MANAGER'</span>,<span class="hljs-number">7839</span>,<span class="hljs-string">'1981-06-09'</span>,<span class="hljs-number">2450</span>,<span class="hljs-keyword">NULL</span>,<span class="hljs-number">10</span>);</span> <span class="hljs-operator"><span class="hljs-keyword">INSERT</span> <span class="hljs-keyword">INTO</span> EMP <span class="hljs-keyword">VALUES</span>(<span class="hljs-number">7788</span>,<span class="hljs-string">'SCOTT'</span>,<span class="hljs-string">'ANALYST'</span>,<span class="hljs-number">7566</span>,<span class="hljs-string">'1987-07-03'</span>,<span class="hljs-number">3000</span>,<span class="hljs-keyword">NULL</span>,<span class="hljs-number">20</span>);</span> <span class="hljs-operator"><span class="hljs-keyword">INSERT</span> <span class="hljs-keyword">INTO</span> EMP <span class="hljs-keyword">VALUES</span>(<span class="hljs-number">7839</span>,<span class="hljs-string">'KING'</span>,<span class="hljs-string">'PRESIDENT'</span>,<span class="hljs-keyword">NULL</span>,<span class="hljs-string">'1981-11-17'</span>,<span class="hljs-number">5000</span>,<span class="hljs-keyword">NULL</span>,<span class="hljs-number">10</span>);</span> <span class="hljs-operator"><span class="hljs-keyword">INSERT</span> <span class="hljs-keyword">INTO</span> EMP <span class="hljs-keyword">VALUES</span>(<span class="hljs-number">7844</span>,<span class="hljs-string">'TURNER'</span>,<span class="hljs-string">'SALESMAN'</span>,<span class="hljs-number">7698</span>,<span class="hljs-string">'1981-09-08'</span>,<span class="hljs-number">1500</span>,<span class="hljs-number">0</span>,<span class="hljs-number">30</span>);</span> <span class="hljs-operator"><span class="hljs-keyword">INSERT</span> <span class="hljs-keyword">INTO</span> EMP <span class="hljs-keyword">VALUES</span>(<span class="hljs-number">7876</span>,<span class="hljs-string">'ADAMS'</span>,<span class="hljs-string">'CLERK'</span>,<span class="hljs-number">7788</span>,<span class="hljs-string">'1987-07-13'</span>,<span class="hljs-number">1100</span>,<span class="hljs-keyword">NULL</span>,<span class="hljs-number">20</span>);</span> <span class="hljs-operator"><span class="hljs-keyword">INSERT</span> <span class="hljs-keyword">INTO</span> EMP <span class="hljs-keyword">VALUES</span>(<span class="hljs-number">7900</span>,<span class="hljs-string">'JAMES'</span>,<span class="hljs-string">'CLERK'</span>,<span class="hljs-number">7698</span>,<span class="hljs-string">'1981-12-03'</span>,<span class="hljs-number">950</span>,<span class="hljs-keyword">NULL</span>,<span class="hljs-number">30</span>);</span> <span class="hljs-operator"><span class="hljs-keyword">INSERT</span> <span class="hljs-keyword">INTO</span> EMP <span class="hljs-keyword">VALUES</span>(<span class="hljs-number">7902</span>,<span class="hljs-string">'FORD'</span>,<span class="hljs-string">'ANALYST'</span>,<span class="hljs-number">7566</span>,<span class="hljs-string">'1981-12-03'</span>,<span class="hljs-number">3000</span>,<span class="hljs-keyword">NULL</span>,<span class="hljs-number">20</span>);</span> <span class="hljs-operator"><span class="hljs-keyword">INSERT</span> <span class="hljs-keyword">INTO</span> EMP <span class="hljs-keyword">VALUES</span>(<span class="hljs-number">7934</span>,<span class="hljs-string">'MILLER'</span>,<span class="hljs-string">'CLERK'</span>,<span class="hljs-number">7782</span>,<span class="hljs-string">'1981-01-23'</span>,<span class="hljs-number">1300</span>,<span class="hljs-keyword">NULL</span>,<span class="hljs-number">10</span>);</span> <span class="hljs-operator"><span class="hljs-keyword">CREATE</span> <span class="hljs-keyword">TABLE</span> salgrade( grade <span class="hljs-keyword">int</span>,--等级 losal <span class="hljs-keyword">double</span>, --最低工资 hisal <span class="hljs-keyword">double</span> );</span> <span class="hljs-comment">--最高工次</span> <span class="hljs-operator"><span class="hljs-keyword">CREATE</span> <span class="hljs-keyword">TABLE</span> salgrade( grade <span class="hljs-keyword">int</span>, losal <span class="hljs-keyword">double</span>, hisal <span class="hljs-keyword">double</span> );</span> <span class="hljs-operator"><span class="hljs-keyword">INSERT</span> <span class="hljs-keyword">INTO</span> SALGRADE <span class="hljs-keyword">VALUES</span> (<span class="hljs-number">1</span>,<span class="hljs-number">700</span>,<span class="hljs-number">1200</span>);</span> <span class="hljs-operator"><span class="hljs-keyword">INSERT</span> <span class="hljs-keyword">INTO</span> SALGRADE <span class="hljs-keyword">VALUES</span> (<span class="hljs-number">2</span>,<span class="hljs-number">1201</span>,<span class="hljs-number">1400</span>);</span> <span class="hljs-operator"><span class="hljs-keyword">INSERT</span> <span class="hljs-keyword">INTO</span> SALGRADE <span class="hljs-keyword">VALUES</span> (<span class="hljs-number">3</span>,<span class="hljs-number">1401</span>,<span class="hljs-number">2000</span>);</span> <span class="hljs-operator"><span class="hljs-keyword">INSERT</span> <span class="hljs-keyword">INTO</span> SALGRADE <span class="hljs-keyword">VALUES</span> (<span class="hljs-number">4</span>,<span class="hljs-number">2001</span>,<span class="hljs-number">3000</span>);</span> <span class="hljs-operator"><span class="hljs-keyword">INSERT</span> <span class="hljs-keyword">INTO</span> SALGRADE <span class="hljs-keyword">VALUES</span> (<span class="hljs-number">5</span>,<span class="hljs-number">3001</span>,<span class="hljs-number">9999</span>);</span></code>
1、返回拥有员工的部门名、部门号。
2、工资水平多于smith的员工信息。
3、返回员工和所属经理的姓名。
4、返回雇员的雇佣日期早于其经理雇佣日期的员工及其经理姓名。
5、返回员工姓名及其所在的部门名称。
6、返回从事clerk工作的员工姓名和所在部门名称。
7、返回部门号及其本部门的最低工资。
8、返回销售部(sales)所有员工的姓名。
9、返回工资水平多于平均工资的员工。
10、返回与SCOTT从事相同工作的员工。
11、返回与30部门员工工资水平相同的员工姓名与工资。