Database issues errors not able to fix it - mysql

I am trying to crate a set of tables for my schema and I have used the following code. But I'm getting an error. I'm new to mysql and any help would be appreciated.
create table AutoSeller;
create table County(
CountyID INT,
CountyName varchar(25),
PRIMARY KEY (CountyID)
);
create table City (
CityID int,
CityName varchar(50),
CountyID int,
FOREIGN KEY (CountyID) REFERENCES county(CountyID),
PRIMARY KEY (CityID)
);
CREATE table Postcode(
PostcodeID int,
Postcode varchar(8),
CityID int,
FOREIGN KEY(CityID) REFERENCES city(CityID),
PRIMARY KEY (PostcodeID)
);
create table Buyer(
BuyerID int,
BuyerFN varchar(15),
BuyerLN varchar(15),
CountyID int,
CityID int,
Address varchar(30),
PostcodeID int,
ContactNr varchar(15),
FOREIGN KEY (PostcodeID) REFERENCES postcode (PostcodeID),
FOREIGN KEY (CityID) REFERENCES city (CityID),
FOREIGN KEY (CountyID) REFERENCES county (CountyID),
PRIMARY KEY (BuyerID)
);
create table Seller(
SellerID int,
SellerFN varchar(15),
SellerLN varchar(15),
CountyID int,
CityID int,
Address varchar(30),
PostcodeID int,
ContactNr varchar(15),
FOREIGN KEY (PostcodeID) REFERENCES postcode (PostcodeID),
FOREIGN KEY (CityID) REFERENCES city (CityID),
FOREIGN KEY (CountyID) REFERENCES county (CountyID),
PRIMARY KEY (SellerID)
);
create table Make(
CarMakeID int,
CarMake varchar(15),
PRIMARY KEY (CarMakeID)
);
create table Colour(
CarColourID int,
CarColour varchar(10),
PRIMARY KEY (CarColour)
);
create TABLE Fuel_type(
CarFuelID int,
CarFuel varchar(10),
PRIMARY KEY (CarFuelID)
);
create table Body(
CarBodyID int,
CarBody varchar (15),
PRIMARY KEY (CarBodyID)
);
CREATE TABLE car(
CarID int,
CarReg varchar(10),
CarMakeID int,
CarModel varchar(15),
CarColourID int,
CarBodyID int,
CarFuelID int,
CarAge int,
CarEngine int,
CarMileage int,
BuyerID int,
SellerID int,
PRIMARY KEY (CarID),
FOREIGN KEY (CarMakeID) REFERENCES make (CarMakeID),
FOREIGN KEY (CarColourID) REFERENCES colour (CarColourID),
FOREIGN KEY (CarFuelID) REFERENCES fuel_type (CarFuelID),
FOREIGN KEY (BuyerID) REFERENCES buyer (BuyerID),
FOREIGN KEY (SellerID) REFERENCES seller (SellerID)
);
I just start learning mySql and I am struggling with this error.
1005 - Can't create table autoseller.car (errno: 150 "Foreign key constraint is incorrectly formed")
Can somebody help me?

This is the problem:
create table Colour(
CarColourID int,
CarColour varchar(10),
PRIMARY KEY (CarColour)
);
The primary key should be CarColourID, not CarColour. The error you're getting is because the column referenced in a foreign key has to be indexed. Since you didn't make CarColourID the primary key, it doesn't have an index.

Related

mysql key column doesnt exist in table

CREATE TABLE DONOR
(
donor_id int,
medical_history varchar(20),
donor_name varchar(50),
blood_group char(2),
address varchar(80),
contact_number int,
PRIMARY KEY (donor_id)
);
CREATE TABLE RECEPTIONIST
(
recep_id varchar(10),
recep_name varchar(50),
phone_number int,
donor_id int,
PRIMARY KEY (recep_id),
FOREIGN KEY (donor_id) REFERENCES DONOR (donor_id)
);
CREATE TABLE HOSPITAL
(
hospital_id varchar(10),
hospital_name varchar(50),
hospital_address varchar(80),
PRIMARY KEY (hospital_id)
);
CREATE TABLE BLOOD_BANK
(
blood_bank_id varchar(10),
blood_group char(2),
stocks int,
PRIMARY KEY (blood_bank_id),
FOREIGN KEY (recep_id) REFERENCES RECEPTIONIST (recep_id),
FOREIGN KEY (hospital_id) REFERENCES HOSPITAL (hospital_id)
);
CREATE TABLE BLOOD
(
blood_code varchar(10),
blood_group char(2),
expired_date date,
PRIMARY KEY (blood_code)
);
CREATE TABLE PATIENT
(
patient_id varchar(10),
patient_name varchar(50),
contact_number int,
blood_group char(2),
address varchar(80),
PRIMARY KEY(patient_id),
FOREIGN KEY (hospital_id) REFERENCES HOSPITAL (hospital_id)
);
hi, i wrote this for my project in mysql and i dont understand why it said
Key column 'recep_id' doesn't exist
in table when i want to execute the code
it said that i need to define it in receptionist table but i already defined it
CREATE TABLE RECEPTIONIST
(
recep_id varchar(10),
the entity relationship between blood bank and receptionist is one receptionist works at a blood bank.
The problem is this line:
FOREIGN KEY (recep_id) REFERENCES RECEPTIONIST (recep_id),
while creating the table BLOOD_BANK.
The table BLOOD_BANK does not have the column recep_id which you reference in the statement. You ave to add the column to the table BLOOK_BANK
CREATE TABLE BLOOD_BANK
(
blood_bank_id varchar(10),
blood_group char(2),
stocks int,
recep_id varchar(10),
hospital_id varchar(10),
PRIMARY KEY (blood_bank_id),
FOREIGN KEY (recep_id) REFERENCES RECEPTIONIST (recep_id),
FOREIGN KEY (hospital_id) REFERENCES HOSPITAL (hospital_id)
);
Same for the hospital_id in the table BLOOD_BANK and PATIENT
CREATE TABLE PATIENT
(
patient_id varchar(10),
patient_name varchar(50),
contact_number int,
blood_group char(2),
address varchar(80),
hospital_id varchar(10),
PRIMARY KEY(patient_id),
FOREIGN KEY (hospital_id) REFERENCES HOSPITAL (hospital_id)
);

How can i fix "Foreign key constraint is incorrectly formed" issue in my database

I created hotel, room, guest tables. After I created booking table I can't add Foreign key. It makes error.
Can't create table hotelbooking.booking (errno: 150 "Foreign key constraint is incorrectly formed")
CREATE TABLE Booking (
gId int,
hId int,
RoomNo int,
FromDate Date,
ToDate Date,
PaymentMethod varchar(100),
FOREIGN KEY (hid) REFERENCES Hotel(hid),
FOREIGN KEY (gid) REFERENCES Guest(gid),
FOREIGN KEY (RoomNo) REFERENCES Room(RoomNo),
CONSTRAINT PK_bookingRoom PRIMARY KEY (gId, hId, RoomNo, FromDate)
);
CREATE DATABASE hotelBooking;
CREATE TABLE Hotel (
hId int,
Name varchar(100),
Address varchar(255),
City varchar(200),
ContactNumber int(10),
PRIMARY KEY (hid)
);
CREATE TABLE Guest (
gId int,
firstName varchar(100),
lastName varchar(100),
Age int,
Gender varchar(2),
Address varchar(255),
City varchar(50),
ContactNumber int(10),
PRIMARY KEY (gid)
);
CREATE TABLE Room (
hid int,
RoomNo int,
RoomType varchar(100),
Price varchar(50),
FOREIGN KEY (hid) REFERENCES Hotel(hid),
CONSTRAINT PK_roomId PRIMARY KEY (hid, RoomNo)
);
You can define a combined foreign key that fits with teh primary key from Room
CREATE TABLE Room (
hid int,
RoomNo int,
RoomType varchar(100),
Price varchar(50),
FOREIGN KEY (hid) REFERENCES Hotel(hid),
CONSTRAINT PK_roomId PRIMARY KEY (hid, RoomNo)
);
CREATE TABLE Booking (
gId int,
hId int,
rhId int,
RoomNo int,
FromDate Date,
ToDate Date,
PaymentMethod varchar(100),
FOREIGN KEY (hid) REFERENCES Hotel(hid),
FOREIGN KEY (gid) REFERENCES Guest(gid),
FOREIGN KEY (rhid,RoomNo) REFERENCES Room(hid,RoomNo),
CONSTRAINT PK_bookingRoom PRIMARY KEY (gId, hId, RoomNo, FromDate)
);
Or you define an idex on roomNo
CREATE TABLE Room (
hid int,
RoomNo int,
RoomType varchar(100),
Price varchar(50),
FOREIGN KEY (hid) REFERENCES Hotel(hid),
KEY(RoomNo),
CONSTRAINT PK_roomId PRIMARY KEY (hid, RoomNo)
);
CREATE TABLE Booking (
gId int,
hId int,
RoomNo int,
FromDate Date,
ToDate Date,
PaymentMethod varchar(100),
FOREIGN KEY (hid) REFERENCES Hotel(hid),
FOREIGN KEY (gid) REFERENCES Guest(gid),
FOREIGN KEY (RoomNo) REFERENCES Room(RoomNo),
CONSTRAINT PK_bookingRoom PRIMARY KEY (gId, hId, RoomNo, FromDate)
);
You seem to be trying to create the Booking table with foreign key references to the Hotel, Guest, and Room tables, but if you create Booking first, then those tables don't exist yet.
You need to create the other tables first, then create the Booking table that references them.
Or as an alternative, create Booking, but without declaring its foreign keys. Then create the other tables. Then use ALTER TABLE Booking to add foreign key constraints to that table.
However you do it, foreign keys can't reference a table before you create those tables.

Cannot add foreign key constraint error 1215 HY000

I keep getting this error for my ENROLLS table, TAKES table, CONDUCTED_IN table, and EXAM table. when I am uploading my created tables to the database. I am not sure what the problem is can anyone help out? The COURSE, SECTION, CLASS_ROOM, and STUDENT tables were added to the database just fine the others have problems.
create table COURSE(
CourseNo varchar(10),
Course_Name varchar(30),
Department varchar(25),
primary key(CourseNo)
);
create table SECTION(
CourseNo varchar(10),
SectionNo varchar(2),
primary key(CourseNo, SectionNo),
foreign key(CourseNo) references COURSE(CourseNo)
);
create table ENROLLS(
SSN char(9),
SectionNo varchar(2),
CourseNo varchar(10),
primary key(SSN, CourseNo, SectionNo),
foreign key(SSN) references STUDENT(SSN),
foreign key(SectionNo) references SECTION(SectionNo),
foreign key(CourseNo) references SECTION(CourseNo)
);
create table STUDENT(
SSN char(9),
First_Name varchar(20),
Last_Name varchar(20),
Street varchar(15),
City varchar(15),
Zip char(5),
State varchar(20),
primary key(SSN)
);
create table TAKES(
SSN char(9),
CourseNo varchar(10),
SectionNo varchar(2),
ExamNo char(3),
Result char(1),
primary key(SSN, CourseNo, SectionNo, ExamNo),
foreign key(SSN) references STUDENT(SSN),
foreign key(CourseNo) references EXAM(CourseNo),
foreign key(SectionNo) references EXAM(SectionNo),
foreign key(ExamNo) references EXAM(ExamNo)
);
create table EXAM(
CourseNo varchar(10),
SectionNo varchar(2),
ExamNo char(3),
primary key(CourseNo, SectionNo, ExamNo),
foreign key(CourseNo) references SECTION(CourseNo),
foreign key(SectionNo) references SECTION(SectionNo)
);
create table CONDUCTED_IN(
RoomNo int,
Building varchar(30),
CourseNo varchar(10),
SectionNo varchar(2),
ExamNo char(3),
primary key(RoomNo, Building, CourseNo, SectionNo, ExamNo),
foreign key(RoomNo) references CLASS_ROOM(RoomNo),
foreign key(Building) references CLASS_Room(Building),
foreign key(CourseNo) references EXAM(CourseNo),
foreign key(SectionNo) references EXAM(SectionNo),
foreign key(ExamNo) references EXAM(ExamNo)
);
create table CLASS_ROOM(
RoomNo int,
Building varchar(30),
Capacity int,
primary key(RoomNo, Building)
);
The issues in your code are related to the management of relationships
tables must be created in sequence; when a table that refers to another table through a foreign key, it must be created after the parent table
most of your table have compound primary keys (ie primary keys that use multiple columns) - so you need compound foreign keys as well: rather than creating multiple foreign keys over each related column of the relation, you want an single, multi-column key that references the tuple of columns at once
With these base principles in mind, here is an updated version of your code that runs fine in this DB Fiddle.
create table COURSE(
CourseNo varchar(10),
Course_Name varchar(30),
Department varchar(25),
primary key(CourseNo)
);
create table CLASS_ROOM(
RoomNo int,
Building varchar(30),
Capacity int,
primary key(RoomNo, Building)
);
create table STUDENT(
SSN char(9),
First_Name varchar(20),
Last_Name varchar(20),
Street varchar(15),
City varchar(15),
Zip char(5),
State varchar(20),
primary key(SSN)
);
create table SECTION(
CourseNo varchar(10),
SectionNo varchar(2),
primary key(CourseNo, SectionNo),
foreign key(CourseNo) references COURSE(CourseNo)
);
create table ENROLLS(
SSN char(9),
SectionNo varchar(2),
CourseNo varchar(10),
primary key(SSN, CourseNo, SectionNo),
foreign key(SSN) references STUDENT(SSN),
foreign key(CourseNo, SectionNo) references SECTION(CourseNo, SectionNo)
);
create table EXAM(
CourseNo varchar(10),
SectionNo varchar(2),
ExamNo char(3),
primary key(CourseNo, SectionNo, ExamNo),
foreign key(CourseNo, SectionNo) references SECTION(CourseNo, SectionNo)
);
create table TAKES(
SSN char(9),
CourseNo varchar(10),
SectionNo varchar(2),
ExamNo char(3),
Result char(1),
primary key(SSN, CourseNo, SectionNo, ExamNo),
foreign key(SSN) references STUDENT(SSN),
foreign key(CourseNo, SectionNo, ExamNo) references EXAM(CourseNo, SectionNo, ExamNo)
);
create table CONDUCTED_IN(
RoomNo int,
Building varchar(30),
CourseNo varchar(10),
SectionNo varchar(2),
ExamNo char(3),
primary key(RoomNo, Building, CourseNo, SectionNo, ExamNo),
foreign key(RoomNo, Building) references CLASS_ROOM(RoomNo, Building),
foreign key(CourseNo, SectionNo, ExamNo) references EXAM(CourseNo, SectionNo, ExamNo)
);

MySQL "ERROR 1005 (HY000): Can't create table

Would appreciate help. Can not create the section table.
CREATE DATABASE university;
use university;
CREATE TABLE classroom(
building VARCHAR(20),
room_number INT,
capacity INT,
PRIMARY KEY (building,room_number)
) ;
CREATE TABLE Sales(
item VARCHAR(20),
color VARCHAR(20),
clothes_size VARCHAR(20),
quantity INT,
PRIMARY KEY (item,color,clothes_size,quantity)
);
CREATE TABLE Department (
dept_name VARCHAR(20),
building VARCHAR(20),
budget INT,
FOREIGN KEY (building) references classroom(building),
PRIMARY KEY (dept_name,building,budget)
);
CREATE TABLE Course (
course_id VARCHAR(20)PRIMARY KEY,
title VARCHAR(20),
dept_name VARCHAR(20),
credits INT,
FOREIGN KEY (dept_name) references Department(dept_name)
);
CREATE TABLE Instructor(
ID VARCHAR(20) PRIMARY KEY,
neme VARCHAR(20),
dept_name VARCHAR(20),
salary INT,
FOREIGN KEY (dept_name) references Department(dept_name)
);
CREATE TABLE section(
course_id VARCHAR(20),
sec_id INT,
semester VARCHAR(20),
year1 INT,
building1 VARCHAR(20),
room_number INT,
time_slot_id CHAR,
FOREIGN KEY (course_id) REFERENCES Course(course_id),
FOREIGN KEY (building1) references classroom(building),
FOREIGN KEY (room_number) references classroom(room_number),
PRIMARY KEY(course_id,sec_id,semester,year1)
);
Since you have two PK's on section table, you need to create one foreign key for both, not separate two:
CREATE TABLE section(
course_id VARCHAR(20),
sec_id INT,
semester VARCHAR(20),
year1 INT,
building1 VARCHAR(20),
room_number INT,
time_slot_id CHAR,
FOREIGN KEY (course_id) REFERENCES Course(course_id),
FOREIGN KEY (building1,room_number) references classroom(building,room_number),
PRIMARY KEY(course_id,sec_id,semester,year1)
);

ERROR 1215 (HY000): Cannot add foreign key constraint, I've checked for syntax and spelling errors

I am getting a ERROR 1215 (HY000): Cannot add foreign key constraint error with:
Foreign Key (locationID) references Location (locationID). When I comment it out the query goes through with no problem
I can't seem to find the problem I have checked for spelling errors, notepad++ highlights "Location" and "locationID" on both statements. They are also both varchar(3).
create table Location
(
warehouseID varchar(3),
locationID varchar(3),
Aisle int,
Shelf int,
Bin int,
capacity Double,
Primary Key (warehouseID,locationID),
Foreign Key (warehouseID) references Warehouse(warehouseID)
) Engine = InnoDB;
create table Prod_Location
(
warehouseID varchar(3),
locationID varchar(3),
productNum int,
quantityOnHand int,
Foreign Key (warehouseID) references Warehouse(warehouseID),
Foreign Key (productNum) references Product(productNum),
Foreign Key (locationID) references Location(locationID),
Primary Key (warehouseID, locationID, productNum)
) Engine = InnoDB;
Thank you.
Edit:
Full code
create database WareMart30119267;
use WareMart30119267;
create table Department (
dptNumber int Auto_Increment,
dptName varchar(20),
Primary Key (dptNumber))
Engine = InnoDB;
create table Product (
productNum int Auto_Increment,
description varchar(30),
packSize int,
Price Decimal(10,2),
dptNumber int,
Primary Key (productNum),
Foreign Key (dptNumber) references Department(dptNumber))
Engine = InnoDB;
create table CLient (
clientNum int Auto_Increment,
clientName varchar(40),
Primary Key (clientNum))
Engine = InnoDB;
create table Client_Address (
clientNum int Auto_Increment,
addressType varchar(1),
street varchar(20),
city varchar(3),
state varchar(3),
postcode varchar(4),
Primary Key (clientNum, addressType),
Foreign Key (clientNum) references Client(clientNum))
Engine = InnoDB;
create table Stock_Request (
requestNum int Auto_Increment,
requestDate date,
clientNum int,
Primary Key (requestNum),
Foreign Key (clientNum) references Client(clientNum))
Engine = InnoDB;
create table Request_List (
requestNum int,
productNum int,
qtyRequested int,
Primary Key (requestNum, productNum),
Foreign Key (requestNum) references Stock_Request(requestNum),
Foreign Key (productNum) references Product(productNum))
Engine = InnoDB;
create table Warehouse (
warehouseID varchar(3),
street varchar(20),
city varchar(15),
state varchar(3),
postcode varchar(4),
managerID int,
Primary Key (warehouseID))
Engine = InnoDB;
create table Location (
warehouseID varchar(3),
locationID varchar(3),
Aisle int,
Shelf int,
Bin int,
capacity Double,
Primary Key (warehouseID, locationID),
Foreign Key (warehouseID) references Warehouse(warehouseID))
Engine = InnoDB;
create table Employee (
staffID int Auto_Increment,
surname varchar(20),
firstName varchar(15),
dob date,
street varchar(20),
city varchar(15),
state varchar(3),
postcode varchar(4),
salary Decimal(19,4),
warehouseID varchar(3),
supervisedBy int,
Primary Key (staffID),
Foreign Key (supervisedBy) references Employee(staffID))
Engine = InnoDB;
/*Add Foreign Keys to Warehouse and Employee */
alter table Warehouse
add Foreign Key (managerID) references Employee(staffID);
alter table Employee
add Foreign Key (warehouseID) references Warehouse(warehouseID);
create table Prod_Location (
warehouseID varchar(3),
locationID varchar(3),
productNum int,
quantityOnHand int,
Foreign Key (warehouseID) references Warehouse(warehouseID),
Foreign Key (productNum) references Product(productNum),
Foreign Key (locationID) references Location(locationID),
Primary Key (warehouseID,locationID,productNum))
Engine = InnoDB;
create table Picking_List (
warehouseID varchar(3),
locationID varchar(3),
productNum int,
requestNum int,
quantityPicked int,
datePicked date,
pickerStaffID int,
Primary Key (warehouseID, locationID, productNum, requestNum),
/* Foreign Key (warehouseID) references Warehouse(warehouseID), */
Foreign Key (locationID, warehouseID) references Location(locationID, warehouseID),
Foreign Key (productNum) references Product(productNum),
Foreign Key (requestNum) references Stock_Request(requestNum))
Engine = InnoDB;
I feel stupid now, the answer was to move warehouseID and locationID in the primary key so it was like this
create table Location (
warehouseID varchar(3),
locationID varchar(3),
Aisle int,
Shelf int,
Bin int,
capacity Double,
Primary Key (locationID, warehouseID),
Foreign Key (warehouseID) references Warehouse(warehouseID))
Engine = InnoDB;
I don't know why the order made a difference but it fixed the error.
Remove Primary key from Pro_Location Table:
create table Location (
warehouseID varchar(3),
locationID varchar(3),
Aisle int,
Shelf int,
Bin int,
capacity Double,
Primary Key (warehouseID,locationID),
Foreign Key (warehouseID) references Warehouse(warehouseID))
Engine = InnoDB;
create table Prod_Location (
warehouseID varchar(3),
locationID varchar(3),
productNum int,
quantityOnHand int,
Foreign Key (warehouseID) references Warehouse (warehouseID),
Foreign Key (productNum) references Product (productNum),
Foreign Key (locationID) references Location (locationID)
Engine = InnoDB;
Your primary key on location is defined as the composite key:
Primary Key (warehouseID,locationID),
Whereas you are trying to reference this in Prod_Location as a foreign key via just one of its components:
Foreign Key (locationID) references Location(locationID)
You'll need to change the foreign key to be composite with all fields of the referenced key:
Foreign Key (warehouseID, locationID) references Location(warehouseID, locationID)
Or, alternatively, change the primary key of Location to be simple, e.g. just locationID
Edit After the full schema posted
My apologies - the order of the composite keys is important - you'll need to keep the same order of the primary key definition in the foreign key references, viz (warehouseID, locationID).
This also needs to be done for the composite foreign key to Location in Picking_List.
Also, since clientNum is a foreign key in table Client_Address back to client, it itself should not be declared as Auto_Increment, since it must stay in synch with Client and be explicitly assigned in code.
One of the common problems with composite foreign keys is the temptation to reference and join to the ultimate component key tables directly, instead of to the link table via the composite keys. I believe that the direct relationship between Prod_Location and warehouseId via Foreign Key (warehouseID) references Warehouse(warehouseID) may be an example of this.
SqlFiddle here
I feel stupid now, the answer was to move warehouseID and locationID in the primary key so it was like this
create table Location (
warehouseID varchar(3),
locationID varchar(3),
Aisle int,
Shelf int,
Bin int,
capacity Double,
Primary Key (locationID, warehouseID),
Foreign Key (warehouseID) references Warehouse(warehouseID))
Engine = InnoDB;
I don't know why the order made a difference but it fixed the error.