I am creating Database for a Hotel and I am not able to create "Item" table.
The error shown is
#1005 - Can't create table 'xxx.item' (errno: 150)
Below are my SQL queries:
create table menu (
menu_id INT
, menu_name varchar(100)
, PRIMARY KEY (menu_id)
);
create table categories (
categories_id INT
, category_name varchar(100)
, PRIMARY KEY (categories_id)
);
create table menu_category_item (
mci_id INT
, menu_id INT
, categories_id INT
, item_id INT
, item_type INT
, restaurant_id INT
, PRIMARY KEY (mci_id)
, FOREIGN KEY (menu_id)
REFERENCES menu(menu_id)
, FOREIGN KEY (categories_id)
REFERENCES categories(categories_id)
, FOREIGN KEY (restaurant_id)
REFERENCES restaurants(restaurant_id)
);
create table item (
item_id INT
, item_name varchar(100)
, item_price decimal
, FOREIGN KEY (item_id)
REFERENCES menu_category_item (item_id)
);
Please help me out of this!!
I think the problem is in the structure of the database, you should have item_id as the primary key in the item table, then let the menu_category_item table reference that. Like:
create table item (
item_id INT
, item_name varchar(100)
, item_price decimal
, PRIMARY KEY (item_id));
create table menu_category_item (
mci_id INT
, menu_id INT
, categories_id INT
, item_id INT
, item_type INT
, restaurant_id INT
, PRIMARY KEY (mci_id)
, FOREIGN KEY (menu_id)
REFERENCES menu(menu_id)
, FOREIGN KEY (categories_id)
REFERENCES categories(categories_id)
, FOREIGN KEY (restaurant_id)
REFERENCES restaurants(restaurant_id)
, FOREIGN KEY (item_id)
REFERENCES item(item_id)
);
If I'm correct, your item table would contain the items, you would need a primary key there. And menu_item_category is basically matching items to menus, so there is where you add your foreign key.
Comment from #Adam below:
As a rule: 1 - every table should have a primary key and 2 - foreign
keys should usually be against the primary key the target table. There
are exceptions, but those rules are good starting places for basic DB
design IMO
Assuming that is all the SQL you're running, the problem here is that there's no restaurants table, so trying to create the menu_category_item gives error 150.
Since menu_category_item failed to create, then item can't reference it, so is also giving the 150 error.
This is detailed in the InnoDB error codes under ER_CANT_CREATE_TABLE.
I think you need a unique index/constraint on the target of a foriegn key (in this case menu_category_item.item_id so that it knows which row in the target table is referenced. But i may be getting my SQL flavours mixed up
menu_category_item.item_id has no index defined.
It is required to police the FOREIGN KEY constraint.
Added this - ALTER TABLE menu_category_item ADD INDEX menu_category_item (menu_category_item)
CREATE TABLE menu_category_item (
mci_id INT ,
menu_id INT,
categories_id INT ,
item_id INT ,
item_type INT,
restaurant_id INT ,
PRIMARY KEY (mci_id),
FOREIGN KEY (menu_id) REFERENCES menu(menu_id),
FOREIGN KEY (categories_id) REFERENCES categories(categories_id),
FOREIGN KEY (restaurant_id) REFERENCES restaurants(restaurant_id),
ALTER TABLE `menu_category_item` ADD INDEX `item_id` (`item_id`)
);
Related
trying the following code:
create table donation(
donation_number int not null primary key ,
product_id int not null
);
create table stock (
product_id int not null primary key,
available_qty int not null,
FOREIGN KEY (product_id ) REFERENCES donation(product_id)
);
Give back
Error Code: 1822. Failed to add the foreign key constraint. Missing index for constraint 'stock_ibfk_1' in the referenced table 'donation'.
Why? how can I solve this problem?
to create a foreign key relationship, the parent table column on which you are creating relation must be unique or primary and they must have the same datatype and size also
product_id in donation table is not unique.
What I am doing incorrect? Trying to create these tables in sqlfiddle
does not work gives
Cannot add foreign key constraint
create table product (
pid int NOT NULL,
name varchar(10),
PRIMARY KEY (pid)
);
create table trans (
tid int NOT NULL ,
productId int NOT NULL,
userId int NOT NULL,
PRIMARY KEY (tid),
FOREIGN KEY (productId) REFERENCES product(pid),
FOREIGN KEY (userId) REFERENCES user1(uid)
);
create table user1 (
uid int NOT NULL ,
location varchar(22),
PRIMARY KEY (uid)
);
As #BillKarwin mentioned, the definitions for tables containing primary keys referenced by the trans table should appear before the definition for the trans table. So you should move the definition for the trans table to last.
However, even doing this still results in an error in SQLFiddle:
SQLFiddle (uncomment the foreign key reference in trans)
SQLFiddle seems to have some sort of problem with accepting this table schema. This is not surprising, as the site seems to have such problems frequently.
What is order you create tables. You need first to create product and user1 and at the end - trans.
I have 2 different tables: Profile and Transaction
Profile consists of: pID, firstName, lastName, phoneNumb
Transaction consists of: transID, sellerID, buyerID, itemID
My question is:
How to make sure that both sellerID and buyerID act as a foreign key in reference to profileID in Profile table?
My current code right now:
CREATE TABLE PROFILE
(
pID INT NOT NULL AUTO_INCREMENT ,
firstName VARCHAR(20) NOT NULL ,
lastName INT(20) NOT NULL ,
phoneNumb INT NOT NULL ,
PRIMARY KEY (pID)
) ENGINE = InnoDB DEFAULT CHARSET=latin1;
CREATE TABLE TRANSACTION
(
tID INT NOT NULL AUTO_INCREMENT ,
sellerID INT ,
buyerID INT,
itemID INT,
PRIMARY KEY (tID),
FOREIGN KEY (sellerID, buyerID) REFERENCES PROFILE(pID),
FOREIGN KEY (itemID) REFERENCES ITEM (itemID)
) ENGINE = InnoDB DEFAULT CHARSET=latin1;
I tried this and it gave me this kind of error
1239 - Incorrect foreign key definition for 'foreign key without name': Key reference and table reference don't match
Thanks.
I would go about it this way:
CREATE TABLE TRANSACTION
(
tID INT NOT NULL AUTO_INCREMENT,
sellerID INT,
buyerID INT,
itemID INT,
PRIMARY KEY (tID),
CONSTRAINT fk1 FOREIGN KEY (sellerID) REFERENCES PROFILE(pID)
CONSTRAINT fk2 FOREIGN KEY (buyerID) REFERENCES PROFILE(pID)
CONSTRAINT itemKey FOREIGN KEY (itemID) REFERENCES ITEM (itemID)
) ENGINE = InnoDB DEFAULT CHARSET=latin1;
This assumes that a table called ITEM exists which has a primary key called itemID. Your original problem mentioned only two tables. If ITEM does not exist, then either create it or remove the foreign key constraint from TRANSACTION.
I have the following tables:
CREATE TABLE `OBL2`.`item` (
`itemID` INT NOT NULL AUTO_INCREMENT ,
`itemName` VARCHAR(45) NOT NULL ,
PRIMARY KEY (`itemID`) ,
INDEX `itemName` (`itemName` ASC) );
CREATE TABLE `OBL2`.`subject` (
`subjectID` INT NOT NULL ,
`subjectName` VARCHAR(45) NOT NULL ,
PRIMARY KEY (`subjectID`) );
Now since the connection is many to many, each item can have many subject and each subject can be related to many items - I'd like to set a connection table.
This is my code:
CREATE TABLE `OBL2`.`itemsubjects` (
`itemID` INT NOT NULL ,
`subjectID` INT NOT NULL ,
PRIMARY KEY (`itemID`, `subjectID`) ,
INDEX `itemID_idx` (`itemID` ASC) ,
INDEX `subjectID_idx` (`subjectID` ASC) ,
CONSTRAINT `itemID`
FOREIGN KEY (`itemID` )
REFERENCES `OBL2`.`item` (`itemID` )
ON DELETE CASCADE
ON UPDATE CASCADE,
CONSTRAINT `subjectID`
FOREIGN KEY (`subjectID` )
REFERENCES `OBL2`.`subject` (`subjectID` )
ON DELETE CASCADE
ON UPDATE CASCADE);
but for some reason the code of the 3rd table is not being accepted.
I get an error message:
ERROR 1005: Can't create table 'obl2.itemsubjects' (errno: 121)
I've read about the error on the internet and it says it's a known issue of MYSQL yet there are no solutions.
Any thoughts?
The MySQL docs say in FOREIGN KEY Constraints (emphasis mine):
If the CONSTRAINT symbol clause is given, the symbol value must be unique in the database. If the clause is not given, InnoDB creates the name automatically.
So, the reason that the itemsubject table creation failed, was that you had another (foreign key) constraint, named itemID, or one named subjectID in some other table of the database.
It's good to have a naming conevntion that is standard across the database. Just as you have ColumnName_idx for indices, you can use ReferencedTable_ReferencingTable_FK for foreign key constraints:
CREATE TABLE OBL2.itemsubjects (
itemID INT NOT NULL ,
subjectID INT NOT NULL ,
PRIMARY KEY
(itemID, subjectID) ,
INDEX itemID_idx -- I like these
(itemID ASC) ,
INDEX subjectID_idx -- two
(subjectID ASC) ,
CONSTRAINT item_itemsubject_FK -- what I propose, here
FOREIGN KEY (itemID)
REFERENCES OBL2.item (itemID)
ON DELETE CASCADE
ON UPDATE CASCADE,
CONSTRAINT subject_itemsubject_FK -- and here
FOREIGN KEY (subjectID)
REFERENCES OBL2.subject (subjectID)
ON DELETE CASCADE
ON UPDATE CASCADE
);
Here's my script:
create table Country
(
CountryId int primary key,
Name varchar(255)
);
create table Person
(
PersonId int primary key,
Name varchar(255),
FOREIGN KEY (CountryId) references Country(CountryId)
);
I'm transitioning from MS SQL and trying to get a grasp on MySQL and starting off with my typical hello world of Person->Country relationship to get a feel for foreign keys.
I'm getting this error on PHPMyAdmin:
SQL query:
CREATE TABLE Person(
PersonId INT PRIMARY KEY , Name VARCHAR( 255 ) , FOREIGN KEY (
CountryId ) REFERENCES Country( CountryId ) );
MySQL said:
1072 - Key column 'CountryId' doesn't exist in table
What newbie mistake am I making here?
That's because you had not created the column in Person that would be used in the foreign key, thus, Key column 'CountryId' doesn't exist in table. Here's how you'd do it:
CREATE TABLE Person(
PersonId INT PRIMARY KEY ,
Name VARCHAR( 255 ) ,
CountryId int,
FOREIGN KEY ( CountryId ) REFERENCES Country( CountryId )
);