MySQL Foreign Key implementation - mysql

I'm new to MySQL and I have two tables named Person & Patient. I'm trying to create a simple foreign key relationship in Patient to a primary key in Person. All examples I've seen online follow the same structure I'm using, but I keep getting errors. Any help is greatly appreciated!
create table PERSON(
PatientID smallint UNSIGNED NOT NULL,
Firstname varchar (25),
Lastname varchar (25),
CONSTRAINT PatientID_pk PRIMARY KEY (PatientID)
);
And this the table I'm trying to add a foreign key to:
CREATE TABLE PATIENT(
PatientID smallint UNSIGNED NOT NULL,
DoctorID smallint UNSIGNED NOT NULL,
FOREIGN KEY (PatientID) REFERENCES PERSON(PatientID);

I think this is what you wanted
create table PERSON
(
PersonID smallint UNSIGNED NOT NULL AUTO_INCREMENT,
Firstname varchar (25),
Lastname varchar (25),
CONSTRAINT PersonID_pk PRIMARY KEY (PersonID)
);
CREATE TABLE PATIENT
(
PatientID smallint UNSIGNED NOT NULL,
DoctorID smallint UNSIGNED NOT NULL,
FOREIGN KEY (PatientID) REFERENCES PERSON(PersonID),
FOREIGN KEY (DoctorID) REFERENCES PERSON(PersonID),
UNIQUE KEY unique_key (PatientID, DoctorID)
);

Related

Failed to add foreign key constraints. Missing index for constraints

I keep getting this error on SQL - I've checked the answers, but as far as I can see the datatypes are the exact same? Not sure if I'm missing something.
Create table books (
ISBN BIGINT(13) UNSIGNED,
Title varchar(100) NOT NULL,
Author varchar(100) NOT NULL,
Publishing_Year int,
Publisher varchar(100),
Genre varchar(20),
Copies int
);
CREATE TABLE loans (
loan_id BIGINT(10) UNSIGNED,
user_id INT(5) UNSIGNED,
card_id INT(8) UNSIGNED,
ISBN BIGINT(13) UNSIGNED,
loan_date DATE,
return_by DATE,
Primary key (loan_id),
Foreign key (user_id) references users (user_id),
Foreign key (card_id) references library_cards (card_id),
Foreign key (ISBN) references books (ISBN)
);

Why does it fail to open the referenced table if it already exists?

Somehow I can't create a join table for two entities I already have.
Here is what I run on MySQL just fine, the last one is the one I'm struggling with.
create table instructor_detail_table(
instructor_detail_id int not null primary key auto_increment,
instructor_class varchar(40) not null
);
create table instructor_table(
instructor_id int not null primary key auto_increment,
instructor_name varchar(40) not null,
instructor_detail_id int not null,
foreign key (instructor_detail_id) references instructor_detail_table (instructor_detail_id)
);
create table course_table(
course_id int not null primary key auto_increment,
course_class varchar(40) not null,
instructor_id int not null,
foreign key (instructor_id) references instructor_table (instructor_id)
);
create table student_table(
student_id int not null primary key auto_increment,
student_name varchar(40) not null,
student_age int not null
);
create table student_course_table(
student_id int not null,
course_id int not null,
foreign key (student_id) references student_table (student_id),
foreign key (course_id) references course_table (course_id),
primary key (student_id, course_id)
);
When I try to create the student_course_table MySQL complaints it can't open the referenced table student_table
Thanks in advance
You can have this problem if the storage engine is not the good one.
For example 'MyISAM' instead of 'InnoDB' for the table 'student_table'.
Because MyISAM doesn't support foreign key.

Error 1072 MySQL

I keep on getting error 1072 when trying to add a foreign key attribute and linking my attribute to another table. I've tried different ways of writing this but I keep on getting the message that "department_id doesn't exist in the table". If you could give me a hand that would be awesome! thank you so so much!
Here is the my code:
Alter table employee
add constraint department_fk
foreign key ( department_id)
references department(department_id);
And here is the rest of my tables:
Create table Department (
department_id integer auto_increment primary key not null,
department_name varchar (50) not null,
Office_number varchar(50) not null,
phone char (20) not null
);
Create table employee (
employee_id integer auto_increment primary key not null,
first_name varchar (25) not null,
last_name varchar (25) not null,
phone char(20) not null,
email varchar (100) not null,
salary decimal (10,2)
);
Create table project (
project_id integer auto_increment primary key not null,
max_hours time not null,
start_date datetime not null,
end_date datetime not null,
Project_lead integer,
Constraint project_fk1
foreign key (employee_id)
references employee(employee_id),
Department_id integer,
Constraint project_fk2
foreign key (department_id)
references department (department_id)
);
Create table assignment (
employee_id integer not null,
project_id integer not null,
hours_worked time not null,
primary key (employee_id, project_id),
constraint assignment_fk1
foreign key(employee_id)
references employee(employee_id),
constraint assignment_fk2
foreign key (project_id)
references project(project_id)
);
Alter table project
drop foreign key department_id;
Thank you in advance!
You need a Foreign key column in your employee table:
Create table employee (
employee_id integer auto_increment primary key not null,
first_name varchar (25) not null,
last_name varchar (25) not null,
phone char(20) not null,
email varchar (100) not null,
dept_id integer,
salary decimal (10,2)
);
Alter table employee
add constraint department_fk
foreign key ( dept_id)
references department(department_id);
Trust the message ;) - the column "department_id" doesn't exist in your employee table!

mysql - cannot add foreign key constraint when creating a table

I'm new to sql.
I get this error when I try to create foreign keys:
cannot add foreign key constraint
when I'm trying to create the ORDERS table. here is my code:
drop database if exists Company;
create database Company;
use Company;
create table WORKERS(w_id varchar(4), w_name varchar(20) not null, telephone varchar(12), e_mail varchar(35) unique);
create table CUSTOMERS(customer_id varchar(4), customer_name varchar(20) not null, telephone varchar(12), e_mail varchar(35) unique, credit bool);
create table WAREHOUSE(m_id int unsigned primary key, describes varchar(20) not null, fl char(1));
create table ITEM(p_id int(4) unsigned primary key, p_name varchar(15) not null, p_price float not null);
create table INVENTORY(in_id int auto_increment primary key, m_id int unsigned not null, p_id int(4) unsigned not null, amount int not null,
foreign key (m_id) references WAREHOUSE(m_id),
foreign key (p_id) references ITEM(p_id)
);
create table ORDERS(o_id int auto_increment primary key, customer_id varchar(4),
p_id int(4) unsigned, w_id varchar(4), amount int unsigned not null,
date_of_order date not null,
foreign key (p_id) references ITEM(p_id),
foreign key (w_id) references WORKERS(w_id),
foreign key (customer_id) references CUSTOMERS(customer_id)
);
I put the ORDERS (where I have the problem) on different lines to make it easier to you to read.
I search here for an answer, but didn't found anything specific that answer my question.
anyone got any idea what the problem is? thank you!
The problem is that you are trying to create a FOREIGN KEY relationship to a field in the WORKERS and CUSTOMERS tables that aren't set up as a PRIMARY KEY.
The FOREIGN KEY needs to be pointing to a PRIMARY KEY. Change your create script to the following:
create table WORKERS(w_id varchar(4) primary key, w_name varchar(20) not null, telephone varchar(12), e_mail varchar(35) unique);
create table CUSTOMERS(customer_id varchar(4) primary key, customer_name varchar(20) not null, telephone varchar(12), e_mail varchar(35) unique, credit bool);
A word of caution, though. I would recommend not using VARCHAR (4) as a PRIMARY KEY field. I would recommend using an INT AUTO_INCREMENT instead.
The column of the referenced table should be declared as primary key to assign a foreign key. For your orders table, WORKERS(w_id) and CUSTOMERS(customer_id) are not declared as primary key, hence you get the error.
Modified Statements:
drop database if exists Company;
create database Company;
use Company;
create table WORKERS(w_id varchar(4) primary key, w_name varchar(20) not null, telephone varchar(12), e_mail varchar(35) unique);
create table CUSTOMERS(customer_id varchar(4) primary key, customer_name varchar(20) not null, telephone varchar(12), e_mail varchar(35) unique, credit bool);
create table WAREHOUSE(m_id int unsigned primary key, describes varchar(20) not null, fl char(1));
create table ITEM(p_id int(4) unsigned primary key, p_name varchar(15) not null, p_price float not null);
create table INVENTORY(in_id int auto_increment primary key, m_id int unsigned not null, p_id int(4) unsigned not null, amount int not null,
foreign key (m_id) references WAREHOUSE(m_id),
foreign key (p_id) references ITEM(p_id)
);
create table ORDERS(o_id int auto_increment primary key, customer_id varchar(4),
p_id int(4) unsigned, w_id varchar(4), amount int unsigned not null,
date_of_order date not null,
foreign key (p_id) references ITEM(p_id),
foreign key (w_id) references WORKERS(w_id),
foreign key (customer_id) references CUSTOMERS(customer_id)
);
SQLFiddle Demo

MySQL error: Cannot add foreign key constraint?

There always be the error:"Cannot add foreign key constraint" when I create my last table.
System: Mac OS X 10.9
DB : MySQL 5.6.14
DBM : Sequel Pro
CREATE TABLE users (
uid INT AUTO_INCREMENT PRIMARY KEY,
uname VARCHAR(20) NOT NULL,
uemail VARCHAR(20) NOT NULL,
ucity VARCHAR(20),
upassw VARCHAR(20) NOT NULL
);
CREATE TABLE songs(
sid INT AUTO_INCREMENT PRIMARY KEY,
sname VARCHAR(20) NOT NULL,
srldate DATE NOT NULL
);
CREATE TABLE albums (
albid INT AUTO_INCREMENT PRIMARY KEY,
albname VARCHAR(20) NOT NULL,
albrldate DATE NOT NULL,
albrltime TIME NOT NULL
);
CREATE TABLE artists (
aid INT AUTO_INCREMENT PRIMARY KEY,
aname VARCHAR(20) NOT NULL
);
CREATE TABLE genres (
gid INT AUTO_INCREMENT PRIMARY KEY,
gname VARCHAR(20) NOT NULL
);
CREATE TABLE playlists (
uid INT NOT NULL,
sid INT NOT NULL,
plname VARCHAR(20) NOT NULL,
plmdate DATE NOT NULL,
plmtime TIME NOT NULL,
PRIMARY KEY(uid, sid, plname, plmdate, plmtime),
FOREIGN KEY(uid) REFERENCES users(uid),
FOREIGN KEY(sid) REFERENCES songs(sid)
);
CREATE TABLE u_like_a (
aid INT NOT NULL,
uid INT NOT NULL,
PRIMARY KEY(aid, uid),
FOREIGN KEY(aid) REFERENCES artists(aid),
FOREIGN KEY(uid) REFERENCES users(uid)
);
CREATE TABLE be_fan (
aid INT NOT NULL,
uid INT NOT NULL,
PRIMARY KEY(aid, uid),
FOREIGN KEY(aid) REFERENCES artists(aid),
FOREIGN KEY(uid) REFERENCES users(uid)
);
CREATE TABLE follow (
uid INT NOT NULL,
to_uid INT NOT NULL,
PRIMARY KEY(uid, to_uid),
FOREIGN KEY(uid) REFERENCES users(uid),
FOREIGN KEY(to_uid) REFERENCES users(uid)
);
CREATE TABLE u_like_g (
gid INT NOT NULL,
uid INT NOT NULL,
PRIMARY KEY(gid, uid),
FOREIGN KEY(gid) REFERENCES genres(gid),
FOREIGN KEY(uid) REFERENCES users(uid)
);
CREATE TABLE u_share_pl(
uid INT NOT NULL,
from_uid INT NOT NULL,
plname VARCHAR(20) NOT NULL,
plmdate DATE NOT NULL,
plmtime TIME NOT NULL,
PRIMARY KEY(uid, from_uid, plname, plmdate, plmtime),
FOREIGN KEY(uid) REFERENCES users(uid),
FOREIGN KEY(from_uid) REFERENCES users(uid),
FOREIGN KEY(plname) REFERENCES playlists(plname),
FOREIGN KEY(plmdate) REFERENCES playlists(plmdate),
FOREIGN KEY(plmtime) REFERENCES playlists(plmtime)
); #####---> This is the last table.
ERROR comes from here. I really don't why.
I have check the type for all attributes. The type and name of attributes have no problem.
But the mysql always say "Cannot add foreign key constraint"
Here is you reference wrong forgien REFERENCES users(from_uid) in last table.
FOREIGN KEY(from_uid) REFERENCES users(from_uid)
from_uid not belong to users
This should be
FOREIGN KEY(from_uid) REFERENCES users(uid)
your playLists table has primary key combination of four columns, so you should supply all these four columns as forieng key in u_share_pl table.
Another composite key as a reference should be a single constraint like
FOREIGN KEY(from_uid,sid,plname,plmdate,plmtime) REFERENCES playlists(uid,sid,plname,plmdate,plmtime)
Your last table Create should be:
CREATE TABLE u_share_pl(
uid INT NOT NULL,
from_uid INT NOT NULL,
sid INT NOT NULL,
plname VARCHAR(20) NOT NULL,
plmdate DATE NOT NULL,
plmtime TIME NOT NULL,
PRIMARY KEY(uid, from_uid, plname, plmdate, plmtime),
FOREIGN KEY(uid) REFERENCES users(uid),
FOREIGN KEY(from_uid,sid,plname,plmdate,plmtime) REFERENCES playlists(uid,sid,plname,plmdate,plmtime)
);