MySQL Syntax in creating Foreign Key

后端 未结 3 1014
攒了一身酷
攒了一身酷 2021-01-11 22:48

Is this syntax correct in creating a Foreign Key?

create table department
(
  departmentID int not null auto_increment primary key,
  name varchar(30)
) type         


        
相关标签:
3条回答
  • 2021-01-11 23:26

    It looks like MySQL accepts it (doesn't complain about the syntax) but the foreign key is not actually created.

    To create this foreign key, run this command:

    ALTER TABLE employee ADD CONSTRAINT fk_department FOREIGN KEY (departmentID) REFERENCES department (departmentID);
    
    0 讨论(0)
  • 2021-01-11 23:30
    FOREIGN KEY (departmentID) REFERENCES department(departmentID)
    

    Thanks.!

    0 讨论(0)
  • 2021-01-11 23:35
    create table employee
    (
      employeeID int not null auto_increment primary key,
      name varchar(80),
      job varchar(30),
      departmentID int not null ADD CONSTRAINT fk_department FOREIGN KEY (departmentID) references department(departmentID)
    ) 
    
    0 讨论(0)
提交回复
热议问题