MySQL Error on creating tables - mysql

I am quite new at SQL. I have created a file full of complete table requests, all are working except one. The error relates to the ProductPrice foreign Key in the Orders_Info table. I can not see or find what I have done wrong. Anyone able to help give me a pointer please?
Create table Orders
(
Order_Number INT (11) NOT NULL AUTO_INCREMENT,
Date Date, Account_Number INT(11),
FOREIGN KEY (Account_Number) REFERENCES User_Accounts(Account_Number) ON DELETE CASCADE,
Address_ID INT(11),
FOREIGN KEY (Address_ID) REFERENCES Address_Book(Address_ID) ON DELETE CASCADE,
Order_Status VARCHAR(30) NOT NULL, PRIMARY KEY (Order_Number)
)ENGINE=InnoDB;
Create table Order_Info
(
Order_Number INT (11),
FOREIGN KEY (Order_Number) REFERENCES Orders(Order_Number) ON DELETE CASCADE,
Product_Number INT (11),
FOREIGN KEY (Product_Number) REFERENCES Products(Product_ID) ON DELETE CASCADE,
Product_Quantity INT (11), ProductPrice Decimal(10,2) NOT NULL,
FOREIGN KEY (ProductPrice) REFERENCES Products(ProductPrice) ON DELETE CASCADE
)ENGINE=InnoDB;

Related

How to define Many to Many linking table

I have a schema with a store and product table. A store record, may have many products, and a product may be available to many stores (many to many) To link these, a third table has the PRIMARY KEY of the store, and product records. Should this linking table, have a compound PRIMARY KEY? Or, should this simply be two FOREIGN KEYS, each with an INDEX()?
CREATE TABLE store(
id INT NOT NULL AUTO_INCREMENT,
name VARCHAR(50) NOT NULL,
PRIMARY KEY(id)
) ENGINE=INNODB;
CREATE TABLE product(
id INT NOT NULL AUTO_INCREMENT,
name VARCHAR(50) NOT NULL,
PRIMARY KEY(id)
) ENGINE=INNODB;
So, should the linking table be like..
CREATE TABLE store_product_link(
store_id INT NOT NULL,
product_id INT NOT NULL,
INDEX(store_id),
INDEX(product_id),
FOREIGN KEY (store_id) REFERENCES store(id) ON DELETE CASCADE,
FOREIGN KEY (product_id) REFERENCES product(id) ON DELETE CASCADE
) ENGINE=INNODB;
Or should this be
CREATE TABLE store_product_link(
store_id INT NOT NULL,
product_id INT NOT NULL,
PRIMARY KEY(store_id, product_id)
) ENGINE=INNODB;
Both, it should have a primary key and both foreign keys.
CREATE TABLE store_product_link(store_id INT NOT NULL, product_id INT NOT NULL,
PRIMARY KEY(store_id, product_id),
FOREIGN KEY (store_id) REFERENCES store(id) ON DELETE CASCADE,
FOREIGN KEY (product_id) REFERENCES product(id) ON DELETE CASCADE) ENGINE=INNODB;

Cannot add foreign key constraint MySQL Workbench

I'm getting "Error Code: 1215. Cannot add foreign key constraint" when trying to create the "orders" table.
credit_cards table:
CREATE TABLE credit_cards (
customer VARCHAR(30),
card_no CHAR(16),
PRIMARY KEY (customer, card_no),
FOREIGN KEY (customer) REFERENCES customers(username));
orders table:
CREATE TABLE orders (
order_no INT AUTO_INCREMENT,
customer VARCHAR(30) NOT NULL,
date_time TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
credit_card CHAR(16) NOT NULL,
PRIMARY KEY (order_no),
FOREIGN KEY (customer)
REFERENCES customers (username),
FOREIGN KEY (credit_card)
REFERENCES credit_cards (card_no));
The report from SHOW ENGINE INNODB STATUS says that the problem is FOREIGN KEY (credit_card) REFERENCES credit_cards(card_no))
I've read a bunch of resolved questions and still can't figure it out. I'm using MySQL Workbench. Thanks.
You need to reference all the keys in a primary key (or unique key) relationship. I would recommend:
CREATE TABLE credit_cards (
credit_card_id int auto_increment primary key,
customer VARCHAR(30),
card_no CHAR(16),
UNIQUE KEY (customer, card_no),
FOREIGN KEY (customer) REFERENCES customers(username));
orders table:
CREATE TABLE orders (
order_no INT AUTO_INCREMENT,
customer VARCHAR(30) NOT NULL,
date_time TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
credit_card_id int NOT NULL,
PRIMARY KEY (order_no),
FOREIGN KEY (customer)
REFERENCES customers (username),
FOREIGN KEY (credit_card_id)
REFERENCES credit_cards (credit_card_id));

MySQL: Cannot add foreign key constraint error in school project

I'm building a really basic database for a school project and am getting a "cannot add foreign key constraint" error in MySQL. I've been scratching my head on this one for the past day, read all of the related posts and haven't been able to figure it out.
Here are the first two tables of my project:
CREATE TABLE CUSTOMER (
CUST_ID INTEGER NOT NULL AUTO_INCREMENT UNIQUE,
CUST_LNAME VARCHAR(25) NOT NULL,
CUST_FNAME VARCHAR(25) NOT NULL,
CUST_INITIAL CHAR(1),
CUST_STREET_NO VARCHAR(6),
CUST_STREET_NAME VARCHAR(25),
CUST_APT_NO VARCHAR(10),
CUST_CITY VARCHAR(25),
CUST_STATE CHAR(2),
CUST_ZIP_CODE CHAR(5),
CUST_HOME_AC CHAR(3),
CUST_HOME_PHONE CHAR(8),
PRIMARY KEY (CUST_ID))
ENGINE = InnoDB;
CREATE TABLE INVOICE (
INVOICE_ID INTEGER NOT NULL AUTO_INCREMENT UNIQUE,
CUST_ID INTEGER NOT NULL,
INV_DATE DATE NOT NULL,
SPECIAL_HANDLING VARCHAR(35),
PRIMARY KEY (INVOICE_ID),
FOREIGN KEY (CUST_ID) REFERENCES CUSTOMER ON UPDATE CASCADE)
ENGINE = InnoDB;
I'm sure it's something super easy that I'm missing. Any ideas?
You are missing the column to be referred in your foreign key constraint def.
Use:
FOREIGN KEY (CUST_ID) REFERENCES CUSTOMER(CUST_ID) ON UPDATE CASCADE)
instead of
FOREIGN KEY (CUST_ID) REFERENCES CUSTOMER ON UPDATE CASCADE)
So, create table becomes:
CREATE TABLE INVOICE (
INVOICE_ID INTEGER NOT NULL AUTO_INCREMENT UNIQUE,
CUST_ID INTEGER NOT NULL,
INV_DATE DATE NOT NULL,
SPECIAL_HANDLING VARCHAR(35),
PRIMARY KEY (INVOICE_ID),
FOREIGN KEY (CUST_ID) REFERENCES CUSTOMER(CUST_ID) ON UPDATE CASCADE)
ENGINE = InnoDB;
try to declare the name of the foreign key, and then declare the column of the current table and the table and the reference column
CONSTRAINT fk_customer FOREIGN KEY (INVOICE_ID)
REFERENCES customer(CUST_ID)

Mysql can not add foreign key

I have a study case where three tables has to be created. there creation statements are as following.
create table COMMUNITY(
c_id varchar(10) primary key,
name varchar(30) not null,
longitude float,
latitude float,
post_code varchar(15) not null,
key(c_id))
create table UNIT(
c_id varchar(10) not null,
u_id int not null,
name varchar(20) not null,
key(c_id, u_id),
primary key(c_id, u_id),
constraint unique(c_id, u_id),
constraint FK_UNIT foreign key(c_id) references COMMUNITY(c_id)
on delete cascade on update cascade)
create table ROOM(
r_id int not null,
u_id int not null,
c_id varchar(10) not null,
name varchar(20),
primary key(c_id, u_id, r_id),
constraint FK_ROOM_UID foreign key(u_id) references UNIT(u_id)
on delete cascade on update cascade,
constraint FK_ROOM_CID foreign key(c_id) references UNIT(c_id)
on delete cascade on update cascade)
the Community and unit tables are created successfully, but when I try to create room, mysql gives me Error Code: 1215. Cannot add foreign key constraint
I wonder what's going here and How I can create them? (I knew InnoDB can solve this problem, but is there any other way I can do that?)
Thanks
This looks like the culprit of the actual error you're seeing:
constraint FK_ROOM_UID foreign key(u_id) references UNIT(u_id)
constraint FK_ROOM_UID foreign key(c_id) references UNIT(c_id)
It should be:
constraint FK_ROOM_UID foreign key(c_id, u_id) references UNIT(c_id, u_id)
Referencing the double-column key in the UNIT table
You'll also need to use InnoDB to actually create the foreign key indexes, so:
CREATE TABLE UNIT(
....
) ENGINE=InnoDB;
I finally fugured this out, according to the MySQL log
"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."
All I have to do is to create primary key at the first column of a table and make sure the type matches. so I changed my statements as following
create table UNIT(
primary key(c_id, u_id),
c_id varchar(10) not null,
u_id int not null,
name varchar(20) not null,
constraint unique(c_id, u_id),
constraint FK_UNIT foreign key(c_id) references COMMUNITY(c_id)
on delete cascade on update cascade)
create table ROOM(
primary key(c_id, u_id, r_id),
r_id int not null,
u_id int not null,
c_id varchar(10) not null,
name varchar(20),
constraint FK_ROOM_UID foreign key(c_id, u_id)
references UNIT(c_id, u_id) on delete cascade on update cascade)
Now I can create tables properly. I have been struggling on this problem since yesterday.

multiple foreign keys cannot be added

I am wondering why i cannot be able to add this foreign keys.This is my schema
CREATE TABLE members(
member_id INT NOT NULL AUTO_INCREMENT,
name VARCHAR(50) NOT NULL,
num_1 int,
num_2 int,
password VARCHAR(50) NOT NULL,
PRIMARY KEY (member_id)
)ENGINE=InnoDB DEFAULT CHARSET=utf8;
CREATE TABLE contacts(
contact_id INT NOT NULL AUTO_INCREMENT,
s1 int,
phone_number VARCHAR(10) NOT NULL,
s2 int,
s3 int,
PRIMARY KEY (contact_id),
FOREIGN KEY (s1) REFERENCES members(num_1) ON DELETE CASCADE ON UPDATE CASCADE,
FOREIGN KEY (s2) REFERENCES members(num_2) ON DELETE CASCADE ON UPDATE CASCADE,
FOREIGN KEY (s3) REFERENCES members(member_id) ON DELETE CASCADE ON UPDATE CASCADE
)ENGINE=InnoDB DEFAULT CHARSET=utf8
I get this error on the mysql terminal
ERROR 1215 (HY000): Cannot add foreign key constraint
Is there a problem with my schema?.
Works for me this way:
CREATE TABLE members(
member_id INT NOT NULL AUTO_INCREMENT,
name VARCHAR(50) NOT NULL,
num_1 int,
num_2 int,
password VARCHAR(50) NOT NULL,
PRIMARY KEY (member_id),
key idx_num1 (num_1),
key idx_num2 (num_2)
)ENGINE=InnoDB DEFAULT CHARSET=utf8;
CREATE TABLE contacts(
contact_id INT NOT NULL AUTO_INCREMENT,
s1 int,
phone_number VARCHAR(10) NOT NULL,
s2 int,
s3 int,
PRIMARY KEY (contact_id),
FOREIGN KEY (s1) REFERENCES members(num_1) ON DELETE CASCADE ON UPDATE CASCADE,
FOREIGN KEY (s2) REFERENCES members(num_2) ON DELETE CASCADE ON UPDATE CASCADE,
FOREIGN KEY (s3) REFERENCES members(member_id) ON DELETE CASCADE ON UPDATE CASCADE
)ENGINE=InnoDB DEFAULT CHARSET=utf8;
Just added
key idx_num1 (num_1),
key idx_num2 (num_2)
in table members. Foreign keys need to reference an indexed column (not necessarily unique and not necessarily NOT NULLable).
From the manual:
InnoDB permits a foreign key to reference any index column or group of columns. However, in the referenced table, there must be an index where the referenced columns are listed as the first columns in the same order.