I had a problem with my sql program with foreign keys - mysql

create database MALL;
use MALL;
create table customer (
customer_id int not null,
name varchar (20),
lastname varchar(20),
registration_date date,
primary key (customer_id)
);
create table inventory (
item_id int not null,
item_name varchar(20),
cost int,
primary key (item_id)
);
create table purchase (
purchase_date date,
purchase_count int,
customer_id int,
item_name varchar(20),
foreign key (customer_id) references customer(customer_id),
foreign key (item_name) references inventory(item_name)
);
I get this error
ERROR 1822 (HY000): Failed to add the foreign key constraint. Missing
index for constraint 'purchase_ibfk_2' in the referenced table
'inventory'

Here:
create table purchase (
...
foreign key (item_name) references inventory(item_name)
)
The column referenced by the foreign key needs a unique index: inventory(item_name) does not have that. I would simply recommend referencing the primary key of inventory rather than some other column:
create table purchase (
purchase_date date,
purchase_count int,
customer_id int,
item_id int,
foreign key (customer_id) references customer(customer_id),
foreign key (item_id) references inventory(item_id)
);

Related

SQL error - Failed to add the foreign key constraint

CREATE TABLE employee (
emp_id INT,
first_name VARCHAR(40),
last_name VARCHAR(40),
birth_day DATE,
sex VARCHAR(1),
salary INT,
super_id INT,
branch_id INT,
PRIMARY KEY (emp_id)
);
CREATE TABLE branch(
branch_id INT,
branch_name VARCHAR(40),
mgr_id INT,
mgr_start_date DATE,
FOREIGN KEY(mgr_id) REFERENCES employee(emp_id) ON DELETE SET NULL
);
ALTER TABLE employee
ADD FOREIGN KEY(branch_id)
REFERENCES branch(branch_id)
ON DELETE SET NULL;
I'm not sure what I'm doing wrong. When I run the lasts query, I keep getting the
"failed to add the foreign key constraint"
error saying there's a missing index for constraint 'employee_ibfk_2' in the reference table 'branch'
Columns referenced in a foreign key must be a key.
Presumably you forgot to declare branch_id as primary key?
Try:
...
CREATE TABLE branch(
branch_id INT,
branch_name VARCHAR(40),
mgr_id INT,
mgr_start_date DATE,
PRIMARY KEY (branch_id),
FOREIGN KEY(mgr_id) REFERENCES employee(emp_id) ON DELETE SET NULL
);
...
The branch_id column in your branch table is not indexed. a requirement for foreign key constraints.
Add an index like this:
ALTER TABLE `branch`
ADD INDEX `branch_id` (`branch_id` ASC) VISIBLE;
;

Cannot add foreign key constraint in MYSQL, can't find any error in my code

This is the task:
Create a table payment with attributes: p_id, c_id, staff_id, amount, payment_date where p_id is primary key and c_id and staff_id is foreign key which refer to table customer and staff respectively. Display the p_id and staff_id from the table payment where payment_date is 8 august 2020
Please help I am getting this error even though my code seems right to me
create table payment(
p_id int primary key,
amount int,
payment_date date,
c_id int,
staff_id int
);
create table customer(
cus_id int primary key,
c_id int,
foreign key (c_id) references payment(c_id)
);
ERROR 1215 (HY000): Cannot add foreign key constraint
You have your foreign key relationship backwards. payment should have FK to customer and staff.
create table customer (
c_id int primary key,
-- other columns
);
create table staff (
s_id int primary key
-- other columns
);
create table payment(
p_id int primary key,
amount int,
payment_date date,
c_id int,
staff_id int,
foreign key (c_id) references customer(c_id),
foreign key (staff_id) references staff(s_id)
);

ER_FK_NO_INDEX_PARENT: Failed to add the foreign key constraint. Missing index for constraint 'employee_ibfk_10' in the referenced table 'branch'

I try to create a table for the company database.
As I tried to add some foreign keys in there, something goes wrong.
CREATE TABLE employee (
emp_id INT PRIMARY KEY,
first_name VARCHAR(40),
last_name VARCHAR(40),
birth_day DATE,
sex VARCHAR(1),
salary INT,
super_id INT,
branch_id INT unique
);
CREATE TABLE branch (
branch_id INT PRIMARY KEY,
branch_name VARCHAR(40),
mgr_id INT,
mgr_start_date DATE,
FOREIGN KEY(mgr_id) REFERENCES employee(emp_id) ON DELETE SET NULL
);
ALTER TABLE employee
ADD FOREIGN KEY(branch_id)
REFERENCES branch(branch_id)
ON DELETE SET NULL;
ALTER TABLE employee
ADD FOREIGN KEY(super_id)
REFERENCES employee(emp_id)
ON DELETE SET NULL
The "alter table employee" part to add foreign key (branch) failed with the following statement.
ER_FK_NO_INDEX_PARENT: Failed to add the foreign key constraint. Missing index for constraint 'employee_ibfk_10' in the referenced table 'branch'
Could this be some problem with my setting?

MySql table with Foreign key

I cannot set the primary key for the invoice_2 table because it gives an error
here is my code. invoice_1 is the other table that contains the foreign key of the invoice_2 table
CREATE TABLE invoice_2
(
itemID VARCHAR(20) PRIMARY KEY ,
invoiceNumber INT,FOREIGN KEY REFERENCES invoice_1.invoiceNumber,
quantity INT,
sellingPrice REAL,
lineTotal REAL
)
Try below syntax:
CREATE TABLE invoice_2
(
itemID VARCHAR(20),
invoiceNumber INT,
quantity INT,
sellingPrice REAL,
lineTotal REAL,
PRIMARY KEY (itemID),
CONSTRAINT `FK_invoiceNumber` FOREIGN KEY (`invoiceNumber`) REFERENCES `invoice_1` (`invoiceNumber`)
);
Have a look at the mysql syntax for creating tables and setting PK
CREATE TABLE table_name
(
column1 column_definition,
column2 column_definition,
...
CONSTRAINT [constraint_name]
PRIMARY KEY [ USING BTREE | HASH ] (column1, column2, ... column_n));
TRY: adding a pk constraint
CREATE TABLE invoice_2(
itemID VARCHAR(20),
invoiceNumber INT,FOREIGN KEY REFERENCES invoice_1.invoiceNumber,
quantity INT,
sellingPrice REAL,
lineTotal REAL
CONSTRAINT itemID PRIMARY KEY (itemID)
);

error in SQL syntax create table

I was trying to feed the following commands to MySQL CLI with
, which seems to be good, however when I added one more table, it complained there was an error in syntax.
Here are the commands
CREATE TABLE IF NOT EXISTS User(
uid INT,
name VARCHAR(64) UNIQUE,
birthday date,
PRIMARY KEY(uid)
) ENGINE = InnoDB ;
CREATE TABLE IF NOT EXISTS UserEmail(
uid INT,
email VARCHAR(64),
PRIMARY KEY(uid, email),
FOREIGN KEY(uid) REFERENCES User(uid)
);
However if I wanted to add one more table, it said there's an syntax error near the ')' at the line where PRIMARY KEY(uid) lies.
CREATE TABLE IF NOT EXISTS User(
uid INT,
name VARCHAR(64) UNIQUE,
birthday date,
PRIMARY KEY(uid)
) ENGINE = InnoDB ;
CREATE TABLE IF NOT EXISTS UserEmail(
uid INT,
email VARCHAR(64),
PRIMARY KEY(uid, email),
FOREIGN KEY(uid) REFERENCES User(uid)
);
CREATE TABLE friendship(
invite_uid INT,
accept_uid INT,
start_date DATE,
PRIMARY KEY(invite_uid, accept_uid),
FOREIGN KEY(invite_uid) REFERENCES User(uid),
FOREIGN KEY(accept_uid) REFERENCES User(uid),
);
Not sure where went wrong since the error was not complained in the newly added command.
--UPDATE--
Well that was fixed, but there's one more problem.
Adding the following table throws
cannot create table "estore.contains" (errorno: 150)
CREATE TABLE contains(
uid INT,
wid INT,
pid INT,
PRIMARY KEY(uid, wid, pid),
FOREIGN KEY(uid) REFERENCES User(uid),
FOREIGN KEY(wid) REFERENCES Wishlist(wid),
FOREIGN KEY(pid) REFERENCES Product(pid)
);
--UPDATE 2--
Full Tables that I want to add
CREATE TABLE IF NOT EXISTS User(
uid INT,
name VARCHAR(64) UNIQUE,
birthday date,
PRIMARY KEY(uid)
) ENGINE = InnoDB ;
CREATE TABLE IF NOT EXISTS UserEmail(
uid INT,
email VARCHAR(64),
PRIMARY KEY(uid, email),
FOREIGN KEY(uid) REFERENCES User(uid)
);
CREATE TABLE friendship(
invite_uid INT,
accept_uid INT,
start_date DATE,
PRIMARY KEY(invite_uid, accept_uid),
FOREIGN KEY(invite_uid) REFERENCES User(uid),
FOREIGN KEY(accept_uid) REFERENCES User(uid)
);
CREATE TABLE Seller(
sid INT,
name VARCHAR(64),
PRIMARY KEY(sid)
);
CREATE TABLE Product(
pid INT,
sid INT,
name VARCHAR(64),
description TEXT,
price DOUBLE,
PRIMARY KEY(pid),
FOREIGN KEY(sid) REFERENCES Seller(sid)
);
CREATE TABLE buy(
uid INT,
pid INT,
time DATE,
PRIMARY KEY(uid, pid),
FOREIGN KEY(uid) REFERENCES User(uid),
FOREIGN KEY(pid) REFERENCES Product(pid)
);
CREATE TABLE Wishlist(
uid INT,
wid INT,
start_time DATE,
end_time DATE,
PRIMARY KEY(uid,wid),
FOREIGN KEY(uid) REFERENCES User(uid)
);
CREATE TABLE conntains(
uid INT,
wid INT,
pid INT,
PRIMARY KEY(uid, wid, pid),
FOREIGN KEY(uid) REFERENCES User(uid),
FOREIGN KEY(wid) REFERENCES Wishlist(wid),
FOREIGN KEY(pid) REFERENCES Product(pid)
)ENGINE = InnoDB;
Anyone could help? Thx
Check the last line of your code
FOREIGN KEY(accept_uid) REFERENCES User(uid),
Remove the comma at the end.
remove , a end of line
FOREIGN KEY(accept_uid) REFERENCES User(uid),
--------------------------------------------^
There is an extra comma on the third table.
CREATE TABLE IF NOT EXISTS User(
uid INT,
name VARCHAR(64) UNIQUE,
birthday date,
PRIMARY KEY(uid)
) ENGINE = InnoDB ;
CREATE TABLE IF NOT EXISTS UserEmail(
uid INT,
email VARCHAR(64),
PRIMARY KEY(uid, email),
FOREIGN KEY(uid) REFERENCES User(uid)
);
CREATE TABLE friendship(
invite_uid INT,
accept_uid INT,
start_date DATE,
PRIMARY KEY(invite_uid, accept_uid),
FOREIGN KEY(invite_uid) REFERENCES User(uid),
FOREIGN KEY(accept_uid) REFERENCES User(uid), <---- Extra comma
);
FOREIGN KEY(accept_uid) REFERENCES User(uid),<-- Comma should be removed
Try this:
CREATE TABLE friendship(
invite_uid INT,
accept_uid INT,
start_date DATE,
PRIMARY KEY(invite_uid, accept_uid),
FOREIGN KEY(invite_uid) REFERENCES User(uid),
FOREIGN KEY(accept_uid) REFERENCES User(uid)
);
#Daniel, you cannot use the name CONTAINS for a table since its an SQL keyword. Please create the table with some other name.
Fortunately I figured the second problem out, the foreign key was not made right, I need to use
FOREIGN KEY(uid, wid) REFERENCES Wishlist(uid, wid),
to refer the the primary key combination in Wishlist