您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

MySQL外键关联操作如何实现

2024/4/7 1:11:21发布16次查看
mysql 的外键约束注意,mysql 的 innodb 表引擎才支持外键关联,myisam 不支持。set foreign_key_checks = 0/1 可以用于手动打开或关闭 mysql 的外键约束。
mysql的外键约束的最大好处是它可以帮助我们完成数据的一致性验证。使用默认的 restrict 外键类型,在进行记录的创建、修改或删除时,都会进行引用合法性检查。
假设我们的数据库中包含 posts(id, author_id, content) 和 authors(id, name) 两张表,在执行如下所示的操作时都会触发数据库对外键的检查:
向 posts 表中插入数据时,检查 author_id 是否在 authors 表中存在;
修改 posts 表中的数据时,检查 author_id 是否在 authors 表中存在;
删除 authors 表中的数据时,检查 posts 中是否存在引用当前记录的外键;
作为专门用于管理数据的系统,数据库与应用服务相比能够更好地保证完整性,而上述的这些操作都是引入外键带来的额外工作,不过这也是数据库保证数据完整性的必要代价。我们可以进行简单的定量分析,来了解引入外键对性能的具体影响,而不仅仅是进行理论上的定性分析。
创建表时定义外键(references,参照)
在 create table 语句中,通过 foreign key 关键字来指定外键,具体的语法格式如下:
[constraint <外键名>] foreign key 字段名 [,字段名2,…] references <主表名> 主键列1 [,主键列2,…]
示例:
# 部门表 tb_dept1(主表)create table tb_dept1( id int(11) primary key, name varchar(22) not null, location varchar(50)) engine=innodb default charset=gb2312; # 员工表 tb_emp6(从表),创建外键约束,让 deptid 作为外键关联到 tb_dept1 的主键 id。create table tb_emp6( id int(11) primary key, name varchar(25), deptid int(11), salary float, constraint fk_emp_dept1 foreign key(deptid) references tb_dept1(id)) engine=innodb default charset=gb2312;
note:从表的外键关联的必须是主表的主键,且主键和外键的数据类型必须一致。
以上语句执行成功之后,在表示 tb_emp6 上添加了名称为 fk_emp_dept1 的外键约束,外键名称为 deptid,其依赖于表 tb_dept1 的主键 id。
查看主表的约束信息
mariadb [test_db]> select * from information_schema.key_column_usage where referenced_table_name='tb_dept1'\g;*************************** 1. row *************************** constraint_catalog: def constraint_schema: test_db constraint_name: fk_emp_dept1 table_catalog: def table_schema: test_db table_name: tb_emp6 column_name: deptid ordinal_position: 1position_in_unique_constraint: 1 referenced_table_schema: test_db referenced_table_name: tb_dept1 referenced_column_name: id1 row in set (0.00 sec)
修改原有表的外键约束外键约束也可以在修改表时添加,但是添加外键约束的前提是:从表中外键列中的数据必须与主表中主键列中的数据一致或者是没有数据。
在修改数据表时添加外键约束的语法格式如下:
alter table <数据表名> add constraint <外键名> foreign key(<列名>) references <主表名> (<列名>);
示例:修改数据表 tb_emp2,将字段 deptid 设置为外键,与数据表 tb_dept1 的主键 id 进行关联。
# 创建 tb_emp2(从表)create table tb_emp2( id int(11) primary key, name varchar(25), deptid int(11), salary float) engine=innodb default charset=gb2312; mariadb [test_db]> desc tb_emp2;+--------+-------------+------+-----+---------+-------+| field | type | null | key | default | extra |+--------+-------------+------+-----+---------+-------+| id | int(11) | no | pri | null | || name | varchar(25) | yes | | null | || deptid | int(11) | yes | | null | || salary | float | yes | | null | |+--------+-------------+------+-----+---------+-------+ # 添加外键约束alter table tb_emp2 add constraint fk_tb_dept1 foreign key(deptid) references tb_dept1(id); mariadb [test_db]> desc tb_emp2;+--------+-------------+------+-----+---------+-------+| field | type | null | key | default | extra |+--------+-------------+------+-----+---------+-------+| id | int(11) | no | pri | null | || name | varchar(25) | yes | | null | || deptid | int(11) | yes | mul | null | || salary | float | yes | | null | |+--------+-------------+------+-----+---------+-------+ mariadb [test_db]> show create table tb_emp2\g*************************** 1. row *************************** table: tb_emp2create table: create table `tb_emp2` ( `id` int(11) not null, `name` varchar(25) default null, `deptid` int(11) default null, `salary` float default null, primary key (`id`), key `fk_tb_dept1` (`deptid`), constraint `fk_tb_dept1` foreign key (`deptid`) references `tb_dept1` (`id`)) engine=innodb default charset=gb2312
删除外键约束当一个表中不需要外键约束时,就需要从表中将其删除。外键一旦删除,就会解除主表和从表之间的关联关系。
删除外键约束的语法格式如下所示:
alter table <表名> drop foreign key <外键约束名>;
示例:删除数据表 tb_emp2 中的外键约束 fk_tb_dept1。
alter table tb_emp2 drop foreign key fk_tb_dept1; mariadb [test_db]> show create table tb_emp2\g*************************** 1. row *************************** table: tb_emp2create table: create table `tb_emp2` ( `id` int(11) not null, `name` varchar(25) default null, `deptid` int(11) default null, `salary` float default null, primary key (`id`), key `fk_tb_dept1` (`deptid`)) engine=innodb default charset=gb2312
以上就是mysql外键关联操作如何实现的详细内容。
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product