简体   繁体   中英

Is it good practice to use dots within table names in MySQL

Correct me if I'm wrong, but my understanding is that, in MSSQL, sub-structures of a database like Views, Schemas and Tables can be referenced using object notation such as:

Database.Schema.Table.Column

Each of these objects I believe has their own properties.

I need to replicate the structure of an MSSQL DB in MySQL and I am unsure what is the best practice.

I am thinking about creating tables in MySQL with the following naming convention:

Database
|---SubStructureX.Table
|   |---Column_A
|   |---Column_B
|---SubStructureY.Table
|   |---Column_C
|   |---Column_D
|
|

Therefore a MySQL query could look like this:

SELECT Column_A, Column_B FROM SubStructureX.Table

In short, "SubstructureX.Table" is just a table name that contains a dot. I would be doing this for ease of use during replication of the MSSQL structure. I don't care if the things before and after the dot are not objects in MySQL.

Is this good MySQL practice?

In MySQL? No, I would think that it's not good practice to use periods in table names at all. I would think that it's very bad practice. The dot is the reference operator in SQL. That means if you want to refer to a column using fully qualified notation, you do so like this:

SELECT Table.Column_A ...

Or, with backtick quoting:

SELECT `Table`.`Column_A` ...

Now, imagine if your table is named StructureX.Table . Just like with a space, you've got to quote that to escape it because you don't want MySQL to think the dot is an operator. That means your SQL has to look like this:

SELECT `StructureX.Table`.Column_A ...

Or, with backtick quoting:

SELECT `StructureX.Table`.`Column_A` ...

Doesn't that look like a syntax error to you? Like maybe it's supposed to be like this:

SELECT `StructureX`.`Table`.`Column_A` ...

This would be a nightmare to maintain and as a systems analyst I would hate any application or developer that inflicted this nomenclature on me. It makes me want to claw my eyes out.

Microsoft SQL Server is different because it supports multiple schemas within a single database, while MySQL treats schema as a synonym for database. In MS SQL Server, schemas are collections of objects, and you can use them to organize your tables, or apply security to tables as a group. The default schema is dbo , which is why you see that one listed so often. In MS SQL Server syntax, this:

SELECT [StructureX].[Table].[Column_A] ...

Means within the current database, the schema named StructureX , table named Table , and column name Column_A . MS SQL Server actually supports a four part name , with the fourth part being the database:

SELECT [MyDatabase].[StructureX].[Table].[Column_A] ...

Here, MyDatabase is the database name.

That same style works in MySQL, except you have to remember that schema and database are synonymous. So there, this:

SELECT `StructureX`.`Table`.`Column_A` ...

Would mean database StructureX , table Table , and column Column_A .

I Can say yes:

But instead of using table name, make a table some alias like this,

select a.column1 from yourTable as a

Using table alias is a good practice.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM