Error 1215 MySQL Cannot add foreign key constraint - mysql

I am creating a WebApp for my stage tech crew to proper manage our hours worked. When I try to create a table 'Worked' I get a foreign key constraint error.
USE MySLS;
DROP TABLE IF EXISTS People;
DROP TABLE IF EXISTS Events;
DROP TABLE IF EXISTS Worked;
CREATE TABLE People
(pid int PRIMARY KEY,
pname varchar(255)
);
CREATE TABLE Events
(eid int PRIMARY KEY,
ename varchar(255),
edate DATE
);
CREATE TABLE Worked
(
pid int,
eid int,
hours decimal(3,1),
FOREIGN KEY (pid) REFERENCES People,
FOREIGN KEY (eid) REFERENCES Events
);
Data types are the same, I see no reason why this shouldn't work. Any thoughts?

shouldn't it go like this :
FOREIGN KEY(pid) REFERENCES People(pid)
FOREIGN KEY (eid) REFERENCES Events(eid)

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

ErrorNo 150 mySQL

I have this problem when I try to create two foreign key in the same table
how I can fix it & thank you
create table employee(
employeeNumber INT (11) primary key,
);
create table projects(
projectNumber INT(11) primary key ,
);
create table workat(
wemployeeNumber INT(11),
wprojectNUmber INT (11),
primary key (wemployeeNumber,wprojectNUmber),
FOREIGN key workat(wemployeeNumber) references employee(employeeNumber),
FOREIGN key workat(wprojectNUmber) references projects(projectNumber)
);
Your FOREIGN KEY syntax looks off to me, and I don't know why you wrap the definition in workat. Try this version:
CREATE TABLE workat (
wemployeeNumber INT(11),
wprojectNUmber INT(11),
PRIMARY KEY (wemployeeNumber, wprojectNumber),
FOREIGN KEY (wemployeeNumber) REFERENCES employee (employeeNumber),
-- projects, not project
FOREIGN KEY (wprojectNUmber) REFERENCES projects (projectNumber)
);
Here is a demo showing that your code compiles after making the above changes:
Demo

MySql Error 1005 erno:105 Can't create table

drop table if exists patient;
drop table if exists pan;
drop table if exists device;
drop table if exists sensor;
drop table if exists actuator;
drop table if exists wears;
drop table if exists connects;
drop table if exists period;
drop table if exists reading;
drop table if exists setting;
drop table if exists lives;
drop table if exists municipality;
create table patient
(patient_name varchar(255),
patient_number integer(10),
patient_address varchar(255),
primary key(patient_number));
create table pan
(pan_domain varchar(255),
pan_phone integer(9),
primary key(pan_domain));
create table device
(device_snum integer(10),
device_manuf varchar(255),
device_description varchar(255),
primary key(device_snum, device_manuf));
create table municipality
(municipality_nut4code integer(10),
municipality_name varchar(255),
primary key(municipality_nut4code));
create table period
(period_start time,
period_end time,
primary key(period_start, period_end));
create table sensor
(sensor_snum integer(10),
sensor_manuf varchar(255),
sensor_units varchar(255),
primary key(sensor_snum, sensor_manuf),
foreign key(sensor_snum) references device(device_snum),
foreign key(sensor_manuf) references device(device_manuf));
create table actuator
(actuator_snum integer(10),
actuator_manuf varchar(255),
actuator_units varchar(255),
primary key(actuator_snum, actuator_manuf),
foreign key(actuator_snum) references device(device_snum),
foreign key(actuator_manuf) references device(device_manuf));
create table wears
(wears_start time,
wears_end time,
wears_patient integer(10),
wears_pan varchar(255),
primary key(wears_start, wears_end, wears_patient),
foreign key(wears_start) references period(period_start),
foreign key(wears_end) references period(period_end),
foreign key(wears_patient) references patient(patient_number),
foreign key(wears_pan) references pan(pan_domain));
create table connects
(connects_start time,
connects_end time,
connects_snum integer(10),
connects_manuf varchar(255),
connects_pan varchar(255),
primary key(connects_start, connects_end, connects_snum, connects_manuf),
foreign key(connects_start) references period(period_start),
foreign key(connects_end) references period(period_end),
foreign key(connects_snum) references device(device_snum),
foreign key(connects_manuf) references device(device_manuf),
foreign key(connects_pan) references pan(pan_domain));
create table reading
(reading_snum integer(10),
reading_manuf varchar(255),
reading_datetime date,
reading_value numeric(10,2),
primary key(reading_snum, reading_manuf, reading_datetime),
foreign key(reading_snum) references sensor(sensor_snum),
foreign key(reading_manuf) references sensor(sensor_manuf));
create table setting
(setting_snum integer(10),
setting_manuf varchar(255),
setting_datetime date,
setting_value numeric(10,2),
primary key(setting_snum, setting_manuf, setting_datetime),
foreign key(setting_snum) references actuator(actuator_snum),
foreign key(setting_manuf) references actuator(actuator_manuf));
create table lives
(lives_start time,
lives_end time,
lives_patient integer(10),
lives_municipality integer(10),
primary key(lives_start, lives_end, lives_patient),
foreign key(lives_start) references period(period_start),
foreign key(lives_end) references period(period_end),
foreign key(lives_patient) references patient(patient_number),
foreign key(lives_municipality) references municipality(municipality_nut4code));
When I try to create this database on MySQL I get an error saying ERROR 1005 (HY000) can't create table ***** (errno: 150). The tables that can't be created are the ones which have foreign keys. I already checked the data types and that the references match a primary key of a previously created table.
I don't get the error. Can someone help?
Since you have composite keys in the tables you're referencing, you should make the foreign key composite as well. For example:
create table sensor
(sensor_snum integer(10),
sensor_manuf varchar(255),
sensor_units varchar(255),
primary key(sensor_snum, sensor_manuf),
foreign key(sensor_snum, sensor_manuf) references device(device_snum, device_manuf));
Also, if you have two tables with the same foreign key, you need to give names to the foreign keys. All foreign keys need to have different names, and if you don't give a name explicitly, MySQL generates the name based just on the columns in the foreign key, and you'll end up with duplicates.

Error 1215: Cannot add foreign key constraint - MySQL (Simple Tables)

The current tables I have are as follows:
CREATE TABLE course(
CourseNum INT(11),
CourseName VARCHAR(30),
NumOfUnit INT(11),
PRIMARY KEY(CourseNum)
);
CREATE TABLE timeandloc(
CourseNum INT(11),
Quarter VARCHAR(20),
DayTime VARCHAR(40),
RoomNum INT,
PRIMARY KEY(CourseNum, Quarter, DayTime),
FOREIGN KEY(CourseNum) REFERENCES course (CourseNum)
);
I was able to add those fine using a query, but when I try to add this table:
CREATE TABLE student(
StudentName VARCHAR(30),
CourseNum INT(11),
Quarter VARCHAR(20),
PRIMARY KEY(StudentName, CourseNum, Quarter),
FOREIGN KEY(CourseNum) REFERENCES course(CourseNum),
FOREIGN KEY(Quarter) REFERENCES timeandloc(Quarter)
);
I get
Error code: 1215. Cannot add foreign key constraint.
It seems to be this line that's the culprit:
FOREIGN KEY(Quarter) REFERENCES timeandloc(Quarter)
When I try to add the table without that line, everything works fine without a hitch.
I'm very new to MySQL and databases in general so I'm not sure what's wrong. Any help would be great. Thanks.
create a separate index on Quarter field in timeandloc table and then create problematic create table query.
alter table timeandloc add index idx_Quarter(Quarter);
CREATE TABLE student(
StudentName VARCHAR(30),
CourseNum INT(11),
Quarter VARCHAR(20),
PRIMARY KEY(StudentName, CourseNum, Quarter),
FOREIGN KEY(CourseNum) REFERENCES course(CourseNum),
FOREIGN KEY(Quarter) REFERENCES timeandloc(Quarter)
);
Update:
For performance point of view joining field in both tables (parent/child) should be indexed. When we create foreign key then mysql itself create an index on the field we create foreign key but could not allow without index on referenced column in referenced table. As index works from left to right, so in your case index for Quarter column will not be used from primary key, so need to create a separate index on it.

Syntax error User_id not found in table help !

Sql wont let me create these table because of a syntax error can someone help me.
drop table users;
drop table intrest;
drop table friendships;
create table users(
id INT,
Fname char(15),
Lname char(15),
email char(20),
street char(15),
state char(2),
zip INT,
age INT,
gender char (2),
phone INT,
User_password char(15),
primary key (id),
foreign key (intrest_id) references intrest(id)
);
create table Intrests(
id INT,
description char(30),
Primary key (id),
foreign key (users_id) references users(id)
);
create table User_intrest(
foreign key (users_id) references users(id),
foreign key (intrest_id) references intrest(id)
);
create table friendships(
User_1_id INT,
User_2_id INT,
description Char(20),
foreign key (users_id) references users(id)
);
create table Intrests( id INT, description char(30),
Primary key (id),
foreign key (users_id) references users(id) );
create table User_intrest( foreign key (users_id) references users(id),
foreign key (intrest_id) references intrest(id) );
For interests table, where is the column users_id defined?
Column definitions for user_interest table?
You have a cyclical FK relationship:
Users has Interests(id) as a FK, and Interests has user(id) as a FK.
You don't need EITHER of these since you have a user_intrest table to link them!
You also have several typos with table names, like intrests and intrest.
Also, you should make both your User fields in Friendships be FK to Users, I'm not sure why you want a third, unrelated Users_Id field.
The first (of many) syntax errors you have is in
create table users(
...
primary key (id),
foreign key (intrest_id) references intrest(id) <--- there is no table intrest
);
I would suggest:
But STOP! Don't just copy-paste. Try yourself to see why MySQL gives you that error message: Key column 'intrest_id' doesn't exist in table, isn't the error you get?
Try to first fix that error. What do you have to do? Add an intrest_id field in table users, not just declare it as FOREIGN KEY. (It's not a useful field to have in the end, but do it anyway).
Then rerun your query and try to fix next error. One by one. If you realy get stuck somewhere, well, you know a site to ask questions :)
So, try to fix errors one by one, until you have a script that is running without any errors at all. You'll have learned much more than simply copying and pasting any answer.
CREATE TABLE Users(
id INT,
Fname char(15),
Lname char(15),
email char(50), <-- 20 is too small for emails
street char(15),
state char(2),
zip INT,
age INT,
gender char (2),
phone char(20), <-- phone should be char, not INT
user_password char(15),
PRIMARY KEY (id)
); <-- You don't really need a foreign key to Interests,
<-- that's why you have the User_interest "middle" table
CREATE TABLE Interests(
id INT,
description char(30),
PRIMARY KEY (id)
); <-- You don't really need a foreign key to Users,
<-- for the same reasons.
CREATE TABLE User_interest(
user_id INT, <-- These foreign keys are
interest_id INT, <-- good, but you need to
<-- declare them as fields, too
PRIMARY KEY (user_id, interest_id), <-- It's good if all tables have PK
FOREIGN KEY (user_id) REFERENCES Users(id),
FOREIGN KEY (interest_id) REFERENCES Interests(id)
);
CREATE TABLE Friendships(
user_1_id INT,
user_2_id INT,
description char(20),
PRIMARY KEY (user_1_id, user_2_id),
FOREIGN KEY (user_1_id) REFERENCES Users(id),
FOREIGN KEY (user_2_id) REFERENCES Users(id)
);