markdown SQLite3,外键

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了markdown SQLite3,外键相关的知识,希望对你有一定的参考价值。

foreign key is disabled when using sqlite3 by default. if you want to enable this: 
```
PRAGMA foreign_keys=ON
```

Sample:
```
SQLite version 3.19.3 2017-06-27 16:48:08
Enter ".help" for usage hints.
/* create two test table */
sqlite> CREATE TABLE artist(
   ...> artistid INTEGER PRIMARY KEY,
   ...> artistname TEXT
   ...> );
sqlite> CREATE TABLE track(
   ...> trackid INTEGER,
   ...> trackname TEXT,
   ...> trackartist INTEGER,
   ...> FOREIGN KEY(trackartist) REFERENCES artist(artistid)
   ...> );
   
/* when foreign key disable, no limitation when insert track table */
sqlite> insert into track values (1, '01', 1);
sqlite> select * from track;
1|01|1
/* after enable foreign key */
sqlite> PRAGMA foreign_keys=ON;
sqlite> insert into track values (1, '01', 2);
Error: FOREIGN KEY constraint failed
sqlite> insert into artist values (2, 'jack');
sqlite> select * from artist;
2|jack
sqlite> insert into track values (2, '01', 2);
sqlite> .head
Usage: .headers on|off
sqlite> .header on 
sqlite> .mode column
sqlite> select * from track;
trackid     trackname   trackartist
----------  ----------  -----------
1           01          1          
2           01          2 
```

[Official Document](https://sqlite.org/foreignkeys.html)

以上是关于markdown SQLite3,外键的主要内容,如果未能解决你的问题,请参考以下文章

markdown Sqlite3数据库类

markdown SQLite3 :: BusyException:数据库被锁定

markdown rest-api使用node.js和sqlite3

markdown 用Postgres编写外键的不同方法

SQLite外键

markdown 在Laravel 5.7种子文件中截断具有外键约束的表。