mysql怎么查看外键名 mysql 查看外键

SQL数据库的、外键和查询

增加外键

公司主营业务:做网站、成都做网站、移动网站开发等业务。帮助企业客户真正实现互联网宣传,提高企业的竞争能力。创新互联是一支青春激扬、勤奋敬业、活力青春激扬、勤奋敬业、活力澎湃、和谐高效的团队。公司秉承以“开放、自由、严谨、自律”为核心的企业文化,感谢他们对我们的高要求,感谢他们从不同领域给我们带来的挑战,让我们激情的团队有机会用头脑与智慧不断的给客户带来惊喜。创新互联推出伊春免费做网站回馈大家。

创建表的时候增加外键:在所有的表字段之后,使用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 6.0.4怎样查看外键名称

select * from information_schema.TABLE_CONSTRAINTS t where t.TABLE_NAME='itcast_student' and CONSTRAINT_TYPE='FOREIGN KEY';

不加type的话,就是所有的约束了,你知道的,还有可以用图形界面的工具啊操作很方便,不用写sql就可以删除,比如:navicat for mysql,mysql workbench 都是很好用的管理工具!

在MySQL中如何设置主键和外键

工具/原料

电脑  MySQL

方法/步骤

设置主键:

1、通过终端进入到mysql命令行工具。

2、通过use关键字进行到目标数据库里。

3、如原表已有主键,先把原来的主键删除掉,通过DROPPRIMARYKEY命令:ALTERTABLE`jingyan`DROPPRIMARYKEY;。

4、主键已经没有了。

5、通过命令:ADDPRIMARYKEY来添加ALTERTABLE`jingyan`ADDPRIMARYKEY(`id`)。

6、输入后按下回车键即可看到queryok执行成功的字符。

7、回到数据库的可视化工具,即可显示现在的表在id列上添加了主键了。

设置外键:

1、创建好主从表。

2、选择主表,点击设计表,进入到表设计界面。

3、点击外键,进入到外键设置界面。

4、先设置外键名称和选择主表的外键字段。

5、然后在设置外键字段对应从表的数据库、表名和字。

6、点击保存就完成外键设置了。

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

SELECT CONSTRAINT_CATALOG,

CONSTRAINT_SCHEMA,

CONSTRAINT_NAME,

TABLE_SCHEMA,

TABLE_NAME,

CONSTRAINT_TYPE

FROM

information_schema.TABLE_CONSTRAINTS

WHERE

TABLE_NAME='表名'

表名替换成你要看的表

mysql 如何查看某个库中的一个表是哪些表的外键?

#查看数据库所有表

SELECT tba.TABLE_NAME FROM information_schema.TABLES tba WHERE tba.TABLE_SCHEMA= '你要查的数据库名字'

#查看某个库中的一个表是哪些表的外键

SELECT TABLE_NAME FROM KEY_COLUMN_USAGE WHERE CONSTRAINT_NAME='FK_PRODUCT_ID' AND REFERENCED_TABLE_NAME ='表的名字'AND REFERENCED_TABLE_SCHEMA='表的的数据名字'

求采纳良心sql啊

information_schema数据库又称为信息架构,数据表保存了MySQL服务器所有数据库的信息。如数据库名,数据库的表,表栏的数据类型与访问权限等。

performance_schema数据库主要用于收集数据库服务器性能参数,以便优化mysql数据库性能。

mysql数据库是存储着已MySQL运行相关的基本信息等数据管理的数据库。

sys 数据库是mysql5.7增加的,通过这个库可以快速的了解系统的元数据信息

网页题目:mysql怎么查看外键名 mysql 查看外键
分享路径:https://www.cdcxhl.com/article20/hjgdjo.html

成都网站建设公司_创新互联,为您提供网站改版面包屑导航手机网站建设网站营销营销型网站建设云服务器

广告

声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联

小程序开发