Bri*_*ell 763
根据文档,它是
CREATE TABLE something (
column1,
column2,
column3,
PRIMARY KEY (column1, column2)
);
Run Code Online (Sandbox Code Playgroud)
小智 152
CREATE TABLE something (
column1 INTEGER NOT NULL,
column2 INTEGER NOT NULL,
value,
PRIMARY KEY ( column1, column2)
);
Run Code Online (Sandbox Code Playgroud)
jsm*_*kus 40
是.但请记住,这样的主键允许NULL两列中的值多次.
像这样创建一个表:
sqlite> CREATE TABLE something (
column1, column2, value, PRIMARY KEY (column1, column2));
Run Code Online (Sandbox Code Playgroud)
现在这没有任何警告:
sqlite> insert into something (value) VALUES ('bla-bla');
sqlite> insert into something (value) VALUES ('bla-bla');
sqlite> select * from something;
NULL|NULL|bla-bla
NULL|NULL|bla-bla
Run Code Online (Sandbox Code Playgroud)
com*_*879 26
基础:
CREATE TABLE table1 (
columnA INTEGER NOT NULL,
columnB INTEGER NOT NULL,
PRIMARY KEY (columnA, columnB)
);
Run Code Online (Sandbox Code Playgroud)
如果您的列是其他表的外键(常见情况):
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
);
Run Code Online (Sandbox Code Playgroud)
小智 14
主键字段应声明为非null(这是非标准的,因为主键的定义是它必须是唯一的而不是null).但对于任何DBMS中的所有多列主键,以下是一个很好的做法.
create table foo
(
fooint integer not null
,foobar string not null
,fooval real
,primary key (fooint, foobar)
)
;
Run Code Online (Sandbox Code Playgroud)
从SQLite的3.8.2版开始,显式NOT NULL规范的替代方法是"WITHOUT ROWID"规范:[ 1 ]
NOT NULL is enforced on every column of the PRIMARY KEY
in a WITHOUT ROWID table.
Run Code Online (Sandbox Code Playgroud)
"没有ROWID"表具有潜在的效率优势,因此更简洁的替代方案是:
CREATE TABLE t (
c1,
c2,
c3,
PRIMARY KEY (c1, c2)
) WITHOUT ROWID;
Run Code Online (Sandbox Code Playgroud)
例如,在sqlite3提示符下:
sqlite> insert into t values(1,null,3);
Error: NOT NULL constraint failed: t.c2
以下代码在 SQLite 中创建一个包含2 列作为主键的表。
解决方案:
CREATE TABLE IF NOT EXISTS users (
id TEXT NOT NULL,
name TEXT NOT NULL,
pet_name TEXT,
PRIMARY KEY (id, name)
)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
248432 次 |
| 最近记录: |