如何将学生表中的 StudentId 等指定属性定义为 sqlite 中的主键
最佳答案
CREATE TABLE Student(
id INTEGER PRIMARY KEY,
first_name TEXT,
last_name TEXT
);
从 Sqlite 规范:
One exception to the typelessness of SQLite is a column whose type is INTEGER PRIMARY KEY. (And you must use "INTEGER" not "INT". A column of type INT PRIMARY KEY is typeless just like any other.) INTEGER PRIMARY KEY columns must contain a 32-bit signed integer. Any attempt to insert non-integer data will result in an error.
http://www.sqlite.org/datatypes.html
您还可以在任意 blobish 数据上放置一个主键,例如:
CREATE TABLE Student(id PRIMARY KEY, name)
这有点冒险的原因
INSERT INTO Student(1, "hello")
INSERT INTO Student("1", "hello")
将导致两行。
如果你需要对其他东西的唯一约束,你可以尝试使用 Create Index命令
关于sqlite - 如何在 Sqlite 中指定主键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1101815/