Unable To Create Table, Unsure of what to do (Error: 150) - mysql

I am creating several tables at once and I keep getting the error(150):
#1005 - Can't create table 'waget.tour' (errno: 150)
and despite me knowing what the error is, I simply can't fix it. All tables references in the table "tour" are all there and exist, with keys where Foreign keys are referenced. I've checked it over plenty of times, and simply can't find anything. Can anyone see what I am doing wrong?
(Getting error when creating the table "tours")
CREATE TABLE IF NOT EXISTS tourPayment(
tourPaymentNumber int,
tourCost int(7),
PRIMARY KEY (tourPaymentNumber),
KEY (tourCost)
);
CREATE TABLE IF NOT EXISTS hotel(
hotelID int AUTO_INCREMENT,
hotelName varchar(30),
PRIMARY KEY (hotelID),
KEY (hotelName)
);
CREATE TABLE IF NOT EXISTS salutation(
salutationID int AUTO_INCREMENT,
salutation varchar(4),
KEY (salutation)
);
CREATE TABLE IF NOT EXISTS customer(
custID int AUTO_INCREMENT,
custSalutation varchar(4),
custLname varchar(30),
custAdd varchar(100),
custPcode varchar(4),
custState varChar(20),
custPhone varchar(10),
custHotel varchar(30),
PRIMARY KEY (custID),
FOREIGN KEY (custHotel) REFERENCES hotel(hotelName),
FOREIGN KEY (custSalutation) REFERENCES salutation(salutation)
);
CREATE TABLE IF NOT EXISTS bus(
busID int AUTO_INCREMENT,
busMake varchar(30),
busSeats varchar(3),
PRIMARY KEY (busID)
);
CREATE TABLE IF NOT EXISTS busDriver(
driverID int AUTO_INCREMENT,
driverName varchar(20),
driverEmail varchar(40),
busID int,
PRIMARY KEY (driverID),
FOREIGN KEY (busID) REFERENCES bus(busID)
);
CREATE TABLE IF NOT EXISTS tour(
DailyTourID int AUTO_INCREMENT,
NoOfPeople int(3),
tourDate DATE,
tourTime TIME,
tourName varchar(30),
tourCost int(7),
tourDriverID varchar(20),
PRIMARY KEY (DailyTourID),
FOREIGN KEY (tourDriverID) REFERENCES busDriver(driverID),
FOREIGN KEY (tourCost) REFERENCES tourPayment(tourCost)
);
CREATE TABLE IF NOT EXISTS TourCustLink(
TourCustLinkID int AUTO_INCREMENT,
TourID int,
custID int,
PRIMARY KEY (TourCustLinkID),
FOREIGN KEY (TourID) REFERENCES tour(DailyTourID),
FOREIGN KEY (custID) REFERENCES customer(custID)
);

Here is your problem:
FOREIGN KEY (tourDriverID) REFERENCES busDriver(driverID)
references an INT but is declared as varchar(20)

AUTO_INCREMENT fields all need to be PRIMARY KEY's, so try changing salutation to be
CREATE TABLE IF NOT EXISTS salutation(
salutationID int AUTO_INCREMENT,
salutation varchar(4),
PRIMARY KEY (salutationID),
KEY (salutation)
);
Additionally in tour you have the wrong datatype for tourDriverID, it should be INT to match the referenced key
CREATE TABLE IF NOT EXISTS tour(
DailyTourID int AUTO_INCREMENT,
NoOfPeople int(3),
tourDate DATE,
tourTime TIME,
tourName varchar(30),
tourCost int(7),
tourDriverID INT,
PRIMARY KEY (DailyTourID),
FOREIGN KEY (tourDriverID) REFERENCES busDriver(driverID),
FOREIGN KEY (tourCost) REFERENCES tourPayment(tourCost)
);
SQL fiddle example in full can be found here : http://sqlfiddle.com/#!2/840b9

Actually it's easy to try and create tables one by one and you would get a better error message..
Your Problem is the table salutation since you have to define the AUTO_INCREMENT field (in your case salutationID) as KEY and not the fieldsalutation`
CREATE TABLE IF NOT EXISTS salutation(
salutationID int AUTO_INCREMENT,
salutation varchar(4),
KEY (salutationID) -- here is your error
);

Is this the code you ran? if so, the error is about the AUTO_INCREMENT needing to be a Key.
CREATE TABLE IF NOT EXISTS salutation(
salutationID int AUTO_INCREMENT,
salutation varchar(4),
KEY (salutation)
);
Try:
CREATE TABLE IF NOT EXISTS salutation(
salutationID int AUTO_INCREMENT,
salutation varchar(4),
KEY (salutationID)
);
and make other a FOREIGN KEY if you need to.
Cheers.

Related

MYSQL 1215 cannot add foreign constraint

So I don't know why i cant add the foreign constraint in the table ORDER_LINE, i made sure all the types are correct. Please help, it keeps on giving me the error
Error Code: 1215. Cannot add foreign key constraint
CREATE TABLE IF NOT EXISTS Customer (
Customer_ID int NOT NULL AUTO_INCREMENT,
Customer_Name VARCHAR(50) NOT NULL,
Customer_Age INT UNIQUE,
Customer_Address VARCHAR(255),
Customer_City VARCHAR(255),
Customer_State VARCHAR(50),
Customer_Zip VARCHAR(20),
PRIMARY KEY(Customer_ID)
);
CREATE TABLE IF NOT EXISTS Sales_order (
Order_ID int AUTO_INCREMENT,
Order_date DATE,
Customer_ID int,
PRIMARY KEY(Order_ID),
FOREIGN KEY(Customer_ID) REFERENCES Customer(Customer_ID) ON DELETE
CASCADE
);
CREATE TABLE IF NOT EXISTS Products (
Product_ID int AUTO_INCREMENT,
Product_Description VARCHAR(255),
Product_Finish VARCHAR(50),
Standard_Price DECIMAL,
Product_Line_ID INT,
PRIMARY KEY(Product_ID)
);
CREATE TABLE IF NOT EXISTS ORDER_LINE (
Order_ID int,
Product_ID int,
Ordered_Quantity int,
PRIMARY KEY(Order_ID, Product_ID),
FOREIGN KEY(Order_ID) REFERENCES Sales_order(Order_ID) ON DELETE SET NULL,
FOREIGN KEY(Product_ID) REFERENCES Products(Product_ID)
);
This might be because primary key columns are made NOT NULL, so your ON DELETE SET NULL is trying to set an invalid value.
EDIT: In fact, I just tested and that does seem to be the issue -- it creates the table if I remove either the SET NULL or the primary key.

Error 1005 (HY000): Can't create table db.Wine (errno: 150)

I have this code to create some tables:
CREATE TABLE Appelations (
No int AUTO_INCREMENT,
Appelation varchar(35),
County varchar(20),
State varchar(15),
Area varchar(25),
IsAVA varchar(3),
PRIMARY KEY (No)
);
CREATE TABLE Grapes (
ID int AUTO_INCREMENT,
Grape varchar(25),
Color varchar(10),
PRIMARY KEY (ID)
);
CREATE TABLE Wine (
No int AUTO_INCREMENT,
Grape varchar(25),
Winery varchar(40),
Appelation varchar(35),
Name varchar(40),
Year year,
Price int,
Score int,
Cases int,
PRIMARY KEY (No),
FOREIGN KEY (Grape) REFERENCES Grapes(Grape),
FOREIGN KEY (Appelation) REFERENCES Appelations(Appelation)
);
But when I run it, I get this error:
ERROR 1005 (HY000): Can't create table 'db.Wine' (errno: 150)
Does anyone know how to fix this? Thanks.
You need indexes on your foreign keys: "MySQL requires indexes on foreign keys and referenced keys so that foreign key checks can be fast and not require a table scan."
Example here: http://sqlfiddle.com/#!9/d228b8
CREATE TABLE Appelations (
No int AUTO_INCREMENT,
Appelation varchar(35),
County varchar(20),
State varchar(15),
Area varchar(25),
IsAVA varchar(3),
INDEX appelation_ind (Appelation), /*<---*/
PRIMARY KEY (No)
);
CREATE TABLE Grapes (
ID int AUTO_INCREMENT,
Grape varchar(25),
Color varchar(10),
INDEX grape_ind (Grape), /*<---*/
PRIMARY KEY (ID)
);
CREATE TABLE Wine (
No int AUTO_INCREMENT,
Grape varchar(25),
Winery varchar(40),
Appelation varchar(35),
Name varchar(40),
Year year,
Price int,
Score int,
Cases int,
PRIMARY KEY (No),
FOREIGN KEY (Grape) REFERENCES Grapes(Grape),
FOREIGN KEY (Appelation) REFERENCES Appelations(Appelation)
);
That's because the FOREIGH KEY columns in the table are referencing the columns that are neither primary keys nor have indexes on them (in parent tables). You can change the type and make them point to ID and No columns of Grapes and Appelations tables respectively, e.g.:
CREATE TABLE Wine (
No int AUTO_INCREMENT,
Grape_id int,
Winery varchar(40),
Appelation_no int,
Name varchar(40),
Year year,
Price int,
Score int,
Cases int,
PRIMARY KEY (No),
FOREIGN KEY (Grape_id) REFERENCES Grapes(id),
FOREIGN KEY (Appelation_no) REFERENCES Appelations(No)
);
Here's the SQL Fiddle.

How to change sql code to mysql

i have this script running on sql, but i need to optimize it to mysql using the workbench, but it shows me an error in the foreign key and i don't know how to change it.
create table EspecialidadesMedicas(
IdEspecialidad int(4) primary key,
DescripcionEspecialidad varchar(30));
create table Doctores(
IdDoctor int(5) PRIMARY KEY,
NombreDoctor varchar(30),
Salario int(12.2),
Especialidad int(4),citascitas
Especialidad FOREIGN KEY references EspecialidadesMedicas(IdEspecialidad));
The following is working syntax:
create table EspecialidadesMedicas (
IdEspecialidad int(4) primary key,
DescripcionEspecialidad varchar(30)
);
create table Doctores(
IdDoctor int(5) PRIMARY KEY,
NombreDoctor varchar(30),
Salario int(12),
Especialidad int(4),
citascitas varchar(30),
constraint fk_Especialidad FOREIGN KEY (Especialidad) references EspecialidadesMedicas(IdEspecialidad)
);

Can not CREATE TABLE?

I am working library project. I want to create a database and table, but I do not know why my codes do not work. I check all create table syntax no errors.
Could you teach me how to fix them?
DROP DATABASE IF EXISTS LIBRARYS;
CREATE DATABASE LIBRARYS;
USE LIBRARYS;
DROP TABLE IF EXISTS BOOK;
CREATE TABLE BOOK
(
title VARCHAR(50) NOT NULL,
author VARCHAR(30),
BookId INT auto_increment,
ISBN INT,
Edition VARCHAR(50),
YearBought INT,
Category VARCHAR (30),
LibraryBranchID INT auto_increment,
PRIMARY KEY(BookId),
FOREIGN KEY ( LibraryBranchID) references LibraryBranch(BranchName)
);
DROP TABLE IF EXISTS Person;
CREATE TABLE Person
(PersonId INT AUTO_INCREMENT,
uNAME VARCHAR(30) NOT NULL,
age INT,
UserType VARCHAR (30),
PreferredBranch INT,
updatedOn timestamp not null on update current_timestamp,
PRIMARY KEY (PersonId),
FOREIGN KEY ( PreferredBranch) references LibraryBranch(LibraryBranchID)
) ;
ALTER table Person AUTO_INCREMENT = 1001;
DROP TABLE IF EXISTS LOAN;
CREATE TABLE LOAN
(LoanId INT auto_increment,
Pid INT,
Bid INT,
loanDate DATE DEFAULT '0000-00-00',
overdue BOOLEAN DEFAULT FALSE,
PRIMARY KEY(LoanId),
FOREIGN KEY (Pid) references Person (PersonId),
FOREIGN KEY (Bid) references Book(BookId)
) ;
DROP TABLE IF EXISTS LibraryBranch ;
CREATE TABLE LibraryBranch
(uID INT AUTO_INCREMENT,
LibraryBranchID INT auto_increment,
BranchName INT,
updatedOn timestamp not null on update current_timestamp,
PRIMARY KEY (LibraryBranchID)
) ;
DROP TABLE IF EXISTS Rating;
CREATE TABLE Rating
(RatingId INT ,
RatingDate INT,
BookId INT,
PersonId INT,
Stars BOOLEAN DEFAULT FALSE,
PRIMARY KEY(RatingId),
FOREIGN KEY (PersonId) references Person (PersonId),
FOREIGN KEY (BookId) references BOOK(BookId)
) ;
LOAD DATA LOCAL INFILE '' INTO TABLE BOOK;
LOAD DATA LOCAL INFILE '' INTO TABLE Person;
LOAD DATA LOCAL INFILE '' INTO TABLE LOAN;
Here I got those results
1 row(s) affected
0 row(s) affected
0 row(s) affected, 1 warning(s): 1051 Unknown table 'librarys.book'
Error Code: 1075. Incorrect table definition; there can be only one auto column and it must be defined as a key
CREATE TABLE LibraryBranch should be before your call FOREIGN KEY ( LibraryBranchID) references LibraryBranch(BranchName)
CREATE TABLE BOOK
(
...
BookId INT auto_increment,
...
LibraryBranchID INT auto_increment,
...
PRIMARY KEY(BookId),
FOREIGN KEY ( LibraryBranchID) references LibraryBranch(BranchName)
);
and if FOREIGN KEY ( LibraryBranchID) references LibraryBranch(BranchName) this LibraryBranchID can't be auto_increment
FOREIGN KEY ( LibraryBranchID) references LibraryBranch(BranchName) wrong field BranchName called should be LibraryBranchID:
FOREIGN KEY ( LibraryBranchID) references LibraryBranch(LibraryBranchID)
and there is no need to DROP TABLE IF EXISTS each table since you just created the database
UPDATE http://sqlfiddle.com/#!9/86bb96
CREATE TABLE LibraryBranch
(uID INT,
LibraryBranchID INT auto_increment,
BranchName INT,
updatedOn timestamp not null on update current_timestamp,
PRIMARY KEY (LibraryBranchID)
) ;
CREATE TABLE BOOK
(
title VARCHAR(50) NOT NULL,
author VARCHAR(30),
BookId INT auto_increment,
ISBN INT,
Edition VARCHAR(50),
YearBought INT,
Category VARCHAR (30),
LibraryBranchID INT ,
PRIMARY KEY(BookId),
FOREIGN KEY ( LibraryBranchID) references LibraryBranch(LibraryBranchID)
);
CREATE TABLE Person
(PersonId INT AUTO_INCREMENT,
uNAME VARCHAR(30) NOT NULL,
age INT,
UserType VARCHAR (30),
PreferredBranch INT,
updatedOn timestamp not null on update current_timestamp,
PRIMARY KEY (PersonId),
FOREIGN KEY ( PreferredBranch) references LibraryBranch(LibraryBranchID)
) ;
ALTER table Person AUTO_INCREMENT = 1001;
CREATE TABLE LOAN
(LoanId INT auto_increment,
Pid INT,
Bid INT,
loanDate DATE DEFAULT '0000-00-00',
overdue BOOLEAN DEFAULT FALSE,
PRIMARY KEY(LoanId),
FOREIGN KEY (Pid) references Person (PersonId),
FOREIGN KEY (Bid) references Book(BookId)
) ;
CREATE TABLE Rating
(RatingId INT ,
RatingDate INT,
BookId INT,
PersonId INT,
Stars BOOLEAN DEFAULT FALSE,
PRIMARY KEY(RatingId),
FOREIGN KEY (PersonId) references Person (PersonId),
FOREIGN KEY (BookId) references BOOK(BookId)
) ;
In this code:
CREATE TABLE LibraryBranch
(uID INT AUTO_INCREMENT,
LibraryBranchID INT auto_increment,
BranchName INT,
updatedOn timestamp not null on update current_timestamp,
PRIMARY KEY (LibraryBranchID)
) ;
You made uID an auto column, but LibraryBranchID is the PK.
You cannot have two auto_increment fields in mysql innodb tables.
You can use trigger for second field if you want it to auto increment.

I cant reference to other table mysql

CREATE TABLE IF NOT EXISTS super(
id int (20) PRIMARY KEY AUTO_INCREMENT,
cod varchar(5)
);
CREATE TABLE IF NOT EXISTS super2(
id2 int (20) PRIMARY KEY AUTO_INCREMENT,
cod2 varchar(5),
FOREIGN KEY (cod2) REFERENCES super(cod)
);
I cant create share tables please help and thanks
with FOREIGN KEY errno 150...
It is because cod is not the primary key in the super table.