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.
Related
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,...
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?
I have 2 tables
CREATE TABLE table1 (
id1 int(10) NOT NULL PRIMARY KEY,
name varchar(20)
);
CREATE TABLE table2 (
newid int(10) NOT NULL PRIMARY KEY,
f_id int(10)
);
Now, I have added a foreign key constrain
ALTER TABLE table2
ADD CONSTRAINT fk_id FOREIGN KEY (f_id) REFERENCES table1 (id)
ON DELETE CASCADE ON UPDATE CASCADE;
Now, the problem is I am unable to update/delete/truncate the table 'table2'. What is the exact problem here and how to solve it?
Foreign key enforces referential integrity,try to remove the record from table 1 and then table 2. You cannot truncate a table which is having FK
I created two tables students and orders, I tried to add a foreign key to the table "orders" but i have this error :
Cannot add or update a child row: a foreign key constraint fails
table STUDENTS:
CREATE TABLE STUDENTS (
ID varchar(50) NOT NULL,
NAME VARCHAR (20) NOT NULL,
AGE INT NOT NULL,
ADDRESS CHAR (25),
PRIMARY KEY (ID)
)
Table ORDERS
CREATE TABLE Orders
(O_Id int NOT NULL PRIMAY KEY,
Order_No int NOT NULL,ID varchar(50))
Add foreign key to "orders":
ALTER TABLE Orders
ADD CONSTRAINT fk_PerOrders
FOREIGN KEY(ID)
REFERENCES STUDENTS (ID)
Check if [Orders] table currently has any IDs that are not in [STUDENTS] table.
I think that you are trying to add foreign key constraint and there is no corresponding primery key in parent table.
Try to run
SET FOREIGN_KEY_CHECKS=0
your ALTER TABLE statement. When you finish run
SET FOREIGN_KEY_CHECKS=1
Here's the basic gist of what I'm trying to do:
create table main(id char(1) primary key);
create table other (
id int primary key auto_increment,
main_id char(1),
key m (main_id),
constraint fk foreign key (main_id) references main(id)
);
insert into main(id) values('a');
insert into other(main_id) values('a');
update main inner join other on other.main_id=main.id
set main.id='b', other.main_id='b'
where main.id='a';
This results in a foreign key constraint failure. Is there any way to accomplish this without dropping the foreign keys (not really an option on my large production database)?
You can do this simply by temporarily setting foreign_key_checks=0 in your session:
set foreign_key_checks=0;
update main inner join other on other.main_id=main.id
set main.id='b', other.main_id='b'
where main.id='a';
Another option is to configure the foreign key with the ON UPDATE CASCADE option so that if the primary key is updated on the parent table it will cascade to the child table:
create table main(id char(1) primary key);
create table other (
id int primary key auto_increment,
main_id char(1),
key m (main_id),
constraint fk foreign key (main_id) references main(id) on update cascade
);