"cannot add foreign key constraint" - mysql

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)

Related

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!

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)

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.

error message "ORA-02270: no matching unique or primary key for this column-list"

I am having trouble with mapping things on separate tables of data. There will be tables for DEPARTMENT_LOCATIONS, DEPARTMENT, EMPLOYEE, and PROJECT.
So far all of the tables are created other that PROJECT, because I am receiving the error message for the title of my question.
Below is the information that I used to create the tables that were created without errors:
EMPLOYEE Table:
CREATE TABLE EMPLOYEE
(FNAME VARCHAR(25) NOT NULL,
MINIT CHAR(1),
LNAME VARCHAR(25) NOT NULL,
SSN NUMBER(10) NOT NULL,
BDATE DATE NOT NULL,
ADDRESS VARCHAR(30) NOT NULL,
SEX CHAR(1) NOT NULL,
SALARY DECIMAL(6,2) NOT NULL,
SUPERSSN NUMBER(10),
DNO NUMBER (1) NOT NULL,
PRIMARY KEY (SSN),
FOREIGN KEY (DNO) REFERENCES DEPT_LOCATIONS(DNUMBER));
DEPARTMENT_LOCATIONS Table:
CREATE TABLE DEPT_LOCATIONS
(DNUMBER NUMBER(1) NOT NULL,
DLOCATION VARCHAR(25) NOT NULL,
PRIMARY KEY (DNUMBER));
DEPARTMENT Table:
CREATE TABLE DEPARTMENT
(DNUMBER NUMBER(1) NOT NULL,
DNAME VARCHAR(25) NOT NULL,
MGRSTARTDATE DATE NOT NULL,
MGRSSN NUMBER(10) NOT NULL,
PRIMARY KEY (DNUMBER),
FOREIGN KEY (DNUMBER) REFERENCES DEPT_LOCATIONS(DNUMBER),
FOREIGN KEY (MGRSSN) REFERENCES EMPLOYEE(SSN));
Now when I enter the following information for the PROJECT Table, I receive the error message "ORA-02270: no matching unique or primary key for this column-list.)
CREATE TABLE PROJECT
(PNUMBER NUMBER(2,1) NOT NULL PRIMARY KEY,
PNAME VARCHAR(25) NOT NULL,
PLOCATION VARCHAR(25) NOT NULL,
DNUM NUMBER(1) NOT NULL,
FOREIGN KEY (PLOCATION) REFERENCES DEPT_LOCATIONS(DLOCATION),
FOREIGN KEY (DNUM) REFERENCES DEPT_LOCATIONS(DNUMBER));
Why is this error occurring and what do I need to do to correct it? Thank you.
You have to declare dlocation as unique, inorder to reference it in another table.
CREATE TABLE DEPT_LOCATIONS
(DNUMBER NUMBER(1) NOT NULL,
DLOCATION VARCHAR(25) UNIQUE,
PRIMARY KEY (DNUMBER));

#1005 - Can't create table on ALTER TABLE when connecting table via FOREIGN KEY

I am working on a homework assignment. I have to build a database for a video store. All of the following works:
CREATE TABLE Stock
(
PKStock_ID VARCHAR(8) NOT NULL,
FKTitle VARCHAR(8) NOT NULL,
NoOfDVD INT(10) NOT NULL,
NoOfVHS INT(10) NOT NULL,
PRIMARY KEY (PKStock_ID)
);
CREATE TABLE Inventory
(
PKUnique_ID VARCHAR(8) NOT NULL,
DistributorSerialNo VARCHAR(8) NOT NULL,
Distributor_ID VARCHAR(8) NOT NULL,
FKTitle_ID VARCHAR(8) NOT NULL,
InStock CHAR(1) NOT NULL,
DateOut TIMESTAMP,
DateBack TIMESTAMP,
Customer_ID VARCHAR(8) NOT NULL,
Rental_Price DECIMAL(4,2) NOT NULL,
PRIMARY KEY (PKUnique_ID)
);
CREATE TABLE Movie
(
PKTitle_ID VARCHAR(8) NOT NULL,
FKTitle_ID VARCHAR(8) NOT NULL,
Title VARCHAR(30),
Genre VARCHAR(8),
YearReleased INT,
Length INT,
PRIMARY KEY (PKTitle_ID)
);
CREATE TABLE Actors
(
PKActor_ID VARCHAR(8) NOT NULL,
FKActor_ID VARCHAR(8) NOT NULL,
Actors VARCHAR(30),
PRIMARY KEY (PKActor_ID)
);
CREATE TABLE Awards
(
PKAward_ID VARCHAR(8) NOT NULL,
FKAward_ID VARCHAR(8) NOT NULL,
Awards VARCHAR(30),
PRIMARY KEY (PKAward_ID)
);
CREATE TABLE Directors
(
PKDirector_ID VARCHAR(8) NOT NULL,
FKDirector_ID VARCHAR(8) NOT NULL,
Directors VARCHAR(30),
PRIMARY KEY (PKDirector_ID)
);
CREATE TABLE ElectronicCatalogue
(
PKElectronicCatalogue VARCHAR(8) NOT NULL,
FKDistributor_ID VARCHAR(8) NOT NULL,
DistributorSerialNo VARCHAR(8) NOT NULL,
Price DECIMAL(6,2) NOT NULL,
PRIMARY KEY (PKElectronicCatalogue)
);
CREATE TABLE Distributors
(
PKDistributor_ID VARCHAR(8) NOT NULL,
FKDistributor_ID VARCHAR(8) NOT NULL,
NameOfDistributer VARCHAR(30) NOT NULL,
Horror CHAR(1) NOT NULL,
Drama CHAR(1) NOT NULL,
Comedy CHAR(1) NOT NULL,
Action CHAR(1) NOT NULL,
Thrillers CHAR(1) NOT NULL,
PRIMARY KEY (PKDistributor_ID)
);
CREATE TABLE Customers
(
PKCustomer_ID VARCHAR(8) NOT NULL,
FKUnique_ID VARCHAR(8) NOT NULL,
Name VARCHAR(30),
Address VARCHAR(100),
Phone INT NOT NULL,
PRIMARY KEY (PKCustomer_ID)
);
CREATE TABLE Fees
(
PKFee_ID VARCHAR(8) NOT NULL,
FK_ID VARCHAR(8) NOT NULL,
Damages DECIMAL(10,2) NOT NULL,
Late DECIMAL(10,2) NOT NULL,
PRIMARY KEY (PKFee_ID)
);
ALTER TABLE Stock
ADD FOREIGN KEY (FKTitle)
REFERENCES Inventory(PKUnique_ID);
ALTER TABLE Movie
ADD FOREIGN KEY (FKTitle_ID)
REFERENCES Stock (PKStock_ID);
ALTER TABLE Actors
ADD FOREIGN KEY (FKActor_ID)
REFERENCES Movie (PKTitle_ID);
ALTER TABLE Awards
ADD FOREIGN KEY (FKAward_ID)
REFERENCES Movie (PKTitle_ID);
ALTER TABLE Directors
ADD FOREIGN KEY (FKDirector_ID)
REFERENCES Movie (PKTitle_ID);
ALTER TABLE ElectronicCatalogue
ADD FOREIGN KEY (FKDistributor_ID)
REFERENCES Inventory (PKUnique_ID);
ALTER TABLE Distributors
ADD FOREIGN KEY (FKDistributor_ID)
REFERENCES ElectronicCatalogue (PKElectronicCatalogue);
I next want to connect the Inventory table to the customers table. When I do the following:
ALTER TABLE Customers
ADD FOREIGN KEY (FKUnique_ID)
REFERENCES Inventory (Customer_ID);
I get this error:
#1005 - Can't create table 'mm.#sql-9f69_110' (errno: 150)
What am I doing wrong?
Foreign key should point to a unique column (primary key or unique). Your Inventory (Customer_ID) is not unique.
I think you are trying to :
ALTER TABLE Inventory
ADD CONSTRAINT fk1_Inv FOREIGN KEY (Customer_ID)
REFERENCES Customers (PKCustomer_ID);
Not sure why you didn't get the full message but there's a command line tool bundled with MySQL that provides further information about cryptic error messages like this (or you can just Google for the error code):
C:>perror 150
MySQL error code 150: Foreign key constraint is incorrectly formed
If you have the SUPER privilege, you can get further details with this query:
show engine innodb status
And in this case you see this:
LATEST FOREIGN KEY ERROR
------------------------
130226 21:00:25 Error in foreign key constraint of table test/#sql-1d98_1:
FOREIGN KEY (FKUnique_ID)
REFERENCES Inventory (Customer_ID):
Cannot find an index in the referenced table where the
referenced columns appear as the first columns, or column types
in the table and the referenced table do not match for constraint.
Note that the internal storage type of ENUM and SET changed in
tables created with >= InnoDB-4.1.12, and such columns in old tables
cannot be referenced by such columns in new tables.
See http://dev.mysql.com/doc/refman/5.0/en/innodb-foreign-key-constraints.html
for correct foreign key definition.
So you are missing an index as explained.
Edit: As other answers point out, if there's no index it's because you're linking to the wrong column.
As Álvaro G. Vicario says you are missing an index, this is because you are not correctly using foreign keys.
ALTER TABLE Customers
ADD FOREIGN KEY (FKUnique_ID)
REFERENCES Inventory (Customer_ID);
This should be:
ALTER TABLE Inventory
ADD FOREIGN KEY (Customer_ID)
REFERENCES Customer(PKCustomer_ID);
The foreign key checks if the customer in the Inventory table actually exists in the Customers table. Thus Inventory here is the table you want to add the foreign key too and it references in primary key in the Customer table.
What you are trying to do is reference the Customer_ID in Inventory, which is just an VARCHAR(8)column and not an Primary Key (Index).
You should double check your other alter statements as well.
This turns out that you have different collation settings between these tables. In my case we had latin_swedish_ci and utf8_general_ci