I wrote a lot on this topic: if you read something, I will be clear that I probably mean, in particular, access to Jet aka MS Access.
In Jet, tables are physically ordered in PRIMARY KEY using an unordered clustered index (cluster on a compact). If the table does not have PK, but has candidate keys defined using UNIQUE constraints on NOT NULL columns, then the engine will select one for the clustered index (if your table does not have a clustered index, then it is called a heap, it may not be a table at all! ) How does the engine select the candidate key? Can he choose one that includes columns with a null value? I really do not know. The fact is that in Jet, the only explicit way to specify a cluster index for the engine is to use PRIMARY KEY. Of course, for a PC in Jet, for example, there are other ways to use it. it will be used as a key if it is excluded from the FOREIGN KEY declaration in SQL DDL, but again, why not be explicit.
The problem with Jet is that most people creating tables do not know or care about clustered indexes. In fact, most users (I set) put the Autonumber autoincrement column in each table and determine the PRIMARY KEY exclusively in this column, without creating any unique restrictions on the natural key and candidate keys (can I actually read the autoincrement column key without exposing it to end users, - this is another discussion in itself). I will not go into details about clustered indexes here, but suffice it to say that IMO is the only auto-increment column, rarely suitable for the perfect choice.
No matter what kind of SQL engine you are, the choice of PRIMARY KEY is arbitrary and engine dependent. Typically, the engine will attach particular importance to the PC, so you should find out what it is and use it to your advantage. I urge people to use NOT NULL UNIQUE constraints in the hope that they will pay more attention to all candidate keys, especially when they decide to use "autonumber" columns, which (should) make no sense in the data model. But I would prefer that people choose one well-considered key and use PRIMARY KEY instead of putting it in the auto-increment column out of habit.
Should all tables have a PC? I say yes, because otherwise, at least you lose the small advantage that the PC gives, and in the worst case, you do not have data integrity.
BTW Chris OC makes a good point here about temporary tables that require primary keys with primary keys (lowercase letters) that cannot be implemented using simple PRIMARY KEY constraints (SQL keywords in upper case).
onedaywhen Oct 02 '08 at 10:02 2008-10-02 10:02
source share