在SQLite中实现/使用外键? [英] Implement/use foreign keys in SQLite?

查看:220
本文介绍了在SQLite中实现/使用外键?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我怎样才能在SQLite中实现一个外键?我在想这样的事情:
$ b $ pre $ CREATE TABLE雇主(_id INTEGER,employer_name TEXT NOT NULL,...);



其中 employer_id _id 来自表雇主。这会工作吗?有没有另一种快速,也许不太容易出错的方式?也许与触发器?可能我不明白的问题,但如果这是你想要的约束,只要这样做:

pre $ ALTER TABLE作业
添加FOREIGN KEY(employer_id)
REFERENCES雇主(_id)
ON DELETE NO ACTION
ON UPDATE NO ACTION;


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?

解决方案

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;

这篇关于在SQLite中实现/使用外键?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆