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

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)
);

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

Error implementing relational model in Oracle

MER
I need help creating this MER in Oracle. Specifically in FOTOS table as I have this code:
CREATE TABLE "FOTOS"
(
"ID_FOTO" INT NOT NULL ENABLE,
"ID_USU" INT NOT NULL ENABLE,
"FECHA" DATE NOT NULL ENABLE,
CONSTRAINT "FOTOS_PK"
PRIMARY KEY ("ID_FOTO") ENABLE,
CONSTRAINT "FOTOS_FK"
FOREIGN KEY ("ID_USU") REFERENCES "USUARIOS" ("ID_USU") ENABLE
)
However I keep getting this error:
ORA-02270: no matching unique or primary key for this column-list
ID_USU is one of two primary keys in USUARIOS
USUARIOS TABLE
ID_USU is one of two primary keys in USUARIOS
No. A table can have just one primary key. It can have more than one unique key, but you can only make one of them primary.
What you mistakenly did instead is create a composite primary key. You want the ID to be unique and the name to be unique, but instead you made the combination of ID and name unique, thus allowing duplicate IDs and duplicate names in the table.
What you have:
create table usuarios
(
id_usu number not null,
nomusu varchar2(50) not null,
...
constraint pk_usuarios primary key (id_usu, nomusu)
);
What you want instead:
create table usuarios
(
id_usu number not null,
nomusu varchar2(50) not null,
...
constraint pk_usuarios primary key (id_usu),
constraint uq_usuarios_nomusu unique (nomusu)
);
Foreign keys have to match the primary/unique key they reference column for column. Since the primary key of "USUARIOS" are columns "ID_USU" and "NOMUSU" you need two columns to add FK from "FOTOS" to "USUARIOS", so just add "NOMUSU" column to "FOTOS" and write:
CREATE TABLE "FOTOS"
(
"ID_FOTO" INT NOT NULL ENABLE,
"ID_USU" INT NOT NULL ENABLE,
"NOMUSU" VARCHAR2(50) NOT NULL ENABLE,
"FECHA" DATE NOT NULL ENABLE,
CONSTRAINT "FOTOS_PK"
PRIMARY KEY ("ID_FOTO") ENABLE,
CONSTRAINT "FOTOS_FK"
FOREIGN KEY ("ID_USU","NOMUSU") REFERENCES "USUARIOS" ("ID_USU","NOMUSU") ENABLE);

create a foreign key on a primary key of another table

CREATE TABLE public.impiegato(
CF varchar NOT NULL,
codice_reparto int4 NOT NULL,
mansione varchar NULL,
CONSTRAINT impiegato_pkey PRIMARY KEY (CF),
CONSTRAINT impiegato_fkey FOREIGN KEY (codice_reparto) REFERENCES reparto(codice),
);
CREATE TABLE public.reparto(
codice int4 NOT NULL,
nome varchar NULL,
cf_responsabile varchar NULL,
nome_responsabile varchar NULL UNIQUE,
CONSTRAINT reparto_pkey PRIMARY KEY (codice),
CONSTRAINT reparto_cf_responsabile_fkey FOREIGN KEY (cf_responsabile) REFERENCES impiegato(CF)
);
When i run the sql code it tells me that the impiegato table doesn't exist. Can I run a foreign key on a primary key of another table?
The referenced table must exists when a foreign key is declared.
Since in your case the two tables reference each other, it's not possible to solve this by simply creating the right one first.
You have to create the first one (any of them) first without the foreign key constraint, create the second on and then add the foreign key constraint to the first one.
Something along the lines of:
CREATE TABLE public.impiegato
(cf varchar
NOT NULL,
codice_reparto int4
NOT NULL,
mansione varchar
NULL,
CONSTRAINT impiegato_pkey
PRIMARY KEY (cf));
CREATE TABLE public.reparto
(codice int4
NOT NULL,
nome varchar
NULL,
cf_responsabile varchar
NULL,
nome_responsabile varchar
NULL
UNIQUE,
CONSTRAINT reparto_pkey
PRIMARY KEY (codice),
CONSTRAINT reparto_cf_responsabile_fkey
FOREIGN KEY (cf_responsabile)
REFERENCES impiegato
(cf));
ALTER TABLE public.impiegato
ADD CONSTRAINT impiegato_fkey
FOREIGN KEY (codice_reparto)
REFERENCES reparto
(codice);
Unfortunately, you didn't tag your DBMS. From some details I guessed it might be Postgres and the code above hence is Postgres code. If you don't use Postgres, you might need to adapt the ALTER TABLE statement, they can differ between DBMS.

I'm trying to create a primary key from 2 columns, but it doesn't work well

I'm learning Oracle by myself.
Here's my code:
create table Schedule
(
Schedule_SN number(10) primary key,
ScreeningDate date not null,
Price number(6) not null
);
create table Seat
(
Schedule_SN number(10) REFERENCES Schedule(Schedule_SN),
Seat_SN varchar2(4) not null
);
create table Reservation
(
Reservation_SN number(15) primary key,
DCtype number(2) not null,
DCamount number(7),
PaymentMethod number(1) not null,
TotalPrice number(7) not null,
ReservationDate date not null
);
create table Reservation_details ** I need help here **
(
Reservation_SN number(15) REFERENCES Reservation(Reservation_SN),
Schedule_SN number(10) REFERENCES Schedule(Schedule_SN),
Seat_SN varchar2(10) REFERENCES Seat(Seat_SN),
CONSTRAINT Reservation_detailesPK primary key (Reservation_SN, Schedule_SN)
);
Error messages:
Errors - ORA-02270: no matching unique or primary key for this column-list
02270. 00000 - "no matching unique or primary key for this column-list"
*Cause: A REFERENCES clause in a CREATE/ALTER TABLE statement gives a column-list for which there is no matching unique or primary key constraint in the referenced table.
*Action: Find the correct column names using the ALL_CONS_COLUMNS catalog view
How can I make my 2 columns (Reservation_SN, Schedule_SN) into a primary key?
The problem is with seat_sn. You want child column in reservation_details to reference parent column in seat, but the parent column is not a primary or unique key. Actually, seat has no primary key; just make seat_sn the primay key of this table (if this fits your use case), and the rest should run fine:
create table seat (
schedule_sn nmber(10) references schedule(schedule_sn),
seat_sn varchar3(4) primary key
)
Demo on DB Fiddle

error: there is no unique constraint matching given keys for referenced table "incident"

I know that this question has been already answered a million of times, but I couldn't find any solution. Well I have these three tables on postgres sql.
CREATE TABLE user_account (
id SERIAL not null,
firstName VARCHAR(60) not null,
lastName VARCHAR(60) not null,
password VARCHAR(150) not null,
email VARCHAR(40) not null UNIQUE,
isVolunteer BOOLEAN,
complete BOOLEAN,
CONSTRAINT pk_user PRIMARY KEY (id));
CREATE TABLE incident (
id SERIAL not null,
patientId INTEGER not null,
incidentTime VARCHAR(10) not null,
latitude NUMERIC not null,
longitude NUMERIC not null,
city VARCHAR(60) not null,
state VARCHAR(60),
country VARCHAR(60),
complete BOOLEAN,
CONSTRAINT pk_incident PRIMARY KEY (id, patientId),
CONSTRAINT fk_incident FOREIGN KEY (patientId)
REFERENCES user_account (id) MATCH SIMPLE
ON UPDATE CASCADE ON DELETE CASCADE);
CREATE TABLE incident_has_volunteer (
incidentId INTEGER not null,
volunteerId INTEGER not null,
incidentTime VARCHAR(10) not null,
complete BOOLEAN,
CONSTRAINT pk_incident_has_volunteer PRIMARY KEY (incidentId, volunteerId),
CONSTRAINT fk_volunteer FOREIGN KEY (volunteerId)
REFERENCES user_account (id) MATCH SIMPLE
ON UPDATE CASCADE ON DELETE CASCADE,
CONSTRAINT fk_incident FOREIGN KEY (incidentId)
REFERENCES incident (id) MATCH SIMPLE
ON UPDATE CASCADE ON DELETE CASCADE);
When I try to create the table incident_has_volunteer it throws the error there is no unique constraint matching given keys for referenced table "incident".
I tried to add on the third table and the patientId as a foreign key from table incident table but with no luck. I can't understand why it throws this error even if I have already set the primary keys on the incident table.
I'm not an expert in postgres, but I believe that the problem is while fk_incident is referencing incident.id, incident's primary key is made of id + patientId. Since incident.id is guaranteed to be unique only in combination with patientId, there's no way to ensure referential integrity.
I believe that if you add a unique constraint to incident.id (I'm assuming that it would be unique), your foreign key will be legal.
Very simply - one table of primary key acts as a foreign key for another table, so you must ensure that both key is referenced or not.
Simply you will not assign foreign key to the column of another table which does not have primary key. this is called as RDBMS.
Thanks