I would like to create a table that has multiple foreign keys to multiple different tables (as the relationship is many to many).
#creating t1
CREATE TABLE t1
(ID INT AUTO_INCREMENT primary key,
x1 VARCHAR(50)
);
#Creating t2
CREATE TABLE t2
(v1 VARCHAR(50),
v2 VARCHAR(50),
primary key (v1, v2)
);
#creating attended table
CREATE TABLE t3
(ID INT,
v1 VARCHAR(50),
v2 VARCHAR(50),
primary key (ID, v1, v2 ),
foreign key(v1) references t2(v1),
foreign key(v2) references t2(v2),
foreign key(ID) references t1(ID)
);
Above is my code. I get no errors for creating t1 and t2. However, I get the following code when I try to create t3:
ERROR 1215 (HY000): Cannot add foreign key constraint
The foreign key is the complete key of the other table - you can not only use half of it as FK.
t2 has a combined primary key. when referencing a fk in t3 you need both.
See Why use multiple columns as primary keys (composite primary key)
To create a complex FK see SQL Server: adding foreign key on multiple columns
or for MySql see Multiple-column foreign key in MySQL?
Related
Ok, let's say i have 2 tables.
tbl_1
1.id_tbl1 (primary key)
2.name
tbl_2
1.id_tbl2 (primary key)
2.id_tbl1 (foreign key)
3.name
P.S If i want to delete data tbl_1 then the id_tbl1 as fk will also be deleted in tbl_2 ... *how can work with php?
You need to use ON DELETE CASCADE when defining the foreign key. See below:
create table tbl_1 (
id_tbl1 int primary key not null,
name varchar(10)
);
create table tbl_2 (
id_tbl2 int primary key not null,
id_tbl1 int,
constraint fk1 foreign key (id_tbl1)
references tbl_1 (id_tbl1) on delete cascade
);
See running example at DB Fiddle.
Is there any difference in declaring foreign keys on tables between this two options?
OPTION 1
create table Table1 (
name varchar(255),
id_fkey int references Table2 (id)
);
OPTION 2
create table Table1 (
name varchar(255),
id_fkey int,
foreign key (id_fkey) references Table2 (id)
);
Are both declarations of a proper foreign key or do they have any difference?
These are two ways to do the same thing. The first syntax is called column constraint, the second table constraint.
The only real difference is that a foreign key over more than one column can only be written as a table constraint.
Misson:
I'm trying to create two tables in MySQL, the first table has two primary keys (composite). The second table has three, two of which are foreign keys that reference the first tables two primary keys. So now I'm trying to bridge them, which isolates the problem code and can be seen below.
Issue:
MySQL workbench refuses allow me to create a table that references another table's two primary keys. It just gives me the error code: 1215. Cannot add foreign key constraint.
I tried:
Changing the attribute types from datetime to varchar(). Changing the attribute names. Checked spelling 5 times. Referencing only one key works fine, but I need both.
Problem Code (SQL Table):
CREATE TABLE IF NOT EXISTS TestInformation2
(
WorkOrder varchar(15),
Date datetime,
TechnicianID smallint,
Primary key (WorkOrder, Date)
) ENGINE = InnoDB;
CREATE TABLE IF NOT EXISTS TestBridge2
(
TestBridgeID integer Primary key,
WorkOrder varchar(15),
Date datetime,
Foreign key (WorkOrder) references TestInformation (WorkOrder),
Foreign key (Date) references TestInformation (Date)
) ENGINE = InnoDB;
Result I want:
Create the table 'TestBridge' which has the two foreign key attributes: WorkOrder and Date
It should be one composite foreign key, not two:
CREATE TABLE `TestBridge2` (
`TestBridgeID` INTEGER NOT NULL,
`WorkOrder` VARCHAR(15),
`Date` DATETIME,
PRIMARY KEY (`TestBridgeID`),
CONSTRAINT FOREIGN KEY (`WorkOrder`, `Date`) REFERENCES `TestInformation2` (`WorkOrder`, `Date`)
)
ENGINE=InnoDB;
One single foreign key has to be created for this like:
CREATE TABLE IF NOT EXISTS TestBridge2
(
TestBridgeID integer Primary key,
WorkOrder varchar(15),
Date datetime,
Foreign key (WorkOrder, Date) references TestInformation2 (WorkOrder, Date)
) ENGINE = InnoDB;
I have 2 tables here for example table A and table B, how can i insert data when both of the tables consist of foreign key of each other? like table A got an attribute is foreign key references of table B, and table B got an attribute is foreign key references of table A
create table abc
(ID varchar(10),
subID varchar(10),
primary key (ID),
foreign key (subID) references def(SubID)
)
create table def
(SubID varchar(10),
ID varchar(10),
primary key (SubID),
foreign key (ID) references abc(ID)
)
somehow like this(i skipped other various informations)
I don't think it will be possible in your current design.
If you really have the need to do insert with cross dependency over the two tables, remove the one foreign key. Then you can do an insert on table ABC and then DEF.
I also think your DB design is incorrect.
Since version 7.3 MySQL Cluster should be capable of foreign key constraints. But here is what happens:
DROP TABLE IF EXISTS t2;
DROP TABLE IF EXISTS t1;
CREATE TABLE t1 (
id INT PRIMARY KEY
) ENGINE='InnoDB';
CREATE TABLE t2 (
id INT PRIMARY KEY,
t1id INT
) ENGINE='InnoDB';
ALTER TABLE t2
ADD CONSTRAINT t2t1 FOREIGN KEY (t1id) REFERENCES t1 (id)
ON DELETE CASCADE ON UPDATE CASCADE;
This is using InnoDB and everything works just fine. Now try it with NDB:
DROP TABLE IF EXISTS t2;
DROP TABLE IF EXISTS t1;
CREATE TABLE t1 (
id INT PRIMARY KEY
) ENGINE='NDB';
CREATE TABLE t2 (
id INT PRIMARY KEY,
t1id INT
) ENGINE='NDB';
ALTER TABLE t2
ADD CONSTRAINT t2t1 FOREIGN KEY (t1id) REFERENCES t1 (id)
ON DELETE CASCADE ON UPDATE CASCADE;
-- ERROR 150 (HY000): Cannot add foreign key constraint
And now the weird part:
DROP TABLE IF EXISTS t2;
DROP TABLE IF EXISTS t1;
CREATE TABLE t1 (
id INT UNIQUE KEY
) ENGINE='NDB';
CREATE TABLE t2 (
id INT PRIMARY KEY,
t1id INT
) ENGINE='NDB';
ALTER TABLE t2
ADD CONSTRAINT t2t1 FOREIGN KEY (t1id) REFERENCES t1 (id)
ON DELETE CASCADE ON UPDATE CASCADE;
Works just fine.
Is there any rule that says "with the NDB storage engine you cannot reference a primary key column in a foreign key constraint" or "with NDB you have to reference UNIQUE KEYS in foreign key constraints"?
To make things even stranger:
If you replace the definition of t1 by
CREATE TABLE t1 (
id INT UNIQUE KEY NOT NULL
) ENGINE='NDB';
you get the same error.
I'm thinking that PRIMARY KEY implies NOT NULL and that the problem lies not with the former but with the latter.
From what I gathered from MySQL site and Dev blogs:
An important difference to note with the Foreign Key implementation in
InnoDB is that MySQL Cluster does not support the updating of Primary
Keys from within the Data Nodes themselves - instead the UPDATE is
emulated with a DELETE followed by an INSERT operation. Therefore an
UPDATE operation will return an error if the parent reference is using
a Primary Key, unless using CASCADE action, in which case the delete
operation will result in the corresponding rows in the child table
being deleted. The Engineering team plans to change this behavior in a
subsequent preview release.
Foreign Keys in MySQL Cluster
New MySQL Cluster 7.3 Previews
MySQL Cluster 7.3 Labs Release – Foreign Keys Are In!
Recent MySQL Cluster posts
Support for such operation is not yet confirmed.