对同一表的列实施外键约束

在下表中输入值时,如何在 SQL 中对同一表的列强制执行外键约束:

How to enforce a constraint of foreign key on columns of same table in SQL while entering values in the following table:

员工:

  • empid 编号,
  • 经理编号(必须是现有员工)

推荐答案

CREATE TABLE TABLE_NAME (
    `empid_number`    int     (  11) NOT NULL auto_increment,   
    `employee`        varchar ( 100) NOT NULL               ,
    `manager_number`  int     (  11) NOT NULL               ,
     PRIMARY KEY  (`empid_number`),
     CONSTRAINT `manager_references_employee`
     FOREIGN KEY (`manager_number`) REFERENCES (`empid_number`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8

希望对你有帮助!

相关文章