Difference between #3 and #4 of
Guidelines for good schema design

Revision #4 has been created by fsb on Jun 23, 2012, 5:01:08 PM with the memo:

Modified the "Name a table's PK 'id'" section, giving it more specific context.
« previous (#3) next (#5) »


Title unchanged

Guidelines for good schema design

Category unchanged


Yii version unchanged

Tags unchanged

database, schema design

Content changed

class TblComment extends CActiveRecord { // NO
class Comment extends CActiveRecord { // YES
It's very distracting to see the prefix everywhere in the code.

DO name
eacha table's primary keyown ID column "id" ------------------------------------- Mostany tables will have a single unique primary keyhave their own independent, single-column unique primary key (`int NOT NULL AUTO_INCREMENT PRIMARY KEY` is a common example), and things work a bit more smoothly if it' is named `id` (not `commentid` or `postid`).

Though Yii figures out the primary key by reading the database schema no matter what you call it, other parts of the system may not be able to follow, and explicitly depend on the key being `id`.
Example: [CArrayDataProvider](http://www.yiiframework.com/doc/api/1.1/CArrayDataProvider#keyField-detail) assumes the key is `id`, and though you can override it with the `keyField` attribute, it's more convenient to avoid the need for it in the first place.

Clearly this won't work for tabSometimes this rule doesn't apply, for examples with a composite primary key, but these should be relatively uncommon.hen a table has a multi-column primary key or when a table's primary key is a foreign key to another table's ID. 

AVOID semantically-meaningful primary key names
A classic design mistake is creating a table with a primary key that has actual meaning. In this example, the user table makes the username the primary key:
122 0
Viewed: 91 031 times
Version: 1.1
Category: Tips
Written by: Steve Friedl
Last updated by: softark
Created on: Aug 27, 2011
Last updated: 5 years ago
Update Article


View all history