How do I add a foreign key to an existing SQLite table?

前端 未结 11 1710
旧时难觅i
旧时难觅i 2020-11-27 12:44

I have the following table:

CREATE TABLE child( 
  id INTEGER PRIMARY KEY, 
  parent_id INTEGER, 
  description TEXT);

How do I add a forei

11条回答
  •  日久生厌
    2020-11-27 13:05

    If you are using the Firefox add-on sqlite-manager you can do the following:

    Instead of dropping and creating the table again one can just modify it like this.

    In the Columns text box, right click on the last column name listed to bring up the context menu and select Edit Column. Note that if the last column in the TABLE definition is the PRIMARY KEY then it will be necessary to first add a new column and then edit the column type of the new column in order to add the FOREIGN KEY definition. Within the Column Type box , append a comma and the

    FOREIGN KEY (parent_id) REFERENCES parent(id)
    

    definition after data type. Click on the Change button and then click the Yes button on the Dangerous Operation dialog box.

    Reference: Sqlite Manager

提交回复
热议问题