Implement/use foreign keys in SQLite?

て烟熏妆下的殇ゞ 提交于 2019-12-11 12:54:54

问题


How can I implement a foreign key in SQLite? I was thinking something like this:

CREATE TABLE job (_id INTEGER PRIMARY KEY AUTOINCREMENT, employer_id INTEGER, ...);
CREATE TABLE employer(_id INTEGER, employer_name TEXT NOT NULL, ...);

Where employer_id is the _id from the table employer. Would this work? Is there another fast, maybe less prone to error way? Maybe with triggers?


回答1:


Maybe I don't understand the question, but if it's the constraint you want, just do this:

ALTER TABLE Job
  ADD FOREIGN KEY (employer_id)
    REFERENCES Employer(_id)
    ON DELETE NO ACTION
    ON UPDATE NO ACTION;



回答2:


See SQLite (3.6.19) Foreign Key Support

(Earlier version of SQLite do not support enforced FK relationships.)



来源:https://stackoverflow.com/questions/3297363/implement-use-foreign-keys-in-sqlite

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!