SQLiteStudio: How to create unique row (not column)?

1.4k views Asked by At

I want to create a table that only allows unique rows with SqliteStudio:

This should work:

|--ID--|--Column01--|--Column01--|
| 1     "brun/brune"   "brown"   |
| 2     "yellow"       "brown"   |

This shouldn´t work:

|--ID--|--Column01--|--Column01--|
|   1    "brun/brune"   "brown"  |
|   2    "brun/brune"   "brown"  |

Hope you can help me C: ~Henri

4

There are 4 answers

0
Googie On BEST ANSWER

Since the question was asked in context of SQLiteStudio - you can create composite contraints in the table window. While you can add columns in upper part of the table window, the lower part of window is for managing composite constraints: enter image description here

enter image description here enter image description here

enter image description here

1
Himanshu On

create a multivalued primary key like this

CREATE TABLE something (
  column01, 
  column02, 
  PRIMARY KEY (column01, column02)
);

Both single column and composite (multiple column) primary keys are supported.

See https://www.sqlite.org/lang_createtable.html

2
Siyual On

You can add a UNIQUE CONSTRAINT to the table:

Create Table YourTable
(
    Id       INTEGER PRIMARY KEY AUTOINCREMENT,
    Column01 VARCHAR,
    Column02 VARCHAR,
    CONSTRAINT col01_col02_unique UNIQUE (Column01, Column02)
)
0
Guru0008 On

In the example below There is only ONE PRIMARY KEY (PK_Person). However, the VALUE of the primary key is made up of TWO COLUMNS (ID + LastName).

CREATE TABLE mytable (
    ID int NOT NULL,
    LastName varchar(255) NOT NULL,
    FirstName varchar(255),
    Age int,
    CONSTRAINT PK_name PRIMARY KEY (ID,LastName)
);