Error 1072 MySQL - 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!

Related

MySQL Error Code 1072- Can't create a Foreign Key

I created this table:
create table users
(
id_user int not null auto_increment,
username varchar (255) not null,
email varchar (255) not null,
CONSTRAINT id_user PRIMARY KEY (id_user)
);
And now I am trying to create this one:
create table games
(
id_game int not null auto_increment,
game_name varchar (255) not null,
preco decimal (6,2),
autor varchar (255) not null,
CONSTRAINT id_game PRIMARY KEY (id_game),
CONSTRAINT autor FOREIGN KEY (username) REFERENCES users (username)
);
The idea is to make the column username from the table users a foreign key in the table games as "autor". However, I am getting this error:
Error 1072. Key Column "username" doesn't exist in table.

"cannot add foreign key constraint"

First i created this table name DEPARTMENT and afterwards i created Dept_Locations which has the foreign key reference in this table at Dnumber.
create table DEPARTMENT(
Dname varchar(20) NOT NULL,
Dnumber int NOT NULL,
Mgr_ssn char(9) NOT NULL,
Mgr_start_date date NOT NULL,
Primary key(Dnumber),
Unique(Dname)
);
Code for Dept_Locations table:
create table Dept_Locations(
Dnum char(4),
Dlocation varchar(16),
primary key(Dnum,Dlocation),
foreign key(Dnum) references DEPARTMENT(Dnumber));
Now when i execute this code for creating Dept_Locations I get an error:
Cannot add foreign key constraint.
The foreign key must be the same data type as the column it's referring to.
The column Dnumber in DEPARTMENT is int, and the column Dnum is defined as char(4) - but it must be defined as int.
Think the datatypes differ.
Dnum char(4)
Dnumber int
You primary key and the column with the foreign key constraint are different sizes - try:
create table DEPARTMENT(
Dname varchar(20) NOT NULL,
Dnumber char(4) NOT NULL, # <-- Note the size change
Mgr_ssn char(9) NOT NULL,
Mgr_start_date date NOT NULL,
Primary key(Dnumber),
Unique(Dname)
);
(or update the other to match)

SQL Cannot add foreign key constraint.Do not know where the error is

I am working on learning sql and trying to make a database, but I am getting errors in this sql that I do not know why they are popping up.
CREATE TABLE EMPLOYEE (
Fname varchar(15) NOT NULL,
Minit char,
Lname varchar(15) NOT NULL,
Ssn char(9) NOT NULL,
Bdate date,
Address varchar(30),
Sex char,
Salary decimal(10, 2),
Super_ssn char(9),
Dno int NOT NULL,
PRIMARY KEY (Ssn),
FOREIGN KEY (Super_ssn) REFERENCES EMPLOYEE (Ssn),
FOREIGN KEY (Dno) REFERENCES DEPARTMENT (Dnumber)
);
CREATE TABLE DEPARTMENT (
Dname varchar(15) NOT NULL,
Dnumber int NOT NULL,
Mgr_ssn char(9) NOT NULL,
Mgr_start_date date,
PRIMARY KEY (Dnumber),
UNIQUE (Dname),
FOREIGN KEY (Mgr_ssn) REFERENCES EMPLOYEE (Ssn)
);
CREATE TABLE DEPT_LOCATIONS (
Dnumber int NOT NULL,
Dlocation varchar(15) NOT NULL,
PRIMARY KEY (Dnumber, Dlocation),
FOREIGN KEY (Dnumber) REFERENCES DEPARTMENT (Dnumber)
);
CREATE TABLE PROJECT (
Pname varchar(15) NOT NULL,
Pnumber int NOT NULL,
Plocation varchar(15),
Dnum int NOT NULL,
PRIMARY KEY (Pnumber),
UNIQUE (Pname),
FOREIGN KEY (Dnum) REFERENCES DEPARTMENT (Dnumber)
);
CREATE TABLE WORKS_ON (
Essn char(9) NOT NULL,
Pno int NOT NULL,
Hours decimal(3, 1) NOT NULL,
PRIMARY KEY (Essn, Pno),
FOREIGN KEY (Essn) REFERENCES EMPLOYEE (Ssn),
FOREIGN KEY (Pno) REFERENCES PROJECT (Pnumber)
);
CREATE TABLE DEPENDENT (
Essn char(9) NOT NULL,
Dependent_name varchar(15) NOT NULL,
Sex char,
Bdate date,
Relationship varchar(8),
PRIMARY KEY (Essn, Dependent_name),
FOREIGN KEY (Essn) REFERENCES EMPLOYEE (Ssn)
);
Any help would be really good I have no idea what is going wrong
The problem is that you're referencing department from employee table and vice vera but when the database engines tries to create an employee department table doesn't exist just yet.
To fix this you can create an FK on department table once both tables created.
CREATE TABLE DEPARTMENT (
Dname varchar(15) NOT NULL,
Dnumber int NOT NULL,
Mgr_ssn char(9) NOT NULL,
Mgr_start_date date,
PRIMARY KEY (Dnumber),
UNIQUE (Dname)
);
CREATE TABLE EMPLOYEE (
Fname varchar(15) NOT NULL,
Minit char,
Lname varchar(15) NOT NULL,
Ssn char(9) NOT NULL,
Bdate date,
Address varchar(30),
Sex char,
Salary decimal(10, 2),
Super_ssn char(9),
Dno int NOT NULL,
PRIMARY KEY (Ssn),
FOREIGN KEY (Super_ssn) REFERENCES EMPLOYEE (Ssn),
FOREIGN KEY (Dno) REFERENCES DEPARTMENT (Dnumber)
);
ALTER TABLE DEPARTMENT ADD FOREIGN KEY (Mgr_ssn) REFERENCES EMPLOYEE (Ssn);
-- rest of script
Here is a working SQLFiddle.
Try to create all the tables first only with primary key and unique constraints. After that you can create foreign key constraints as below
Remove this line in first table creation
FOREIGN KEY (Dno) REFERENCES DEPARTMENT (Dnumber)
Later add below line
ALTER TABLE EMPLOYEE
ADD CONSTRAINT FK_Employee
FOREIGN KEY (Dno)
REFERENCES Department(Dnumber)

can someone explain why im getting this error with sql query

create table Department (
Dep_ID int not null,
Dep_Name varchar(30),
primary key (Dep_ID),
)
create table Course (
C_ID int not null,
C_Name varchar (30) not null,
C_Duration varchar (10) not null,
DegreeType varchar (20),
Dep_ID int,
primary key (C_ID),
constraint DEP_ID1 foreign key (Dep_ID) references Department (Dep_ID) on update cascade,
)
create table Student (
St_ID int not null,
St_Name varchar (100),
St_age smallint,
St_gender Varchar(6),
St_tel int,
St_ADD varchar (100) not null,
St_city varchar (50)not null,
St_type varchar (20) not null,
St_nationality varchar (5) not null,
Dep_ID int,
C_ID int,
primary key (St_ID),
constraint DEP_ID foreign key (Dep_ID) references Department(Dep_ID) on update cascade,
constraint CO_ID foreign key (C_ID) references Course(C_ID) on update cascade,
)
create table Staff (
Sta_ID int not null,
Sta_Name varchar (100) not null,
Sta_type varchar (20) not null,
Sta_Add varchar (100) not null,
Sta_tel int ,
Dep_ID int,
primary key (Sta_ID),
constraint DEeP_ID foreign key (Dep_ID) references Department (Dep_ID) on update cascade,
)
this is the error im getting why cant i use cascade update on
composite keys
Msg 1785, Level 16, State 0, Line 19
Introducing FOREIGN KEY constraint 'CO_ID' on table 'Student' may cause cycles or multiple cascade paths. Specify ON DELETE NO ACTION or ON UPDATE NO ACTION, or modify other FOREIGN KEY constraints.
Msg 1750, Level 16, State 0, Line 19
Could not create constraint. See previous errors.
I had done a research and get to know that this problem can be resolve by changing the line :
constraint CO_ID foreign key (C_ID) references Course(C_ID) on update cascade, to constraint CO_ID foreign key (C_ID) references Course(C_ID) on update No Action,
I have get a explanation on the following link :
Foreign key constraint may cause cycles or multiple cascade paths?
This may resolve your problem.
Your problem is in table 'Exam'. Since you specified no source for this table I can only guess ;-)
Exam contains a foreign key constraint Course_ID2
It seems that this foreign key also refrences to Department or Course.
So have a look into this constraint first. If you need further assistance, please post source of table definition Exam, Department and Course
When you are adding foreign key
They need to be exactly the same data type in both tables.
Try with your query it works fine with me i just remove "," from end of your column
CREATE TABLE Department (
Dep_ID INT NOT NULL,
Dep_Name VARCHAR(30),
PRIMARY KEY (Dep_ID)
)
CREATE TABLE Course (
C_ID INT NOT NULL,
C_Name VARCHAR (30) NOT NULL,
C_Duration VARCHAR (10) NOT NULL,
DegreeType VARCHAR (20),
Dep_ID INT,
PRIMARY KEY (C_ID),
CONSTRAINT DEP_ID1 FOREIGN KEY (Dep_ID) REFERENCES Department (Dep_ID) ON UPDATE CASCADE
)
CREATE TABLE Student (
St_ID INT NOT NULL,
St_Name VARCHAR (100),
St_age SMALLINT,
St_gender VARCHAR(6),
St_tel INT,
St_ADD VARCHAR (100) NOT NULL,
St_city VARCHAR (50)NOT NULL,
St_type VARCHAR (20) NOT NULL,
St_nationality VARCHAR (5) NOT NULL,
Dep_ID INT,
C_ID INT,
PRIMARY KEY (St_ID),
CONSTRAINT DEP_ID FOREIGN KEY (Dep_ID) REFERENCES Department(Dep_ID) ON UPDATE CASCADE,
CONSTRAINT CO_ID FOREIGN KEY (C_ID) REFERENCES Course(C_ID) ON UPDATE CASCADE
)
CREATE TABLE Staff (
Sta_ID INT NOT NULL,
Sta_Name VARCHAR (100) NOT NULL,
Sta_type VARCHAR (20) NOT NULL,
Sta_Add VARCHAR (100) NOT NULL,
Sta_tel INT ,
Dep_ID INT,
PRIMARY KEY (Sta_ID),
CONSTRAINT DEeP_ID FOREIGN KEY (Dep_ID) REFERENCES Department (Dep_ID) ON UPDATE CASCADE
)

MySQL Foreign Key implementation

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