成都创新互联网站制作重庆分公司

怎么查mysql外键约束,mysql外键约束有哪些

SQL数据库的、外键和查询

增加外键

新罗ssl适用于网站、小程序/APP、API接口等需要进行数据传输应用场景,ssl证书未来市场广阔!成为成都创新互联的ssl证书销售渠道,可以享受市场价格4-6折优惠!如果有意向欢迎电话联系或者加微信:028-86922220(备注:SSL证书合作)期待与您的合作!

创建表的时候增加外键:在所有的表字段之后,使用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;

如何在MySQL中设置外键约束以及外键的作用

1.外键的作用,主要有两个:

一个是让数据库自己通过外键来保证数据的完整性和一致性

一个就是能够增加ER图的可读性

2.外键的配置

1)先创建一个主表,代码如下:

#创建表student,并添加各种约束

create

table

student

(

id

int

primary

key

,

#主键约束

name

varchar(20)

,

#唯一约束

age

int

NOT

NULL,

#非空约束

sex

varchar(2)

,

address

varchar(20)

default

'重庆'

#默认约束

)

;

再通过一个外键,创建一个分数表,这样的话,就可以方便查询。代码如下:

#创建分数表

create

table

score

(

id

int

primary

key

,

sid

int

,

china

int

,

history

int,

english

int,

constraint

FK_sid

foreign

key(sid)

references

student(id)

#通过外键创建链接

)

;

创建外键的方法有很多,其中最常见创建外键的格式是:constraint

FK_***

foreign

key(**)

references

链接的外表

删除外键:

alter

table

drop

foreign

key

'外键名'.

注意:

只有在定义外键时,用constraint

外键名

foreign

key

....

方便进行外键的删除

MySQL — 关联

来自MySQL的学习笔记,写的不对的地方大家多多指教哦

什么是外键?

假设有 2 个表,分别是表 A 和表 B,它们通过一个公共字段“id”发生关联关系,我们把这个关联关系叫做 R。如果“id”在表 A 中是主键,那么,表 A 就是这个关系 R 中的主表。相应的,表 B 就是这个关系中的从表,表 B 中的“id”,就是表 B 用来引用表 A 中数据的,叫外键。所以,外键就是从表中用来引用主表中数据的那个公共字段。

语法结构:

在创建表时添加外键约束:

在修改表时定义外键约束:

例子1:创建表时添加外键约束

首先创建主表:importhead

创建从表:test_mysql.importdetails

查询外键约束的相关信息:

查询结果为:

例子2:修改表时定义外键约束

修改表时定义从表test_mysql.importdetails的外键约束

删除外键约束使用DROP,语法结构为:

例子:删除从表test_mysql.importdetails的外键约束

在 MySQL 中,有 2 种类型的连接,分别是内连接(INNER JOIN)和外连接(OUTER JOIN)。

在 MySQL 里面,关键字 JOIN、INNER JOIN、CROSS JOIN 的含义是一样的,都表示内连接。我们可以通过 JOIN 把两个表关联起来,来查询两个表中的数据。

例子:有一张销售表,如下图:

有一张会员信息表,如下图:

通过内连接,查询会员的销售记录:

运行语句,结果如下:

根据上面的结果,其实可以得知:内连接查询到结果集为两个表的交集部分。

跟内连接只返回符合连接条件的记录不同的是,外连接还可以返回表中的所有记录,它包括两类,分别是左连接和右连接。

例子1:左外连接

如果需要查询所有销售记录,则可以使用左外连接

运行语句,结果为:

从上面的结果可以得知,LEFT JOIN左边的表会返回全部记录,而右边的表只返回符合连接条件的记录

例子2:右外连接:

运行语句,结果为:

从上面的结果可以得知,RIGHT JOIN右边的表会返回全部记录,而左边的表只返回符合连接条件的记录

mysql如何查看主键外键约束名

SELECT CONSTRAINT_CATALOG,

CONSTRAINT_SCHEMA,

CONSTRAINT_NAME,

TABLE_SCHEMA,

TABLE_NAME,

CONSTRAINT_TYPE

FROM

information_schema.TABLE_CONSTRAINTS

WHERE

TABLE_NAME='表名'

表名替换成你要看的表


分享名称:怎么查mysql外键约束,mysql外键约束有哪些
本文来源:http://cxhlcq.com/article/hdggsi.html

其他资讯

在线咨询

微信咨询

电话咨询

028-86922220(工作日)

18980820575(7×24)

提交需求

返回顶部