加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

如何在Sqlite中指定主键

发布时间:2020-12-12 18:52:58 所属栏目:百科 来源:网络整理
导读:如何将学生表中的指定属性(如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
如何将学生表中的指定属性(如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命令

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读