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.
Related
I was creating a database for a school project and I have this issue while doing it.
I'm using MySQL workbench 8.0.29
This is the database structure i'm following
Database_design
And the SQL:
CREATE DATABASE AEROPORTS;
USE AEROPORTS;
CREATE TABLE PILOTS (
IDENTIFICADOR INT,
NOM VARCHAR(15),
COGNOMS VARCHAR(30),
HORES_VOL INT,
PRIMARY KEY (IDENTIFICADOR)
)engine=innodb;
CREATE TABLE AEROPORTS (
NOM VARCHAR(20),
CIUTAT VARCHAR(20),
PRIMARY KEY (NOM)
)engine=innodb;
CREATE TABLE COMPANYIES (
IDENTIFICADOR INT,
NOM VARCHAR(20),
NACIONALITAT VARCHAR(20),
LOGO varbinary(50),
PRIMARY KEY (IDENTIFICADOR)
)engine=innodb;
CREATE TABLE VOLS (
COMPANYIA INT,
NUMERO_VOL INT,
SORTIDA DATETIME,
ARRIBADA DATETIME,
ORIGEN VARCHAR(20),
DESTI VARCHAR(20),
PRIMARY KEY (COMPANYIA, NUMERO_VOL),
FOREIGN KEY (COMPANYIA) REFERENCES COMPANYIES (IDENTIFICADOR),
FOREIGN KEY (DESTI) REFERENCES AEROPORTS (NOM)
)engine=innodb;
CREATE TABLE PASSATGERS (
COMPANYIA INT,
VOL INT,
NOM VARCHAR(15),
COGNOMS VARCHAR(30),
CLASSE VARCHAR(15),
PRIMARY KEY (COMPANYIA, VOL, NOM, COGNOMS),
FOREIGN KEY (COMPANYIA) REFERENCES VOLS (COMPANYIA),
FOREIGN KEY (VOL) REFERENCES VOLS (NUMERO_VOL)
)engine=innodb;
CREATE TABLE PILOTAR (
COMPANYIA INT,
VOL INT,
PILOT INT,
PRIMARY KEY (COMPANYIA, VOL, PILOT),
FOREIGN KEY (COMPANYIA) REFERENCES VOLS (COMPANYIA),
FOREIGN KEY (PILOT) REFERENCES PILOTS (IDENTIFICADOR)
)engine=innodb;
CREATE TABLE AVIONS (
NUMERO_AVIO INT,
HORES_VOLS DATETIME,
PLACES_PRIMERA INT,
PLACES_TURISTA INT,
COMPANYIA INT,
PRIMARY KEY (NUMERO_AVIO),
FOREIGN KEY (COMPANYIA) REFERENCES COMPANYIES (IDENTIFICADOR)
)engine=innodb;
I'm having some problems linking "COMPANYIES" from "PASSATGERS" table
This is the error that the application throws me
error_code
To reference a multi-column primary key in VOLS, make the foreign key have the same number of columns.
WRONG:
FOREIGN KEY (COMPANYIA) REFERENCES VOLS (COMPANYIA),
FOREIGN KEY (VOL) REFERENCES VOLS (NUMERO_VOL)
RIGHT:
FOREIGN KEY (COMPANYIA, VOL) REFERENCES VOLS (COMPANYIA, NUMERO_VOL)
CREATE TABLE Estudante (
Matrícula int,
RG int,
Nome varchar(20),
Email varchar(20),
Endereço_atual varchar(20),
Telefone_atual varchar(20),
Endereço_permanente varchar(20),
Telefone_permanente varchar(20),
Data_de_nascimento date,
Sexo varchar(1),
Série varchar(3),
Grau varchar(10),
Curso varchar(20),
PRIMARY KEY(Matrícula,RG)
);
CREATE TABLE Professor (
Matrícula int,
RG int,
CPF int,
Nome varchar(10),
Classificação varchar(3),
Endereço varchar(20),
Email varchar(10),
PRIMARY KEY(Matrícula,RG,CPF)
);
CREATE TABLE Departamento (
Nome varchar(20),
Código_departamento int,
Matrícula_prof int,
Ramal varchar(10),
Campus varchar(10),
PRIMARY KEY(Nome,Código_departamento),
CONSTRAINT FK_MatriculaProf FOREIGN KEY(Matrícula_prof) REFERENCES Professor (Matrícula)
);
CREATE TABLE Disciplina (
Código int,
Departamento_resp int,
Nome varchar(20),
Descrição varchar(20),
Carga_horária varchar(10),
Duração varchar(10),
Nível varchar(10),
PRIMARY KEY(Código),
CONSTRAINT FK_DepartamentoResp FOREIGN KEY(Departamento_resp) REFERENCES Departamento (Código_departamento)
);
CREATE TABLE Turma (
ID_Turma int,
Disciplina int,
Matricula_Instrutor int,
Período_aplicação varchar(10),
Ano int,
PRIMARY KEY(ID_Turma),
CONSTRAINT FK_Disciplina FOREIGN KEY(Disciplina) REFERENCES Disciplina (Código),
CONSTRAINT FK_MatriculaInstrutor FOREIGN KEY(Matricula_Instrutor) REFERENCES Professor (Matrícula)
);
CREATE TABLE Relatório_Notas (
Matricula_Estudante int,
Turma int,
Disciplina int,
Notas int,
PRIMARY KEY(Matricula_Estudante),
CONSTRAINT FK_MatriculaEstudante FOREIGN KEY(Matricula_Estudante) REFERENCES Estudante (Matrícula,RG),
CONSTRAINT FK_Turma FOREIGN KEY(Turma) REFERENCES Turma (ID_Turma),
CONSTRAINT FK_Disciplina FOREIGN KEY(Disciplina) REFERENCES Disciplina (Código)
);
The 3 first tables are ok, but on the "Disciplina" table I got this error:
Error Code MySQL Workbench: 1215 Cannot add foreign key constraint
You define a foreign key as
CONSTRAINT FK_DepartamentoResp FOREIGN KEY(Departamento_resp) REFERENCES Departamento (Código_departamento)
but there is no index starting with column Código_departamento on the parent table.
If you change the primary key on Departamento from (Nome,Código_departamento) to (Código_departamento, Nome) (reverse column order) or add an extra index on (Código_departamento) things should work.
In general a SHOW WARNINGS after receiving an Cannot add foreign key constraint error reveals more detailed information on the problem.
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)
);
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
I'm working on a database assignment and have run into this error.
The database should look like...
Legend: **Primary Key** *Foreign Key
.
Movies (**title, year**, length, genre, *studioName, *producerID)
StarsIn(***movieTitle, *movieYear, *starName**)
MovieStar(**name**, birthdate, address, gender)
MovieMaker(**ID**, name, address)
Studio (**name**, address, *presidentID)
And my code looks like this...
CREATE TABLE Movies (
title varchar(50),
year int,
length int,
genre varchar(50),
studioName varchar(50),
producerID varchar(50),
PRIMARY KEY (title,year)
FOREIGN KEY (studioName) REFERENCES Studio(name),
FOREIGN KEY (producerID) REFERENCES MovieMaker(ID),
);
CREATE TABLE StarsIn (
movieTitle varchar(50),
movieYear int,
starName varchar(50),
PRIMARY KEY (movieTitle,movieYear,starName),
FOREIGN KEY (movieTitle) REFERENCES Movies(title),
FOREIGN KEY (movieYear) REFERENCES Movies(year),
FOREIGN KEY (starName) REFERENCES MovieStar(name)
);
CREATE TABLE MovieStar (
name varchar(50),
birthdate int,
address varchar(50),
gender varchar(50),
PRIMARY KEY (name)
);
CREATE TABLE MovieMaker (
ID varchar(50),
name varchar(50),
address varchar(50),
PRIMARY KEY (ID)
);
CREATE TABLE Studio (
name varchar(50),
address varchar(50),
presidentID varchar(50),
PRIMARY KEY (name),
FOREIGN KEY (presidentID) REFERENCES MovieMaker(ID)
);
However, I get quite a few errors stating that the syntax for my foreign keys are off. Any chance someone could lend a helping hand?
Tables referencing other tables via foreign keys may only be defined after the referenced tables already exist. You must therefore reorder your tables so that those which are referenced in FOREIGN KEY constraints get created first.
Additionally, the foreign keys referencing Movies.year and Movies.title are going to fail because you do not have indices defined on those columns individually. You do have them as a composite PRIMARY KEY, but they need their own indices.
/* MovieStar, MovieMaker are referenced by other tables
but have no FKs of their own, so create them first */
CREATE TABLE MovieStar (
name varchar(50),
birthdate int,
address varchar(50),
gender varchar(50),
PRIMARY KEY (name)
);
CREATE TABLE MovieMaker (
ID varchar(50),
name varchar(50),
address varchar(50),
PRIMARY KEY (ID)
);
/* Studio can be created next, referencing only MovieMaker */
CREATE TABLE Studio (
name varchar(50),
address varchar(50),
presidentID varchar(50),
PRIMARY KEY (name),
FOREIGN KEY (presidentID) REFERENCES MovieMaker(ID)
);
/* Movies references 2 of the above */
CREATE TABLE Movies (
title varchar(50),
year int,
length int,
genre varchar(50),
studioName varchar(50),
producerID varchar(50),
/* A comma was missing here... */
PRIMARY KEY (title,year),
FOREIGN KEY (studioName) REFERENCES Studio(name),
FOREIGN KEY (producerID) REFERENCES MovieMaker(ID),
/* Add individual indices on Movies. Omitting this would result in errno 150 */
INDEX (year),
INDEX (title)
);
CREATE TABLE StarsIn (
movieTitle varchar(50),
movieYear int,
starName varchar(50),
PRIMARY KEY (movieTitle,movieYear,starName),
FOREIGN KEY (movieTitle) REFERENCES Movies(title),
FOREIGN KEY (movieYear) REFERENCES Movies(year),
FOREIGN KEY (starName) REFERENCES MovieStar(name)
);
Here is a demo of the whole thing building properly: http://sqlfiddle.com/#!2/eaf70c