SQL: Primary key generation

EvilManagedCare

Senior member
Nov 6, 2004
324
0
0
I'm wondering, can the primary key of a record actually be a derived attribute? Is this considered bad form? Is it even possible? At first thought it seems a risky idea, but I still wonder.

I've been working at data modeling for an upcoming database for a long while today so it's distinctly possible I'm just delirious.
 
Last edited:

Ken g6

Programming Moderator, Elite Member
Moderator
Dec 11, 1999
16,360
4,067
75
What you probably want is to define the primary key as multiple columns.

Wikipedia: "In the SQL Standard, primary keys may consist of one or multiple columns. Each column participating in the primary key is implicitly defined as NOT NULL."
 

KentState

Diamond Member
Oct 19, 2001
8,397
393
126
I would avoid composite primary keys if possible. The exceptions are typically cross-reference tables which relates two distinct concepts together.
 

beginner99

Diamond Member
Jun 2, 2009
5,231
1,605
136
basically there are 2 school, some say composite key is better others say surrogate key (=autogenerated integer) is better.
It depends...both have up and downsides.

One possible downside of surrogate key is when inserting data into multiple joined tables you need to be able to retrieve the auto-generated key. This depends heavily on the used technology (stored procedure,.net, java,...) and not all databases handle this in the same way, especially Oracle.
If you use a "natural key", eg. data you already have present before inserting, you don't' need to retrieve the generated key. I'm not sure but I guess that can improve performance a lot if you do many inserts.
 

Jeraden

Platinum Member
Oct 9, 1999
2,518
1
76
It's perfectly fine to have a multi-field primary key. However, whether that is good design or not really depends on the function of the table. If it's a table that is going to be linked to via foreign keys in other tables, it's probably not a good idea. But if it's more of a stand-alone table, where you'd be accessing it by the multiple columns anyways, then it's fine, as having a sequential primary key would be a waste really since you'd never need it, and never be storing that id in other tables.
 

the182guy

Member
Sep 28, 2011
27
0
0
It's fine if it meets your requirements but don't use a composite key unless you have a reason to.
 

EvilManagedCare

Senior member
Nov 6, 2004
324
0
0
Per jeraden:
"...But if it's more of a stand-alone table, where you'd be accessing it by the multiple columns anyways, then it's fine, as having a sequential primary key would be a waste really since you'd never need it, and never be storing that id in other tables."

This best matches the situation of the table in question. It is unlikely to ever be referenced by another. I will keep the composite key. Thanks for the input everyone.
 
sale-70-410-exam    | Exam-200-125-pdf    | we-sale-70-410-exam    | hot-sale-70-410-exam    | Latest-exam-700-603-Dumps    | Dumps-98-363-exams-date    | Certs-200-125-date    | Dumps-300-075-exams-date    | hot-sale-book-C8010-726-book    | Hot-Sale-200-310-Exam    | Exam-Description-200-310-dumps?    | hot-sale-book-200-125-book    | Latest-Updated-300-209-Exam    | Dumps-210-260-exams-date    | Download-200-125-Exam-PDF    | Exam-Description-300-101-dumps    | Certs-300-101-date    | Hot-Sale-300-075-Exam    | Latest-exam-200-125-Dumps    | Exam-Description-200-125-dumps    | Latest-Updated-300-075-Exam    | hot-sale-book-210-260-book    | Dumps-200-901-exams-date    | Certs-200-901-date    | Latest-exam-1Z0-062-Dumps    | Hot-Sale-1Z0-062-Exam    | Certs-CSSLP-date    | 100%-Pass-70-383-Exams    | Latest-JN0-360-real-exam-questions    | 100%-Pass-4A0-100-Real-Exam-Questions    | Dumps-300-135-exams-date    | Passed-200-105-Tech-Exams    | Latest-Updated-200-310-Exam    | Download-300-070-Exam-PDF    | Hot-Sale-JN0-360-Exam    | 100%-Pass-JN0-360-Exams    | 100%-Pass-JN0-360-Real-Exam-Questions    | Dumps-JN0-360-exams-date    | Exam-Description-1Z0-876-dumps    | Latest-exam-1Z0-876-Dumps    | Dumps-HPE0-Y53-exams-date    | 2017-Latest-HPE0-Y53-Exam    | 100%-Pass-HPE0-Y53-Real-Exam-Questions    | Pass-4A0-100-Exam    | Latest-4A0-100-Questions    | Dumps-98-365-exams-date    | 2017-Latest-98-365-Exam    | 100%-Pass-VCS-254-Exams    | 2017-Latest-VCS-273-Exam    | Dumps-200-355-exams-date    | 2017-Latest-300-320-Exam    | Pass-300-101-Exam    | 100%-Pass-300-115-Exams    |
http://www.portvapes.co.uk/    | http://www.portvapes.co.uk/    |