MySQL Workbench cannot add foreign key constraint - mysql

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 ΠΡΟΪΟΝΤΑ(ΚΩΔΙΚΟΣ_ΠΡΟΪΟΝΤΟΣ)
);

Related

When ever I run MySQL query I get syntax error

so basically I'm trying to create multiple tables at the same time from a Entity Relation diagram from lucidchart which it generates the query of the the E-R
CREATE TABLE `Centro_Vacunacion` (
`ID_Centro_Vacunacion` INT,
`Direccion_Centro_Vacunacion` NVARCHAR,
`Fecha_Inicio_Operacion` DATE,
`ID_Provincia` INT,
PRIMARY KEY (`ID_Centro_Vacunacion`)
);
CREATE TABLE `Personal_Medico` (
`ID_Personal_Medico` INT,
`Nombre_Personal_Medico` NVARCHAR,
`Apellido_Personal_Medico` NVARCHAR,
`Cedula_Personal_Medico` TINYINT,
PRIMARY KEY (`ID_Personal_Medico`),
KEY `UNIQUE` (`Cedula_Personal_Medico`)
);
CREATE TABLE `Personal_Centro` (
`ID_Personal_Medico` INT,
`ID_Centro_Vacunacion` INT,
FOREIGN KEY (`ID_Personal_Medico`) REFERENCES `Centro_Vacunacion`(`Fecha_Inicio_Operacion`),
FOREIGN KEY (`ID_Personal_Medico`) REFERENCES `Personal_Medico`(`Nombre_Personal_Medico`)
);
CREATE TABLE `Provincia` (
`ID_Provincia` INT,
`Nombre_Provincia` NVARCHAR,
`ID_Pais` INT,
PRIMARY KEY (`ID_Provincia`)
);
CREATE TABLE `Almacen` (
`ID_Almacen` INT,
`Nombre_Almacen` NVARCHAR,
`Direccion_Almacen` TINYTEXT,
`Cantidad_Vacunas` INT,
`Cantidad_Lotes` INT,
`ID_Vacuna` INT,
PRIMARY KEY (`ID_Almacen`)
);
CREATE TABLE `Persona` (
`ID_Persona` INT,
`Nombre_Persona` NVARCHAR,
`Apellido_Persona` NVARCHAR,
`Cedula_Persona` TINYINT,
`Fecha_Nacimiento` DATE,
`Sexo` NVARCHAR(12),
PRIMARY KEY (`ID_Persona`),
KEY `UNIQUE` (`Cedula_Persona`)
);
CREATE TABLE `Municipio` (
`ID_Municpio` INT,
`Nombre_Municipio` NVARCHAR,
`ID_Provincia` INT,
PRIMARY KEY (`ID_Municpio`),
FOREIGN KEY (`ID_Municpio`) REFERENCES `Provincia`(`ID_Provincia`)
);
CREATE TABLE `Pais` (
`ID_Pais` INT,
`Nombre_Pais` NVARCHAR,
PRIMARY KEY (`ID_Pais`)
);
CREATE TABLE `Laboratorio` (
`ID_Laboratorio` INT,
`Nombre_Laboratorio` NVARCHAR,
`Direccion_Laboratorio` NVARCHAR,
`Fecha_Registro` DATE,
`ID_Pais` INT,
`Vacunas_En_Laboratorio` INT,
PRIMARY KEY (`ID_Laboratorio`),
FOREIGN KEY (`Direccion_Laboratorio`) REFERENCES `Pais`(`ID_Pais`)
);
CREATE TABLE `Vacuna` (
`ID_Vacuna` INT,
`Nombre_Vacuna` NVARCHAR,
`Lote` NVARCHAR,
`Laboratorio_Fabricacion` TINYTEXT(50),
`Cantidad_Dosis` TINYINT,
`Intervalo_Dosis` TINYINT,
`Fecha_Creacion` DATE,
`Fecha_Vencimiento` DATE,
`ID_Almacen` INT,
PRIMARY KEY (`ID_Vacuna`)
);
CREATE TABLE `Datos_Vacunacion` (
`ID_Centro_Vacunacion` INT,
`ID_Persona` INT,
`Edad_Actual` TINYINT,
`ID_Vacuna` INT,
`Fecha_Vacunacion` DATE,
`ID_Personal_Medico` INT,
PRIMARY KEY (`ID_Centro_Vacunacion`),
FOREIGN KEY (`Edad_Actual`) REFERENCES `Persona`(`Apellido_Persona`),
FOREIGN KEY (`Edad_Actual`) REFERENCES `Vacuna`(`Laboratorio_Fabricacion`)
);
CREATE TABLE `Vacuna_Laboratorio` (
`ID_Vacuna` INT,
`ID_Laboratorio` INT,
`Vacunas_Totales_Producidas` INT
);
CREATE TABLE `Centro_Almacen` (
`ID_Centro_Vacunacion` INT,
`ID_Almacen` INT,
FOREIGN KEY (`ID_Almacen`) REFERENCES `Almacen`(`Direccion_Almacen`),
FOREIGN KEY (`ID_Centro_Vacunacion`) REFERENCES `Centro_Vacunacion`(`Direccion_Centro_Vacunacion`)
);
When ever I run the query I get a syntax error but I cant figure out where. I ve tried to recreate one table with a mysql table generator but it seems to generate the same query.
you must define a size for NVARCHAR like NVARCHAR(255)
CREATE TABLE `Personal_Medico` (
`ID_Personal_Medico` INT,
`Nombre_Personal_Medico` NVARCHAR(255),
`Apellido_Personal_Medico` NVARCHAR(255),
`Cedula_Personal_Medico` TINYINT,
PRIMARY KEY (`ID_Personal_Medico`),
KEY `UNIQUE` (`Cedula_Personal_Medico`)
);
i suspect your code is not for MYSQL
see it runs in MySQL with VARCHAR
https://www.db-fiddle.com/f/nJMBURY4NcXG2hgYLFhbBf/0

MySQL Cannot add Foreign Key Constraint 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

mySQL trying to create table with constraint foreign key

I have this simple code
create table transport (
CODE varchar (10),
TDATE date,
ID integer,
PNAME varchar (10),
primary key (CODE,TDATE,ID,PNAME) );
create table planes (
NAME varchar (10),
NSEATS integer,
FSEATS integer,
ECSEATS integer,
primary key (NAME),
CONSTRAINT foreign key(NAME) references transport(PNAME));
But it doesn't let me do the foreign key thing as it says errno: 150 "Foreign key constraint is incorrectly formed
Any tips appreciated
Thanks
The use of composite primary keys in this case is not reasonable. If you want a foreign key reference, you need to reference all columns in the primary key. So, this version is simpler:
create table transport (
transportId int not null auto_increment primary key,
CODE varchar (10),
TDATE date,
ID integer,
PNAME varchar (10),
unique (CODE, TDATE, ID, PNAME)
);
create table planes (
planeId int not null auto_increment primary key,
NAME varchar (10),
NSEATS integer,
FSEATS integer,
ECSEATS integer,
primary key (NAME),
transportId int,
CONSTRAINT foreign key(transportId) references transport(transportId)
);
Rule is that : A Foreign key in one table points to a Primary Key in another table.

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

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