创建表的时候增加外键:在所有的表字段之后,使用foreign key(外键字段) references 外部表(主键字段)
在新增表之后增加外键:修改表结构,使用alter table 表名 add [constraint 外键名字] foreign key(外键字段) references 父表(主键字段)
修改外键&删除外键
alter table 表名 drop foreign key 外键名
外键条件
外键要存在,首先必须保证表的存储引擎是innodb
列类型必须与父表的主键类型一致
一张表中的外键名字不能重复
增加外键的字段数据已经存在,必须保证数据与父表主键要求对应
外键约束
有三种约束模式
district:严格模式(默认的)
cascade:级联模式
set null:置空模式
语法:foreign key(外键字段) references 父表(主键字段) on delete 模式 on update 模式
联合查询
基本语法:
select 语句1
union [union 选项]
select 语句2……
union 选项
all:保留所有,不管重复
distinct:去重,默认的
子查询(sub query)
按位置分类
from子查询
where子查询
exists子查询
按结果分类
标量子查询
列子查询
行子查询
表子查询
子查询
列子查询
=any等价于in -- 其中一个即可
any等价于some-- 二者是一样的
=all为全部
-- 创建外键
create table my_foreign1(
idint primary key auto_increment,
name varchar (20)not null comment
'学生姓名',
c_idint comment'班级id',
-- 增加外键
foreign key(c_id)references
my_class(id)
)charset utf8
-- 创建表
create table my_foreign2(
idint primary key auto_increment,
name varchar (20)not null comment
'学生姓名',
c_idint comment'班级id' -- 普通字段
)charset utf8
-- 增加外键
alter table my_foreign2add
-- 指定外键的名字
constraint student_class_1 -- 可以指定多个外键 但是名字不能相同
-- 指定外键的字段
foreign key(c_id)
-- 引用父表主键
references my_class(id)
-- 删除外键
alter table my_foreign1drop
foreign key my_foreign1_ibfk_1 -- my_foreign1_ibfk_1 通过外键的名字来删
-- 插入数据;外键字段在父表不存在
insert into my_foreign2values (
null,'郭富城',4) -- 没有4号班级
insert into my_foreign2values (
null,'项羽',1)
insert into my_foreign2values (
null,'刘邦',2)
insert into my_foreign2values (
null,'韩信',3)
-- 更新父表的记录
update my_classset id=4 where id=1 -- 失败;id=1记录已经被学生引用
update my_foreign2set c_id=2 where id=4 -- 更新
update my_classset id=4 where id=3 -- 可以;没有学生引用此班级
-- mysql中添加外键约束遇到一下情况:
-- cannot add foreign key constraint
-- 出现这个问题的原因是,外键的使用:
-- 1. 外键字段不能为该表的主键;
-- 2. 外键字段参考字段必须为参考表的主键
-- 插入数据
insert into my_foreign1values (
null,'马超','3'
)
-- 增加外键
alter table my_foreign1add
foreign key(c_id)references
my_class(id) -- 失败;因为没有3号班了
-- 创建外键,指定模式;删除置空;更新级联
create table my_foreign3(
idint primary key auto_increment,
name varchar (20)not null,
c_idint,
-- 增加外键
foreign key (c_id)
-- 引用表
references my_class(id)
-- 指定删除模式
on delete set null
-- 指定更新模式
on update cascade
)charset utf8
-- 插入数据
insert into my_foreign3values (
null,'刘备',1),
(null,'曹 *** ',1),
(null,'孙权',1),
(null,'祝贺量',2),
(null,'周瑜',2)
-- 解除My_foreign2表的外键
alter table my_foreign2drop
foreign key student_class_1
-- 更新父表主键
update my_classset id=3 where id=1
-- 删除父表主键
delete from my_classwhere id=2
-- 联合查询
select * from my_class
union -- 默认去重
select * from my_class
select * from my_class
union all -- 不去重
select * from my_class
select id,c_name,roomfrom my_class
union all -- 不去重
select name,number,idfrom my_student
-- 需求;男生升序;女生降序(年龄)
(select * from my_student
where sex='男'
order by ageasc limit9999999)
union
(select * from my_student
where sex='女'
order by agedesc limit9999999)
select * from my_studentwhere
c_id=(
-- 标量子查询
select idfrom my_classwhere
c_name='python1903')-- id一定只有一个值(一行一列)
insert into my_classvalues (1,
'python1907','B407')
-- 列子查询
select * from my_studentwhere
c_idin(select idfrom my_class)
-- any,some,all
select * from my_studentwhere
c_id=any(select idfrom my_class)
select * from my_studentwhere
c_id=some(select idfrom my_class)
select * from my_studentwhere
c_id=all(select idfrom my_class)
select * from my_studentwhere
c_id!=any(select idfrom my_class) -- 所有结果(null除外)
select * from my_studentwhere
c_id!=some(select idfrom my_class) -- 所有结果(null除外)
select * from my_studentwhere
c_id!=all(select idfrom my_class) -- 所有2号班级(null除外)
select * from my_studentwhere
age=(select max(age)from
my_student)
and
height=(select max(height))from
my_student)
-- 行子查询
select * from my_student
-- (age,height)称之内为行元素
where (age,height)=(select max(
age),max(height)from my_student)
update my_studentset height=188
where name='王五'
select * from my_studentorder by
agedesc,heightdesc limit1
select * from my_studentorder by
heightdesc
-- 表子查询
select * from my_studentgroup by
c_idorder by heightdesc -- 每个班选出第一个学生再按身高排序
select * from (select * from
my_studentorder by heightdesc)
as studentgroup by student.c_id
1)查询表中的前8条记录select * from area where rownum <= 8
查询结果如下:
2)查询第2到第8条记录
对于这种形式的查询,oracle不像mysql那么方便,它必须使用子查询或者是集合 *** 作来实现。我们可以使用以下3种方式可以实现:
A: select id,province,city,district from (select id,province,city,district,rownum as num from area) where num between 2 and 8
首先根据select id,province,city,district,rownum as num from area得到一个临时表,这个临时表中有一个rownum列(一个伪列,类似与rowid,但又不同于rowid,因为rowid是物理存在的一个列,也就是说Oracle数据库中任何一个表都有一个rowid列,而rownum不是物理存在的),然后在临时表中来查询。
B: select * from area where rownum <= 8 minus select * from area where rownum <2
使用集合减运算符minus,该 *** 作返回在第一个select中出现而不在第二个select中出现的记录。
C: select id,province,city,district from (select id,province,city,district,rownum as num from area) where num >=2
intersect
select * from area where rownum <= 8
使用集合交运算符intersect,这里绕了一个弯(不过这个弯实现了rownum大于某个数的查询),它是首先利用A的方式查询得到所有rownum大于2的记录,然后再与rownum小于等于8的记录集合做交运算。三种 *** 作得到的结果一样,如下图所示:
3)rownum需要注意的问题
[1] rownum不支持以下方式的查询
a: select * from area where rownum >2
b: select * from area where rownum = n–where n is a integer number lager than 1
注:rownum只支持select * from area where rownum =1的查询。Oracle的官方文档说明如下:
Conditions testing for ROWNUM values greater than a positive integer are always false.
For example, this query returns no rows:
SELECT * FROM employees
WHERE ROWNUM >1
The first row fetched is assigned a ROWNUM of 1 and makes the condition false. The
second row to be fetched is now the first row and is also assigned a ROWNUM of 1 and
makes the condition false. All rows subsequently fail to satisfy the condition, so no
rows are returned.
因为rownum是根据查询的结果集来对记录进行编号,所以当你查询rownum大于2的记录时会得到一个空的结果集。因为当oracle查询得到第1条记录时,发现rownum为1不满足条件,然后就继续查询第2条记录,但此时第2条记录又被编号为1(也即rownum变为1),所以查询得到的始终是rownum=1,因此无法满足约束,最终查询的结果集为空。
[2] rownum的排序查询问题
Rownum的排序查询是根据表中数据的初始顺序来进行的。Oracle官方文档中说明如下:
If an ORDER BY clause follows ROWNUM in the same query, then the rows will be
reordered by the ORDER BY clause. The results can vary depending on the way the
rows are accessed. For example, if the ORDER BY clause causes Oracle to use an index
to access the data, then Oracle may retrieve the rows in a different order than without
the index.
例如:select * from area where rownum <= 8 order by district
其结果如下图所示:
发现没有,它只对area表中的前8条记录进行排序。那么,如果我要取表中的前8条记录并且要求是全表有序,那怎么办呢?还是老办法,使用子查询。我们可以使用以下语句得到:
select * from (select * from area order by district)
where rownum <= 8
查询的结果如下图所示:
结束语:
Oracle中的rownum与mysql的limit实现的功能相同,但没有mysql来的容易,它一般通过一个子查询来实现。mysql的易用性也是它能够纵横开源数据库的原因,它不像postgresql那样的学院派,它的那种简单易用性或许在大型软件项目的开发中值得借鉴。最近听说sql server 2008也实现了limit的查询,不过还没去试过,Oracle在这方面也要加油啊,用户容易使用才是王道
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)