Dropping a constraint with foreign key on POPSQL - mysql

I have tried to drop the employee table that I created but was unable to due to the constraints of the foreign key. when I drop the employee I get this error (Cannot drop table 'employee' referenced by a foreign key constraint 'branch_ibfk_1' on table 'branch')
CREATE TABLE employee (
emp_id INT PRIMARY KEY,
first_name VARCHAR(40),
last_name VARCHAR(40),
birth_day DATE,
sex VARCHAR(1),
salary INT,
super_id INT,
branch_id INT
);
ALTER TABLE employee
ADD FOREIGN KEY(branch_id)
REFERENCES branch(branch_id)
ON DELETE SET NULL;
ALTER TABLE employee
ADD FOREIGN KEY(super_id)
REFERENCES employee(emp_id)
ON DELETE SET NULL;
CREATE TABLE branch (
branch_id INT PRIMARY KEY,
branch_name VARCHAR(40),
mgr_id INT,
mgr_start_date DATE,
FOREIGN KEY(mgr_id) REFERENCES employee(emp_id) ON DELETE SET NULL
);

Do this :
Drop table branch;
Drop table employee;

Related

ERROR 1215 (HY000): Cannot add foreign key constraint error

This is what I have written:
create table employee (
emp_id int,
emp_first_name varchar(20),
emp_last_name varchar(20),
dept_id int,
email varchar(255),
primary key(emp_id, dept_id)
);
create table department(
dept_id int primary key,
dept_name varchar(255),
gm_no int,
foreign key(dept_id) references employee(dept_id)
);
but when I try to create the department table, it gives me the error(ERROR 1215 (HY000): Cannot add foreign key constraint)
all help is appreciated. thank you.
You have the logic backwards. The dept_id is defined in the department table as the primary key. The foreign key constraint belongs in the tables that reference the department:
create table department(
dept_id int primary key,
dept_name varchar(255),
gm_no int
);
create table employee (
emp_id int,
emp_first_name varchar(20),
emp_last_name varchar(20),
dept_id int,
email varchar(255),
primary key(emp_id, dept_id)
foreign key (dept_id) references department (dept_id)
);
Note that this data structure seems suspicious. I would expect a company called employee to have a unique id for empid. Period.
Employees are obvious in departments, but that could change over time. You would then want another table, say employeeDepartment that captured the changing relationship over time.
Your data model, for instance, would get confusing if an employee starts in department A, switches to B, and then returns to A.
The dept_id in the employee table is not key of any type. Make the dept_in column a KEY, UNIQUE KEY, or PRIMARY KEY on the employee. In your situation, just making it UNIQUE KEY is enough.
As #Sharofiddin said:
create table employee (
emp_id int,
emp_first_name varchar(20),
emp_last_name varchar(20),
dept_id int,
email varchar(255),
primary key(emp_id),
UNIQUE KEY (dept_id) /* add this line and run the query again */
);
create table department(
dept_id int primary key,
dept_name varchar(255),
gm_no int,
foreign key(dept_id) references employee(dept_id)
);
Thank you.

SQL error - Failed to add the foreign key constraint

CREATE TABLE employee (
emp_id INT,
first_name VARCHAR(40),
last_name VARCHAR(40),
birth_day DATE,
sex VARCHAR(1),
salary INT,
super_id INT,
branch_id INT,
PRIMARY KEY (emp_id)
);
CREATE TABLE branch(
branch_id INT,
branch_name VARCHAR(40),
mgr_id INT,
mgr_start_date DATE,
FOREIGN KEY(mgr_id) REFERENCES employee(emp_id) ON DELETE SET NULL
);
ALTER TABLE employee
ADD FOREIGN KEY(branch_id)
REFERENCES branch(branch_id)
ON DELETE SET NULL;
I'm not sure what I'm doing wrong. When I run the lasts query, I keep getting the
"failed to add the foreign key constraint"
error saying there's a missing index for constraint 'employee_ibfk_2' in the reference table 'branch'
Columns referenced in a foreign key must be a key.
Presumably you forgot to declare branch_id as primary key?
Try:
...
CREATE TABLE branch(
branch_id INT,
branch_name VARCHAR(40),
mgr_id INT,
mgr_start_date DATE,
PRIMARY KEY (branch_id),
FOREIGN KEY(mgr_id) REFERENCES employee(emp_id) ON DELETE SET NULL
);
...
The branch_id column in your branch table is not indexed. a requirement for foreign key constraints.
Add an index like this:
ALTER TABLE `branch`
ADD INDEX `branch_id` (`branch_id` ASC) VISIBLE;
;

Constraint does not exist unable to drop the table

CREATE TABLE employee (
emp_id INT PRIMARY KEY,
first_name VARCHAR(40),
last_name VARCHAR(40),
birth_day DATE,
sex VARCHAR(1),
salary INT,
super_id INT,
branch_id INT
);
CREATE TABLE branch (
branch_id INT PRIMARY KEY,
branch_name VARCHAR(40),
mgr_id INT,
mgr_start_date DATE,
FOREIGN KEY(mgr_id) REFERENCES employee(emp_id) ON DELETE SET NULL
);
ALTER TABLE employee
ADD FOREIGN KEY(branch_id)
REFERENCES branch(branch_id)
ON DELETE SET NULL;
ALTER TABLE employee
ADD FOREIGN KEY(super_id)
REFERENCES employee(emp_id)
ON DELETE SET NULL;
ALTER TABLE employee DROP CONSTRAINT branch_id;
Trying to Drop Constraint branch_id error says 'Constraint 'branch_id' does not exist.'
All I want to do is drop the employee table.
First find the name of the constraint with
SHOW CREATE TABLE employee
The name isn't "branch_id".
The actual name of your foreign key constraint is not branch_id, it is something else. The better approach here would be to name the constraint explicitly:
ALTER TABLE employee
ADD CONSTRAINT fk_branch_id FOREIGN KEY (branch_id)
REFERENCES branch(branch_id);
Then, delete it using the explicit constraint name you used above:
ALTER TABLE employee DROP FOREIGN KEY fk_branch_id;

ER_FK_NO_INDEX_PARENT: Failed to add the foreign key constraint. Missing index for constraint 'employee_ibfk_10' in the referenced table 'branch'

I try to create a table for the company database.
As I tried to add some foreign keys in there, something goes wrong.
CREATE TABLE employee (
emp_id INT PRIMARY KEY,
first_name VARCHAR(40),
last_name VARCHAR(40),
birth_day DATE,
sex VARCHAR(1),
salary INT,
super_id INT,
branch_id INT unique
);
CREATE TABLE branch (
branch_id INT PRIMARY KEY,
branch_name VARCHAR(40),
mgr_id INT,
mgr_start_date DATE,
FOREIGN KEY(mgr_id) REFERENCES employee(emp_id) ON DELETE SET NULL
);
ALTER TABLE employee
ADD FOREIGN KEY(branch_id)
REFERENCES branch(branch_id)
ON DELETE SET NULL;
ALTER TABLE employee
ADD FOREIGN KEY(super_id)
REFERENCES employee(emp_id)
ON DELETE SET NULL
The "alter table employee" part to add foreign key (branch) failed with the following statement.
ER_FK_NO_INDEX_PARENT: Failed to add the foreign key constraint. Missing index for constraint 'employee_ibfk_10' in the referenced table 'branch'
Could this be some problem with my setting?

Popsql is showing me the error. What is right syntax of foriegn key?

CREATE TABLE employee(
emp_id INT,
emp_name VARCHAR(20),
emp_salary INT,
PRIMARY KEY(emp_id)
);
CREATE TABLE branch(
branch_id INT,
branch_name VARCHAR(20),
mgr_id INT,
FORIEGN KEY(mgr_id) REFRENCES employee(emp_id) ON DELETE SET NULL
);
Apparently you only have two typos in the syntax:
FORIEGN = FOREIGN
REFRENCES = REFERENCES