Using column name prefixes
I was working on some database documentation for a customer recently & noticed something very handy about having columns named uniquely in a database which might be worth taking into consideration for your naming standards documentation.
In the database I was working on, a table name indicator is prefixed to column names to "uniquefy" them. Eg, the first_name column in the customers table is named [cus_first_name]. In the employees table, the column would be named [emp_first_name]. This allows a simple table / column / procedure mapping query to be run which documents which table.columns are accessed by which procedures & vice versa, eg:
select distinct pname, tname, cname
from (select so.name 'pname', sp.text 'ptext'
from syscomments sp
join sysobjects so on sp.id = so.id
where so.xtype = 'P') procs
, (select so.name 'tname', sc.name 'cname'
from sysobjects so
join syscolumns sc on so.id = sc.id
where so.xtype = 'U') cols
where charindex(cols.cname, procs.ptext, 1) > 0
This provides a nice easy to read list in the format:
[TableName], [ColumnName], [Procedure]
Very handy for determining dependency & producing documentation. I put the output into Excel, threw on an auto-filter & now I can easily see which table / columns are accessed by which procedures & vice versa, which really helps with what I'm doing today.
If column names weren't unique in the database though, this wouldn't be possible without a TSQL parser. The uniqueness allows simple pattern matching rather than reliance on TSQL parsers (which aren't readily available)
Is this a good thing to include in a naming standards document? I guess there are arguments for & against doing this but this has been very helpful to me today so I thought I'd put it out there for others to comment on.
Personally, I like the idea as it also avoids the need for specifically aliasing columns in queries where multiple relations are joined, which is a discipline which is often neglected under the heat of development pressures.