Short and direct response
In short, you can create the standard you want as long as it is well documented and internationalized.
So you can say you’re wrong when:
- Does not have adequate documentation (obviously).
- It has no internationalized standard. (still a controversial issue, unfortunately)
Detailed answer, with examples, case study, etc, follows reading below:
There’s not a rule that says whether it’s right or wrong.
Each decides a pattern they want. Obviously, if the pattern is well documented, there is no problem.
Redundancy
Particularly, the example you posted in the question I do not like because it is redundant to have the table name in the columns within the table. But it’s a personal taste of mine. Of course, if I take a project with a pattern of this kind, I won’t be crying.. rsrs As a professional, I tolerate, respect and do my duty. But if I take a ZERO project, I prefer to use a standard without redundant nomenclatures.
Example of redundancy:
tabela1
tabela1_coluna1
tabela1_coluna2
tabela1_coluna3
It’s redundant and makes the name too long.
This is enough here:
tabela1
coluna1
coluna2
coluna3
At least that’s the pattern I normally use.
When I need to reference one table with another, there yes, I describe the table name
tabela1
id
foo
bar
tabela2
id
tabela1_id
outra_coluna
outra_coluna2
In this example in "table2", the column "table1_id" references the column "id" of the table "table1".
The thing complicates when a table has a slightly longer name.
Example "table_com_name_major"
If there is a column with an extended name, "coluna_com_name_large", the result would be "table_com_name_maior_coluna_com_name_big". Anyway, it’s not wrong, but it’s awful. And you can still have problems with systems that don’t allow very long names.
To avoid this I always try to name with as short names as possible.
Pattern with initial letters
In Brazil has a "standard" that consists of using only the initial letters.
Note that "default" is in quotes, ok?
Example of how I would do for a table of clients and addresses of these clients:
clientes
id
nome
blablabla
clientes_enderecos
id
clientes_id
cep
bairro
blabla
Now, translating the same structure to a "pattern" widely used in Brazil, as I mentioned above, the pattern consists of using only the initials.
c
id
nome
blablabla
c_e
id
c_id
cep
bairro
blabla
I remember that when I started programming around 1998, in a Delphi course the teacher taught with this pattern using only the initials. The time passed and I did not remember and I never used like this, but recently I met a person who uses this pattern and I remembered this thing from 20 years ago.
I don’t know how it is in Brazil, if it has other patterns, etc, but it seems that this pattern with initial letters still exists probably because it is taught in courses like what I did in 1998.
This pattern is nice because even a table with an extended name is quite short. The problem is that you will always have an extra job of consulting the documentation to only know what the name of a table means if you don’t remember. It’s a small nuisance, but it’s still a nuisance. Sometimes you’re on super-concentrated total gas so you have to stop to read the documentation to understand that merd** means c_o_i_o
, for example. If the table has an intuitive name, you don’t need to stop and lose concentration. But anyway, it’s just a small, tolerable example.
The most "flawed" point in this pattern is the collision of names. In a small project hardly collides names but such a situation may be common. Illustrative example: customer_duplicated
and customer_deleted
would be c_d
. It would have to change one or the other. Then "Deleted" can change to "excluded". But if there is already a "c_e" table, you will have to use creativity without deviating from the original meaning "Deleted". A new option could be "archived". It would be "c_a". But, what if "c_a" already exists?
It’s a simple limitation that causes a huge complication. You waste time curled up in something so small it could be solved with another more suitable pattern. At the end of the day it would probably solve in a hurry by putting a number "c_d2". It contradicts the pattern of the project and starts a series of gambiarras. After all, a misdemeanor "here" can be made "there" too.
Internationalization
Before I begin, I will cite a case to understand the importance of an internationalized standard.
Moving halfway around the world in Japan, I’ve dealt with a company where the programmer is an ultra-nationalist. These guys hate anything that isn’t national. In the database pattern, the names of the tables and columns were all in kanjis ideograms. The guy avoided even the hiragana, katakana or transliteration in Romaji. To make matters worse, it used terms that are not easily found in dictionaries. Even, the initial project was to use POSTGRE or Mysql, but both did not support multibyte table and column names. The brilliant solution to maintain the nationalist pose was to opt for Oracle. Forced the company to pay licenses, modify the codes of the programs and still have an extra cost with certified Oracle professional. Nothing against Oracle, but it was an extremely ridiculous and unnecessary situation because it uses not even 5% of the potential of SGDB.
As if that were not enough, it was quite difficult to have to read and translate the 300 tables, each with more than 20 columns. Everything in Kianji, one of the most beautiful.. rsrs. Even my Japanese colleagues at the time had difficulty understanding the names.
Finally, a childishness which cost expensive for the company. Obviously who paid was the customer who ordered the service. And it pays until today. rsrs.
To avoid situations like this, at the very least, you must have an internationalized standard. The language for internationalization is English.
The same goes for a Brazilian or any other nationality. One day your company can hire an American, an Indian, a Russian, a Vietnamese, expand services to another country, anyway. People from anywhere on the planet can handle English with ease. If the application is not internationalized, it will cost more because the programmer will waste time in translating and understanding the structure and still with risks of misunderstanding. Also remembering that obviously this is valid for documentation and not only for codes, table names, function names, etc.
Reserved names
It is recommended to avoid reserved names such as date
, data
, name
, among others that vary according to the DBMS.
However, this has been changing recently. Currently Sgdbs allow to use the name "whatever", but some may still present conflicts with reserved names if not escaped for a literal interpretation.
Example with Mysql
tabela
id
date
bla_bla_bla
Problems may occur in a SELECT, for example
SELECT id, date FROM tabela
To avoid problems, just escape the names of columns and tables with backtick
SELECT `id`, `date` FROM `tabela`
For more details: https://stackoverflow.com/questions/2889871/how-do-i-escape-reserved-words-used-as-column-names-mysql-create-table
You can also choose to use a different nomenclature:
SELECT id, date_created FROM tabela
Finally, each decides on the pattern of their own project.
Example, for "Client Addresses" we would normally create a table like "customer_address".
Best known patterns:
- Camelcase.
Example: CustomerAddress
or customerAddress
- Underscore.
Words are connected with an underscore/underscore. Example: customer_address
- Hyphenate.
Words are connected with a hyphen. Example: customer-address
- Uppercase / Lowercase.
A pattern where all letters are written in uppercase or lowercase letters.
Example: CUSTOMER_ADDRESS
, CUSTOMERADDRESS
, CUSTOMER-ADDRESS
, customer_address
, customeraddress
, customer-address
Regarding the name Convention, each one chooses what they like or what is most convenient. Particularly, I always prefer the simplest, portable, etc.
An example of the difficulty you can have in choosing a pattern. The hyphen, for example, you can take a hosting system or host that doesn’t allow you to hyphenate tables and column names. Then you will have a big problem in adapting the codes and, as you will do everything in a hurry, the gambiarra will eat loose. rsrs
The use of uppercase letter may also have some problem, but it is usually easy to solve. An example, with Mysql, we have the configuration lower_case_table_names. It’s very common for an inexperienced person to fall into this problem. In the development environment creates a project where Mysql is with "lower_case_table_names" configured as "case insensitive". And when you install on the production server (online), the hosting is with lower_case_table_names
as "sensitive", which starts to generate several bugs in the functioning of the system. If the bug runs in a hidden way, in a process where no errors are triggered, then you may have a serious problem if it involves financial issues and very important data.
For these and others, particularly prefer a simple standard and that works well in linux, windows, mac, online, offline, anyway. The default is all in lowercase letter and composed words separated by underline. That’s enough to avoid a lot of problems.
Let’s make one more complicated?
If you opt for Camelcase, as would be the default for compound names with words that are abbreviations and usually capitalized?
Example: "HTTP Something". It would look like this
HTTPAlgumaCoisa
or HttpAlgumaCoisa
?
And if you need "XML HTTP Something"?
XMLHTTPAlgumaCoisa
? or XmlHttpAlgumaCoisa
?
And when you come across plural terms?
addresses or address? city or cities? customer or customers?
Finally, they are just basic observations for you to survey the project from the beginning. Thus avoid wasting time in having to modify the documentation with the project in progress because, small breaks like this one go consuming time, dispersing the focus on the business model and, the deadline will shorten.
The structures of the examples are fictitious, with didactic purpose. The focus is not modeling, but rather nomenclature within the context of the question.
Are two answers about sql, worth just take a look. Worst coding standard you’ve Ever had to follow? What was the strangest coding standard Rule that you Were Forced to follow?.
– rray
Take a look at my answer, please can help you.
– lolol