September 4, 2009 at 10:10 am
Primary keys are not essential in the Relational Model. Every relation / relvar must of course have at least one candidate key but if it has more than one such key then it makes no difference whether you designate any of those keys to be a "primary" one or not.
SQL's PRIMARY KEY constraint is also superfluous because NOT NULL UNIQUE does the same job.
So if the PRIMARY KEY syntax was dropped from the SQL language would anyone actually miss it?
The main reason to use PRIMARY KEY seems to be just convention - do it because everyone else does. Another possible argument in its favour might be that certain developer tools and data management tools depend on the presence of a PRIMARY KEY constraint. However, I don't find that a very good excuse because the power and usefulness of such software would be improved if it supported any candidate key equally and not just those declared as PRIMARY KEY. The PRIMARY KEY constraint ought not to excuse software vendors from improving their software.
Are there any other reasons to use PRIMARY KEYs?
September 4, 2009 at 10:17 am
Can you explain the reasons to NOT use a PRIMARY KEY?
Exactly how will not using them improve anything?
September 4, 2009 at 10:21 am
It can make developing software a lot easier - this is probably the best reason
September 4, 2009 at 10:22 am
Speed.
the PK determines the actual order of the data, so the any SQL's that use that "PK = somevalue" is very fast...because that PK index is used, and makes index seeks or scans faster.
Also, the PK is actually stored at the leaf level in the page, so it's faster because the table data does not have to be accessed to get the value.
so while "In Theory", PK's are not required, "In Practice", they are required to make any data access acceptable as far as speed goes.
Lowell
September 4, 2009 at 10:23 am
Lowell (9/4/2009)
Speed.the PK determines the actual order of the data, so the any SQL's that use that "PK = somevalue" is very fast...because that PK index is used, and makes index seeks or scans faster.
Also, the PK is actually stored at the leaf level in the page, so it's faster because the table data does not have to be accessed to get the value.
so while "In Theory", PK's are not required, "In Practice", they are required to make any data access acceptable as far as speed goes.
Lowell, you are talking about clustered indexes. Not PRIMARY KEYs. They aren't the same thing and one does not depend on the other.
Regards,
September 4, 2009 at 10:25 am
Ahh David, yeah, i fell into that SQL trap where i assume all my PK's are also the clustered index...my bad.
Lowell
September 4, 2009 at 10:44 am
steveb (9/4/2009)
It can make developing software a lot easier - this is probably the best reason
Do you mean that having Primary Keys makes it easier, or not having them makes it easier?
Either way, how does it make it easier?
September 4, 2009 at 11:54 am
How is primary key not an essential concept in RDBMS'es? They go directly to the definition of a well-formed table, and are the basis by which Codd's Guaranteed access rule is implemented.
Primary key is not an column, it's not a property of an index, it's a whole lot more than that. Among other thing it's what should be used as a foreign key if any natural keys are not suitable (and even in some cases where there might be a natural key).
It is something that is use for consistency. Consistency of relationship. It's also the one and only one you have to worry about changing in the future. I've seen many many projects where you start out with a dozen candidate keys, and not one survives as unique and addressable. How do you then guarantee access to the data you're storing in this amorphous object that used to be a table?
----------------------------------------------------------------------------------
Your lack of planning does not constitute an emergency on my part...unless you're my manager...or a director and above...or a really loud-spoken end-user..All right - what was my emergency again?
September 4, 2009 at 12:34 pm
Matt Miller (9/4/2009)
How is primary key not an essential concept in RDBMS'es? They go directly to the definition of a well-formed table, and are the basis by which Codd's Guaranteed access rule is implemented.
Any candidate key will suffice for guaranteed access and if every table has at least one candidate key there is no special reason to call any of them "primary". Codd says that where there is more than one candidate key the choice of primary key is "arbitrary" and suggests it should be optional.
Among other thing it's what should be used as a foreign key if any natural keys are not suitable (and even in some cases where there might be a natural key).
I don't know what you mean by that. A primary key could be "natural" or otherwise. In relational terms "primary key" implies nothing about the type of data in that key other than that it qualifies as a candidate key (though in fact SQL doesn't even require that much, which is one reason to deprecate SQL's misappropriation of the term "PRIMARY KEY").
It is something that is use for consistency. Consistency of relationship. It's also the one and only one you have to worry about changing in the future.
This is a fair point. Designating some key as "primary" acts as an aide-mémoire during design. It can be used to indicate that a key has some "special" significance. My own view is that that's really the job of data modelling tools and a data dictionary. It's not useful or necessary in the DBMS.
September 5, 2009 at 12:26 am
Lowell (9/4/2009)
the PK determines the actual order of the data
Um... no... sorry. It doesn't. You can have PK's that are not a clustered PK.
--Jeff Moden
Change is inevitable... Change for the better is not.
September 5, 2009 at 12:28 am
Jeff Moden (9/5/2009)
Lowell (9/4/2009)
the PK determines the actual order of the dataUm... no... sorry. It doesn't. You can have PK's that are not a clustered PK.
Heh... and I fell into my own trap of not reading all the posts before responding. I see this was already corrected. Sorry for my duplication.
--Jeff Moden
Change is inevitable... Change for the better is not.
September 9, 2009 at 1:26 pm
So what are my reasons for wanting to abolish the PRIMARY KEY?
One good reason is the enormous amount of needless misinformation and confusion about keys in online articles and even in some books. Try this expirement. Take a look at many popular database books or anyone else talking about "primary keys" (including many posts of the in SQL Server Central). 9 times out of 10 you'll find they aren't talking about primary keys at all. They mean a candidate key.
If the PRIMARY KEY constraint was deprecated and no longer a topic of discussion in database design then the quality of that discussion and information would be improved. Students and practitioners would be the better for it because their mental model of what keys are would more accurately reflect real database design issues.
Another reason to discontinue the PRIMARY KEY constraint is one already mentioned: SQL PRIMARY KEY is not the same as what the relational model calls a primary key so overloading those different meanings can only lead to confusion. (Yes I'm aware that SQL isn't relational either but that's no excuse)
September 9, 2009 at 8:19 pm
Ummm... I really want my doctor to know the differerence between a transverse colon and a descending colon even though they're both just a part of the same large intestine especially if he intends to operate.
Although the technical differences are virtually NIL between a PK and a CK and they're both candidate keys, I still prefer that people know the subtle differences that can occur with their creation. I also enjoy the ease in which I can create a PK when only one candidate key needs to be present on a table.
--Jeff Moden
Change is inevitable... Change for the better is not.
September 10, 2009 at 12:16 am
Jeff Moden (9/9/2009)
Ummm... I really want my doctor to know the differerence between a transverse colon and a descending colon even though they're both just a part of the same large intestine especially if he intends to operate.Although the technical differences are virtually NIL between a PK and a CK and they're both candidate keys, I still prefer that people know the subtle differences that can occur with their creation. I also enjoy the ease in which I can create a PK when only one candidate key needs to be present on a table.
The subtle differences are just minor quirks of the syntax in SQL Server. Obviously if the syntax was removed then those differences would disappear too, so why would that matter? The only thing "easier" about PRIMARY KEY is that it is slightly fewer keystrokes than NOT NULL UNIQUE.
September 10, 2009 at 3:30 am
Michael Valentine Jones (9/4/2009)
steveb (9/4/2009)
It can make developing software a lot easier - this is probably the best reasonDo you mean that having Primary Keys makes it easier, or not having them makes it easier?
Either way, how does it make it easier?
Sorry I wasn’t very clear, I meant it makes it easier to have primary keys as I can define them in the database and then everyone working on the project from developers, testers to project managers and business analysts will know exactly what the primary key should be on the table as it will show up in SSMS, Visual Studio and generated diagrams as the primary key.
Sure the same thing can be done with a unique non-null index but it is not immediately obvious that this is the primary key.
Viewing 15 posts - 1 through 15 (of 27 total)
You must be logged in to reply to this topic. Login to reply