Alter Table Error:150 - mysql

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.

Related

MYSQL Foreign key incorrectly formed

Have an error where my first foreign key constraint is incorrectly formed. I'm waiting to hear back from my lecturer if she can spot the issue but thought I'd ask here
My SQL query is as follows
CREATE DATABASE rugby;
USE rugby;
CREATE TABLE address(
address_ID INT AUTO_INCREMENT NOT NULL,
address_name VARCHAR(50) NOT NULL,
PRIMARY KEY (address_ID)
) ENGINE=INNODB;
CREATE TABLE team(
team_ID INT AUTO_INCREMENT NOT NULL,
team_Name VARCHAR(25) NOT NULL,
team_Year INT NOT NULL,
PRIMARY KEY(team_ID)
) ENGINE=INNODB;
CREATE TABLE person(
person_ID INT AUTO_INCREMENT NOT NULL,
name VARCHAR(30) NOT NULL,
phone INT NOT NULL,
address_ID INT NOT NULL,
email VARCHAR(50),
photo BLOB,
PRIMARY KEY(person_ID),
FOREIGN KEY(address_ID) REFERENCES address(address_ID) ON DELETE SET NULL ON UPDATE CASCADE
) ENGINE=INNODB;
I have followed how we were taught but can't spot the issue.
Any help appreciated
In table person you have defined address_ID as NOT NULL, but
when you define the FOREIGN KEY you set:
ON DELETE SET NULL
which contradicts the NOT NULL definition.

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

mysql - cannot add foreign key constraint when creating a table

I'm new to sql.
I get this error when I try to create foreign keys:
cannot add foreign key constraint
when I'm trying to create the ORDERS table. here is my code:
drop database if exists Company;
create database Company;
use Company;
create table WORKERS(w_id varchar(4), w_name varchar(20) not null, telephone varchar(12), e_mail varchar(35) unique);
create table CUSTOMERS(customer_id varchar(4), customer_name varchar(20) not null, telephone varchar(12), e_mail varchar(35) unique, credit bool);
create table WAREHOUSE(m_id int unsigned primary key, describes varchar(20) not null, fl char(1));
create table ITEM(p_id int(4) unsigned primary key, p_name varchar(15) not null, p_price float not null);
create table INVENTORY(in_id int auto_increment primary key, m_id int unsigned not null, p_id int(4) unsigned not null, amount int not null,
foreign key (m_id) references WAREHOUSE(m_id),
foreign key (p_id) references ITEM(p_id)
);
create table ORDERS(o_id int auto_increment primary key, customer_id varchar(4),
p_id int(4) unsigned, w_id varchar(4), amount int unsigned not null,
date_of_order date not null,
foreign key (p_id) references ITEM(p_id),
foreign key (w_id) references WORKERS(w_id),
foreign key (customer_id) references CUSTOMERS(customer_id)
);
I put the ORDERS (where I have the problem) on different lines to make it easier to you to read.
I search here for an answer, but didn't found anything specific that answer my question.
anyone got any idea what the problem is? thank you!
The problem is that you are trying to create a FOREIGN KEY relationship to a field in the WORKERS and CUSTOMERS tables that aren't set up as a PRIMARY KEY.
The FOREIGN KEY needs to be pointing to a PRIMARY KEY. Change your create script to the following:
create table WORKERS(w_id varchar(4) primary key, w_name varchar(20) not null, telephone varchar(12), e_mail varchar(35) unique);
create table CUSTOMERS(customer_id varchar(4) primary key, customer_name varchar(20) not null, telephone varchar(12), e_mail varchar(35) unique, credit bool);
A word of caution, though. I would recommend not using VARCHAR (4) as a PRIMARY KEY field. I would recommend using an INT AUTO_INCREMENT instead.
The column of the referenced table should be declared as primary key to assign a foreign key. For your orders table, WORKERS(w_id) and CUSTOMERS(customer_id) are not declared as primary key, hence you get the error.
Modified Statements:
drop database if exists Company;
create database Company;
use Company;
create table WORKERS(w_id varchar(4) primary key, w_name varchar(20) not null, telephone varchar(12), e_mail varchar(35) unique);
create table CUSTOMERS(customer_id varchar(4) primary key, customer_name varchar(20) not null, telephone varchar(12), e_mail varchar(35) unique, credit bool);
create table WAREHOUSE(m_id int unsigned primary key, describes varchar(20) not null, fl char(1));
create table ITEM(p_id int(4) unsigned primary key, p_name varchar(15) not null, p_price float not null);
create table INVENTORY(in_id int auto_increment primary key, m_id int unsigned not null, p_id int(4) unsigned not null, amount int not null,
foreign key (m_id) references WAREHOUSE(m_id),
foreign key (p_id) references ITEM(p_id)
);
create table ORDERS(o_id int auto_increment primary key, customer_id varchar(4),
p_id int(4) unsigned, w_id varchar(4), amount int unsigned not null,
date_of_order date not null,
foreign key (p_id) references ITEM(p_id),
foreign key (w_id) references WORKERS(w_id),
foreign key (customer_id) references CUSTOMERS(customer_id)
);
SQLFiddle Demo

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

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.

PHP6 chapter 3 - #1215 - Cannot add foreign key constraint

I try to learn from book Professional PHP6 and in chapter 3 I need to create tables:
CREATE TABLE `entity` (
`entityid` SERIAL PRIMARY KEY NOT NULL,
`name1` varchar(100) NOT NULL,
`name2` varchar(100) NOT NULL,
`type` char(1) NOT NULL
);
and
CREATE TABLE `entityaddress` (
`addressid` SERIAL PRIMARY KEY NOT NULL,
`entityid` int,
`saddress1` varchar(255),
`saddress2` varchar(255),
`scity` varchar(255),
`cstate` char(2),
`spostalcode` varchar(10),
`stype` varchar(50),
CONSTRAINT `fk_entityaddress_entityid`
FOREIGN KEY (`entityid`) REFERENCES `entity`(`entityid`)
);
result is error: #1215 - Cannot add foreign key constraint
I check in original code for that book and there is sql file, which give me same error.
...is there anything wrong, or is something with my db in xampp?
I try to create only tables and then create relation in designers, but I got program error...
I set InnoDB engine.
Thanks for any suggestion.
The type of entityid has to be the same in both tables. SERIAL is an alias for BIGINT UNSIGNED NOT NULL AUTO_INCREMENT UNIQUE. So change
`entityid` int,
to
`entityid` BIGINT UNSIGNED NOT NULL,