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 )
);
Related
Hello I have an issue when trying inserting multiple foreign keys to a table. I have searched a lot of hours and still I don't figure it out.. It pops this error. I don't know what else I can do about that. Also I tried to add constraint .. foreign key ... references command and it didn't work.
DROP DATABASE IF EXISTS db;
CREATE DATABASE db;
USE db;
CREATE TABLE BOOKS(
Bno int not null primary key auto_increment,
Title text,
PDate date,
Cno int,
Cname text
);
CREATE TABLE AUTHORS(
Ano int not null primary key auto_increment,
Asurname text,
Aname text
);
CREATE TABLE CATEGORIES(
Cno int not null primary key auto_increment,
Cname text,
No_Of_Books int
);
CREATE TABLE SUMMARY_LANG(
Bno int not null primary key auto_increment,
Language text,
FOREIGN KEY (Bno) REFERENCES BOOKS(Bno)
);
CREATE TABLE WRITER(
Bno int,
Ano int,
Asurname text,
Aname text,
FOREIGN KEY (Bno) REFERENCES BOOKS(Bno),
FOREIGN KEY (Ano) REFERENCES AUTHORS(Ano),
FOREIGN KEY (Asurname) REFERENCES AUTHORS(Asurname),
FOREIGN KEY (Aname) REFERENCES AUTHORS(Aname)
);
INSERT INTO BOOKS(Title,PDate,Cname)
VALUES
('A first course in database systems','2014-01-01','DATABASE'),
('FUNDAMENTAL CONCEPTS OF PROGRAMMING SYSTEMS','1976-01-01','PROGRAMMING');
ALTER TABLE AUTHORS auto_increment = 100;
INSERT INTO AUTHORS(Asurname,Aname)
VALUES
('ULLMAN','JEFF'),
('WIDOM','JENNIFER');
ALTER TABLE CATEGORIES auto_increment = 10;
INSERT INTO CATEGORIES(Cname, No_Of_Books)
VALUES
('DATABASE',1),
('PROGRAMMING',1);
INSERT INTO SUMMARY_LANG(Language)
VALUES
('ENG'),
('GRE'),
('ENG'),
('FRA');
Your definition of SUMMARY_LANG is wrong
CREATE TABLE SUMMARY_LANG(
Bno int not null primary key auto_increment,
Language text,
FOREIGN KEY (Bno) REFERENCES BOOKS(Bno) <-- remove this reference
);
Remove the foreign key, because this is a Table that is used only as reference number to another table also called a helper table, because the text would be redundant in the referenced table.
But i can't see any column that references language.
So add a column to BOOKS, where you add the reference to SUMMARY_LANG and when you add new rows SUMMARY_LANG you won't get any errors anymore.
So the new tables can be like this
CREATE TABLE BOOKS (
Bno INT NOT NULL PRIMARY KEY AUTO_INCREMENT,
Title TEXT,
PDate DATE,
Cno INT,
Cname TEXT,
SNno int,
FOREIGN KEY (SNno)
REFERENCES SUMMARY_LANG (SNno)
);
CREATE TABLE SUMMARY_LANG(
SNno int not null primary key auto_increment,
Language text
);
use one_to_many;
create table models(
model_id int primary key auto_increment unique,
name varchar(50),
manufacturer_id int
);
create table manufacturers(
manufacturer_id int primary key,
name varchar(50),
established_on timestamp,
constraint fk_manufacturers_models foreign key(manufacturer_id)
references models(manufacturer_id)
);
When i try to create one to many model like this example https://ibb.co/bPRGQ5 , it throws an error 1215 cannot add foreign key constraint. What am i doing wrong?
models.manufacturer_id must be indexed.
But normally you would put the foreign key constraint on the many table (models) not the one table.
If I understood correctly what you want to get try this
create table manufacturers(
manufacturer_id int primary key auto_increment unique,
name varchar(50),
established_on timestamp
);
create table models(
model_id int primary key auto_increment unique,
name varchar(50),
manufacturer_id int,
foreign key fk_manufacturer(manufacturer_id)
references manufacturers(manufacturer_id)
);
I am new to SQL and I did everything on my understanding but i can't create the table. says error 1005 can't create table user.EMPLOYEE and user.STORE (errno 150) help
DROP TABLE IF EXISTS EMPLOYEE, STORE, REGION;
CREATE TABLE REGION (
REGION_CODE int NOT NULL AUTO_INCREMENT,
REGION_DESCRIPT varchar(20),
PRIMARY KEY (REGION_CODE)
)Engine=InnoDB;
CREATE TABLE EMPLOYEE (
EMP_CODE int NOT NULL AUTO_INCREMENT,
EMP_TITLE varchar(4),
EMP_LNAME varchar(15),
EMP_FNAME varchar(15),
EMP_INITIAL varchar(1),
EMP_DOB date,
STR_CODE int NOT NULL ,
PRIMARY KEY (EMP_CODE,STR_CODE),
FOREIGN key (STR_CODE) REFERENCES STORE(STORE_CODE)
) Engine=InnoDB;
CREATE TABLE STORE (
STORE_CODE int NOT NULL AUTO_INCREMENT,
STORE_NAME varchar(20),
STORE_YRD_SALES numeric,
REGION_CODE int,
EMP_CODE int NOT NULL,
PRIMARY KEY (STORE_CODE),
FOREIGN KEY (REGION_CODE) REFERENCES REGION(REGION_CODE),
FOREIGN KEY (EMP_CODE) REFERENCES EMPLOYEE(EMP_CODE)
) Engine=InnoDB;
Error 150 is an invalid key constraint which, if that's the order you're creating tables in, is probably caused by the fact you can't add a constraint in employee for the currently-not-existing store.
Not being able to create store is simply a side effect of that issue since employee is not created. Once you fix the problem preventing that, the foreign key restraints in store should be okay.
The easiest solution is to probably create employee without the constraint, then add it in later once store exists. This would be something like (note the missing constraint in the EMPLOYEE table creation):
CREATE TABLE EMPLOYEE (
EMP_CODE int NOT NULL AUTO_INCREMENT,
EMP_TITLE varchar(4),
EMP_LNAME varchar(15),
EMP_FNAME varchar(15),
EMP_INITIAL varchar(1),
EMP_DOB date,
STR_CODE int NOT NULL,
PRIMARY KEY (EMP_CODE,STR_CODE)
);
CREATE TABLE STORE ( <<no change here>> );
ALTER TABLE EMPLOYEE ADD CONSTRAINT EMP2STORE
FOREIGN KEY (STR_CODE) REFERENCES STORE(STORE_CODE);
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`)
);
I am trying to create three tables however I am getting error as
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'REFERENCES personal_details(personID)
)'
The above error is for third table i.e. table hobbies_person.
I am creating tables as follow.
CREATE TABLE personal_details (
personID INT PRIMARY KEY,
firstName varchar(30),
middleName varchar(30),
lastName varchar(30),
age INT,
aboutMe varchar(500)
);
CREATE TABLE hobbies (
hobbID INT PRIMARY KEY,
hobbName varchar(30)
);
CREATE TABLE hobbies_person (
personID INT,
hobbID INT,
PRIMARY KEY (personID, hobbID),
FOREIGN KEY personID REFERENCES personal_details(personID)
);
I also tried with
CREATE TABLE hobbies_person (
personID INT,
hobbID INT,
PRIMARY KEY (personID, hobbID),
FOREIGN KEY personID REFERENCES personal_details(personID),
FOREIGN KEY hobbID REFERENCES hobbies(hobbID)
);
but still same error.
Link to check query
Any idea how to tackle this?
NOTE
In table hobbies_person I am using composite primary key as PRIMARY KEY (personID, hobbID),
You need to enclose the column personID in brackets for the FK definition:
CREATE TABLE hobbies_person (
personID INT NOT NULL,
hobbID INT NOT NULL,
PRIMARY KEY (personID, hobbID),
FOREIGN KEY (personID) REFERENCES personal_details(personID)
);
You forgot the () around your foreign key
FOREIGN KEY (personID) REFERENCES personal_details(personID)