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

Question about SQLite.

In CREATE TABLE SQL, we can add UNIQUE constraints in any case: a column constraint or a table constraint. My question is simple. Do they work differently?

The only difference I could find was that in the table constraint there could be several indexed-column in one constraint.

Restriction column: enter image description here

Limit table: enter image description here

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 ) 

Different?

+4
source share
1 answer

In this case, there is no difference.

However, you can create a unique constraint for a table that spans 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 more information: SQLite table constraint - unique across multiple columns

+6
source

Source: https://habr.com/ru/post/1482923/


All Articles