SQLite对单个列的多个引用
比如说SQLite对单个列的多个引用,sql,sqlite,foreign-keys,Sql,Sqlite,Foreign Keys,比如说 Table1 id|v1|v2|v3 1 |10|20|30 2 |20|30|40 3 |30|40|50 Table2 id |a|b|c 100|2|1|1 200|1|3|2 300|3|2|2 表2.a、b和c应该是表1.id的外键 如果我尝试: CREATE Table2( id, a, b, c, FOREIGN KEY(a, b, c) REFERENCES Table1(id)); I get:错误:外键中的列数与引用表中的列数不匹配 如果我这样做 CREATE
Table1
id|v1|v2|v3
1 |10|20|30
2 |20|30|40
3 |30|40|50
Table2
id |a|b|c
100|2|1|1
200|1|3|2
300|3|2|2
表2.a、b和c应该是表1.id的外键
如果我尝试:
CREATE Table2(
id, a, b, c,
FOREIGN KEY(a, b, c) REFERENCES Table1(id));
I get:错误:外键中的列数与引用表中的列数不匹配
如果我这样做
CREATE Table2(
id,
a REFERENCES(id),
b REFERENCES(id),
c REFERENCES(id));
我刚刚得到:Error:near id:syntax Error正确的语法如下:
CREATE TABLE Table2 (
id int,
a int,
b int,
c int,
FOREIGN KEY(a) REFERENCES Table1(id),
FOREIGN KEY(b) REFERENCES Table1(id),
FOREIGN KEY(c) REFERENCES Table1(id)
);
正确的语法如下所示:
CREATE TABLE Table2 (
id int,
a int,
b int,
c int,
FOREIGN KEY(a) REFERENCES Table1(id),
FOREIGN KEY(b) REFERENCES Table1(id),
FOREIGN KEY(c) REFERENCES Table1(id)
);
您的第二次尝试不远,只是错过了引用的表名。对于单列外键,内联和未命名FK约束定义更紧凑,而对于复合FK,必须在表的末尾指定FK
C:\Users\DDevienne>sqlite3
SQLite version 3.8.3.1 2014-02-11 14:52:19
Enter ".help" for instructions
Enter SQL statements terminated with a ";"
sqlite> create table t1 (id primary key);
sqlite> create table t2 (
...> id primary key,
...> a references t1(id),
...> b references t1(id),
...> c references t1(id)
...> );
sqlite>
您的第二次尝试不远,只是错过了引用的表名。对于单列外键,内联和未命名FK约束定义更紧凑,而对于复合FK,必须在表的末尾指定FK
C:\Users\DDevienne>sqlite3
SQLite version 3.8.3.1 2014-02-11 14:52:19
Enter ".help" for instructions
Enter SQL statements terminated with a ";"
sqlite> create table t1 (id primary key);
sqlite> create table t2 (
...> id primary key,
...> a references t1(id),
...> b references t1(id),
...> c references t1(id)
...> );
sqlite>
那么这里的目标是什么呢?表1包含了几个人及其相关数据,表2包含了组、关于组的信息以及外键中的人员。一个人可以在多个组中。那么这里的目标是什么呢?表1包含多个人及其相关数据,表2包含组、关于组的信息以及外键中的人员。一个人可以在多个组中。