SQLITE and foreign key



我正在查看一些定义如下的数据库表:

这是第一张表-

CREATE TABLE Point 
(
    Position integer unique not null,  
    Name text not null,                
);

第二张表是这样的:

CREATE TABLE namesPerPoint 
(
    PointKey integer not null,            -- unique ROWID in points table
    name text unique not null
);

现在,我的问题是sqlitedb如何知道PointKey引用了Positions表中的rowid。为了方便用户,它只是在评论中提到的。如果我自己必须这样做,我将使用FOREIGN KEY约束。那么,这个语法正确吗?但这是成功运行的大型系统的一部分,所有的表都是这样定义的。所以,我想我错过了什么。

感谢

否,除非您创建了外键约束。但首先,您需要turn on支持的外键

sqlite> PRAGMA foreign_keys = ON;

将主键添加到主表

CREATE TABLE Point 
(
    Position integer PRIMARY KEY,  
    Name text not null,                
);

然后添加外键

CREATE TABLE namesPerPoint 
(
    PointKey integer not null,            -- unique ROWID in points table
    name text unique not null,
    FOREIGN KEY (PointKey) REFERENCES Point(Position)
);

主键

来源:SQLite外键支持

最新更新