Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SqlServer identity naming convention [closed]

I have table Widgets and table Persons

both of them contains Identity column ( seed)

what is the recommended naming convention for the identity column ?

is it "id" ( for both tables ) :

Widgets.Id
Persons.Id

or

Widgets.WidgetId
Persons.PersonId

Is there advantages / disadvantages in the former compare to the latter ?

like image 282
Royi Namir Avatar asked Nov 28 '11 14:11

Royi Namir


2 Answers

This is one case where I'd go for redundancy and have Thing.ThingID
Otherwise you have to alias it everywhere when you JOIN

Note: this was already done to death and beyond on programmers.se:
Why is naming a table's Primary Key column “Id” considered bad practice?

like image 200
gbn Avatar answered Nov 03 '22 19:11

gbn


The best practice for this is to preface with the table name.

This will make it much clearer when joining which field you are referring to in the SELECT list.

It also makes accidental bad JOIN conditions impossible, i.e.

JOIN TableA ON ID = TableA.ID

As a rule, duplicate field names across tables should be avoided unless they represent identical data.

like image 3
JNK Avatar answered Nov 03 '22 19:11

JNK