Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

Relationship Question Expand / Collapse
Author
Message
Posted Thursday, September 19, 2013 8:24 AM
SSC Journeyman

SSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC Journeyman

Group: General Forum Members
Last Login: Monday, August 25, 2014 11:46 AM
Points: 98, Visits: 241
Hey guys -

I have a relationship question....for SQL Server that is. ;)

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?




Post #1496427
Posted Thursday, September 19, 2013 8:52 AM
Hall of Fame

Hall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of Fame

Group: General Forum Members
Last Login: 2 days ago @ 3:14 PM
Points: 3,145, Visits: 1,366
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
Post #1496443
Posted Thursday, September 19, 2013 9:16 AM
SSC Journeyman

SSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC Journeyman

Group: General Forum Members
Last Login: Monday, August 25, 2014 11:46 AM
Points: 98, Visits: 241
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...

Post #1496453
Posted Friday, September 20, 2013 3:34 PM


Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Today @ 8:09 AM
Points: 1,226, Visits: 2,547
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
Post #1497059
Posted Saturday, September 21, 2013 10:15 AM


SSC-Dedicated

SSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-Dedicated

Group: General Forum Members
Last Login: Today @ 12:41 PM
Points: 37,078, Visits: 31,638
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."

(play on words) "Just because you CAN do something in T-SQL, doesn't mean you SHOULDN'T." --22 Aug 2013

Helpful Links:
How to post code problems
How to post performance problems
Post #1497145
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse