MySql table with Foreign key - mysql

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)
);

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;
;

I had a problem with my sql program with foreign keys

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)
);

How to create a table with a composite key made of 2 foreign keys referencing to other tables?

I have three tables A, B and funding:
Table A has a primary key partner_id
Table B has a primary key branch_id
When I try to create table C with the following code:
CREATE TABLE Funding (
partner_id INT,
branch_id INT,
total_fund FLOAT,
PRIMARY KEY (partners_id, branch_id),
FOREIGN KEY (partners_id) REFERENCES A(partner_id) ON delete SET NULL,
FOREIGN KEY (branch_id) REFERENCES B(branch_id) ON delete SET NULL
);
I get error message:
1830: column partner_id cannot be NOT NULL: needed in a foreign key constraint.
How can I solve this problem?
Create a separted ID for PK:
SQL DEMO
CREATE TABLE A (
partner_id INT,
PRIMARY KEY (partner_id)
);
CREATE TABLE B (
branch_id INT,
PRIMARY KEY (branch_id)
);
CREATE TABLE Funding (
id INT PRIMARY KEY AUTO_INCREMENT,
partner_id INT,
branch_id INT,
total_fund FLOAT,
FOREIGN KEY (partner_id) REFERENCES A(partner_id) ON DELETE SET NULL,
FOREIGN KEY (branch_id) REFERENCES B(branch_id) ON DELETE SET NULL
);
You can also add:
ALTER TABLE `Funding` ADD UNIQUE `unique_index`(partner_id, branch_id);
But that can cause problem when multiple partners from same branch are deleted
In CREATE TABLE Funding just add NOT NULL statement to partner_id and branch_id.
CREATE TABLE Funding ( partner_id INT NOT NULL, branch_id INT NOT NULL,...

How to Link Foreign Key with Different Name

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.

mySQL trying to create table with constraint foreign key

I have this simple code
create table transport (
CODE varchar (10),
TDATE date,
ID integer,
PNAME varchar (10),
primary key (CODE,TDATE,ID,PNAME) );
create table planes (
NAME varchar (10),
NSEATS integer,
FSEATS integer,
ECSEATS integer,
primary key (NAME),
CONSTRAINT foreign key(NAME) references transport(PNAME));
But it doesn't let me do the foreign key thing as it says errno: 150 "Foreign key constraint is incorrectly formed
Any tips appreciated
Thanks
The use of composite primary keys in this case is not reasonable. If you want a foreign key reference, you need to reference all columns in the primary key. So, this version is simpler:
create table transport (
transportId int not null auto_increment primary key,
CODE varchar (10),
TDATE date,
ID integer,
PNAME varchar (10),
unique (CODE, TDATE, ID, PNAME)
);
create table planes (
planeId int not null auto_increment primary key,
NAME varchar (10),
NSEATS integer,
FSEATS integer,
ECSEATS integer,
primary key (NAME),
transportId int,
CONSTRAINT foreign key(transportId) references transport(transportId)
);
Rule is that : A Foreign key in one table points to a Primary Key in another table.