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
Related
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
Hope somone can help me.
Session Table is here:
CREATE TABLE User (
UID int,
Name varchar(40),
Vorname varchar(15),
Titel varchar(40),
Geschlecht int,
EMailAdr varchar(40),
SHID int,
Guthaben int ,
PRIMARY KEY (UID),
FOREIGN KEY (SHID) REFERENCES Schule(SHID),
UNIQUE (EMailAdr)
);
CREATE TABLE Schule (
SHID int,
Name varchar(40),
Adr varchar(80),
PRIMARY KEY (SHID),
UNIQUE (Adr),
UNIQUE (Name)
);
CREATE TABLE Lokal (
LID int,
Name varchar(40),
Adr varchar(40),
KontoNr varchar(40),
PRIMAYR KEY (LID),
UNIQUE (Adr),
UNIQUE (Name)
);
CREATE TABLE Zahlungsarten (
ZID int,
Bezeichnung varchar(40),
PRIMARY KEY (ZID)
);
CREATE TABLE BestellteArtikel (
ArID int,
BID int,
Anzahl int,
PRIMARY KEY (ArID,BID)
);
CREATE TABLE Status (
SID int,
Bezeichnung varchar(40),
PRIMARY KEY (SID)
);
CREATE TABLE Artikel (
ArID int,
Bezeichnuhg varchar(40),
Beschreibung varchar(120),
Preis int,
LID int,
Liefervolumen int,
PRIMARY KEY (ArID),
UNIQUE (Bezeichnung)
);
CREATE TABLE Bestellungen (
BID int,
UID int,
Datum Date,
ZID int,
SID int,
SHID int,
LID int,
PRIMARY KEY (BID),
FOREIGN KEY (UID) REFERENCES User(UID),
FOREIGN KEY (ZID) REFERENCES Zahkungsarten(ZID),
FOREIGN KEY (SID) REFERENCES Status(SID),
FOREIGN KEY (SHID) REFERENCES Schule(SHID),
FOERIGN KEY (LID) REFERENCES Lokal(LID)
);
There are several problems.
In table User:
You're referencing the table Schule before you have created the latter table. The table must exist before you can declare a foreign key that references it. So this works if you create Schule before you create User (I tested it, and it works).
In table Lokal:
PRIMAYR KEY (LID),
You mistyped PRIMARY.
In table Artikel:
Bezeichnuhg varchar(40),
I think you mistyped Bezeichnung.
In table Bestellungen:
FOERIGN KEY (LID) REFERENCES Lokal(LID)
You mistyped FOREIGN.
With all due respect, you should have been able to correct these mistakes yourself without posting a question to Stack Overflow. Most of them are obvious spelling errors.
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)
);
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)
);
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 ΠΡΟΪΟΝΤΑ(ΚΩΔΙΚΟΣ_ΠΡΟΪΟΝΤΟΣ)
);