I have 2 tables
Table1(PKFK1, PKFK2)
Table2(PKFK1, PKFK2, PKFK3)
I want to do an update cascade on table2 so that every time pkfk2 is updated in table1 is does the same in table2. I have tried the following:
alter table Table2
ADD CONSTRAINT fk_cascadeUP FOREIGN KEY (PKFK1) REFERENCES Table1(PKFK1) ON UPDATE CASCADE,
ADD CONSTRAINT fk_cascadeUP2 FOREIGN KEY (PKFK2) REFERENCES Table1 (PKFK2) ON UPDATE CASCADE;
This gives an error. How can I do the update cascade?
Each ALTER TABLE needs to be its own statement. Try this:
alter table Table2
ADD CONSTRAINT fk_cascadeUP FOREIGN KEY (PKFK1) REFERENCES Table1(PKFK1) ON UPDATE CASCADE
alter table Table2
ADD CONSTRAINT fk_cascadeUP2 FOREIGN KEY (PKFK2) REFERENCES Table1 (PKFK2) ON UPDATE CASCADE
First, you will have to delete the foreign key, and then add the foreign key with your desired requirements. Drop the foreign key from the table and then add foreign key with "ON UPDATE CASCADE".
It would be somewhat like:
ALTER TABLE Table2
DROP FOREIGN KEY [foreign key name]
ADD CONSTRAINT fk_cascadeup2 FOREIGN KEY (PKFK2) REFERENCES Table1 (PKFK2) ON UPDATE
CASCADE;
The other method can be "set foreign_key_checks = 0" and after altering the foreign key, you can set it back to "1".
Related
I am applying a foreign key constraint to a table(datareal.official). Should I always refer to the same columns in the primary key of the other tabel (datareal.officialcarriage). Or could I theoretically also use difference in the reference table?
ALTER TABLE datareal.official
ADD CONSTRAINT FK_carriage FOREIGN KEY
(target,goal)
REFERENCES datareal.officialcarriage (target,goal)
ON DELETE CASCADE
ON UPDATE CASCADE
Here how I created tables:
CREATE TABLE TABLE_A(
id uuid NOT NULL, UNIQUE
name text
);
CREATE TABLE TABLE_B(
id uuid NOT NULL, UNIQUE
name text
);
-- custom realization of many-to-many association
CREATE TABLE TABLE_A_B(
id uuid NOT NULL, UNIQUE
a_id uuid REFERENCES TABLE_A(id) ON UPDATE CASCADE,
B_id uuid REFERENCES TABLE_B(id) ON UPDATE CASCADE
);
I've already created tables and now can't update it by adding ON DELETE CASCADE.
And I need now to add ON DELETE CASCADE to staging table TABLE_A_B. How to do it ?(
You use ON DELETE CASCADE:
CREATE TABLE TABLE_A_B(
id uuid NOT NULL UNIQUE,
a_id uuid REFERENCES TABLE_A(id) ON UPDATE CASCADE ON DELETE CASCADE,
B_id uuid REFERENCES TABLE_B(id) ON UPDATE CASCADE ON DELETE CASCADE
);
Here is a db<>fiddle that fixed some typos in your code.
In particular, the foreign key reference should be to a primary key. Although allowed to a unique key, the purpose of primary keys is really to identify individual rows -- and one main use is for foreign key references.
EDIT:
If the constraints already exist, then do the following.
First, get their names:
select *
from information_schema.table_constraints
where constraint_type = 'FOREIGN KEY' and table_name = 'table_a_b';
Note: You can assign names to skip this step.
Then drop the existing foreign key constraint:
alter table table_a_b
drop constraint table_a_b_a_id_fkey;
Finally, add a new one:
alter table table_a_b
add constraint fk_table_a_b_a
foreign key (a_id) references table_a(id)
on update cascade
on delete cascade;
I'm having the following problem with DB2:
I need to change a column name (column "A"), but the thing is that column A has a PK constraint and a FK constraint. So I need to drop this constraints first, change the column name and then create the constraints again. But I remember a Professor told me once that you can't create a foreign key in a column which already has values. Is that true?
This is my script:
ALTER TABLE TARGET_TABLE
DROP PRIMARY KEY PK_A CASCADE;
ALTER TABLE TARGET_TABLE
RENAME COLUMN A TO B;
alter table TARGET_TABLE
add CONSTRAINT PK_B PRIMARY KEY( B);
alter table TARGET_TABLE add CONSTRAINT FK_B FOREIGN KEY( B) REFERENCES OTHER_TABLE(C);
Thanks in advance.
You can create a foreign key on a column which already has values. The only restriction is that the values must be be valid for the FK you are defining.
If not you will get an error such as
SQL0667N The FOREIGN KEY "I..." cannot be created because the table contains
rows with foreign key values that cannot be found in the parent key of the
parent table. SQLSTATE=23520
Hello i am beginner in SQL and i have one question:
How to add new Column_B(int) which is foreign key to an existing Column_A(id) in same Table_A?
I tried this but i got Error Code: 1215. Cannot add foreign key constraint
ALTER TABLE Table_A ADD COLUMN Column_B int;
ALTER TABLE Table_A
ADD fk_Table_A FOREIGN KEY (Column_B) REFERENCES Table_A (Column_A);
alter table Table_A
ADD constraint fk_Table_A FOREIGN KEY (Column_B) REFERENCES Table_A (Column_A);
It is possible that your references does not match. It can only add a foreign key when all the rows satisfy the foreign key condition, which is in your case that every value of Column_B is in table_A.Column_A.
SQL How to Alter Constraint
Below is 1 of my constraint
CONSTRAINT ACTIVEPROG_FKEY1 FOREIGN KEY(ActiveProgCode) REFERENCES PROGRAM(ActiveProgCode),
I want to add in
ON DELETE CASCADE
to the constraint above.
How do i alter that existing constraint ACTIVEPROG_FKEY1 and add
ON DELETE CASCADE
to constraint ACTIVEPROG_FKEY1
Consider ACTIVEPROG_FKEY1 is at Table ACTIVEPROG
You can not alter constraints ever but you can drop them and then recreate.
Have look on this
ALTER TABLE your_table DROP CONSTRAINT ACTIVEPROG_FKEY1;
and then recreate it with ON DELETE CASCADE like this
ALTER TABLE your_table
add CONSTRAINT ACTIVEPROG_FKEY1 FOREIGN KEY(ActiveProgCode) REFERENCES PROGRAM(ActiveProgCode)
ON DELETE CASCADE;
hope this help
No. We cannot alter the constraint, only thing we can do is drop and recreate it
ALTER TABLE [TABLENAME] DROP CONSTRAINT [CONSTRAINTNAME]
Foreign Key Constraint
Alter Table Table1 Add Constraint [CONSTRAINTNAME] Foreign Key (Column) References Table2 (Column) On Update Cascade On Delete Cascade
Primary Key constraint
Alter Table Table add constraint [Primary Key] Primary key(Column1,Column2,.....)