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

前端 未结 11 1687
旧时难觅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 12:59

    First add a column in child table Cid as int then alter table with the code below. This way you can add the foreign key Cid as the primary key of parent table and use it as the foreign key in child table ... hope it will help you as it is good for me:

    ALTER TABLE [child] 
      ADD CONSTRAINT [CId] 
      FOREIGN KEY ([CId]) 
      REFERENCES [Parent]([CId]) 
      ON DELETE CASCADE ON UPDATE NO ACTION;
    GO
    

提交回复
热议问题