【问题标题】:Sqlite foreign key insert should fail [duplicate]Sqlite外键插入应该失败[重复]
【发布时间】:2016-05-22 15:28:30
【问题描述】:

为什么 Sqlite 允许我在 table2 中插入与 table1 中的任何主键都不匹配的外键? 我正在使用 .NET 和 System.Data.SQLite.dll

这是我的设置:

create table [table1] (
  [Id] integer primary key NOT NULL
, [col1] nvarchar(100)  NULL
);

create table [table2] (
  [Id] integer primary key NOT NULL
, [col2] integer  NOT NULL
, FOREIGN KEY(col2) REFERENCES table1(Id)
);

INSERT INTO [table1] ([col1]) VALUES ('val1');
INSERT INTO [table2] ([col2]) VALUES ('2');

SQL 小提琴:http://sqlfiddle.com/#!7/4fccc/1/0

【问题讨论】:

  • 似乎是重复,尽管我通过在连接字符串中启用外键约束来解决它,如下所示:“Data Source=:memory:;foreign keys=true;”

标签: sqlite system.data.sqlite


【解决方案1】:

在连接字符串中启用外键约束,如下所示:“Data Source=:memory:;foreign keys=true;”

【讨论】:

    猜你喜欢
    • 2011-01-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-23
    • 1970-01-01
    相关资源
    最近更新 更多