MySQL Cannot add Foreign Key Constraint MySQL - mysql

I am having a hard time understanding why my code is giving me cannot add foreign key constraint error. I just started coding in MySQL so I am sorry for anything dumb.
drop table if exists COURSE_INSTRUCTORJG;
drop table if exists CLASSJG;
drop table if exists COURSEJG;
drop table if exists INSTRUCTORJG;
create table INSTRUCTORJG(
InstructorID int,
DepartHead int,
primary key(InstructorID)
);
create table COURSEJG(
CourseNo char(10),
ComputerNeeded smallint,
primary key(CourseNo)
);
create table CLASSJG(
Building CHAR(20),
RoomNum smallint,
Maxcap int,
primary key(Building, RoomNum)
);
create table COURSE_INSTRUCTORJG (
COURSEJG_CourseNo CHAR(10),
INSTRUCTORJG_InstructorID INT,
Semester CHAR(20),
Section INT,
CLASSJG_Building char(20),
CLASSJG_RoomNum smallint,
primary key(COURSEJG_CourseNo, INSTRUCTORJG_InstructorID, Semester, Section),
foreign key (COURSEJG_CourseNo) references COURSEJG(CourseNo),
foreign key (INSTRUCTORJG_InstructorID) references INSTRUCTORJG(InstructorID),
foreign key (CLASSJG_Building) references CLASSJG(Building),
foreign key (CLASSJG_RoomNum) references CLASSJG(RoomNum)
);
My error is coming from the line: foreign key (CLASSJG_RoomNum) references CLASSJG(RoomNum).

You can reference primary key, keys or unique comnstrqaint, but in table CLASSJG you have a double primary key, so reference both columns or define another key
create table CLASSJG(
Building CHAR(20),
RoomNum smallint,
Maxcap int,
primary key(Building, RoomNum),
KEY(RoomNum)
);
see sample fiddle

Related

how to create a double foreign key

I have to create a table how's structure is
create table reparto
(
numrep integer,
nomespec varchar(20),
nomeosp varchar(20),
cittaosp varchar(25),
primary key (numrep,nomespec,nomeosp,cittaosp),
foreign key(nomeosp,cittaosp) references ospedale(nomeosp,cittaosp),
foreign key nomespec references specializzazione(nomeospe)
);
of course I've already create the tables
create table ospedale
(
nomeosp varchar(20),
cittaosp varchar(25),
numasl integer,
idasp varchar(4),
primary key(nomeosp,cittaosp)
);
and
create table specializzazione
(
nomespe varchar (20) primary key
);
of course it doesn't work and I don't know what to do, can someone tell me how to create several differentsforeign key?
There's a few obvious things we can point out here. The column name given in the foreign key definition:
references specializzazione(nomeospe)
^
Doesn't match the column name in the table definition
... specializzazione ( nomespe ...
^^^^^^^
And the column list for the foreign key should be enclosed in parens
... foreign key (nomespec)
^ ^
You have some literal error and incorect order creating tables please check this:
create table specializzazione
(
nomespe varchar (20) primary key
);
create table ospedale
(
nomeosp varchar(20),
cittaosp varchar(25),
numasl integer,
idasp varchar(4),
primary key(nomeosp,cittaosp)
);
create table reparto
(
numrep integer,
nomespec varchar(20),
nomeosp varchar(20),
cittaosp varchar(25),
primary key (numrep,nomespec,nomeosp,cittaosp),
foreign key(nomeosp,cittaosp) references ospedale(nomeosp,cittaosp),
foreign key (nomespec) references specializzazione(nomespe)
);

Issues adding CONSTRAINT FOREIGN KEY in PhpMyAdmin

I am trying to create 5 tables (should not be too hard), but I am having issues assigning the foreign keys. PhpMyAdmin gives me this error:
Can't create table 'databasexx.gave' (errno: 150)
Basically all the tables with no foreign keys are created.
DROP TABLE IF EXISTS oppbygging;
CREATE TABLE oppbygging (
gnr INT,
dnr INT,
ant INT,
CONSTRAINT dnr_grn_pk PRIMARY KEY (gnr, dnr)
) ENGINE=InnoDB;
DROP TABLE IF EXISTS onske;
CREATE TABLE onske (
onr INT,
pnr INT,
gnr INT,
prioriet INT,
ferdig INT,
CONSTRAINT pnr_gnr_pk PRIMARY KEY (pnr, gnr)
) ENGINE=InnoDB;
DROP TABLE IF EXISTS person;
CREATE TABLE person (
pnr INT,
fornavn VARCHAR(64),
etternavn VARCHAR(64),
fdato DATE,
CONSTRAINT pnr_pk PRIMARY KEY (pnr),
CONSTRAINT person_pnr_fk FOREIGN KEY (pnr) REFERENCES onske(pnr)
) ENGINE=InnoDB;
DROP TABLE IF EXISTS gave;
CREATE TABLE gave
(
gnr int,
navn varchar (255) UNIQUE,
prod_tid int NOT NULL,
CONSTRAINT gnr_pk PRIMARY KEY (gnr),
CONSTRAINT gave_gnr_fk FOREIGN KEY (gnr) REFERENCES oppbygging(gnr),
CONSTRAINT gave_gnr_fk FOREIGN KEY (gnr) REFERENCES onske(gnr)
) ENGINE=InnoDB;
DROP TABLE IF EXISTS del;
CREATE TABLE del (
dnr INT,
navn VARCHAR(64),
lager_ant INT NOT NULL,
CONSTRAINT dnr_pk PRIMARY KEY (dnr),
CONSTRAINT del_dnr_fk FOREIGN KEY (dnr) REFERENCES oppbygging(dnr)
) ENGINE=InnoDB;
I am sure that I am making some sort of obvious mistake, but I just cannot figure it out. Any help would be very much appreciated.
Here is the relationship view, ignore the Eiendom table:
TO CREATE ANOTHER TABLE WITH A FOREIGN KEY, THE FOREIGN KEY
SPECIFIED MUST BE THE PRIMARY KEY OF AT LEAST ONE TABLE
THAT HAS ALREADY BEEN CREATED,E.G.
CREATE TABLE 1 WITH THE PRIMARY KEY:
CREATE TABLE PERSONS(
PERSON_ID INT(10),
FNAME VARCHAR(20)NOT NULL,
LNAME VARCHAR(20),
DOB DATETIME,
AMOUNTPAID DECIMAL(15,2),
PRIMARY KEY(PERSON_ID)
);
THEN CREATE TABLE 2 WITH THE FOREIGN KEY:
CREATE TABLE COMMENTS(
COMMENTID INT AUTO_INCREMENT,
COMMENTS VARCHAR(500),
PERSON_ID INT(10),
PRIMARY KEY(COMMENTID),
FOREIGN KEY (PERSON_ID) REFERENCES PERSONS(PERSON_ID)
);

MySQL Workbench cannot add foreign key constraint

My SQL script works fine, till some point where error 1215 appears. I don't know what is wrong whith the code.
Here's the code:
CREATE DATABASE IF NOT EXISTS ΝΟΣΟΚΟΜΕΙΟ;
CREATE TABLE IF NOT EXISTS ΤΜΗΜΑΤΑ
(ΚΩΔΙΚΟΣ_ΤΜΗΜΑΤΟΣ INT,
ΟΝΟΜΑΣΙΑ_ΤΜΗΜΑΤΟΣ CHAR,
ΣΥΝΟΛΙΚΟΣ_ΑΡΙΘΜΟΣ_ΚΛΙΝΩΝ INT,
ΔΙΑΘΕΣΙΜΟΣ_ΑΡΙΘΜΟΣ_ΚΛΙΝΩΝ INT,
PRIMARY KEY (ΚΩΔΙΚΟΣ_ΤΜΗΜΑΤΟΣ));
CREATE TABLE IF NOT EXISTS ΑΣΘΕΝΕΙΣ
(ΚΩΔΙΚΟΣ_ΑΣΘΕΝΗ INT,
ΟΝΟΜΑ_ΑΣΘΕΝΗ VARCHAR (20),
ΕΠΩΝΥΜΟ_ΑΣΘΕΝΗ VARCHAR (20),
ΦΥΛΛΟ VARCHAR (1),
ΗΜ_ΝΙΑ_ΓΕΝΝΗΣΗΣ DATE,
ΔΙΕΥΘΥΝΣΗ_ΑΣΘΕΝΗ CHAR ,
ΤΗΛΕΦΩΝΟ_ΑΣΘΕΝΗ VARCHAR (10),
PRIMARY KEY (ΚΩΔΙΚΟΣ_ΑΣΘΕΝΗ));
CREATE TABLE IF NOT EXISTS ΙΑΤΡΟΙ
(ΚΩΔΙΚΟΣ_ΙΑΤΡΟΥ INT ,
ΟΝΟΜΑ_ΙΑΤΡΟΥ VARCHAR (20),
ΕΠΩΝΥΜΟ_ΙΑΤΡΟΥ VARCHAR (20),
ΕΙΔΙΚΟΤΗΤΑ CHAR,
ΚΩΔΙΚΟΣ_ΤΜΗΜΑΤΟΣ INT,
ΔΙΕΥΘΥΝΣΗ_ΙΑΤΡΟΥ CHAR,
ΤΗΛΕΦΩΝΟ_ΙΑΤΡΟΥ VARCHAR (10),
PRIMARY KEY (ΚΩΔΙΚΟΣ_ΙΑΤΡΟΥ),
FOREIGN KEY (ΚΩΔΙΚΟΣ_ΤΜΗΜΑΤΟΣ) REFERENCES ΤΜΗΜΑΤΑ(ΚΩΔΙΚΟΣ_ΤΜΗΜΑΤΟΣ)
);
CREATE TABLE IF NOT EXISTS ΠΕΡΙΣΤΑΤΙΚΑ
(ΚΩΔΙΚΟΣ_ΠΕΡΙΣΤΑΤΙΚΟΥ INT,
ΚΩΔΙΚΟΣ_ΑΣΘΕΝΗ INT,
ΚΩΔΙΚΟΣ_ΤΜΗΜΑΤΟΣ INT,
ΚΩΔΙΚΟΣ_ΙΑΤΡΟΥ INT,
ΗΜ_ΝΙΑ_ΕΙΣΑΓΩΓΗΣ DATE,
ΗΜ_ΝΙΑ_ΕΞΙΤΗΡΙΟΥ DATE,
ΑΞΙΑ_ΠΕΡΙΣΤΑΤΙΚΟΥ INT,
PRIMARY KEY (ΚΩΔΙΚΟΣ_ΠΕΡΙΣΤΑΤΙΚΟΥ),
FOREIGN KEY (ΚΩΔΙΚΟΣ_ΑΣΘΕΝΗ) REFERENCES ασθενεισ(ΚΩΔΙΚΟΣ_ΑΣΘΕΝΗ),
FOREIGN KEY (ΚΩΔΙΚΟΣ_ΤΜΗΜΑΤΟΣ) REFERENCES τμηματα(ΚΩΔΙΚΟΣ_ΤΜΗΜΑΤΟΣ),
FOREIGN KEY (ΚΩΔΙΚΟΣ_ΙΑΤΡΟΥ) REFERENCES ιατροι(ΚΩΔΙΚΟΣ_ΙΑΤΡΟΥ)
);
CREATE TABLE IF NOT EXISTS ΚΑΤΗΓΟΡΙΕΣ_ΠΡΟΪΟΝΤΩΝ
(ΚΩΔΙΚΟΣ_ΚΑΤΗΓΟΡΙΑΣ INT,
ΟΝΟΜΑΣΙΑ_ΚΑΤΗΓΟΡΙΑΣ CHAR,
PRIMARY KEY (ΚΩΔΙΚΟΣ_ΚΑΤΗΓΟΡΙΑΣ)
);
CREATE TABLE IF NOT EXISTS ΠΡΟΪΟΝΤΑ
(ΚΩΔΙΚΟΣ_ΠΡΟΪΟΝΤΟΣ INT,
ΚΩΔΙΚΟΣ_ΚΑΤΗΓΟΡΙΑΣ INT,
ΟΝΟΜΑΣΙΑ_ΠΡΟΪΟΝΤΟΣ CHAR,
ΑΞΙΑ_ΜΟΝΑΔΑΣ_ΠΡΟΪΟΝΤΟΣ INT,
PRIMARY KEY (ΚΩΔΙΚΟΣ_ΠΡΟΪΟΝΤΟΣ),
FOREIGN KEY (ΚΩΔΙΚΟΣ_ΚΑΤΗΓΟΡΙΑΣ) REFERENCES ΚΑΤΗΓΟΡΙΕΣ_ΠΡΟΪΟΝΤΩΝ(ΚΩΔΙΚΟΣ_ΚΑΤΗΓΟΡΙΑΣ)
);
CREATE TABLE IF NOT EXISTS ΧΡΕΩΣΕΙΣ_ΠΕΡΙΣΤΑΤΙΚΩΝ
(ΚΩΔΙΚΟΣ_ΠΕΡΙΣΤΑΤΙΚΟΥ INT,
ΚΩΔΙΚΟΣ_ΠΡΟΪΟΝΤΟΣ INT,
ΗΜΕΡΟΜΗΝΙΑ_ΧΡΕΩΣΗΣ DATE,
ΠΟΣΟΤΗΤΑ INT,
PRIMARY KEY (ΗΜΕΡΟΜΗΝΙΑ_ΧΡΕΩΣΗΣ),
FOREIGN KEY (ΚΩΔΙΚΟΣ_ΠΕΡΙΣΤΑΤΙΚΟΥ) REFERENCES ΠΕΡΙΣΤΑΤΙΚΑ(ΚΩΔΙΚΟΣ_ΠΕΡΙΣΤΑΤΙΚΟΥ),
FOREIGN KEY (ΚΩΔΙΚΟΣ_ΠΡΟΪΟΝΤΟΣ) REFERENCES ΠΡΟΙΟΝΤΑ(ΚΩΔΙΚΟΣ_ΠΡΟΪΟΝΤΟΣ)
);
...........................................
I have a problem with the last two tables where that error occurs.Is there anyone that could help me ?
Thanks, Dimitris
I tested this on Linux, MySQL 5.6.17. I was able to get this to work with two changes:
Match the case of table names to the way they are spelled when you create them:
CREATE TABLE IF NOT EXISTS ΠΕΡΙΣΤΑΤΙΚΑ
(
. . .
FOREIGN KEY (ΚΩΔΙΚΟΣ_ΑΣΘΕΝΗ) REFERENCES ΑΣΘΕΝΕΙΣ(ΚΩΔΙΚΟΣ_ΑΣΘΕΝΗ),
FOREIGN KEY (ΚΩΔΙΚΟΣ_ΤΜΗΜΑΤΟΣ) REFERENCES ΤΜΗΜΑΤΑ(ΚΩΔΙΚΟΣ_ΤΜΗΜΑΤΟΣ),
FOREIGN KEY (ΚΩΔΙΚΟΣ_ΙΑΤΡΟΥ) REFERENCES ΙΑΤΡΟΙ(ΚΩΔΙΚΟΣ_ΙΑΤΡΟΥ)
);
Use the same spelling, including diacritics in ΠΡΟΪΟΝΤΑ:
CREATE TABLE IF NOT EXISTS ΧΡΕΩΣΕΙΣ_ΠΕΡΙΣΤΑΤΙΚΩΝ
(
. . .
FOREIGN KEY (ΚΩΔΙΚΟΣ_ΠΡΟΪΟΝΤΟΣ) REFERENCES ΠΡΟΪΟΝΤΑ(ΚΩΔΙΚΟΣ_ΠΡΟΪΟΝΤΟΣ)
);

error in SQL syntax create table

I was trying to feed the following commands to MySQL CLI with
, which seems to be good, however when I added one more table, it complained there was an error in syntax.
Here are the commands
CREATE TABLE IF NOT EXISTS User(
uid INT,
name VARCHAR(64) UNIQUE,
birthday date,
PRIMARY KEY(uid)
) ENGINE = InnoDB ;
CREATE TABLE IF NOT EXISTS UserEmail(
uid INT,
email VARCHAR(64),
PRIMARY KEY(uid, email),
FOREIGN KEY(uid) REFERENCES User(uid)
);
However if I wanted to add one more table, it said there's an syntax error near the ')' at the line where PRIMARY KEY(uid) lies.
CREATE TABLE IF NOT EXISTS User(
uid INT,
name VARCHAR(64) UNIQUE,
birthday date,
PRIMARY KEY(uid)
) ENGINE = InnoDB ;
CREATE TABLE IF NOT EXISTS UserEmail(
uid INT,
email VARCHAR(64),
PRIMARY KEY(uid, email),
FOREIGN KEY(uid) REFERENCES User(uid)
);
CREATE TABLE friendship(
invite_uid INT,
accept_uid INT,
start_date DATE,
PRIMARY KEY(invite_uid, accept_uid),
FOREIGN KEY(invite_uid) REFERENCES User(uid),
FOREIGN KEY(accept_uid) REFERENCES User(uid),
);
Not sure where went wrong since the error was not complained in the newly added command.
--UPDATE--
Well that was fixed, but there's one more problem.
Adding the following table throws
cannot create table "estore.contains" (errorno: 150)
CREATE TABLE contains(
uid INT,
wid INT,
pid INT,
PRIMARY KEY(uid, wid, pid),
FOREIGN KEY(uid) REFERENCES User(uid),
FOREIGN KEY(wid) REFERENCES Wishlist(wid),
FOREIGN KEY(pid) REFERENCES Product(pid)
);
--UPDATE 2--
Full Tables that I want to add
CREATE TABLE IF NOT EXISTS User(
uid INT,
name VARCHAR(64) UNIQUE,
birthday date,
PRIMARY KEY(uid)
) ENGINE = InnoDB ;
CREATE TABLE IF NOT EXISTS UserEmail(
uid INT,
email VARCHAR(64),
PRIMARY KEY(uid, email),
FOREIGN KEY(uid) REFERENCES User(uid)
);
CREATE TABLE friendship(
invite_uid INT,
accept_uid INT,
start_date DATE,
PRIMARY KEY(invite_uid, accept_uid),
FOREIGN KEY(invite_uid) REFERENCES User(uid),
FOREIGN KEY(accept_uid) REFERENCES User(uid)
);
CREATE TABLE Seller(
sid INT,
name VARCHAR(64),
PRIMARY KEY(sid)
);
CREATE TABLE Product(
pid INT,
sid INT,
name VARCHAR(64),
description TEXT,
price DOUBLE,
PRIMARY KEY(pid),
FOREIGN KEY(sid) REFERENCES Seller(sid)
);
CREATE TABLE buy(
uid INT,
pid INT,
time DATE,
PRIMARY KEY(uid, pid),
FOREIGN KEY(uid) REFERENCES User(uid),
FOREIGN KEY(pid) REFERENCES Product(pid)
);
CREATE TABLE Wishlist(
uid INT,
wid INT,
start_time DATE,
end_time DATE,
PRIMARY KEY(uid,wid),
FOREIGN KEY(uid) REFERENCES User(uid)
);
CREATE TABLE conntains(
uid INT,
wid INT,
pid INT,
PRIMARY KEY(uid, wid, pid),
FOREIGN KEY(uid) REFERENCES User(uid),
FOREIGN KEY(wid) REFERENCES Wishlist(wid),
FOREIGN KEY(pid) REFERENCES Product(pid)
)ENGINE = InnoDB;
Anyone could help? Thx
Check the last line of your code
FOREIGN KEY(accept_uid) REFERENCES User(uid),
Remove the comma at the end.
remove , a end of line
FOREIGN KEY(accept_uid) REFERENCES User(uid),
--------------------------------------------^
There is an extra comma on the third table.
CREATE TABLE IF NOT EXISTS User(
uid INT,
name VARCHAR(64) UNIQUE,
birthday date,
PRIMARY KEY(uid)
) ENGINE = InnoDB ;
CREATE TABLE IF NOT EXISTS UserEmail(
uid INT,
email VARCHAR(64),
PRIMARY KEY(uid, email),
FOREIGN KEY(uid) REFERENCES User(uid)
);
CREATE TABLE friendship(
invite_uid INT,
accept_uid INT,
start_date DATE,
PRIMARY KEY(invite_uid, accept_uid),
FOREIGN KEY(invite_uid) REFERENCES User(uid),
FOREIGN KEY(accept_uid) REFERENCES User(uid), <---- Extra comma
);
FOREIGN KEY(accept_uid) REFERENCES User(uid),<-- Comma should be removed
Try this:
CREATE TABLE friendship(
invite_uid INT,
accept_uid INT,
start_date DATE,
PRIMARY KEY(invite_uid, accept_uid),
FOREIGN KEY(invite_uid) REFERENCES User(uid),
FOREIGN KEY(accept_uid) REFERENCES User(uid)
);
#Daniel, you cannot use the name CONTAINS for a table since its an SQL keyword. Please create the table with some other name.
Fortunately I figured the second problem out, the foreign key was not made right, I need to use
FOREIGN KEY(uid, wid) REFERENCES Wishlist(uid, wid),
to refer the the primary key combination in Wishlist

One of Composite primary key as Foreign key Mysql

I Have two Tables.
CREATE TABLE One(
Oneid int,
Twoid int,
data char(20),
PRIMARY KEY(Oneid,Twoid) )
Table One is Oneid and Twoid as primary key.
CREATE TABLE Two(
Twoid int,
data char(20),
PRIMARY KEY(Twoid) )
And I want to One.Twoid is foreign key for Table Two.
How to solve it.
Thank a lot.
Add the constraint in the CREATE TABLE statement:
CREATE TABLE Two(
Twoid int,
data char(20),
PRIMARY KEY (Twoid));
CREATE TABLE One(
Oneid int,
Twoid int,
data char(20),
PRIMARY KEY (Oneid,Twoid),
FOREIGN KEY (Twoid) REFERENCES Two(Twoid)); -- <== here
See fiddle.
Or use ALTER TABLE if your tables already exist:
ALTER TABLE One
ADD CONSTRAINT FK_Twoid FOREIGN KEY (Twoid) REFERENCES Two (Twoid);
See fiddle.