SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Relationship Question


Relationship Question

Author
Message
RedBirdOBX
RedBirdOBX
SSC-Enthusiastic
SSC-Enthusiastic (150 reputation)SSC-Enthusiastic (150 reputation)SSC-Enthusiastic (150 reputation)SSC-Enthusiastic (150 reputation)SSC-Enthusiastic (150 reputation)SSC-Enthusiastic (150 reputation)SSC-Enthusiastic (150 reputation)SSC-Enthusiastic (150 reputation)

Group: General Forum Members
Points: 150 Visits: 243
Hey guys -

I have a relationship question....for SQL Server that is. Wink

I'm in the early stages of my first large "ETL" project which I'll take data from a lot of old tables and insert them into a more normalized design. I have a little test database which I'll test out new scripts on..over and over. I would like the ability to run a SP to clear out all the test tables in preparation for the next test. So, after creating a "dump' SP, I ran into some very basic, fundamental table relationship problems.

As a test, I have two basic tables. See screenshot from SSMS. Very common relation set up there.



I have set up a couple integrity rules:

Set the FK in "NewCustomers" to go to it's default value of 0 in the event that the child record (NewAccounts) is deleted.

Cascade the changes to NewCustomers in the unlikely event that the PK in NewAccounts is changed.

Seems logical and common practice. Right??

So with both tables populated with test data, I get constraint errors when I try to DELETE or TRUNCATE TABLES.

So when I try to (in this order):

DELETE FROM NewAccounts (and truncate)
DELETE FROM NewCustomers (and truncate)

I get:

[color=#ff0000]The DELETE statement conflicted with the FOREIGN KEY constraint "FK_NewCustomers_NewAccounts". The conflict occurred in database "AcmeCars", table "dbo.NewAccounts", column 'NewAccountID'.
The statement has been terminated.[/color]


It still deletes all the Customers but keeps the Accounts.

I thought by deleting the accounts FIRST, it would set the FKs all to "0" and then turn around and delete the Customers.

I must be missing an obvious concept. Can you enlighten?
Mark Fitzgerald-331224
Mark Fitzgerald-331224
Hall of Fame
Hall of Fame (3.9K reputation)Hall of Fame (3.9K reputation)Hall of Fame (3.9K reputation)Hall of Fame (3.9K reputation)Hall of Fame (3.9K reputation)Hall of Fame (3.9K reputation)Hall of Fame (3.9K reputation)Hall of Fame (3.9K reputation)

Group: General Forum Members
Points: 3877 Visits: 1538
As your default is 0 (not null) this must be in the accounts table.

Example:

Accounts PK values {0,1,2,3,4...}
Customer (Account FK) values {1,2,3,2,3,2,1,2,3,4...}

When you delete the values from Accounts this attempts to change the Customers to

Accounts PK values {} [empty set]
Customer (Account FK) values {0,0,0,0,0,0,0,0,0...} which is invalid due to the PK-FK constraint

If you were to change the Customer (Account FK) to set NULL rather than set DEFAULT then it would work, assuming that the FK allows NULLs.

Probably best to delete the rows from FK to PK and not rely upon the set default or set null.

The customers are deleted by the second DELETE statement.

Fitz
RedBirdOBX
RedBirdOBX
SSC-Enthusiastic
SSC-Enthusiastic (150 reputation)SSC-Enthusiastic (150 reputation)SSC-Enthusiastic (150 reputation)SSC-Enthusiastic (150 reputation)SSC-Enthusiastic (150 reputation)SSC-Enthusiastic (150 reputation)SSC-Enthusiastic (150 reputation)SSC-Enthusiastic (150 reputation)

Group: General Forum Members
Points: 150 Visits: 243
Thanks Mark. I think my basic design was flawed too. I moved the FK in the Accounts Table. The Account belongs to the customer. Not the other way around. So..it seems this design works better (and no fuss from SQL!):

Old Design:

CUSTOMERS
------
CustomerID (PK)
AccountNumID (FK)
FullName
Address


ACCOUNTS
------
AccountID
AccountNum




NEW Design:

CUSTOMERS
-------------
CustomerID
Name
Address....etc...


ACCOUNTS
-----------
AccountID
CustomerID (FK)
AccountNum
...etc...
wolfkillj
wolfkillj
UDP Broadcaster
UDP Broadcaster (1.5K reputation)UDP Broadcaster (1.5K reputation)UDP Broadcaster (1.5K reputation)UDP Broadcaster (1.5K reputation)UDP Broadcaster (1.5K reputation)UDP Broadcaster (1.5K reputation)UDP Broadcaster (1.5K reputation)UDP Broadcaster (1.5K reputation)

Group: General Forum Members
Points: 1460 Visits: 2582
RedBirdOBX (9/19/2013)
Thanks Mark. I think my basic design was flawed too. I moved the FK in the Accounts Table. The Account belongs to the customer. Not the other way around. So..it seems this design works better (and no fuss from SQL!):

Old Design:

CUSTOMERS
------
CustomerID (PK)
AccountNumID (FK)
FullName
Address


ACCOUNTS
------
AccountID
AccountNum




NEW Design:

CUSTOMERS
-------------
CustomerID
Name
Address....etc...


ACCOUNTS
-----------
AccountID
CustomerID (FK)
AccountNum
...etc...



This is a much better design. Now you can delete an account without deleting the customer, but any attempt to delete a customer will fail unless the customer's account is deleted first. Foreign key constraints protect parent-child declarative referential integrity against the occurrence of orphaned child rows (1) preventing the creation of a child row before a corresponding parent row has been created; and (2) blocking the deletion of a parent row that has one or more child rows.

EDIT: Oh, and if all you want to do is create a fresh version of the database each time, why not compile the DDL to first drop and then create the tables, indexes, constraints, and procedures and functions in the correct order into a single script?

Jason Wolfkill
Blog: SQLSouth
Twitter: @SQLSouth
Jeff Moden
Jeff Moden
SSC Guru
SSC Guru (86K reputation)SSC Guru (86K reputation)SSC Guru (86K reputation)SSC Guru (86K reputation)SSC Guru (86K reputation)SSC Guru (86K reputation)SSC Guru (86K reputation)SSC Guru (86K reputation)

Group: General Forum Members
Points: 86083 Visits: 41096
RedBirdOBX (9/19/2013)
I would like the ability to run a SP to clear out all the test tables in preparation for the next test.


I have a frequent need to do such things. The way I do it is quite a bit different though.

I setup the database the way I want it for the tests. Then, I back it up. The backup is usually quite a bit smaller than the "real" database because I've dropped all tables, etc, that aren't a part of the testing. To prep for the "next" test, I simply drop the database and do a restore. Of course, I have a script to do that, as well.

Another trick is to setup the database for testing, as before. Then do a restore from that database to a new database for testing. That usually hums right along.

--Jeff Moden

RBAR is pronounced ree-bar and is a Modenism for Row-By-Agonizing-Row.
First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column.
If you think its expensive to hire a professional to do the job, wait until you hire an amateur. -- Red Adair

Helpful Links:
How to post code problems
How to post performance problems
Forum FAQs
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search