如何在Sqlite中指定主键

前端之家收集整理的这篇文章主要介绍了如何在Sqlite中指定主键前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
如何将学生表中的指定属性(如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相关文章