Need a query to delete pk.

  • I have a table sample in which sample_uid is primary key

    another table i have is test which contains sample_uid as foreign key. when i am trying to delete sample_uid from test table, it gives error integrity foreign no action.

    as test_uid of test table is foreign key in test_parameter.

    I want a query to delete sample_uid from sample.

    I want to delete a single row in sample table but giving error like that is foreign key into another table

    Any help..

  • This is because the foreign key enforces something called referential integrity. This is a good thing because the relationship maintains the integrity of the data. You don't want to have rows in the child table without a parent. This is one way foreign keys protect us from getting into trouble.

    To delete the row from your sample table, first delete the rows in the child with the foreign key value equal to your primary key in sample. For example, to delete sample_uid 1, do something like the following:

    DELETE FROM test

    WHERE sample_uid = 1;

    DELETE FROM sample

    WHERE sample_uid = 1;

    You need to delete all child rows before deleting the parent. But first, make you don't need those rows later in life for history or something similar. If you're going to need them later, you should archive them off somewhere for later reference.

Viewing 2 posts - 1 through 1 (of 1 total)

You must be logged in to reply to this topic. Login to reply