0

I'm doing a school project, and i have the table items, and ticketItems. TicketItems contains a foreign key from the table items (Item_ID). So what if i want to delete one ítem and its ID is also on the another table ?? it'll throw me an error.

appleduardo
  • 59
  • 2
  • 10
  • Check this answer - http://stackoverflow.com/a/14381227/1080354 – gotqn Dec 07 '14 at 09:54
  • possible duplicate of [How to change the foreign key referential action? (behavior)](http://stackoverflow.com/questions/3359329/how-to-change-the-foreign-key-referential-action-behavior) – Dgan Dec 07 '14 at 10:01

1 Answers1

2

Generally, when you've got a parent table (items) referenced by a child table (ticketitems), if you would like to delete an item, you also would like to take an action on the ticketitems table.

If you don't and you have no constraint on ticketitems, you will leave an orphan row on that table, because the item_ID will no more be available.

Probably all you need is to:

delete from ticketitems where item_ID = ItemIdYouWantToDelete

Now that you have deleted from ticketitems you will be able to delete from items too.

When you define a constraint for a foreign key, you can specify how your DBMS will manage that. That is explained in the links in the comments below your question.

kiks73
  • 3,718
  • 3
  • 25
  • 52