sqlite - 如何在 Sqlite 中指定主键

标签 sqlite primary-key

如何将学生表中的 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/

相关文章:

sqlite - 如何读取 SQLite 表中的最后一条记录?

带主键的 Oracle 物化 View

postgresql - 最好使用 SERIAL PRIMARY KEY 或 GENERATED ALWAYS AS IDENTITY 作为 PostgreSQL 中的主键

java - SQLite Java和sqlite4java

c - 从 C 程序将 sqlite3 表导出到文本文件

c# - IComparable 是在字典中强制使用唯一键的最佳方式吗?

asp.net-mvc-3 - MVC3/Razor 添加 Controller "Get-PrimaryKey"无法找到主键

SQL合并两列的值作为主键

sqlite - sqlite3 的 Windows 命令提示符 shell

sqlite清除缓存