SQL Clear table with foreign key

0

Good day, I'm starting with SQL and during the development of the project my question arose.

The issue is that I have a Detail table

And a product table

The problem arises when I want to delete a product, due to the reference that DetailV has on it I can not eliminate it, I know that to eliminate it I would need to eliminate the detail that makes the reference, but, I would like to know if it is possible to eliminate a Product without erasing the detail V that refers to it. I have heard that with ALTER TABLE it is possible to delete the reference from the table, but I just need the reference to be removed from the registry, so that the product is eliminated but the DetailV does not. Thanks for your time.

    
asked by Kenneth Alv 18.11.2018 в 01:15
source

2 answers

1

I recommend that you declare the tables using ON DELETE CASCADE, so that:

CREATE TABLE rooms (
    room_no INT PRIMARY KEY AUTO_INCREMENT,
    room_name VARCHAR(255) NOT NULL,
    building_no INT NOT NULL,
    FOREIGN KEY (building_no)
        REFERENCES buildings (building_no)
        ON DELETE CASCADE
);

In your case you would have to put ON DELETE CASCADE in the declaration of your detail table, so when you delete it from the main table, the associated record (s) will automatically be deleted.

I leave a link where the theory is explained in case you need it:

link

This way you will not get the error that you need to delete it before. However, if you want to save those records, I would create a table like detail, but with another name, historical_detail, in this way, before deleting the associated record, copy the data and save it in the detail_historical table, so you can Remove from the main table and keep those records even if they do not belong to the same detail table. This can be done automatically with a trigger of the detail table that is executed every time you are going to delete a record of it. Here is an example of a trigger:

How to create a trigger that when deleting a row from a table, delete a related row from another table in mysql?

In that case delete from another table, you are interested in adding in another the record that you are going to delete.

What you say about doing the alter table to remove the restriction of the foreign key, I do not recommend it to you. You would have to do it every time you want to delete a record and if you then go back to add the restriction (I'm not sure if will leave this last so freely if you already have data in the tables) you will have an inconsistent DB because it will have records in detail that will not have in the parent table and this may cause errors in the future ..

    
answered by 18.11.2018 в 02:11
1

I always recommend in the trunk tables (Product) to handle a variable 'Status' (true or false), and so when you want to unsubscribe you only update the 'Status' to 'false'. This will prevent you from losing information from your database and it is something that is done anywhere so as not to lose information.

    
answered by 18.11.2018 в 07:01