SQLite - 表约束UNIQUE&列约束UNIQUE? [英] SQLite - Any difference between table-constraint UNIQUE & column-constraint UNIQUE?

查看:2963
本文介绍了SQLite - 表约束UNIQUE&列约束UNIQUE?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有关SQLite的问题。



在CREATE TABLE SQL中,我们可以通过以下方式添加UNIQUE约束:column-constraint或table-constraint。我的问题很简单。它们的工作方式是否不同?



我发现的唯一区别是,在table-constraint中,可能有多个 indexed-column s在单个约束中。



列约束:



表约束:



这里是一个例子:

  CREATE TABLE示例(
_id INTEGER PRIMARY KEY,
name TEXT UNIQUE ON CONFLICT REPLACE,
score INTEGER

  CREATE TABLE示例(
_id INTEGER PRIMARY KEY,
name TEXT,
score INTEGER,
UNIQUE(name)ON CONFLICT REPLACE

解决方案

这种情况没有什么区别。



但是,您可以在表上创建一个唯一的约束,跨越两个不同的列。像这样:

  CREATE TABLE示例(
_id INTEGER PRIMARY KEY,
name TEXT,
index INTEGER,
score INTEGER,
UNIQUE(name,index)ON CONFLICT REPLACE

有关更多详细信息,请参阅此文章:
Sqlite表约束 - 对多个列有唯一性


Question about SQLite.

In the CREATE TABLE SQL, we can add UNIQUE constraints in either way: column-constraint or table-constraint. My question is simple. Do they work differently?

The only difference I could find was, in table-constraint, there could be multiple indexed-columns in a single constraint.

Column-constraint:

Table-constraint:

Here is an example:

CREATE TABLE Example (
    _id INTEGER PRIMARY KEY,
    name TEXT UNIQUE ON CONFLICT REPLACE,
    score INTEGER
)

and

CREATE TABLE Example (
    _id INTEGER PRIMARY KEY,
    name TEXT,
    score INTEGER,
    UNIQUE (name) ON CONFLICT REPLACE
)

Are they different?

解决方案

In this case there is no difference.

However, you could create an unique constraint on table, that would span over two different columns. Like this:

CREATE TABLE Example (
    _id INTEGER PRIMARY KEY,
    name TEXT,
    index INTEGER,
    score INTEGER,
    UNIQUE (name, index) ON CONFLICT REPLACE
)

Consult this post for further details: Sqlite table constraint - unique on multiple columns

这篇关于SQLite - 表约束UNIQUE&列约束UNIQUE?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆