SQLite 添加主键 [英] SQLite add Primary Key
问题描述
我使用 CREATE TABLE AS
语法在 Sqlite 中创建了一个表,以基于 SELECT
语句创建一个表.现在这张表没有主键,但我想添加一个.
I created a table in Sqlite by using the CREATE TABLE AS
syntax to create a table based on a SELECT
statement. Now this table has no primary key but I would like to add one.
执行 ALTER TABLE table_name ADD PRIMARY KEY(col1, col2,...)
给出语法错误near PRIMARY"
Executing ALTER TABLE table_name ADD PRIMARY KEY(col1, col2,...)
gives a syntax error "near PRIMARY"
有没有办法在创建表期间或之后在 Sqlite 中添加主键?
Is there a way to add a primary key either during table creation or afterwards in Sqlite?
创建期间"是指在使用 CREATE TABLE AS
创建期间.
By "during creation" I mean during creation with CREATE TABLE AS
.
推荐答案
SQLite 表在创建后不能以任何重要的方式修改.接受的建议解决方案是创建一个具有正确要求的新表并将您的数据复制到其中,然后删除旧表.
You can't modify SQLite tables in any significant way after they have been created. The accepted suggested solution is to create a new table with the correct requirements and copy your data into it, then drop the old table.
这是关于此的官方文档:http://sqlite.org/faq.html#q11
here is the official documentation about this: http://sqlite.org/faq.html#q11
这篇关于SQLite 添加主键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!