I have a table named master_employee with a column empid as the primary key, and it has 12 rows in it. This empid has been mapped as a foreign key to another table named dep_child. I want to delete the 12 records in the master_employee table but I was unable to do it.
You were unable to do the deletes from master_employees, as there is a referential integrity with dep_child. You would need to either disable the constraint or delete the records from dep_child, before being able to delete records from master_employees
Add this constraints to your master_employee table
ALTER TABLE "master_employee"
ADD CONSTRAINT "fk_emp11"
FOREIGN KEY ("emp_id")
REFERENCES "dep_child" ("emp_id")
ON DELETE CASCADE;
Related
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
i want to add to a primary key in one table a references to the primary key of another table.
my code:
CREATE TABLE[payment]
(ID int Primary key)
CREATE TABLE [tab]
(ID int Primary key references tab2(ID))
Alter Table payment
alter column ID
ADD constraint fk_payment
references tab(ID)
i get the error that the syntax near constraint is wrong, but i don't know what to change
because of the not changeable order of the table Alter table is the only option. to reference from one table to the other doesn't work cause I've references from that table to another one already.
i need two one-to-one-relations from one table to another
If you want to add a FK constraint, just use this code:
ALTER TABLE dbo.payment
ADD CONSTRAINT fk_payment
FOREIGN KEY(ID) REFERENCES dbo.tab(ID)
You don't need to alter the column or table - just add the constraint
I've checked that none of the two tables has any foreign keys. I've checked that they both have Id of type uniqueidentifier. I runt the script and get this eror.
ALTER TABLE [dbo].[Records]
ADD CONSTRAINT [FK_dbo.Records_dbo.Users_UserId]
FOREIGN KEY ([UserId])
REFERENCES [dbo].[Users] ([Id])
--ON DELETE CASCADE
The ALTER TABLE statement conflicted with the FOREIGN KEY constraint "FK_dbo.Records_dbo.Users_UserId". The conflict occurred in database "MyDb", table "dbo.Users", column 'Id'.
Not sure how to troubleshoot it... Tested both with and without the cascade...
It must be something to do with the existing data. There must be some records conflicting the foreign key creation. Try to create the key on an empty schema to see if it works. Use WITH NOCHECK to not to check existing rows at the time of foreign key creation, if you need that data.
Most likely you have a UserId in the Records table that does not exist in the Users table. This will certainly happen if you've been using 0 or some other value as a "default" value.
I have a table that I need to drop the primary key which is a compound key and make it a primary key based on a single value.
I dropped the original Primary key:
SQL> alter table depositor
2 drop primary key;
Table altered.
But when I try try add the new back I get an error message.
SQL> alter table depositor
2 add primary key (account_number);
alter table depositor
*
ERROR at line 1:
ORA-02437: cannot validate (ZSMITH.SYS_C0084996) - primary key violated
Was the PK not dropped? Did I not add it back correctly?
On an existing table, you can only create a primary key if the data in that table would really work as a primary key (i.e. all values distinct and not null).
Im trying to add a constraint to a reservation table, the reservation can either be for a flight or accommodation or both.
First 4 records booked inward flight, outward flight and accommodation
Next 4 records booked a flight only and have acc_id set to NULL
Following 2 records booked only accommodation, hence in flight, out flight and seats are set to null.
Here are my constraints for this table
ALTER TABLE HOLIDAY_RESERVATION ADD CONSTRAINT HOLIDAY_PK PRIMARY KEY (RESV_ID);
ALTER TABLE HOLIDAY_RESERVATION ADD CONSTRAINT CUSTOMER_FK FOREIGN KEY (BOOKING_CUS_ID) REFERENCES CUSTOMER (CUS_ID);
ALTER TABLE HOLIDAY_RESERVATION ADD CONSTRAINT STAFF_FK3 FOREIGN KEY (EMP_ID) REFERENCES STAFF (EMP_ID);
ALTER TABLE HOLIDAY_RESERVATION ADD CONSTRAINT FLIGHT_FK FOREIGN KEY (IN_FLIGHT_ID) REFERENCES FLIGHT (FLI_ID);
ALTER TABLE HOLIDAY_RESERVATION ADD CONSTRAINT FLIGHT_FK1 FOREIGN KEY (OUT_FLIGHT_ID) REFERENCES FLIGHT (FLI_ID);
ALTER TABLE HOLIDAY_RESERVATION ADD CONSTRAINT ACC_FK FOREIGN KEY (ACC_ID) REFERENCES ACCOMMODATION (ACC_ID);
and the only constraint that is yielding an error is;
ALTER TABLE HOLIDAY_RESERVATION ADD CONSTRAINT FLIGHT_FK1 FOREIGN KEY (OUT_FLIGHT_ID) REFERENCES FLIGHT (FLI_ID);
I get
ERROR at line 1:
ORA-02298: cannot validate (U1146815.FLIGHT_FK1) - parent keys not found
What seems to be the problem? i understand that it has to do with orphan childs, but i am setting nulls so i dont understand, please advise
The error indicates that the FLIGHT table does not have an entry for at least one of the FLI_ID values between 11 and 18. You'd need to insert a row in the FLIGHT table for whatever flights are missing or update your table to have a different OUT_FLIGHT_ID.
Delete all the rows from the child table which is being used to alter the column for having the references.
ALTER TABLE sales ADD CONSTRAINT sales_time_fk
FOREIGN KEY (time_id) REFERENCES times (time_id)
RELY DISABLE NOVALIDATE;