Alter table to give foreign key constraint

前端 未结 1 1416
我在风中等你
我在风中等你 2021-02-19 22:01

I have a table which has 2 columns which I copied from two different tables.What I want to do now is give a foreign key constraint on both the column names email and id shown b

1条回答
  •  执笔经年
    2021-02-19 22:44

    You are not adding a constraint in this statement, you are adding constraints: each of the two FOREIGN KEY clauses means a separate constraint. Still, according to the manual, you should be able to add as many foreign key constraints in a single ALTER TABLE statement as necessary. You just need to include ADD before every constraint.

    Note that constraint names apply individually to the constraints you are adding, and so you might want to specify CONSTRAINT name for the second foreign key if you want it to have a specific name. Same with ON UPDATE/ON DELETE: they apply to the foreign key that is directly preceding them.

    So, the corrected statement might look like this:

    ALTER TABLE users_role_map
    
    ADD CONSTRAINT FK_users_role_map1
    FOREIGN KEY (email) REFERENCES usert(email)
    ON UPDATE CASCADE
    ON DELETE CASCADE,
    
    ADD CONSTRAINT FK_users_role_map2
    FOREIGN KEY (id) REFERENCES rolet(id)
    ON UPDATE CASCADE
    ON DELETE CASCADE;
    

    0 讨论(0)
提交回复
热议问题