本文介绍了表上的 SQLite 多主键,其中之一是自动增量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在一张表上有多个(复合)主键,其中一个是自动递增的.然而,有趣的是 SQLite 允许在强制性 PRIMARY KEY 关键字之后使用 AUTOINCREMENT 关键字.

I have multiple (composite) primary keys on a table and one of them will be auto increment. However, interestingly SQLite allows usage of AUTOINCREMENT keyword just after an obligatory PRIMARY KEY keyword.

我的查询是:

CREATE TABLE ticket (
     id INTEGER PRIMARY KEY AUTOINCREMENT,
     seat TEXT, payment INTEGER,
     PRIMARY KEY (id, seat))

然而错误是table "ticket"有多个主键.

实际上我可以避免此表的其他主键.但是我正在编写一个 ORM 框架(是的,我疯了)并且不想更改表的 PRIMARY KEY 约束生成的结构(因为它在 MySQL afaik 中是允许的).

Actually I can avoid other primary keys for this table. But I am coding an ORM framework (hell yeah I'm crazy) and do not want to change structure of PRIMARY KEY constraint generation for a table (because it is allowed in MySQL afaik).

有什么解决办法吗?

推荐答案

不,我不认为这是可能的.

No, I don't think this is possible.

您可以创建一个 UNIQUE INDEX,其效果与 PRIMARY KEY 基本相同:

You can create a UNIQUE INDEX which has essentially the same effect as a PRIMARY KEY:

CREATE UNIQUE INDEX pk_index ON "table1"("field1","field2");

此外,我看不到您架构的逻辑,即 -> 如果一列是自动增量并且您不打算手动弄乱这些值,无论如何它都会是唯一的,所以它很简单短主键.为什么要复合?不过,您可能有充分的理由在列组合上创建另一个索引.

Besides, I fail to see the logic of your schema, that is -> if a column is autoincrement and you don't intend to mess with the values manually, it's going to be unique anyway, so it makes a good simple short primary key. Why the composite? You may have good reasons to make another index on the combination of columns, though.

这篇关于表上的 SQLite 多主键,其中之一是自动增量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-18 18:20