How to add a column and make it a foreign key in single MySQL statement?

前端 未结 4 962
渐次进展
渐次进展 2020-12-07 22:42

In mysql, can I add a column and foreign key in the same statement? And what is the proper syntax for adding the fk?

Here is my SQL:

ALTER TABLE dat         


        
4条回答
  •  天涯浪人
    2020-12-07 22:57

    This can be simplified a bit. You just need to add the "ADD" keyword before "FOREIGN KEY". Adding example below.

    ALTER TABLE database.table
    ADD COLUMN columnname INT DEFAULT(1),
    ADD FOREIGN KEY (fk_name) REFERENCES reftable(refcolumn) ON DELETE CASCADE;
    

提交回复
热议问题