I have this entity relationship:
I want to save information about the current team and the last teams, also I save the different positions in each team.
I create this tables:
table of player:
create table player(
codPlayer varchar(12) primary key,
name varchar(30) unique not null,
codCurrentTeam varchar(12),
position varchar(20),
foreign key (codCurrentTeam) references team(codTeam)
);
table of team:
create table team(
codTeam varchar(12) primary key,
name varchar(40) unique not null
);
table of lastTeam:
create table lastTeam(
startDate date,
finishDate date,
codTeam varchar(12),
codPlayer varchar(12),
constraint pkLastTeam primary key (codTeam,codPlayer),
foreign key (codTeam) references team(codTeam),
foreign Key (codPlayer) references jugador (codPlayer)
);
table of positions:
create table position(
codPlayer varchar(12),
codLastTeam varchar(24),
position varchar(20),
primary key (codPlayer, codLastTeam),
foreign key (codPlayer) references jugador(codPlayer),
foreign key (codLastTeam) references lastTeam(pkLastTeam)
);
I can create all tables except position, mysql returns the next error:
Error Code: 1215. Cannot add foreign key constraint
perhaps my diagram have some mistakes, but I don't know how to implement the design
the issue is you dont have
create table position "pkLastTeam" column in lastTeam t
codPlayer varchar(12),
codLastTeam varchar(24),
position varchar(20),
primary key (codPlayer, codLastTeam),
foreign key (codPlayer) references jugador(codPlayer),
foreign key (codLastTeam) references lastTeam(pkLastTeam)
);
I think I can solve the issue adding a new atributte codPlayer1 and changing pkLastTeam for codTeam, codPlayer1.
the table would look like this:
create table position(
codPlayer varchar(12),
codTeam varchar(12),
codPlayer1 varchar(12),
position varchar(20),
primary key (codPlayer, codTeam,codPlayer1),
foreign key (codPlayer) references player(codPlayer),
foreign key (codPlayer,codPlayer1) references lastTeam(codTeam,codPlayer)
);
Related
CREATE DATABASE employeeDB;
USE employeeDB;
CREATE TABLE employees(
employeeid NUMERIC(9),
firstname VARCHAR(10),
lastname VARCHAR(20),
deptCode CHAR(5),
salary NUMERIC(9, 2),
PRIMARY KEY (employeeid)
);
CREATE TABLE projects(
projectid CHAR(8),
deptcode CHAR(5),
description VARCHAR(200),
startdate DATE,
stopdate DATE,
revenue NUMERIC(12, 2),
PRIMARY KEY (projectid),
FOREIGN KEY (deptcode) REFERENCES employees(deptCode)
);
CREATE TABLE departments(
code CHAR(5),
name VARCHAR(5),
managerid NUMERIC(9),
subdeptof CHAR(5),
PRIMARY KEY (code),
FOREIGN KEY (managerid) REFERENCES employees(employeeid),
FOREIGN KEY (subdeptof) REFERENCES projects(deptcode)
);
ALTER TABLE employees ADD FOREIGN KEY (deptCode) REFERENCES projects(deptcode);
Something wrong at the line CREATE TABLE projects(...). When I run the code in MySQL it give the Error Code 1822. What is the problem ? Any expert can help ?
You cant create foreign key with a non-primary key, and if you really want to create foreign key to non-primary key (column), the column must be indexed with a unique constraint on it.
So either create unique constraint on deptCode column, or reference by already existing primary key, or change the primary key.
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
CREATE TABLE Departament
(d_nume varchar(30), d_nr int, manager varchar(30), PRIMARY KEY(d_nr),
FOREIGN KEY (manager) REFERENCES Angajat(nume));
CREATE TABLE Angajat
(nume varchar(30), prenume varchar(30), cnp char(13), d_nr int, PRIMARY KEY(cnp),
FOREIGN KEY(d_nr) REFERENCES Departament(d_nr));
"Cannot add foreign key constraint"
I use this editor online http://sqlfiddle.com/
When two tables reference each other, you can use alter table:
CREATE TABLE Departament (
d_nr int,
d_nume varchar(30),
manager char(13),
PRIMARY KEY (d_nr)
);
CREATE TABLE Angajat (
cnp char(13),
nume varchar(30),
prenume varchar(30),
d_nr int,
PRIMARY KEY(cnp),
FOREIGN KEY(d_nr) REFERENCES Departament(d_nr)
);
ALTER TABLE Departament ADD CONSTRAINT FOREIGN KEY (manager) REFERENCES Angajat(cnp);
Foreign key references should also be to the primary key, which I normally make the first column in a table. Here is a SQL Fiddle.
CREATE TABLE Transcripts (sID VARCHAR(7), cNo VARCHAR(10),
semester VARCHAR(20), grade CHAR(1), PRIMARY KEY (sID)
);
CREATE TABLE Students (sID CHAR(7), sName VARCHAR(20),
bDate DATE, phone VARCHAR(12), major VARCHAR(30), avgGPA VARCHAR(4), PRIMARY KEY (sID),
FOREIGN KEY (sID)
REFERENCES Transcripts(sID)
);
CREATE TABLE Courses (cNo VARCHAR(10), cTitle VARCHAR(30),
creditHours VARCHAR(2), deptName VARCHAR(30), PRIMARY KEY (cNo),
FOREIGN KEY (cNo)
REFERENCES Transcripts(cNo)
);
So whenever I run this, the first foreign key works just fine, whenever I run the next table it gives me back this error "ERROR 1215 (HY000): Cannot add foreign key constraint" What did I do wrong?
This is the foreign key that produces the error:
FOREIGN KEY (cNo)
REFERENCES Transcripts(cNo)
But the Transcripts(cNo) is not part of any KEY in that table.
A foreign key must reference column(s) of a UNIQUE or PRIMARY KEY of the parent table.
See MySQL Creating tables with Foreign Keys giving errno: 150 for a good checklist required for a foreign key.
Index the foreign key column before adding the foreign key.
CREATE TABLE Courses (cNo VARCHAR(10), cTitle VARCHAR(30),
creditHours VARCHAR(2), deptName VARCHAR(30), PRIMARY KEY (cNo),
INDEX (cNo),
FOREIGN KEY (cNo)
REFERENCES Transcripts(cNo)
);
to define cNo as a foreign key on Courses table, it must be primary key
or a unique key of other table.
CREATE TABLE Transcripts(
sID VARCHAR(7),
cNo VARCHAR(10) NOT NULL UNIQUE,
semester VARCHAR(20),
grade CHAR(1),
PRIMARY KEY (sID)
);
http://sqlfiddle.com/#!9/fddf8
changing Transcripts, as I've written above wil solve your problem.
Isn't the logic backwards? Shouldn't `Transcripts have two FKs referencing the other two tables? And, as already pointed out, declare the other two tables first.
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),
)