html外键如何添加,HTML基础知识(30)外键的运用、条件及约束、

一、增加外键

创建表的时候增加外键:在所有的表字段之后,使用foreign key(外键字段) references 外部表(主键字段)

在新增表之后增加外键:修改表结构,使用alter table 表名 add [constraint 外键名字] foreign key(外键字段) references 父表(主键字段);

二、修改外键&删除外键

alter table 表名 drop foreign key 外键名;

三、外键条件

外键要存在,首先必须保证表的存储引擎是innodb

列类型必须与父表的主键类型一致

一张表中的外键名字不能重复

增加外键的字段数据已经存在,必须保证数据与父表主键要求对应

四、外键约束

有三种约束模式

1、district:严格模式(默认的)

2、cascade:级联模式

3、set null:置空模式

语法:foreign key(外键字段) references 父表(主键字段) on delete 模式 on update 模式;

五、联合查询

1、基本语法:

select 语句1

union [union 选项]

select 语句2……

2、union 选项

all:保留所有,不管重复

distinct:去重,默认的

六、按位置分类

from子查询

where子查询

exists子查询

七、按结果分类

标量子查询

列子查询

行子查询

表子查询

————————代码练习————————————————

-- 创建外键

create table my_foreign1(

idint primary key auto_increment,

namevarchar(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,

namevarchar(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;

-- 插入数据:外键字段在父表不存在

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_classset id=5 where id=3;-- 可以改,没有学生引用此班级

-- 插入数据

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,

namevarchar(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);

select *from my_studentorder by agedesc,heightdesc limit1;

-- 表子查询

select *from my_studentgroup by c_idorder by heightdesc;-- 每个班选出的第一个学生

select *from (select *from my_studentorder by heightdesc)

as studentgroup by c_id;-- 每个班选出的第一个学生再按身高排序

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值