开发者

Implement/use foreign keys in SQLite?

开发者 https://www.devze.com 2023-01-07 23: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, ...);

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 TABL开发者_高级运维E 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?


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;


See SQLite (3.6.19) Foreign Key Support

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

0

精彩评论

暂无评论...
验证码 换一张
取 消