trouble creating table using foreign key - mysql

create table department
(
dept_name varchar(20),
building varchar(20),
budget numeric(12, 2),
primary key(dept_name)
);
This is table that i created before...
now i am writing this:
create table course
(
course_id varchar(7),
title varchar(50),
dept_name varchar(20),
credits numeric(2, 0),
primary key(course_id),
foreign key (dept_name) references department
);
an it shows :
ERROR 1005 (HY000): Can't create table 'satyarth.course' (errno: 150)

You're missing a portion of the FOREIGN KEY requirement, as you cannot simply refer to another table:
foreign key (dept_name) references department(dept_name)

Add the reference table field name PFB
create table course(course_id varchar(7),
title varchar(50),
dept_name varchar(20),
credits numeric(2, 0),
primary key(course_id),
FOREIGN KEY (`dept_name`) REFERENCES `department` (`dept_name`)
);

Related

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

How to add a foreign key with references to a table which will be create in the next line

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.

Can't add my second Foreign Key Constraint

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.

many to many relationship in MySQL

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

It occurs ERROR 1005 (HY000): Can't create table, in studying <<Database System Concepts>>6ed

create table classroom
(building varchar(15),
room_number varchar(7),
capacity numeric(4,0),
primary key (building, room_number)
);
create table department
(dept_name varchar(20),
building varchar(15),
budget numeric(12,2) check (budget > 0),
primary key (dept_name)
);
The first two create sentence is right. But in
create table course
(course_id varchar(8),
title varchar(50),
dept_name varchar(20),
credits numeric(2,0) check (credits > 0),
primary key (course_id),
foreign key (dept_name) references department
on delete set null
);
It occurs ERROR 1005 (HY000): Can't create table ...
I don't know how to fix it.
Obviously, it's data type of foreign key is consistent.
To get details on the error run SHOW ENGINE INNODB STATUS\G and look for the LATEST FOREIGN KEY ERROR. It should be something like this:
------------------------
LATEST FOREIGN KEY ERROR
------------------------
2013-12-04 09:50:19 139428000 Error in foreign key constraint of table test/course:
foreign key (dept_name) references department
on delete set null
):
Syntax error close to:
on delete set null
)
The solution is to specify the column name of the parent table, like this:
create table course
(course_id varchar(8),
title varchar(50),
dept_name varchar(20),
credits numeric(2,0) check (credits > 0),
primary key (course_id),
foreign key (dept_name) references department (dept_name)
on delete set null
);
P.S. You should be aware that the CHECK constraints in your CREATE TABLE statements do not actually do anything. As stated in the manual:
The CHECK clause is parsed but ignored by all storage engines.