SQL error 1215(HY000) cannot add foreign key constraint. - mysql

I looked through other questions on this topic, and the general consensus was to check my data type consistency. I am trying to create the table 'reviews' that has SSN and ProjectID as foreign keys, referencing SSN from Table Reviewer and ProjectID from Table Project
CREATE TABLE Reviews(
ProjectID VARCHAR(10) NOT NULL,
SSN INTEGER NOT NULL CHECK
(SSN>100000000 AND SSN<999999999),
ReviewerRole VARCHAR(50) NOT NULL,
FOREIGN KEY(SSN)REFERENCES Reviewer(SSN),
FOREIGN KEY(ProjectID)REFERENCES Project(ProjectID),
PRIMARY KEY(SSN,ProjectID)
);
The following are the create table statements for 'Project' and 'Reviewer'
CREATE TABLE Reviewer(
SSN INTEGER NOT NULL CHECK (SSN>100000000 AND SSN<999999999),
Firstname VARCHAR(50) NOT NULL,
Lastname VARCHAR(50) NOT NULL
);
CREATE TABLE Project(
ProjectID VARCHAR(10) NOT NULL,
Title VARCHAR(50),
Archived DATE NOT NULL,
ProjectStatus VARCHAR(50) NOT NULL,
PRIMARY KEY(ProjectID)
);
Project and Reviewer created without issue. My create Reviews query works if I remove the FK declaration for SSN and leave the FK declaration for ProjectID, but not the other way around.
What is the reason for the error? Is there another constraint I need to add?

You forgot to add a primary key to the SSN column in the Reviewer table. The query below builds correctly in sqlfiddle:
CREATE TABLE Reviewer(
SSN INTEGER NOT NULL CHECK (SSN>100000000 AND SSN<999999999),
Firstname VARCHAR(50) NOT NULL,
Lastname VARCHAR(50) NOT NULL,
PRIMARY KEY(SSN)
);
CREATE TABLE Project(
ProjectID VARCHAR(10) NOT NULL,
Title VARCHAR(50),
Archived DATE NOT NULL,
ProjectStatus VARCHAR(50) NOT NULL,
PRIMARY KEY(ProjectID)
);
CREATE TABLE Reviews(
ProjectID VARCHAR(10) NOT NULL,
SSN INTEGER NOT NULL CHECK
(SSN>100000000 AND SSN<999999999),
ReviewerRole VARCHAR(50) NOT NULL,
FOREIGN KEY(SSN)REFERENCES Reviewer(SSN),
FOREIGN KEY(ProjectID)REFERENCES Project(ProjectID),
PRIMARY KEY(SSN,ProjectID)
);

A foreign key can only reference to a Primary Key column in another table, you need to make SNN a primary key column.
CREATE TABLE Reviewer(
SSN INTEGER NOT NULL primary key CHECK (SSN>100000000 AND SSN<999999999),
Firstname VARCHAR(50) NOT NULL,
Lastname VARCHAR(50) NOT NULL
);

The referenced column for a foreign key column, needs to be Primary key OR have a unique constraint so your Reviewer table need to be:
CREATE TABLE Reviewer(
SSN INTEGER PRIMARY KEY CHECK (SSN>100000000 AND SSN<999999999),
Firstname VARCHAR(50) NOT NULL,
Lastname VARCHAR(50) NOT NULL
);
or
CREATE TABLE Reviewer(
SSN INTEGER NOT NULL CHECK (SSN>100000000 AND SSN<999999999),
Firstname VARCHAR(50) NOT NULL,
Lastname VARCHAR(50) NOT NULL,
UNIQUE(SSN)
);
Also as far as I know, MySQL Ignores Check Constraints, so you can remove them.

Related

Error Code: 1822. Failed to add the foreign key constraint. Missing index for constraint 'client_fk_name' in the referenced table 'client'

Why am I unable to add these foreign keys?
I'm trying to make clientName and employeeId both foreign keys in the events table. I realize I may not need clientEmail, but my error is currently showing at clientName.
CREATE TABLE client
(
clientName VARCHAR(50) PRIMARY KEY NOT NULL,
clientID INT NOT NULL auto_increment,
clientEmail VARCHAR(50) NOT NULL,
clientPass VARCHAR(50) NOT NULL,
phone VARCHAR(50) NOT NULL
);
CREATE TABLE admin
(
Name VARCHAR(50) NOT NULL,
adminId INT NOT NULL PRIMARY KEY auto_increment,
adminPass VARCHAR(50) NOT NULL,
adminEmail VARCHAR(50) NOT NULL
);
CREATE TABLE employee
(
Name VARCHAR(50) NOT NULL,
employeeId INT NOT NULL PRIMARY KEY auto_increment,
employeePass VARCHAR(50) NOT NULL,
employeeEmail VARCHAR(50) NOT NULL
);
CREATE TABLE event
(
eventId INT PRIMARY KEY auto_increment,
employeeId INT NOT NULL,
clientName VARCHAR(50) NOT NULL,
clientEmail VARCHAR(50) NOT NULL,
Constraint client_fk_name
Foreign Key (clientName)
references client (clientName),
Constraint client_fk_email
Foreign Key (clientEmail)
references client (clientEmail),
Constraint employee_fk_id
Foreign Key (employeeId)
references employee (employeeId)
);
Only these four tables.
So, one way to deal with this is to call the attribute in the parent table UNIQUE. This solves this error. However, as a side effect in my current experience, upon inserting into the database you may experience problems if your values are not unique (possibly obviously). But if you can guarantee uniqueness then just add Unique and you will be fine.
Unique specifies an attribute as a candidate key. Essentially it’s saying this could have been the primary key, but we chose something else.

Beginner - How to place info in tables - CREATE TABLE

I have to make a database and i'm already stuck with something. I want to make a database where i will have 5 tables. User, complaint, missing, wanted and stolen objects.
So I want to make a database where a user puts his info, then file a complaint. The user has the choice about 3 complaint subjects: missing, wanted or stolen object. So after the complaint, it should be placed in the right table.
I'm not sure on how to place the right info in the right table, I'm new with mySQL..
Here is my database for the moment:
CREATE TABLE User (
user_id INT NOT NULL AUTO_INCREMENT,
name VARCHAR(15) NOT NULL,
sexe VARCHAR(1) NOT NULL,
age INT(3) NOT NULL,
birthplace VARCHAR(50),
phoneNumber VARCHAR(20) NOT NULL,
email VARCHAR(50),
PRIMARY KEY (user_id)
);
CREATE TABLE complaint (
complaint_id INT NOT NULL AUTO_INCREMENT,
user INT NOT NULL,
complaint_sort VARCHAR(16),
title VARCHAR(150),
name VARCHAR(15),
date_complaint,
place VARCHAR(50) NOT NULL,
description VARCHAR(2000) NOT NULL,
PRIMARY KEY (complaint_id),
FOREIGN KEY (user_id)
REFERENCES Gebruiker (gebruiker_id)
);
CREATE TABLE missing (
missing_id INT NOT NULL AUTO_INCREMENT,
complaint_id INT,
PRIMARY KEY (missing_id),
FOREIGN KEY (complaint_id)
REFERENCES Complaint (complaint_id)
);
CREATE TABLE wanted (
wanted_id INT NOT NULL AUTO_INCREMENT,
complaint_id INT,
PRIMARY KEY (wanted_id),
FOREIGN KEY (complaint_id)
REFERENCES Complaint (complaint_id)
);
CREATE TABLE Stolen_objects (
Stolen_objects_id INT NOT NULL AUTO_INCREMENT,
complaint_id INT,
PRIMARY KEY (stolen_objects_id),
FOREIGN KEY (complaint_id)
REFERENCES Complaint (complaint_id)
);
There is no need to create three tables for complaint types - missing, wanted, stolen_objects.
Instead of creating three tables just add a flag in complaint table for complaintType with ENUM as data type with values like missing, wanted, stolenObjects.
Try this:
CREATE TABLE USER (
user_id INT NOT NULL AUTO_INCREMENT,
NAME VARCHAR(15) NOT NULL,
sexe VARCHAR(1) NOT NULL,
age INT(3) NOT NULL,
birthplace VARCHAR(50),
phoneNumber VARCHAR(20) NOT NULL,
email VARCHAR(50),
PRIMARY KEY (user_id)
);
CREATE TABLE complaint (
complaint_id INT NOT NULL AUTO_INCREMENT,
USER INT NOT NULL,
complaintType ENUM('Missing','Wanted','stolenObjects') NOT NULL,
complaint_sort VARCHAR(16),
title VARCHAR(150),
NAME VARCHAR(15),
date_complaint DATETIME,
place VARCHAR(50) NOT NULL,
description VARCHAR(2000) NOT NULL,
PRIMARY KEY (complaint_id),
FOREIGN KEY (user_id) REFERENCES USER (user_id)
);

Alter Table Error:150

I cant alter the table to make add a foreign key. Error: 150 comes up. Can't figure out what is wrong. Please assist
CREATE TABLE Staff (staffNo varchar(10) NOT NULL, Fname varchar(50),
Lname varchar(50), Staff_Adress varchar(100), Salary numeric(65),
JobPosition varchar(15), Sex varchar(1), DateOfBirth date, NIN varchar(25),OffceNo varchar(10),
PRIMARY KEY (staffNo),
CHECK (Sex IN ('M', 'F')));
CREATE TABLE Office (OfficeNo INT(10) unsigned NOT NULL, Address varchar(50) NOT NULL, City varchar (25) NOT NULL,
PhoneNo varchar(10) NOT NULL, ManagerNo varchar(10),
PRIMARY KEY (OfficeNo),
FOREIGN KEY (ManagerNo) references Staff(staffNo));
alter table Staff
add foreign key (OffceNo) references Office(OfficeNo) on delete set NULL;
Fix these issues:
Tables have to be using InnoDB
Columns have to have exact same data types
your problem is here :
alter table Staff
add foreign key (OffceNo) references Office(OfficeNo) on delete set NULL;
you have to change this in Staff table
OffceNo varchar(10)
to
OffceNo INT(10) unsigned
Or this in Office table.
OfficeNo INT(10) unsigned NOT NULL
to
OfficeNo varchar(10) NOT NULL
for they will be same structure and same data types to use foreign keys.

How to create table in mysql database with foreign key which is primary key of another table?

CREATE TABLE employee_detail(
e_id int auto_increment,
name varchar(20) not null,
address varchar(20) not null,
status varchar(200) not null,
primary key (e_id),
);
This is my first table(employee_login)and I want e_id as a foreign key in my next table (login) below
CREATE TABLE login(
login_id int auto_increment,
username varchar(20) not null,
password varchar(20) not null,
primary key (login_id),
e_id references employee_detail(e_id)
);
You can do it like follows :
CREATE TABLE person (
id SMALLINT UNSIGNED NOT NULL AUTO_INCREMENT,
name CHAR(60) NOT NULL,
PRIMARY KEY (id)
);
CREATE TABLE shirt (
id SMALLINT UNSIGNED NOT NULL AUTO_INCREMENT,
style ENUM('t-shirt', 'polo', 'dress') NOT NULL,
color ENUM('red', 'blue', 'orange', 'white', 'black') NOT NULL,
owner SMALLINT UNSIGNED NOT NULL REFERENCES person(id),
PRIMARY KEY (id)
);
I hope you understand the create table code. owner in the shirt table is the foreign key referencing id from the person table
You have several mistakes. You were missing e_id column in your Login Table, you can't add a foreign key without adding employee_detail's primary key column which in this case is e_id. Also you can't use password as a column name since it is a preestablished query you'll need to use something else like "pass".
CREATE TABLE employee_detail(
e_id int auto_increment,
name varchar(20) not null,
address varchar(20) not null,
status varchar(200) not null,
primary key (e_id));
CREATE TABLE login(
login_id INT PRIMARY KEY AUTO_INCREMENT,
username VARCHAR(20) NOT NULL,
pass VARCHAR(20) NOT NULL,
e_id INT,
FOREIGN KEY (e_id) REFERENCES employee_detail(e_id)
);

wish to use empID from `users` table to `leave` table

i am relatively new in phpmysql.i need some help...
i have two table namely users and leave.In users table i have following column
empID(int)
username(varchar)
password(varchar)
mac(varchar)
ip(varchar)
and in leave table i have following column
lid(int)
empname(varchar)
username(varchar)
nod(int)
sdate(date)
edate(date)
reason(varchar)
action(varchar)
Now i want to use empID in leave table.how can i use it.i have to say that i already apply a code but its not working.Its giving the following error..
1072 - Key column 'empID' doesn't exist in table
and the code
create table `leave`(
lid INT NOT NULL AUTO_INCREMENT,
empname VARCHAR(255) NOT NULL,
username VARCHAR(255) NOT NULL,
nod INT NOT NULL,
sdate DATE,
edate DATE,
reason VARCHAR(255) NOT NULL,
PRIMARY KEY(lid),
FOREIGN KEY(empID) REFERENCES users(empID)
);
so whats wrong...please help me..
create table `leave`(
lid INT NOT NULL AUTO_INCREMENT,
empname VARCHAR(255) NOT NULL,
username VARCHAR(255) NOT NULL,
nod INT NOT NULL,
sdate DATE,
edate DATE,
reason VARCHAR(255) NOT NULL,
PRIMARY KEY(lid),
FOREIGN KEY(empID) REFERENCES users(empID)
);
should be
create table `leave`(
lid INT NOT NULL AUTO_INCREMENT,
empID int, // here you need one like this
empname VARCHAR(255) NOT NULL,
username VARCHAR(255) NOT NULL,
nod INT NOT NULL,
sdate DATE,
edate DATE,
reason VARCHAR(255) NOT NULL,
PRIMARY KEY(lid),
FOREIGN KEY(empID) REFERENCES users(empID)
);
you are missing a empID column.
Your SQL to create the leave table includes the correct bit to create the foreign key relationship:
FOREIGN KEY(empID) REFERENCES users(empID)
but that only creates the relationship between the tables, based on the empID columns. It doesn't create the empID column for you! You also need a line above, to define the empID column in the leave table, something like this:
create table `leave`(
lid INT NOT NULL AUTO_INCREMENT,
empID INT NOT NULL,
empname ...
... more lines ,
FOREIGN KEY(empID) REFERENCES users(empID)