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.
Related
I am trying to figure out what is wrong with my syntax here. I feel like I have matched the examples I have found over the internet... but something must be amiss. Here is the code I am using. I keep getting the error code "cannot add Foreign Key constraint" when trying to run the build the schema.
CREATE TABLE Employe (
employee_id INT PRIMARY KEY,
first_name VARCHAR(30),
last_name VARCHAR(30),
hire_date DATE,
job_title VARCHAR(30),
shop_id INT,
FOREIGN KEY (shop_id) REFERENCES Coffee_Shop(shop_id)
);
CREATE TABLE Coffee_Shop (
shop_id INT PRIMARY KEY,
shop_name VARCHAR(50),
state CHAR(2),
);
CREATE TABLE Coffee (
coffee_id INT PRIMARY KEY,
shop_id INT,
supplier_id INT,
coffee_name VARCHAR(30),
price_per_pound NUMERIC(5,2),
FOREIGN KEY (shop_id) REFERENCES Coffee_Shop(shop_id),
FOREIGN KEY (supplier_id) REFERENCES Supplier(supplier_id)
);
CREATE TABLE Supplier (
supplier_id INT PRIMARY KEY,
company_name VARCHAR(50),
country VARCHAR(30),
sales_contact_name VARCHAR(60),
email VARCHAR(50) NOT NULL,
);
I just need to figure out what part of my foreign key is incorrectly defined. I know it normally results from an improperly defined primary key, but cannot seem to find where I messed up.
Thanks for your help
You are referencing a KEY that is not set up yet. SQL is linear, so the code gets executed line-by-line and cannot reference future lines (in most cases)
Create your tables in this order:
CREATE TABLE Coffee_Shop (
shop_id INT PRIMARY KEY,
shop_name VARCHAR(50),
state CHAR(2),
);
CREATE TABLE Supplier (
supplier_id INT PRIMARY KEY,
company_name VARCHAR(50),
country VARCHAR(30),
sales_contact_name VARCHAR(60),
email VARCHAR(50) NOT NULL,
);
CREATE TABLE Employe (
employee_id INT PRIMARY KEY,
first_name VARCHAR(30),
last_name VARCHAR(30),
hire_date DATE,
job_title VARCHAR(30),
shop_id INT,
FOREIGN KEY (shop_id) REFERENCES Coffee_Shop(shop_id)
);
CREATE TABLE Coffee (
coffee_id INT PRIMARY KEY,
shop_id INT,
supplier_id INT,
coffee_name VARCHAR(30),
price_per_pound NUMERIC(5,2),
FOREIGN KEY (shop_id) REFERENCES Coffee_Shop(shop_id),
FOREIGN KEY (supplier_id) REFERENCES Supplier(supplier_id)
);
MySQL says:
the alter table statement conflicted with the FOREIGN KEY constraint "FK_PATIENTS_VICTIMS". The conflict occurred in the database " my_database", table "dbo.victims", column "victim_id'.
I tried to alter the table by inputting;
ALTER TABLE PATIENTS WITH NOCHECK ADD CONSTRAINT [FK_PATIENTS_VICTIMS] FOREIGN KEY(VICTIM_ID) REFERENCES VICTIM(ID)
Patients table
create table patients(
victim_id int(4),
blood_type varchar(4),
vaccinations varchar(100),
assigned_practitioner int(10),
hospital_id int(10),
insurance_id int(10),
symptoms varchar(200);
Victims table
create table victims(
victim_id int(4) primary key,
first_name varchar(20),
last_name varchar(20),
sex char(1) constraint persons_sex_ck check(sex IN('F','M')),
PHONE_NUMBER varchar(20),
DOB varchar(13),
address varchar(30),
city varchar(20),
zip int(5),
state varchar(5)
);
You have:
REFERENCES VICTIM(ID)
You need:
REFERENCES VICTIMS(Victim_ID)
I have a table A in a database which have multiple primary keys -Primary key 1 and Primary key 2.
Now I need the create a foreign key in another table having primary key 2 as foreign key
CREATE TABLE ADMIN_INFO(
NAME VARCHAR(20),
EMAIL VARCHAR(20),
PHONE_NO VARCHAR(20),
POST VARCHAR(20),
USER_NAME VARCHAR(16) not null ,
PASSWORD VARCHAR(16),
G_NAME VARCHAR(20) not null,
PRIMARY KEY (user_name,g_name)
);
CREATE tablE Godam(
G_NO INT PRIMARY KEY AUTO_INCREMENT,
BILLNO VARCHAR(20),
MARK VARCHAR(20),
QTY INT,
REMARK VARCHAR(10),
G_NAME VARCHAR(20),
FOREIGN KEY(G_NAME) REFERENCES ADMIN_INFO(G_NAME)
);
You cannot do that, as G_NAME column is not a primary key. You have created a composite key using two columns USER_NAME and G_NAME. So you need to have both columns in your second table Godam and create a foreign key using both the columns.
CREATE TABLE Godam(
G_NO INT PRIMARY KEY AUTO_INCREMENT,
BILLNO VARCHAR(20),
MARK VARCHAR(20),
QTY INT,
REMARK VARCHAR(10),
USER_NAME VARCHAR(16) not null ,
G_NAME VARCHAR(20),
FOREIGN KEY(USER_NAME, G_NAME) REFERENCES ADMIN_INFO(user_name, G_NAME)
);
I'm working on an assignment and came across an error making foreign keys on one of them.
First table:
CREATE TABLE MANUFACTURERS
(
Manufacturers_ID int(5) UNIQUE,
Name varchar(15) UNIQUE,
City varchar(30),
State char(2),
Zip char(5),
Phone char(10),
PRIMARY KEY (Manufacturers_ID)
)
ENGINE= innodb;
Second Table
CREATE TABLE OWNERS
(
Owner_ID int(10),
First_Name varchar(15),
Last_Name varchar(15),
City varchar(30),
State char(2),
Gender varchar(1),
Date_of_Birth varchar(10),
PRIMARY KEY (Owner_ID)
)
ENGINE= innodb;
Third table
CREATE TABLE CARS
(
Vehicle_Identification_Number int(17) NOT NULL UNIQUE,
Manufacturers_ID int(5) UNIQUE,
Owner_ID int(10) UNIQUE,
Model varchar(25),
Manufaturer_Year int(4),
Mileage int(10),
Price int(10),
PRIMARY KEY (Vehicle_Identification_Number),
FOREIGN KEY (Manufacturers_ID) REFERENCES MANUFACTURERS (Manufactures_ID),
FOREIGN KEY (Owner_ID) REFERENCES OWNERS (Owner_ID)
)
ENGINE= innodb;
When I try to execute the third table, I get the message:
"An error has occurred while executing your SQL command.
ERROR: Can't create table 'ds0004.CARS' (errno: 150)"
Any help would be greatly appreciated.
You misspelled the foreign reference in CARS. This is the correct version:
CREATE TABLE CARS
(
Vehicle_Identification_Number int(17) NOT NULL UNIQUE,
Manufacturers_ID int(5) ,
Owner_ID int(10) ,
Model varchar(25),
Manufaturer_Year int(4),
Mileage int(10),
Price int(10),
PRIMARY KEY (Vehicle_Identification_Number),
FOREIGN KEY (Manufacturers_ID) REFERENCES MANUFACTURERS (Manufacturers_ID),
--------------------------------------------------------------------^
FOREIGN KEY (Owner_ID) REFERENCES OWNERS (Owner_ID)
)
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.