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

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

Related

Problems when creating a table referencing other with FK

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

SQL: How to connect a foreign key to primary key in the some table

I have an ERD and I need to connect the foreign key to the primary key on the same table. I have tried the below SQL code:
CREATE TABLE Category(
CategoryID UNIQUEIDENTIFIER
CONSTRAINT cat_cid_pk PRIMARY KEY DEFAULT NEWID(),
CategoryName VARCHAR(100)
CONSTRAINT cat_can_nn NOT NULL,
ParentCategoryID UNIQUEIDENTIFIER
CONSTRAINT cat_pcid_fk REFERENCES SWD6_1B.[Category]([CategoryID]);
)
And below I have the ERD
Does anyone know how to do this?
I think you are trying to do something like:
CREATE TABLE Category(
CategoryID UNIQUEIDENTIFIER CONSTRAINT cat_cid_pk PRIMARY KEY DEFAULT NEWID(),
CategoryName VARCHAR(100) CONSTRAINT cat_can_nn NOT NULL,
ParentCategoryID UNIQUEIDENTIFIER
CONSTRAINT cat_pcid_fk foreign key (ParentCategoryID) REFERENCES [Category]([CategoryID])
);
Create the table and then use alter table:
alter table Category add constraint cat_pcid_fk
foreign key (ParentCategoryID) references SWD6_1B.[Category]([CategoryID]);

SQL Server : constraints and foreign keys

I have an assignment where I must
Create an Entity Relationship diagram of a particular situation, and
Write up the SQL code to represent the diagram
I am new to SQL Server, but I have a table class that has a primary key CRN varchar(10)(UNN) and two foreign keys, emp_id varchar(20) (NN) which has a 1 mandatory relationship with instructor, and room_number varchar(5) (UNN) which also has a 1 mandatory relationship with Classroom.
My code for table Class:
CREATE TABLE class
(
CRN varchar(10) UNSIGNED NOT NULL,
emp_id varchar(20),
room_number varchar(5),
enrollment smallint UNSIGNED NOT NULL,
CONSTRAINT pk_class PRIMARY KEY (CRN),
CONSTRAINT fk_class
FOREIGN KEY (emp_id) REFERENCES instructor (emp_id),
CONSTRAINT fk_class
FOREIGN KEY (room_number) REFERENCES classroom (room_number)
);
The error I'm getting is:
Constraint "FK_CLASS" already exists; SQL statement:
CREATE TABLE class
(CRN varchar(10) UNSIGNED NOT NULL,
emp_id varchar(20),
room_number varchar(5),
enrollment smallint UNSIGNED NOT NULL,
CONSTRAINT pk_class PRIMARY KEY (CRN),
CONSTRAINT fk_class FOREIGN KEY (emp_id) REFERENCES instructor (emp_id),
CONSTRAINT fk_class FOREIGN KEY (room_number) REFERENCES classroom (room_number) ) [90045-193]
I have seen many different examples on how to make a table have two foreign keys, but have had no luck. What am I doing wrong?
Contraints must have a unique name, and you are using name fk_class twice. So naming the one fk_instructor and the other fk_classroom should solve the problem.
Note that there is a shorter notation, which avoids such issues:
CREATE TABLE class (
CRN varchar(10) PRIMARY KEY,
emp_id varchar(20) references instructor (emp_id),
room_number varchar(5) REFERENCES classroom (room_number),
enrollment smallint UNSIGNED NOT NULL
);

Error There are no primary or candidate keys in the referenced table

I'm getting this error when trying to create a table with foreign key:
There are no primary or candidate keys in the referenced table 'TeamToPlayers' that match the referencing column list in the foreign key 'FKey2'.
I don't understand why, there is a primary key in the table TeamToPlayers.
Here are the queries:
create table TeamToPlayers
(TeamName varchar(50) NOT NULL,
PlayerName varchar(50) NOT NULL,
primary key(TeamName,PlayerName),
CONSTRAINT FKey FOREIGN KEY (TeamName) REFERENCES Teams(TeamName)
)
create table Players
(PlayerName varchar(50) NOT NULL,
primary key(PlayerName),
CONSTRAINT FKey2 FOREIGN KEY (PlayerName) REFERENCES TeamToPlayers(PlayerName)
);
Table TeamToPlayers primary key consists of two fields - you must reference both as otherwise it's not a key. I think you may have your key the wrong way round - it should be on TeamToPlayers and referencing Players like so:
create table TeamToPlayers
(
TeamName varchar(50) NOT NULL,
PlayerName varchar(50) NOT NULL,
primary key(TeamName,PlayerName),
CONSTRAINT FKey FOREIGN KEY (TeamName) REFERENCES Teams(TeamName),
CONSTRAINT FKey2 FOREIGN KEY (PlayerName) REFERENCES Players(PlayerName)
)
create table Players
(PlayerName varchar(50) NOT NULL,
primary key(PlayerName),
);

ORA-00904: : invalid identifier two references in Oracle

I cant find a good anwser too my question, I want to create a table that have references too one table but i use it twice in the code..
Vårdnadshavare VARCHAR(11),
FOREIGN KEY (Vårdnadshavare) REFERENCES Person(Personnummer),
Barn VARCHAR(11),
FOREIGN KEY (Barn) REFERENCES Person(Personnummer)
But then i get the error:
ORA-02256: number of referencing columns must match referenced columns
I understand that my reference is wrong some how but can´t figure it out...
Sorry for the swedish words!!
Create table Ärende(
Ärendenr VARCHAR(50) NOT NULL,
Handläggare VARCHAR(50),
FOREIGN KEY (Handläggare) REFERENCES Handläggare(Anställningsnr),
Vårdnadshavare VARCHAR(11),
FOREIGN KEY (Vårdnadshavare) REFERENCES Person(Personnummer),
Barn VARCHAR(11),
FOREIGN KEY (Barn) REFERENCES Person(Personnummer),
In Datum VARCHAR(50),
Ömmande Skäl VARCHAR(5),
Förskola VARCHAR(50),
FOREIGN KEY (Förskola) REFERENCES Förskola(IDnr),
PRIMARY KEY (Ärendenr)
);
I have used Person(Personnumer) as a reference before just not twice in createing a table..
I created this script which does work. Probably there is an issue with your indexes:
create table handläggare
( anställningsnr varchar(50) primary key
)
create table person
( personnummer varchar(11) primary key
)
;
create table förskola
( idnr varchar(50) primary key
);
create table ärende
( ärendenr varchar(50) not null
, handläggare varchar(50)
, foreign key (handläggare) references handläggare(anställningsnr)
, vårdnadshavare varchar(11)
, foreign key (vårdnadshavare) references person(personnummer)
, barn varchar(11)
, foreign key (barn) references person(personnummer)
, indatum varchar(50)
, ömmandeskäl varchar(5)
, förskola varchar(50)
, foreign key (förskola) references förskola(idnr)
, primary key (ärendenr)
);
There are two other issues in your script above which I edited:
In Datum should be one name, not separated by a space;
Ömmande Skäl, same as above.
In you create statement you have provided 4 referenes:
Handläggare VARCHAR(50),
FOREIGN KEY (Handläggare) REFERENCES Handläggare(Anställningsnr),
Vårdnadshavare VARCHAR(11),
FOREIGN KEY (Vårdnadshavare) REFERENCES Person(Personnummer),
Barn VARCHAR(11),
FOREIGN KEY (Barn) REFERENCES Person(Personnummer),
Förskola VARCHAR(50),
FOREIGN KEY (Förskola) REFERENCES Förskola(IDnr),
Please check that all 3 distinct columns are of same data type.
That means:
Handläggare(Anställningsnr) should be VARCHAR(50)
Person(Personnummer) should be VARCHAR(11)
Förskola(IDnr) should be VARCHAR(50).
As per me i think you are making a mistake in
Förskola(IDnr) should be VARCHAR(50)
which should have been a Number