Suppose I have two tables in a parent – child relationship. Let’s call them “sites” and “buildings” where a “site” is a parent of one or more “buildings”. I want the IDs to be unique across databases so I will be using GUIDs for the IDs.
Should I prefer generic names for the ID fields in the tables or strongly typed names and why?
Example 1 (Generic Names):
CREATE TABLE sites (
id VARCHAR(38) NOT NULL,
-- other attributes
);
CREATE TABLE buildings (
id VARCHAR(38) NOT NULL,
parent_id VARCHAR(38) NOT NULL,
-- other attributes
);
Example 2 (Strongly Typed Names):
CREATE TABLE sites (
site_guid VARCHAR(38) NOT NULL,
-- other attributes
);
CREATE TABLE buildings (
building_guid VARCHAR(38) NOT NULL,
site_guid VARCHAR(38) NOT NULL,
-- other attributes
);
I prefer to use building_id, site_id so that the column name defines its contents more explicitly than just “id”. This also makes it possible to use the ANSI join “using” syntax:
Another advantage is that when such columns are used in queries (or views or subqueries), they don’t need re-aliasing so often – like this: