One common issue I run into when naming columns in a new database table is the right name to use for classifying subtypes. The most natural column name is typically type
, but I try to avoid using SQL keywords or reserved words in my naming.
I'm aware that type
is a non-reserved keyword in both MySQL and Postgres, so I can use it, but should I?
What is current best practice around using type
as a column name? Is there a synonym which is so broadly equivalent that it just makes sense to use that?
Over the years I've spent way to much time trying to pick other names and this has come up twice in discussions in the past week, so I wanted to see if there is any clear consensus around this?
In case it helps anyone else, some alternatives I've used in the past to try to get around this include:
category
kind
subtype
group
type_of
role
class
<entity>_type
Naming a column "type" is technically fine it seems. I name my columns to avoid confusion with methods / keywords in the language / frameworks I might use to query to database.
SQL AS keyword is used to give an alias to table or column names in the queries.
You can get the MySQL table columns data type with the help of “information_schema. columns”. SELECT DATA_TYPE from INFORMATION_SCHEMA. COLUMNS where table_schema = 'yourDatabaseName' and table_name = 'yourTableName'.
In SQL Server, we can specify the column name with space in square bracket or parenthesis.
my suggestion is usually to avoid using keywords but not because they're reserved but because they're often actually ambiguous in and of themselves.
For example, say you have a customer table - with a column 'type' this might be filled with segmentation (high value/low value etc) or it could be filled with source (direct marketing/walk in etc).
"Type" is a low-value word. I'd usually recommend being as explicit as possible at the expense of column-length. For example 'CustomerSourceType' or 'InventoryLocationType' or whatever is actually clearer in the scenario.
First rule,
Never ever ever ever use Keywords or Reserver words. The person coming after you will suffer.
If you are only using 'Type' for this time. You could use Types
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With