Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Issue with foreign keys in DB

Author  Topic 

mcoliver88
Starting Member

1 Post

Posted - 2014-05-23 : 17:22:19
Hi All

I am new to SQL so please don't bash my skills i am still learning. I am learning SQL and i have created a database for college administration system, The tables i create are working perfectly, but its when i insert the data i get the problem.

Here is my script:

http://pastebin.com/1yew4h7n

When i add the data i get an issue stating that their is an integrity violation and that the primary key in another table can't be found. I get this with a lot of tables. Any help will be great.

O BTW i am using Oracle SQL plus to build the database

Thanks
Michael

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2014-05-23 : 17:42:20
I don't click on sketchy links, but based on your description you are getting the error because you are inserting the data in the wrong order.

When there are foreign keys, parent data must exist before the child data. And in reverse, you can't delete parent data until the child data has been deleted.

By the way, SQLTeam is for Microsoft SQL Server. For Oracle help, you'll need to find a different site.

Tara Kizer
SQL Server MVP since 2007
http://weblogs.sqlteam.com/tarad/
Go to Top of Page

TG
Master Smack Fu Yak Hacker

6065 Posts

Posted - 2014-05-23 : 17:46:26
This whole site is specific to MS Sql Server so for Oracle questions you should post to an oracle forum (ie: dbforums.com)

However,looking at your script the problem is that you need to insert into the tables in the correct sequence so that you don't violate any foreign key constraints. So start with the table(s) that have no FK constraints, then work your way through so that you don't attempt to insert a row that has a dependency on a row in a different table that hasn't already been inserted.

EDIT:


Be One with the Optimizer
TG
Go to Top of Page

GouravSaxena1987
Starting Member

23 Posts

Posted - 2014-05-27 : 13:30:35
You need to insert data in tables sequentially.
i.e. first insert data in table which has only primary key and no foreign key.
Then insert data in table which has first table's primary key as foreign key and so on....

Use CASCADE Update/Delete.

See below link-
http://technet.microsoft.com/en-us/library/aa902684%28v=sql.80%29.aspx

Regards,
Gourav Saxena
Data Warehouse Counsultant
GouravSaxena1987@gmail.com
Go to Top of Page

TG
Master Smack Fu Yak Hacker

6065 Posts

Posted - 2014-05-27 : 13:46:57
quote:
Originally posted by GouravSaxena1987


Use CASCADE Update/Delete.


I would not suggest you implement cascade actions unless the business requirements want that. Big difference between automatically cascading through your model deleting data from receiving a message saying "you can't delete that".

Be One with the Optimizer
TG
Go to Top of Page
   

- Advertisement -