Adding composite foreign key fails in MySQL5 - mysql

I am trying to enforce that the state/province and country names in the address information for users comes from a set of tables where I list countries and state/provinces. In order to do this I tried running an alter table command like this...
ALTER TABLE User
ADD FOREIGN KEY (stateProvince,country)
REFERENCES `StateProvince`(`name`,`countryName`);
Then I get this message...
Create table 'realtorprint_dev/#sql-d5c_3d' with foreign key
constraint failed. There is no index in the referenced table where the
referenced columns appear as the first columns.
Does anybody have an idea how to handle this error message?
Here is the create for the state and country tables...
CREATE TABLE Country (
name varchar(40) NOT NULL,
abbreviation varchar(4) NOT NULL,
PRIMARY KEY (name)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
CREATE TABLE StateProvince (
countryName varchar(40) NOT NULL,
name varchar(100) NOT NULL,
abbreviation varchar(3) NOT NULL,
PRIMARY KEY (countryName,name)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
alter table StateProvince
add constraint FK_StateProvince_Country
foreign key (countryName)
references Country (name);
And now for the user table...
create table realtorprint_dev.user (
id bigint not null,
created datetime,
email varchar(255) not null,
fax varchar(255),
mobile varchar(255),
name varchar(255),
password varchar(255),
phone varchar(255),
title varchar(255),
tollFree varchar(255),
updated datetime,
web varchar(255),
brokerage_id bigint,
address varchar(255),
city varchar(255),
country varchar(255),
stateProvince varchar(255),
type varchar(255),
zipPostal varchar(255),
activated bit not null,
locked bit not null,
primary key (id),
foreign key FK285FEB6722226 (brokerage_id) references brokerage(id)
);

There is no index in the referenced table where the referenced columns appear as the first columns.
You need an index on StateProvince.(name,countryName), but you have an index on StateProvince.(countryName,name). Try reversing the order of your index, or of your FK reference.

Since you didn't show the create statement i'm not sure, but I beleve this has to do with the realtorprint_dev table not having a primairy key.

Related

how to reference multiple fks from different tables in MySQL

i have been looking around and i cannot find the correct way to do this in sql the current way i have coded it just causes my third table to fail but the rest of the code worked great up until the third table, i just need the third table to include fks from both the previous tables
first table
SET FOREIGN_KEY_CHECKS = 0;
DROP TABLE IF EXISTS Customer;
CREATE TABLE Customer
(
CustomerNumber int NOT NULL,
CustomerName varchar(255),
CustomerAddress varchar(255),
CustomerPhoneNumber varchar(255),
JoinDate varchar(255),
PetName varchar(255),
PayScheme varchar(255),
PremiumPayDate varchar(255),
PRIMARY KEY (CustomerNumber, PetName)
);
second table
DROP TABLE IF EXISTS Policies;
CREATE TABLE Policies
(
PolicyID int NOT NULL,
PolicyNumber int NOT NULL,
PetType varchar(255),
CustomerNumber int NOT NULL,
PetName varchar(255),
EffectiveDate varchar(255),
PRIMARY KEY (PolicyID),
CONSTRAINT fk_CustomerNumber_PetName
FOREIGN KEY (CustomerNumber, PetName)
REFERENCES Customer(CustomerNumber, PetName)
);
third table
DROP TABLE IF EXISTS Claims;
CREATE TABLE Claims
(
ClaimsID int NOT NULL,
AmountForReimbursement varchar(255),
PolicyID int NOT NULL,
PetName varchar(255),
PRIMARY KEY (ClaimsID),
CONSTRAINT fk_PolicyID_PetName
FOREIGN KEY (PolicyID, PetName)
REFERENCES Policies(PolicyID), Customer(PetName)
);
You should consider going through MySQL documentation. You have got the table creation wrong again for Claims. It should be like below
CREATE TABLE Claims
(
ClaimsID int NOT NULL,
AmountForReimbursement varchar(255),
PolicyID int NOT NULL,
PetName varchar(255),
CustomerNumber int NOT NULL,
PRIMARY KEY (ClaimsID),
CONSTRAINT fk_PolicyID_PetName
FOREIGN KEY (PolicyID) REFERENCES Policies(PolicyID),
FOREIGN KEY (CustomerNumber, PetName) REFERENCES Customer(CustomerNumber, PetName)
);
It's always recommended and you should actually refer both the PK column since you have a composite PK defined

mysql error 1064 when creating table. What can I check for?

I'm getting an ERROR 1064 (42000) at line 21, which is VIN_Number in Vehicle table. Entity Inventory gets built without a problem, VIN_Number is of type varchar(17) for all occurrences. I believe the tables are being built in the correct order. I can't find any spelling or punctuation errors. I'm out of ideas of things I should checked. What is it that I'm missing?
Note: I getting an error for Invoice as well, but I know that it can't be created until Vehicle gets created.
ALTER TABLE Vehicle DROP FOREIGN KEY fk_Veh_Vehicle_TypeID;
ALTER TABLE Inventory DROP FOREIGN KEY fk_Inv_Vehicle_TypeID;
ALTER TABLE Invoice DROP FOREIGN KEY fk_Customer_ID;
ALTER TABLE Invoice DROP FOREIGN KEY fk_Sales_Person_ID;
ALTER TABLE Invoice DROP FOREIGN KEY fk_VIN_Number;
DROP TABLE IF EXISTS Vehicle, VehicleType,
Invoice, Customer, SalesPerson, Inventory;
CREATE TABLE VehicleType (
Vehicle_TypeID int NOT NULL,
Veh_Make varchar(15),
Veh_Model varchar(15),
Veh_Year int,
PRIMARY KEY (Vehicle_TypeID)
) Engine=InnoDB;
CREATE TABLE Vehicle (
VIN_Number varchar(17) NOT NULL,
Vehicle_TypeID int NOT NULL,
Condition varchar(10),
Color varchar(8),
PRIMARY KEY (VIN_Number),
CONSTRAINT fk_Veh_Vehicle_TypeID FOREIGN KEY(Vehicle_TypeID)
REFERENCES VehicleType(Vehicle_TypeID)
) Engine=InnoDB;
CREATE TABLE Inventory (
Stock_ID int NOT NULL,
Vehicle_TypeID int NOT NULL,
Quantity int,
PRIMARY KEY (Stock_ID),
CONSTRAINT fk_Inv_Vehicle_TypeID FOREIGN KEY (Vehicle_TypeID)
REFERENCES VehicleType(Vehicle_TypeID)
) Engine=InnoDB;
CREATE TABLE Customer (
Customer_ID varchar(10) NOT NULL,
Cus_LastName varchar(15),
Cus_FirstName varchar(15),
Cus_Street varchar(20),
Cus_City varchar(15),
Cus_Zip varchar(5),
Cus_Phone varchar(10),
PRIMARY KEY (Customer_ID)
) Engine=InnoDB;
CREATE TABLE SalesPerson (
Sales_Person_ID varchar(10) NOT NULL,
Sal_LastName varchar(15),
Sal_FirstName varchar(15),
Sal_Street varchar(15),
Sal_City varchar(15),
Sal_Zip varchar(5),
Sal_Phone varchar(10),
Sal_Years_Worked int,
Sal_Commission_Rate float(4),
PRIMARY KEY (Sales_Person_ID)
) Engine=InnoDB;
CREATE TABLE Invoice (
Invoice_ID varchar(10) NOT NULL,
Customer_ID varchar(10) NOT NULL,
Sales_Person_ID varchar(10) NOT NULL,
VIN_Number varchar(17) NOT NULL,
Price float(10),
PRIMARY KEY (Invoice_ID),
CONSTRAINT fk_Customer_ID FOREIGN KEY (Customer_ID)
REFERENCES Customer(Customer_ID),
CONSTRAINT fk_Sales_Person_ID FOREIGN KEY (Sales_Person_ID)
REFERENCES SalesPerson(Sales_Person_ID),
CONSTRAINT fk_VIN_Number FOREIGN KEY (VIN_Number)
REFERENCES Vehicle(VIN_Number)
) Engine=InnoDB;
In the definition of the Vehicle table you have a column named condition which is a reserved keyword in MySQL (reference). Either use another name for the column or enclose it in backticks like this: `condition`
Using keywords (reserved or not) for object names is generally something you want to avoid.

Mysql Alter command how to alter the existing table to add the primary key

I have table name persons,
CREATE TABLE Persons (
P_Id int NOT NULL PRIMARY KEY,
LastName varchar(255) NOT NULL,
FirstName varchar(255),
Address varchar(255),
City varchar(255)
)
Now i am planning to add the primary key on column 2 Lastname, so i tried alter command:
ALTER TABLE Persons
ADD CONSTRAINT pk_LastName PRIMARY KEY (LastName)
When i run the alter command i have error message: 1068 Multiple primary key defined.
Could you please help me how to fix this ?
Thanks,
Palani
ALTER TABLE `Persons`
CHANGE `LastName` `LastName` VARCHAR(255) CHARSET utf8 COLLATE utf8_danish_ci NOT NULL,
DROP PRIMARY KEY,
ADD PRIMARY KEY (`P_Id`, `LastName`);
You already have a primary key on the table. Don't define another one. Instead, you can add a unique constraint on those columns:
ALTER TABLE Persons ADD CONSTRAINT unq_lastname UNIQUE (LastName);
However, this seems dangerous, because different people can have the same last name.

query on mysql table creation composite primary key from foreign keys of two tables

I have the following tables
books(
bkid varchar(255),
bkname varchar(255),
bkauth varchar(255),
bkpub varchar(255),
bkedn int(10)
)
members(
memid varchar(255),
memname varchar(255),
memaddr varchar(255),
memcon varchar(255),
mememail varchar(255)
)
bkid and memid are primary keys.
Now I am trying to make a composite primary key (bkid and memid) taking them as foreign keys from the tables books and members, the syntax is giving me some errors and I am not able to create the new table.
create table issuebooks(
bkid varchar(255),
memid varchar(255),
issuestatus varchar(255),
references foreign key bkid(books),
references foreign key memid(members),
primary key (bkid,memid)
);
Your create table statement is incorrect. This will work (tested on SQLFiddle)
create table issuebooks(
bkid varchar(255) references bkid(books),
memid varchar(255) references memid(members),
issuestatus varchar(255),
primary key (bkid,memid)
);
Advice... use integer id columns instead of VARCHAR(255) if you can... you won't regret it. And make those columns NOT NULL.

#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