What is the syntax for specifying a primary key on more than 1 column in SQLITE ?
Primary keys must contain UNIQUE values, and cannot contain NULL values. A table can have only ONE primary key; and in the table, this primary key can consist of single or multiple columns (fields).
In SQLite, you can specify the primary key of a table on multiple table columns. Here is the generic syntax: CREATE TABLE table_name ( column1, column2, column3, PRIMARY KEY (column1, column2) );
A primary key column cannot have NULL values. A table can have only one primary key, which may consist of single or multiple fields.
Primary Key Declaration In a table, there can only be one primary key. A primary key can have one or as many columns as possible.
According to the documentation, it's
CREATE TABLE something (
column1,
column2,
column3,
PRIMARY KEY (column1, column2)
);
CREATE TABLE something (
column1 INTEGER NOT NULL,
column2 INTEGER NOT NULL,
value,
PRIMARY KEY ( column1, column2)
);
Yes. But remember that such primary key allow NULL
values in both columns multiple times.
Create a table as such:
sqlite> CREATE TABLE something (
column1, column2, value, PRIMARY KEY (column1, column2));
Now this works without any warning:
sqlite> insert into something (value) VALUES ('bla-bla');
sqlite> insert into something (value) VALUES ('bla-bla');
sqlite> select * from something;
NULL|NULL|bla-bla
NULL|NULL|bla-bla
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With