Download presentation
Presentation is loading. Please wait.
1
The Curious case of the foreign key
2
Ed Ben About me Lisa Bohm The Twitter: @LisaGB_SQL
The Started: Coldfusion Developer Now: DB architect/tSQL dev Ed Ben
3
These keys that you speak of…
Technically a data constraint Two types Primary Key Foreign Key Why have them? Help define relationships! Replication…
4
Primary Keys Unique Can be multi-column
All columns specified not nullable Datatype limits (must support binary ordering) Index…. Clustered? (beyond scope of this presentation)
5
The great debate… Natural or Surrogate?
Natural key Ever change? Surrogate key Translation? Ordering of data on pages…. (IF this is also the clustered index. Indexing is beyond the scope of this presentation. This will have some effect on the NCL if your PK is NOT the clustered index.)
6
Non-incrementing Primary Key (like GUID)
If not, page splits can be a problem: Record AAX Record AAZ Record ABC Record ABM Record ABY Record ACE Record ACO Record ACW Record ADA Record ADJ Record ACK Record AAX Record AAZ Record ABC Record ABM Record ABY Record ACE Record ACK Record ACO Record ACW Record ADA Record ADJ
7
Incrementing Primary Key (like IDENTITY)
Contention on the end? Everyone writing to same data page… Record 123 Record 124 Record 125 Record 126 Record 127 Record 128 Record 129 Record 130 Record 131
8
Foreign Keys Data “reference”
Primary Key - OR – unique index Data integrity (Sales order header, sales order details) Easily find column references PKID VALUE Some Data For Thing tblID PKID VALUE 1 2 3 4 1 2 Addtl Info Detail Other
9
Yo – Data Type Matters Set up a table with the following columns:
pfkey.forPrimaryKey intcol int identity , dt2col datetime2 , varchar20Col varchar(20) , varchar2000Col varchar(2000) , someintID int , guidCol uniqueidentifier “Primary Key” table – inserted 1 million rows “Foreign Key” table – copied the above table structure, added 10 million rows copied from “Primary Key” table Not the same number of rows per primary key
10
Four different queries
Query Options: Select all Select count Select a date range (3 weeks) Select data by 6 different someintID values (think orders by customerID)
11
Primary/Foreign Key types tested
No primary/foreign key int datetime2 GUID varchar(20) varchar(2000) (note – no values more than 1700 chars) int + datetime2 int + GUID int + varchar(20) int + varchar(2000) int + datetime2 + GUID int + datetime2 + varchar(20) int + datetime2 + varchar(2000) (Please note: clustered index always on int column. PK/FK were nonclustered indexes)
12
So…. What did we find?
13
Closer look at some details
14
Interesting findings Expected: Not as expected:
Keys including varchars (the bigger, the worse-er) performed the most poorly Keys including the datetime2 column performed quite well on the date range query “Overall”, the int key performed “the best” Not as expected: Keys including the GUID performed well on the “in 6 someIntID” queries Suspect: GUIDs related to these IDs were “near” each other in the nonclustered index Me <> Bob Ward. No hex editor magic explanation here.
15
Please note: Cleared plan cache between each set
Did NOT run multiple iterations of each query Static data set – all rows inserted at once Not like a “normal” database No page splits/index fragmentation
16
Which to use? Well, “it depends”…
Remember: primary key <> (maybe) clustered index Primary Key = index If multiple column key: any table referring back NEEDS ALL COLUMNS Also use the column in same way… Yeah, that varchar thing. Please don’t. Don’t use GUIDs, in spite of these results
17
Foreign Key Settings Restraints on Updates/Deletes
Cascading referential integrity No action = error Cascade = corresponding rows updated/deleted Set Null = values in foreign key set to null (yes, even with update) Set Default = values in foreign key set to default value (yes…) Note: default has to be: a valid primary key value That exists in pk table
18
DEMO TIME!
19
Some Concerns Triggers… Cascading Caused by original update/delete
After triggers – after all cascading actions If multiple triggers on single table, fire in random order Multiple cascading chains: order in which chains fire triggers unspecified But – an entire chain fires before the next starts … and so forth
20
Any other gotchas? So, one day I was walking along and….. Customer-panic-performance-tanking-hands-waving WideWorldImporters: DELETE FROM Sales.OrderLines WHERE OrderID = 59575;
21
DEMO TIME!
22
What’s going on? Sales. Orderlines Sales. InvoiceLines
Foreign key constraint with on Delete Cascade (No rows) Sales. PromoDiscount Foreign key constraint with on Delete Cascade Sales. InvoiceLines (All foreign key field values NULL)
23
What’s going on, work-around flavor
Sales. Orderlines Foreign key constraint NO Delete Cascade (No rows) Sales. PromoDiscount Foreign key constraint with on Delete Cascade Sales. InvoiceLines (All foreign key field values NULL)
24
Take-home Message(s) Use foreign keys (correctly). Be aware of:
data types Referential integrity options If unexpected tables in stats IO or execution plan Look to foreign keys as a possibility
25
Further Experimentation
What happens if there’s records in the “closet” table? What happens if there’s a default value in the InvoiceLines table linked field instead of NULL?
26
Additional Resources Primary Key Primer for SQL Server Primary and Foreign Key Constraints Questions about Primary and Foreign Keys You Were Too Shy to Ask Identity Column in Concurrency, Multithreading, Parallel Processing SQL Server Foreign Key Update and Delete Rules
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.