SQLite - Any difference between table-constraint UNIQUE & column-constraint UNIQUE?

ぃ、小莉子 提交于 2020-01-01 12:21:16

问题


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?


回答1:


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



来源:https://stackoverflow.com/questions/16769677/sqlite-any-difference-between-table-constraint-unique-column-constraint-uniq

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!