在SQLite中为多个列指定主键的语法是什么?
k2arahey1#
根据CREATE TABLE的文档,特别是table-constraint,它是:
CREATE TABLE
table-constraint
CREATE TABLE something ( column1, column2, column3, PRIMARY KEY (column1, column2));
CREATE TABLE something (
column1,
column2,
column3,
PRIMARY KEY (column1, column2)
);
nmpmafwu2#
CREATE TABLE something ( column1 INTEGER NOT NULL, column2 INTEGER NOT NULL, value, PRIMARY KEY ( column1, column2));
column1 INTEGER NOT NULL,
column2 INTEGER NOT NULL,
value,
PRIMARY KEY ( column1, column2)
nfs0ujit3#
是的。但是记住这样的主键允许NULL值在两列中多次出现。创建一个这样的表:
NULL
sqlite> CREATE TABLE something (column1, column2, value, PRIMARY KEY (column1, column2));
sqlite> CREATE TABLE something (
column1, column2, value, PRIMARY KEY (column1, column2));
现在,它在没有任何警告的情况下工作:
sqlite> insert into something (value) VALUES ('bla-bla');sqlite> insert into something (value) VALUES ('bla-bla');sqlite> select * from something;NULL|NULL|bla-blaNULL|NULL|bla-bla
sqlite> insert into something (value) VALUES ('bla-bla');
sqlite> select * from something;
NULL|NULL|bla-bla
xwbd5t1u4#
基本:
CREATE TABLE table1 ( columnA INTEGER NOT NULL, columnB INTEGER NOT NULL, PRIMARY KEY (columnA, columnB));
CREATE TABLE table1 (
columnA INTEGER NOT NULL,
columnB INTEGER NOT NULL,
PRIMARY KEY (columnA, columnB)
如果您的列是其他表的外键(常见情况):
CREATE TABLE table1 ( table2_id INTEGER NOT NULL, table3_id INTEGER NOT NULL, FOREIGN KEY (table2_id) REFERENCES table2(id), FOREIGN KEY (table3_id) REFERENCES table3(id), PRIMARY KEY (table2_id, table3_id));CREATE TABLE table2 ( id INTEGER NOT NULL, PRIMARY KEY id);CREATE TABLE table3 ( id INTEGER NOT NULL, PRIMARY KEY id);
table2_id INTEGER NOT NULL,
table3_id INTEGER NOT NULL,
FOREIGN KEY (table2_id) REFERENCES table2(id),
FOREIGN KEY (table3_id) REFERENCES table3(id),
PRIMARY KEY (table2_id, table3_id)
CREATE TABLE table2 (
id INTEGER NOT NULL,
PRIMARY KEY id
CREATE TABLE table3 (
9avjhtql5#
主键字段应该声明为非空(这是非标准的,因为主键的定义是它必须是唯一的,而不是空的)。但下面是一个适用于任何DBMS中的所有多列主键的良好实践。
create table foo( fooint integer not null ,foobar string not null ,fooval real ,primary key (fooint, foobar));
create table foo
(
fooint integer not null
,foobar string not null
,fooval real
,primary key (fooint, foobar)
)
;
smdnsysy6#
自SQLite 3.8.2版本以来,显式NOT NULL规范的替代方案是“WITHOUT ROWID”规范:[ 1 ]
NOT NULL is enforced on every column of the PRIMARY KEYin a WITHOUT ROWID table.
NOT NULL is enforced on every column of the PRIMARY KEY
in a WITHOUT ROWID table.
“WITHOUT ROWID”表具有潜在的效率优势,因此可以考虑的一个不太冗长的替代方案是:
CREATE TABLE t ( c1, c2, c3, PRIMARY KEY (c1, c2) ) WITHOUT ROWID;
CREATE TABLE t (
c1,
c2,
c3,
PRIMARY KEY (c1, c2)
) WITHOUT ROWID;
例如,在sqlite3提示符下:sqlite> insert into t values(1,null,3); Error: NOT NULL constraint failed: t.c2
sqlite> insert into t values(1,null,3); Error: NOT NULL constraint failed: t.c2
webghufk7#
下面的代码在SQLite中创建一个表,其中2列作为主键。
解决方案:
CREATE TABLE IF NOT EXISTS users ( id TEXT NOT NULL, name TEXT NOT NULL, pet_name TEXT, PRIMARY KEY (id, name))
CREATE TABLE IF NOT EXISTS users (
id TEXT NOT NULL,
name TEXT NOT NULL,
pet_name TEXT,
PRIMARY KEY (id, name)
wlzqhblo8#
另一种方式,您也可以将 * 两列主键 * unique和 * 自动递增 * 键primary。就像这样:https://stackoverflow.com/a/6157337
unique
primary
zpjtge229#
PRIMARY KEY (id, name)不适合我。相反,添加一个约束完成了这项工作。
CREATE TABLE IF NOT EXISTS customer ( id INTEGER, name TEXT, user INTEGER, CONSTRAINT PK_CUSTOMER PRIMARY KEY (user, id))
CREATE TABLE IF NOT EXISTS customer (
id INTEGER, name TEXT,
user INTEGER,
CONSTRAINT PK_CUSTOMER PRIMARY KEY (user, id)
9条答案
按热度按时间k2arahey1#
根据
CREATE TABLE
的文档,特别是table-constraint
,它是:nmpmafwu2#
nfs0ujit3#
是的。但是记住这样的主键允许
NULL
值在两列中多次出现。创建一个这样的表:
现在,它在没有任何警告的情况下工作:
xwbd5t1u4#
基本:
如果您的列是其他表的外键(常见情况):
9avjhtql5#
主键字段应该声明为非空(这是非标准的,因为主键的定义是它必须是唯一的,而不是空的)。但下面是一个适用于任何DBMS中的所有多列主键的良好实践。
smdnsysy6#
自SQLite 3.8.2版本以来,显式NOT NULL规范的替代方案是“WITHOUT ROWID”规范:[ 1 ]
“WITHOUT ROWID”表具有潜在的效率优势,因此可以考虑的一个不太冗长的替代方案是:
例如,在sqlite3提示符下:
sqlite> insert into t values(1,null,3); Error: NOT NULL constraint failed: t.c2
webghufk7#
下面的代码在SQLite中创建一个表,其中2列作为主键。
解决方案:
wlzqhblo8#
另一种方式,您也可以将 * 两列主键 *
unique
和 * 自动递增 * 键primary
。就像这样:https://stackoverflow.com/a/6157337zpjtge229#
PRIMARY KEY (id, name)
不适合我。相反,添加一个约束完成了这项工作。