constraint foreign key(cid) references tb_category(cid) -- 外键约束 ); # tb_product添加数据 insert into tb_product values (1, '雷神鼠标', 200, 3); -- 报错 /** *ERROR 1452 (23000): Cannot add or update a child row: a fore
--删除外键ALTERTABLEemployeeDROPFOREIGNKEYemp_dep_fk; 3)在创建表后添加外键: ALTER TABLE 表名称 ADD CONSTRAINT 外键名称 FOREIGN KEY (外键字段名称) REFERENCES 主表名称(主表列名称); 代码语言:javascript 代码运行次数:0 运行 AI代码解释 --添加外键ALTERTABLEemployeeADDCONSTRAINTemp_dep_fkFOREIGNKEY(dep_i...
CONSTRAINT`s_id`FOREIGN KEY (`student_id`) REFERENCES`student`(`id`) ) (3)主表删除行,其主键值在从表里存在便阻止删除(要想删除,必须先删除从表的相关行) DELETEFROMstudentWHEREid=1; ERROR1451(23000) :CannotDELETE ORUPDATEaparentROW:aFOREIGNKEYCONSTRAINTfails( `tts`.`student_score`, CONSTRAINT`...
FOREIGNKEY(PersonID)REFERENCESPersons(PersonID) ); To allow naming of aFOREIGN KEYconstraint, and for defining aFOREIGN KEYconstraint on multiple columns, use the following SQL syntax: CREATETABLEOrders ( OrderID intNOTNULL, OrderNumber intNOTNULL, ...
方案1: 在创建表的时候就增加外键: 在表字段之后使用foreign key foreign key(外键字段) references 主表(主键); 方案2: 在创建表之后增加外键: 指定外键名字 alter table 表名 add constraint 外键名 foreign key(外键字段) references 父表(主键字段) ...
CONSTRAINT `xiaodi_ibfk_1` FOREIGN KEY (`dage_id`) REFERENCES `dage` (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=latin1 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 插入个大哥: mysql> insert into dage(name) values('铜锣湾'); ...
语法:ALTER TABLE 表名 ADD CONSTRAINT 外键名 FOREIGN KEY(外键字段名) REFERENCES 外表表名(主键字段名); 举例:为student表添加外键约束 命令:ALTER TABLE student ADD CONSTRAINT FK_ID FOREIGN KEY (gid) REFERENCES grade(id); 也可以在创建表的时候为其添加外键 ...
(0.01 sec) Records: 3 Duplicates: 0 Warnings: 0 mysql> DELETE FROM parent WHERE id=1; ERROR 1451 (23000): Cannot delete or update a parent row: a foreign key constraint fails (`test`.`child`, CONSTRAINT `child_ibfk_1` FOREIGN KEY (`parent_id`) REFERENCES `parent` (`id`) ON ...
MySQL requires that foreign key columns be indexed; if you create a table with a foreign key constraint but no index on a given column, an index is created. You can obtain information about foreign keys from the Information SchemaKEY_COLUMN_USAGEtable. An example of a query against this tab...
FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE ); 元数据查询 可以通过 sql 查询对应的引用信息 SELECT TABLE_NAME, COLUMN_NAME, CONSTRAINT_NAME, REFERENCED_TABLE_NAME, REFERENCED_COLUMN_NAME FROM information_schema.KEY_COLUMN_USAGE ...