mySQL trying to create table with constraint foreign key - mysql

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.

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

how to make a primary key a foreign key in another table?

These are my tables. I have a problem with the last one (Inscription) it doesn't accept CodeProjet as a foreign key. The error says table (Projet) doesn't have a primary key called CodeProjet but it does! I have used every trick that I know and nothing. I altered the table to add constraint, etc. Still nothing. I always get the same error. Here are the tables:
create database Gestion_Stages_Employe
create table Employe
(
NumEmploye int primary key,
NomEmploye varchar(15),
PrenomEmploye varchar(15),
SexeEmploye varchar(10),
DNaissEmploye date,
FonctionEmploye varchar(20)
)
create table TypeProjet
(
TypeProjet varchar(20) primary key,
libelleProjet varchar(20),
DureeProjet date,
)
create table Projet
(
CodeProjet int,
TypeProjet varchar(20),
DateDebut Date,
DateFin Date,
Constraint Pk_CodeProj primary key (CodeProjet,TypeProjet),
Constraint FK_TypeProj foreign key (TypeProjet) references TypeProjet(TypeProjet),
)
create table Inscription
(
NumEmploye int foreign key references Employe(NumEmploye),
CodeProjet int foreign key references Projet(CodeProjet),
dateiscription Date,
primary key (NumEmploye,CodeProjet),
)
As was mentioned in the comment, the Primary Key on the Projet table is a "composite key" (multiple columns are required to enforce uniqueness).
As a result, for your foreign key to work, you need to either remove "TypeProjet" from the primary key of Project (assuming CodeProjet is sufficient to uniquely identify a Projet), or you need to add TypeProjet to the Inscription table and define your foreign key with both columns, for example:
create table Inscription (
NumEmploye int foreign key references Employe(NumEmploye),
CodeProjet int,
dateiscription Date,
TypeProjet varchar(20),
Constraint fk_Inscription_project Foreign key (CodeProjet,TypeProjet) references Projet(CodeProjet,TypeProjet),
primary key (NumEmploye,CodeProjet),
)

Why can't I add a foreign key constraint on this?

CREATE TABLE usertypes (
userType INTEGER,
userName VARCHAR(12),
PRIMARY KEY(userType)
);
CREATE TABLE Users (
loginid CHAR(9),
name VARCHAR(15),
netid VARCHAR(15) NOT NULL,
password VARCHAR(15) NOT NULL,
userType INTEGER,
PRIMARY KEY (loginid),
UNIQUE (netid),
FOREIGN KEY (userType) REFERENCES usertypes(userType)
ON DELETE NO ACTION
);
CREATE TABLE Courses (
majorid CHAR(3),
cid CHAR (3),
secNum CHAR(2),
year CHAR(4),
semesterid CHAR(2),
profID CHAR(9),
PRIMARY KEY (majorid,cid,secNum,year,semesterid),
FOREIGN KEY (majorid) REFERENCES Majors (majorid),
FOREIGN KEY (profID) REFERENCES Users(loginid)
);
create table transcript(
cid char(3),
grade char(2),
primary key(cid, grade),
foreign key(cid) references courses(cid)
);
I can't add a foreign key cid in transcript. The foreign key for majorid in courses works fine but the one in transcript doesn't work.
Here's the error
Error Code: 1215. Cannot add foreign key constraint
Course.cid is not a key, so transcript cannot create a foreign key. Try the following:
CREATE TABLE Courses (
majorid CHAR(3),
cid CHAR (3),
secNum CHAR(2),
year CHAR(4),
semesterid CHAR(2),
profID CHAR(9),
PRIMARY KEY (majorid,cid,secNum,year,semesterid),
KEY (cid)
);
Short version is I don't think you can have a foreign key to something that isn't identified as a primary key or something with a unique constraint, see Foreign Key to non-primary key
I'm not sure how you are using the cid in the Courses table, if it is perhaps 110 and there is a Math 110 and a Physics 110. This has the problem of if someone has 110 on their transcript, does it reference Math or Physics?
If cid is a primary key, a unique value for each class, it should be a primary key all by itself. If you are in the situation of 110 for math and physics, you might be best served by adding a primary key that is unique for every row, such as an identity, auto incrementing key.

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

Problem with foreign keys in sql database

I have a school assignment that that is giving me and my friends headaches... So this is my queries that i have executed, one at a time ofc...
CREATE DATABASE GamblingSociety;
USE GamblingSociety;
CREATE TABLE GamblingDen
(
Name VARCHAR (20) PRIMARY KEY,
Address VARCHAR (50),
Phone VARCHAR (10)
);
CREATE TABLE Room (
RoomNr INT,
GameCapaity INT,
GamblingDenName VARCHAR (20),
PRIMARY KEY (RoomNr, GamblingDenName),
FOREIGN KEY (GamblingDenName) REFERENCES GamblingDen (Name)
);
CREATE TABLE Employee (
SSN CHAR (11) PRIMARY KEY,
Name VARCHAR (20),
Address VARCHAR(50),
Salary INT,
isBoss BOOL,
GamblingDenName VARCHAR (20),
FOREIGN KEY (GamblingDenName) REFERENCES GamblingDen (Name)
);
CREATE TABLE GameType (
Name VARCHAR (20) PRIMARY KEY,
WinningProcentage FLOAT,
ResponsibleEmployee CHAR (11),
FOREIGN KEY (ResponsibleEmployee) REFERENCES Employee (SSN)
);
CREATE TABLE Supplier (
Name VARCHAR (20) PRIMARY KEY,
Address VARCHAR (50)
);
CREATE TABLE SupplierOfGameType (
SupplierName VARCHAR (20),
GameTypeName VARCHAR (20),
PRIMARY KEY(SupplierName, GameTypeName),
FOREIGN KEY (SupplierName) REFERENCES Supplier (Name),
FOREIGN KEY (GameTypeName) REFERENCES GameType (Name)
);
CREATE TABLE GamblingTable (
TableNr INT,
RoomNr INT,
GamblingDenName VARCHAR (20),
GameTypeName VARCHAR (20),
Comments VARCHAR (128),
PRIMARY KEY (RoomNr, TableNr, GamblingDenName, GameTypeName),
FOREIGN KEY (RoomNr, GamblingDenName) REFERENCES Room (RoomNr, GamblingDenName),
FOREIGN KEY (GameTypeName) REFERENCES GameType (Name)
);
CREATE TABLE Shylock (
Phone CHAR (10) PRIMARY KEY,
Name VARCHAR (20),
Contry CHAR (3)
);
CREATE TABLE Customer (
SSN CHAR (11) PRIMARY KEY,
Name VARCHAR (20),
Phone CHAR (10),
Address VARCHAR (50)
);
But this last query does not work:
USE GamblingSociety;
CREATE TABLE GamblingInstance (
StartTime DATETIME,
EndingTime DATETIME,
GamblingDenName VARCHAR (20),
TableNr INT,
GameTypeName VARCHAR (20),
Customer CHAR (11),
Shylock CHAR (10),
Debt INT,
DebtPayed BOOL,
DebtPayedDate DATETIME,
PRIMARY KEY (StartTime, GameTypeName, Customer),
FOREIGN KEY (GamblingDenName, TableNr, GameTypeName) REFERENCES GamblingTable (GamblingDenName, TableNr, GameTypeName),
FOREIGN KEY (Customer) REFERENCES Customer (SSN),
FOREIGN KEY (Shylock) REFERENCES Shylock (Phone)
);
It gives me this error:
"Error Code: 1005. Can't create table 'gamblingsociety.gamblinginstance' (errno: 150)"
if I replace this:
FOREIGN KEY (GamblingDenName, TableNr, GameTypeName) REFERENCES GamblingTable (GamblingDenName, TableNr, GameTypeName),
with this:
FOREIGN KEY (GameTypeName) REFERENCES GamblingTable (GameTypeName),
It works, so it's something with the GamblingDenName and TableNr.
And i have google it and found a lot of tips, but nothing yeat have worked...
So anyone with any idea?
Thanks in advance!
There is no key in GamblingTable that can satisfy this REFERENCE:
REFERENCES GamblingTable (GamblingDenName, TableNr, GameTypeName),
That is, there is no key in GamblingTable for which GamblingDenName, TableNr, GameTypeName are the only columns, or the first three columns.
There is a key that satisfies REFERENCES GamblingTable (GameTypeName), which is why that version works.
(I have to say that I've never actually seen FOREIGN KEYs being used to parent other FOREIGN KEYs, but I guess it works).