Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using `type` as database column name

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
like image 627
Matt Sanders Avatar asked Mar 22 '16 16:03

Matt Sanders


People also ask

Can I use type as column name in MySQL?

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.

Can I use SQL keyword as column name?

SQL AS keyword is used to give an alias to table or column names in the queries.

How do I get column names and data types in SQL?

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'.

How can I mention column name in SQL?

In SQL Server, we can specify the column name with space in square bracket or parenthesis.


2 Answers

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.

like image 89
Mark Taylor Avatar answered Oct 05 '22 23:10

Mark Taylor


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

like image 44
WiredTheories Avatar answered Oct 05 '22 23:10

WiredTheories