Composite Primary Key (Sqlite.Swift) - sqlite.swift

How can I create a table with a composite primary key with this library. For example:
CREATE TABLE something (
column1,
column2,
column3,
PRIMARY KEY (column1, column2)
);
The only example I see in the documentation is a primary key on single column:
try db.run(users.create { t in
t.column(id, primaryKey: true)
t.column(email, unique: true)
t.column(name)
})
I tried the following but fails with an error (...table "" has more than one primary key) :
try db.run(tblTsMosStaged.create { t in
t.column(colTsMosStagedEventId, primaryKey: true)
t.column(colTsMosStagedEventInstance, primaryKey: true)
t.column(colTsMosStagedTaxId, primaryKey: true)
t.column(colTsMosStagedRankId)
...
Or is this only possible via Executing Arbitrary SQL

The SQLite.swift documentation says:
Table Constraints
Additional constraints may be provided outside the scope of a single column using the following functions.
primaryKey adds a PRIMARY KEY constraint to the table. Unlike the column constraint, above, it supports all SQLite types, ascending and descending orders, and composite (multiple column) keys.
t.primaryKey(email.asc, name)
// PRIMARY KEY("email" ASC, "name")
try db.run(tblTsMosStaged.create { t in
t.column(colTsMosStagedEventId)
t.column(colTsMosStagedEventInstance)
t.column(colTsMosStagedTaxId)
t.column(colTsMosStagedRankId)
//ADD this line
t.primaryKey(colTsMosStagedEventId, colTsMosStagedEventInstance, colTsMosStagedTaxId)

Related

Defining a composite primary key in h2

How can I specify that a table has multiple columns that makeup the primary key? When I run this sql statement, I get "unknown data type "("
CREATE TABLE SH_LEAGUE_CONTACT_TEAM_ROLE(ROLE_NAME VARCHAR NOT NULL,
TEAM_ID INT NOT NULL,
CONTACT_ID INT NOT NULL,
FOREIGN_KEY(TEAM_ID) REFERENCES SH_LEAGUE_TEAM(ID),
FOREIGN_KEY(CONTACT_ID) REFERENCES SH_LEAGUE_CONTACT(ID),
PRIMARY KEY(ROLE_NAME, TEAM_ID, CONTACT_ID));
You have a typo in your statement, you have used FOREIGN_KEY (one word) instead of FOREIGN KEY (two words).

How to add composite primary keys?

I have a table with three columns, [Id,QTY,Date]. out of these three, two columns [id and date], should be set as primary keys, because I need to fetch the record one by one, from this table, into a reference.
the data to be inserted into this table is
101,10,NULL
101,20,201220
101,7,201440
102,5,null
102,8,201352
date is in yyyyww format
How do I define two columns as composite primary keys when they have null values, duplicates?
alter table abc add constraint pk primary key (ID, DATE);
if I try to alter the table the error appears
Error report:
SQL Error: ORA-01449: column contains NULL values; cannot alter to NOT NULL
01449. 00000 - "column contains NULL values; cannot alter to NOT NULL"
*Cause:
*Action:
Using table level constraint, you can use this query
alter table your_table add constraint pkc_Name primary key (column1, column2)
but first you need to declare the columns NOT NULL. All parts of a primary key need to be NOT NULL.
The column name of your table is ID and it is still null and non-unique, how is it possible. If it is primary key of other table try adding a surrogate key column for this table and make it primary key.
In case of composite primary key, it should have atleast one not null value(For each row) in the combination of columns. And the combination of column must be unique at all case.
For further details check, http://docs.oracle.com/cd/B10500_01/server.920/a96524/c22integ.htm
Correction - If composite primary key is made up of 3 columns, then no column (among 3) can hold NULL value. And the combination of those 3 columns must be unique.
E.g. (1,2,2)
(1,2,1)
(2,2,1)
(1,2,2) - not valid

Can not set Sorted ASC Index on primary key field in oracle

When I try to add a sorted ASC index on the field MyId the oracle sql developer tool says:
Index MyIdSortedIndex is defined identically to constraint PK_MyTable
CREATE TABLE MyTable
( "MyId" NVARCHAR2(50),
"DESCRIPTION" NVARCHAR2(200),
CONSTRAINT "PK_MyTable" PRIMARY KEY ("MyId")
)
How can I set a sorted ASC index on my tables Primary Key MyId in Oracle ?
It is telling you that you don't need a new index, because you already have an index called "PK_MyTable" on "MyId" in ascending order. When you add a primary key constraint to a table Oracle automatically adds an index of the same name to help enforce it, indexing the columns of the constraint in ascending order (since ascending is the default direction).
If you don't want to use the index Oracle created automatically you can do this:
CREATE TABLE MyTable
( "MyId" NVARCHAR2(50),
"DESCRIPTION" NVARCHAR2(200),
);
CREATE INDEX MyIdSortedIndex ON MyTable ("MyId" ASC);
ALTER TABLE MyTable Add CONSTRAINT "PK_MyTable" PRIMARY KEY ("MyId");
Since your index already exists when the primary key is added Oracle will use that instead of creating its own.

Oracle unique constraint and primary key not null

Can a table have a primary key attribute and a unique constraint to another attribute?
I have the following
CREATE TABLE BRANCH(
BRA_CODE NUMBER NOT NULL PRIMARY KEY,
BRA_NAME VARCHAR(15),
BRA_ADDR VARCHAR(30),
CITY_ID NUMBER);
and im trying to add
ALTER TABLE BRANCH ADD CONSTRAINT UNIQUE_BRANCH_NAME UNIQUE (BRA_NAME);
and i get the following error;
ERROR at line 1:
ORA-02261: such unique or primary key already exists in the table
Q: Can a table have a primary key attribute and a unique constraint to
another attribute?
A: Yes:
A table can have no more than one primary key.
A primary key may consist of multiple columns (a "composite primary key")
Any column may have a "unique constraint", whether or not it's a primary key column
A primary key is always "unique", and always has a "unique" constraint
ERROR at line 1: ORA-02261: such unique or primary key already exists
in the table
A: Check your schema. You've already already got a primary key, and/or you already defined the same unique constraint.
For example:
http://www.shutdownabort.com/dbaqueries/Structure_Constraints.php
col type format a10
col cons_name format a30
select decode(constraint_type,
'C', 'Check',
'O', 'R/O View',
'P', 'Primary',
'R', 'Foreign',
'U', 'Unique',
'V', 'Check view') type
, constraint_name cons_name
, status
, last_change
from dba_constraints
where table_name like 'BRANCH'
order by 1
You can have a unique contraint apart from the primary key, but the message indicates that you already added such a constraint.

Creating multi-column constraints depending on column value

I'm trying to create a constraint on an oracle database that says the following:
If column1 == someValue then the combination of column2 and column3 has to be unique for all entries with column1 == someValue
I'm familiar with the concepts of unique and check constraints, and I've tried expressing the constraint with those constructs. However, I can't seem to find a way to include the condition. Which is why I'm wondering if it is even possible.
The table I want to create constraint for is created by Hibernate mapping the following class hierarchy (most of the attributes ommited for brevity):
class MyClass {
String name;
MyClass parent;
}
class MySubClass extends MyClass {
String businessValue;
}
The classes are mapped using a single table strategy and using different discriminator values for each type. It's a customer requirement that for all instances of MySubClass the combination of name and parent has to be unique (column1 would be the discriminator value). It would be easy to enforce such a constraint on the parent class through a table constraint. However, that constraint must only apply to MySubClass.
There is the possibility of validating the data before entering it into the the database with frameworks such as Hibernate Validator. But since the validation would need database access anyway, a database constraint seems the more performance saving way to do it.
You can't do this with a constraint, but you can do it using a "function-based index" (FBI) like this:
create unique index mytable_idx on mytable
( case when column1 = 'somevalue' then column2 end
, case when column1 = 'somevalue' then column3 end
);
This only creates unique index entries for rows with column1 = 'somevalue', so other rows can contain duplicates but these cannot.

Resources