Which of these is the right way to do foreign keys? - sql

So, i'm trying sqlite3 and i have a question about Foreign Keys and Indices.
Which of these methods is the right one ?
CREATE TABLE artist(
artistid INTEGER PRIMARY KEY,
artistname TEXT
);
CREATE TABLE track(
trackid INTEGER,
trackname TEXT,
trackartist INTEGER REFERENCES artist
);
CREATE INDEX trackindex ON track(trackartist);
OR this one:
CREATE TABLE student(
SudentID INTEGER PRIMARY KEY,
First TEXT,
Last,
Contact Text,
Year INTEGER)
CREATE TABLE loan(
StudentID INTEGER,
ISBN INTEGER,
out INTEGER,
FOREIGN KEY(SudentID)
REFERENCES student(SudentID)
ON DELETE CASCADE
PRIMARY KEY(StudentID, ISBN)
)
CREATE INDEX StudentID_Index ON student(StudentID)

As long as it works, it's a style choice. IMHO coding the REFERENCES in-line on the column is better because it makes it more clear:
CREATE TABLE users (
id INTEGER PRIMARY KEY NOT NULL,
username TEXT NOT NULL,
pwhash TEXT NOT NULL,
email TEXT,
created INTEGER NOT NULL
);
CREATE TABLE posts (
id INTEGER PRIMARY KEY NOT NULL,
title TEXT NOT NULL,
description TEXT NOT NULL,
content TEXT NOT NULL,
id_user INTEGER NOT NULL REFERENCES users ON DELETE CASCADE
);
CREATE INDEX posts_id_user ON posts(id_user);
Also. no need to code the primary key column of the foreign table - referencing just the table is enough (the primary key is assumed).
See SQLFiddle.

I think i found a solution.
I'm doing this:
CREATE TABLE users(
id INTEGER PRIMARY KEY NOT NULL, /* No need for 'autoincrement' keyword */
username TEXT NOT NULL,
pwhash TEXT NOT NULL,
email TEXT,
created INTEGER NOT NULL
);
CREATE TABLE posts (
id INTEGER PRIMARY KEY NOT NULL,
title TEXT NOT NULL,
description TEXT NOT NULL,
content TEXT NOT NULL,
id_user INTEGER NOT NULL,
FOREIGN KEY (id_user)
REFERENCES users(id)
ON DELETE CASCADE
);
CREATE INDEX users_posts ON posts(id_user);

Related

References with PostgreSQL

I have this table:
CREATE TABLE cars_info.cars
(
id SERIAL,
owner_id INTEGER,
brand VARCHAR(50) NOT NULL,
model VARCHAR(50) NOT NULL,
color VARCHAR(50) NOT NULL,
register_number VARCHAR(50) NOT NULL,
created DATE NOT NULL,
PRIMARY KEY(id, brand, model, color, register_number, created),
CONSTRAINT fk_owner_id
FOREIGN KEY(owner_id)
REFERENCES persons_info.persons(id)
);
But when I tried create another table like this:
CREATE TABLE cars_info.violations
(
id SERIAL PRIMARY KEY,
car_id INTEGER NOT NULL,
message VARCHAR(100) NOT NULL,
active BOOLEAN NOT NULL,
CONSTRAINT fk_car_id
FOREIGN KEY(car_id)
REFERENCES cars_info.cars(id)
);
I get an error about that
Target external table "cars" does not have a unique constraint corresponding to the given keys
How can I fix that? I'm a beginner in SQL and don't know how to go about googling that
Your primary key definition for cars
PRIMARY KEY(id, brand, model, color, register_number, created)
makes no sense: The id column, being serial, is itself unique and it alone should be the primary key.
Delete your primary key definition and change the id column definition to:
id serial not null primary key
Unrelated, but best practice is to name table in the singular; name your tables car and violation rather than cars and violations

Oracle (ORA-02270) : no matching unique or primary key for that column list

I have two tables:
CREATE TABLE Trasy_srodki_transportu(
ID_Trasy Integer NOT NULL,
ID_pojazdu Integer NOT NULL
)
/
CREATE TABLE Trasy(
ID_Trasy Integer NOT NULL,
Linia Varchar2(4 ) NOT NULL,
Data_rozpoczecia_kursowania Date NOT NULL,
Data_zakonczenia_kursowania Date,
ID_Pracownika Integer NOT NULL
)
Now i want to add foreign key to Trasy_srodki_transportu referencing to Trasy table:
ALTER TABLE Trasy_srodki_transportu ADD CONSTRAINT Trasa_jest_wykorzystywana FOREIGN KEY (ID_Trasy) REFERENCES Trasy (ID_Trasy)
/
and this throws Oracle (ORA-02270) : no matching unique or primary key for this column-list error. Any suggestions how to fix this?Data modeler view
A foreign key needs to reference a key on the related table, but it's not the case in your example. Change the definition of the second table by adding a PRIMARY KEY constraint in it, as in:
CREATE TABLE Trasy (
ID_Trasy Integer PRIMARY KEY NOT NULL,
Linia Varchar2(4 ) NOT NULL,
Data_rozpoczecia_kursowania Date NOT NULL,
Data_zakonczenia_kursowania Date,
ID_Pracownika Integer NOT NULL
)
Alternatively, you can create a unique constraint on it, that can also serve as a key. For example:
CREATE TABLE Trasy (
ID_Trasy Integer NOT NULL,
Linia Varchar2(4 ) NOT NULL,
Data_rozpoczecia_kursowania Date NOT NULL,
Data_zakonczenia_kursowania Date,
ID_Pracownika Integer NOT NULL,
CONSTRAINT uq_idtrasy UNIQUE (ID_Trasy)
)

Is it possible to find the row using something other than the primaryKey on SQLite?

I want to be able to find the value using either network_id or username.
Yet the following sintax gives the error of more than one primary key (as expected).
CREATE TABLE Player(
network_id TEXT not null,
username varchar2(50) not null,
value INTEGER,
CONSTRAINT player_pk1 PRIMARY KEY (username),
CONSTRAINT player_pk2 PRIMARY KEY (network_id)
);
Is there a way that I could do this in Sqlite?
A primary key has three components to its definition:
NOT NULL
UNIQUE
Only one per table
That is why you cannot have more than one. But you can have any number of NOT NULL UNIQUE columns:
CREATE TABLE Player(
network_id TEXT not null,
username varchar2(50) not null,
value INTEGER,
CONSTRAINT player_pk1 PRIMARY KEY (username),
CONSTRAINT unq_player_network_id UNIQUE (network_id)
);

I have a query which contains mistakes. where are they?

I want to create a table in SQlite but can't:
CREATE TABLE IF NOT EXISTS[Goods]
(GoodId INTEGER PRIMARY KEY AUTOINCREMENT,
GoodName varchar(100) not null,
CategoryId INTEGER FOREIGN KEY (CategoryId) REFERENCES Category (CategoryId),
ProducerId INTEGER FOREIGN KEY (ProducerId) REFERENCES Producer (ProducerId),
Price REAL not null,
GoodCount REAL not null)
here are additional tables but they are all right. I don't have problems with them:
CREATE TABLE IF NOT EXISTS[Category] (CategoryId INTEGER PRIMARY KEY
AUTOINCREMENT, CategoryName varchar(100) not null)
CREATE TABLE IF NOT EXISTS[Producer] (ProducerId INTEGER PRIMARY KEY
AUTOINCREMENT, ProducerName varchar(100) not null)
Inline foreign keys do not take the FOREIGN KEY keyword.
Consider:
CREATE TABLE IF NOT EXISTS[Goods] (
GoodId INTEGER PRIMARY KEY AUTOINCREMENT,
GoodName varchar(100) not null,
CategoryId INTEGER REFERENCES Category (CategoryId),
ProducerId INTEGER REFERENCES Producer (ProducerId),
Price REAL not null,
GoodCount REAL not null
)

Error: Could not create constraint SQL Server 2008

I am creating 6 tables. When creating the first 3 tables, everything works well. But then when I have to use a Foreign Key that I have used previously when creating another table, the system presents an error.
These are the 3 first tables:
CREATE TABLE Employee (
EmployeeID INTEGER PRIMARY KEY NOT NULL,
Name TEXT NOT NULL,
Position TEXT NOT NULL,
Salary REAL NOT NULL,
Remarks TEXT
);
CREATE TABLE Planet (
PlanetID INTEGER PRIMARY KEY NOT NULL,
Name TEXT NOT NULL,
Coordinates REAL NOT NULL
);
CREATE TABLE Has_Clearance (
Employee INTEGER NOT NULL
CONSTRAINT fk_Employee_EmployeeID REFERENCES Employee(EmployeeID),
Planet INTEGER NOT NULL
CONSTRAINT fk_Planet_PlanetID REFERENCES Planet(PlanetID),
Level INTEGER NOT NULL,
PRIMARY KEY(Employee, Planet)
);
Then I create the 4th table:
CREATE TABLE Shipment (
ShipmentID INTEGER PRIMARY KEY NOT NULL,
Date TEXT,
Manager INTEGER NOT NULL
CONSTRAINT fk_Employee_EmployeeID REFERENCES Employee(EmployeeID),
Planet INTEGER NOT NULL
CONSTRAINT fk_Planet_PlanetID REFERENCES Planet(PlanetID)
);
And I get the following error:
"There is already an object named 'fk_Employee_EmployeeID' in the database. Could not create constraint."
Please let me know how to create the FK in this 4th table.
The constraint "fk_Employee_EmployeeID" is first created on table "Has_Clearance" and then you try to create another constraint with the same name - which is not allowed.
Just rename the constraint in the 4-th table like this:
CREATE TABLE Shipment (
ShipmentID INTEGER PRIMARY KEY NOT NULL,
Date TEXT,
Manager INTEGER NOT NULL CONSTRAINT fk_Maneger_EmployeeID REFERENCES Employee(EmployeeID),
Planet INTEGER NOT NULL CONSTRAINT fk_Planet_PlanetID REFERENCES Planet(PlanetID)
);