Problems when creating a table referencing other with FK - sql

I'm trying to create a database for a project that works like a school with some teachers,courses,classes etc.
I need to create a new Table involving 2 tables however I'm getting an error when creating the table Classes_Teachers.
There are no primary or candidate keys in the referenced table 'Classes' that match the referencing column list in the foreign key 'FK__Classes_T__Class__11007AA7'.
Here is my SQL Code:
CREATE TABLE Teachers(
id INT,
name varchar(40),
email varchar(30) FOREIGN KEY REFERENCES Users(email),
PRIMARY KEY(id)
);
CREATE TABLE Courses(
name varchar(20),
acr varchar(4),
teacher int FOREIGN KEY REFERENCES Teachers(id),
PRIMARY KEY(acr)
);
CREATE TABLE Classes(
id varchar(2),
courses_acronym varchar(4) FOREIGN KEY REFERENCES Courses(acr),
year_Semesters varchar(5),
PRIMARY KEY(id,courses_acronym),
);
CREATE TABLE Classes_Teacher(
Classes_id varchar(2) FOREIGN KEY REFERENCES Classes(id),
Teachers_id INT FOREIGN KEY REFERENCES Teachers(id),
courses_acronym varchar(4) FOREIGN KEY REFERENCES Classes(courses_acronym),
PRIMARY KEY(Classes_id,courses_acronym)
);

The error comes from the foreign keys declared in table Classes_Teacher. To relate to the Classes table, you want to use both primary columns of the referred tables, instead of twn separated foreign keys.
Consider:
CREATE TABLE Classes_Teacher(
Classes_id varchar(2),
Teachers_id INT FOREIGN KEY REFERENCES Teachers(id),
courses_acronym varchar(4),
FOREIGN KEY (Classes_id, courses_acronym) REFERENCES Classes(id, courses_acronym),
PRIMARY KEY(Classes_id,courses_acronym)
);
Demo on DB Fiddle.

In table Classes you define a Primary Key on two columns. This is probably not needed if courses_acronym depends on ID.
CREATE TABLE Classes(
id varchar(2),
courses_acronym varchar(4) FOREIGN KEY REFERENCES Courses(acr),
year_Semesters varchar(5),
PRIMARY KEY(id),
);
In table Classes_Teacher you refer Classes twice. Shouldn't the second Foreign Key refer Courses?
CREATE TABLE Classes_Teacher(
Classes_id varchar(2) FOREIGN KEY REFERENCES Classes(id),
Teachers_id INT FOREIGN KEY REFERENCES Teachers(id),
courses_acronym varchar(4) FOREIGN KEY REFERENCES Courses(acr),
PRIMARY KEY(Classes_id,courses_acronym)
);

Related

How do i create a foreign key in sql server with multiple columns?

i'm trying to create foreign key:
create table visit
(Num_vst int primary key,
date_vst date,
num_agnt int,
code_type int,
Constraint fk_vst foreign key (num_agnt,code_type)
references agente,type_de_verification (num_agnt,code_type);
where is the problem?
You are describing two separate relationships to two tables:
num_agnt is related to agente and code_type is related to type_de_verification
In this case you can define both foreign keys as shown below:
create table visit (
Num_vst int primary key,
date_vst date,
num_agnt int,
code_type int,
constraint fk1 foreign key (num_agnt) references agente (num_agnt),
constraint fk2 foreign key (code_type)
references type_de_verification (code_type)
);

Add constraints for a foreign key that references multipleprimary keys from different tables in SQL Plus?

I'm a beginner learning SQL and am having trouble implementing this concept.
Suppose you create the following three tables:
CREATE TABLE dogOwner(
ownerNo VARCHAR(8) CONSTRAINT ownerNo_pk1 PRIMARY KEY,
ownerName VARCHAR(10)
);
CREATE TABLE catOwner(
ownerNo VARCHAR(8) CONSTRAINT ownerNo_pk2 PRIMARY KEY,
ownerName VARCHAR(10)
);
CREATE TABLE petsAdopted(
petNo VARCHAR(8) CONSTRAINT petNo_pk PRIMARY KEY,
ownerNo VARCHAR(8) CONSTRAINT ownerNo_fk1 REFERENCES dogOwner(ownerNo)
CONSTRAINT ownerNo_fk2 REFERENCES catOwner(ownerNo)
);
How do you properly create constraints for the foreign key ownerNo, which references ownerNo from two other tables?
You can't. You could have 2 columns in petsAdopted: dogOwnerNo and catOwnerNo and 2 foreign keys. But the table design doesn't seem to make sense: surely a pet either is a dog or a cat (or something else) regardless of who owns it?
Here is an alternative design:
CREATE TABLE owner(
ownerNo VARCHAR(8) CONSTRAINT ownerNo_pk2 PRIMARY KEY,
ownerName VARCHAR(10)
);
CREATE TABLE petsAdopted(
petNo VARCHAR(8) CONSTRAINT petNo_pk PRIMARY KEY,
petType VARCHAR2(10) NOT NULL CONSTRAINT petTypeChk (CHECK petType in ('CAT','DOG'))
ownerNo VARCHAR(8) CONSTRAINT ownerNo_fk REFERENCES owner(ownerNo)
);
This only address syntax part of your answer which You have got lot of syntax error, but this will not work, Please rethink your design.
CREATE TABLE dogOwner(
ownerNo VARCHAR(8) CONSTRAINT ownerNo_pk1 PRIMARY KEY,
ownerName VARCHAR(10)
);
CREATE TABLE catOwner(
ownerNo VARCHAR(8),
ownerName VARCHAR(10),
CONSTRAINT ownerNo_pk2 PRIMARY KEY (ownerNo),
CONSTRAINT ownerNo_fk1 FOREIGN KEY (ownerNo) REFERENCES dogOwner(ownerNo)
);
CREATE TABLE petsAdopted(
petNo VARCHAR(8) ,
ownerNo VARCHAR(8),
CONSTRAINT petNo_pk PRIMARY KEY (petNo),
CONSTRAINT ownerNo_fk_pet1 FOREIGN KEY (ownerNo) REFERENCES dogOwner(ownerNo),
CONSTRAINT ownerNo_fk_pet2 FOREIGN KEY (ownerNo) REFERENCES catOwner(ownerNo)
);

column "parent_id" referenced in foreign key constraint does not exist when creating SQL table

I am new in SQL and trying to understand Foreign key syntax. I know this was asked in multiple questions but each question I found did not seem to teach me what I am doing wrong here.
This is my SQL code:
CREATE TABLE Customer
(
id int primary key,
name varchar(30),
age int,
gender bool
);
CREATE TABLE Minor
(
FOREIGN KEY (parent_id) REFERENCES Customer(id)
);
CREATE TABLE Adult
(
FOREIGN KEY (parent_id) REFERENCES Customer(id)
);
CREATE TABLE Shop
(
id int primary key
);
CREATE TABLE Drink
(
name varchar(30) primary key
);
CREATE TABLE AlcoholicDrink
(
FOREIGN KEY (name) REFERENCES Drink(name)
);
CREATE TABLE NonAlcoholicDrink
(
FOREIGN KEY (name) REFERENCES Drink(name)
);
And this is the error I am getting:
ERROR: column "parent_id" referenced in foreign key constraint does not exist
SQL state: 42703
You need to add fields in your tables to make the reference. Something like this :
CREATE TABLE Customer
(
id int primary key,
name varchar(30),
age int,
gender bool
);
CREATE TABLE Minor
(
minor_id serial primary key,
parent_id int,
other_fields text etc.
FOREIGN KEY (parent_id) REFERENCES Customer(id)
);
This is simply the reason why it's not working.
Like this
CREATE TABLE Customer
(
id int primary key,
name varchar(30),
age int,
gender bool
);
CREATE TABLE Minor
(
parent_id int ,
FOREIGN KEY (parent_id) REFERENCES Customer(id)
);
To add an answer without just a code snippet:
You've got the right idea with FOREIGN KEY (parent_id) REFERENCES Customer(id). This bit adds a constraint or a "rule" to your table. This constraint ensures that parent_id holds a real id in your Customer table.
The error message told us that column "parent_id" referenced in foreign key constraint does not exist. The confusion comes, understandably, from mistaking the constraint declaration for a column declaration.
As others have pointed out, we need to both 10 declare the foreign key constraint and 2) declare the column.
CREATE TABLE Customer
(
id int primary key,
name varchar(30),
age int,
gender bool
);
CREATE TABLE Minor
(
parent_id int, -- Declare the column
FOREIGN KEY (parent_id) REFERENCES Customer(id) -- Declare the constraint
);

I am creating a database and i keep getting "INVALID IDENTIFIER" Error

CREATE TABLE Review(
VersionID VARCHAR(5) PRIMARY KEY NOT NULL,
rating INT,
FOREIGN KEY (PaperID) REFERENCES Paper(PaperID),
FOREIGN KEY (Revi) REFERENCES Paper(PaperID)
);
You should do it this way:
CREATE TABLE Review(
VersionID VARCHAR(5) PRIMARY KEY NOT NULL,
rating INT,
constraint constraint_1_name FOREIGN KEY (PaperID) REFERENCES Paper(PaperID),
constraint constraint_2_name FOREIGN KEY (Revi) REFERENCES Paper(PaperID)
);
(Of course instead of paperId/Revi you should use existing field names...)

There are no primary or candidate keys in the referenced table 'factIngresosCurso' that match the referencing column

Help please, I'm always getting this message:
There are no primary or candidate keys in the referenced table 'factIngresosCurso' that match the referencing column list in the foreign key 'fk_ingresos_curso'.
My tables:
create table factIngresos
(
cicCod char(6),
curcod char(5) references dimCurso,
cursec char(2),
codSec char(1) references dimSeccion,
tiempoid int references dimTiempo,
codHor int references dimHorario,
proCodUno char(5) references dimPersonal,
vacantes int,
inscritos int,
ingresoProyectado money,
ingresoNeto money,
becas money default 0,
constraint pk_Ingresos primary key(cicCod, curCod, curSec)
)
go
create table factIngCursoAlumno
(
alucod char(8) references dimAlumno,
cursec char(2), ciccod char(6), curcod char(5),
alucurCosto float, alucurefe float,
alucurPF float,
constraint pk_facIC primary key(alucod, cursec, ciccod, curcod),
constraint fk_ingresos_curso
foreign key(cursec, ciccod, curcod) references factIngresos(cursec, ciccod, curcod)
)
go
The primary key in factIngresos is
cicCod, curCod, curSec
Yet in your other table, you reference it using these columns and sequence:
foreign key(cursec, ciccod, curcod)
As you can see, just guessing from the names of the columns, the ordering is not the same as in the primary key specification.
You need to ensure that the column and their ordering (!!) is identical - so my guess is, you would need to use this foreign key configuration instead:
constraint fk_ingresos_curso
foreign key(ciccod, curcod, cursec) references factIngresos(ciccod, curcod, cursec)