September 19, 2013 at 8:24 am
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:
[font="Courier New"]
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.[/font]
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?
September 19, 2013 at 8:52 am
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
September 19, 2013 at 9:16 am
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...
September 20, 2013 at 3:34 pm
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
September 21, 2013 at 10:15 am
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
Change is inevitable... Change for the better is not.
Viewing 5 posts - 1 through 4 (of 4 total)
You must be logged in to reply to this topic. Login to reply