Is it good practice to use dots within table names in MySQL - 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.

Related

What's the difference between `use my_db` and `my_db.my_table` in mysql?

We have lots of database in one instance.
Our query language is:
use db1; select * from table1;
use db2; select * from table2;
But when we change to this style, the MySQL server CPU load dropped a lot:
select * from db1.table1;
select * from db2.table2;
What's the different between them?
Which one should we use?
MySQL can work with multiple databases in a single query. Each table belongs to a single database. Its full name is: database.table.
If you are sticking to a single database, then you probably don’t want to work with the database prefix. USE database allows you to dispense with the prefix.
If you are using multiple databases simultaneously, then you’re never going to be in the right database. At the very lease, you will need to prefix the other database tables.
I don’t know how much work MySQL does when you apply the USE database statement, but I imagine that it’s not very important in the overall scheme of things. Under normal circumstances you are working with a single database so issuing the USE statement once should be the end of it.
Certainly if you’re constantly switching between the two, and especially if you’re joining tables from different databases, then you should prefix the tables.

Is it important to write database name before table name?

I have a database named DELTASTORE in mysql in my cpanel. There are tables like ADMIN,CATAGORY,PRODUCT,ORDER. I have inserted some values in each table.
If I run sql
SELECT * FROM ADMIN
it works nicely.
But if I run sql
SELECT * FROM ORDER
it doesn't work! Instead of that, if I run sql
SELECT * FROM DELTASTORE.ORDER
then it works correctly.
Why does that occur?
Is it important to write database name before table name and give a dot between them all the time?
To leave out the database prefix, you have to set a default database, with
USE databasename
When writing programs to access the database, the API provides a way to do this. For instance, in PHP PDO you specify the default database in the DSN:
mysql:host=hostname;dbname=defaultDB
In MySQLi it's an argument to mysqli_connect(). In the obsolete mysql extension you use mysql_use_database(). There are similar methods in other programming languages.
Additionally, since ORDER is a MySQL keyword, you either have to put it in backticks:
SELECT * FROM `ORDER`
or prefix it with a database:
SELECT * FROM DELTASTORE.ORDER
It's usually best to avoid using MySQL reserved words as table or column names, to prevent problems like this. See Syntax error due to using a reserved word as a table or column name in MySQL
The reason is, that ORDER is a SQL keyword (for ORDER BY, which you use to sort the result lines). So with the database name (or schematic name) you mean the table ORDER.
It's better to not use keywords for the table.
Just need to put brackets.
Select * from `ORDER`

selecting multiple tables in mysql and combine the results [duplicate]

The table names in my mysql database are dynamically generated. Is there some way to select data from tables which have a name matching a pattern? I guess it will look like:
select * from 'table_id_%'
No, you can't do that with MySQL. Tables in a query can't be dynamically specified - you have to build the list in your application (or do several single-table queries).
You can use INFORMATION_SCHEMA TABLES table to find tables you want, here is documentation: http://dev.mysql.com/doc/refman/5.0/en/tables-table.html . TABLES table has column NAME which represents names of tables. After finding table names you can run any sql queries you like.
That's not possible in the way you'd like to do it. However you could probably use prepared statements which are basically query-templates where you specificy the parameters (AFAIK also table names) that get replaced depending on your needs without copy and pasting the same query over and over again for different tables.

How to accomplish "MySQL cross database reference" with PostgreSQL

We will migrate the database from mysql to postgresql in our product(through java). So we need to change the mysql query to postgresql query in java application. How to create the table i.e., databasename.tablename in postgresql.
For mysql, we can directly create the table e.g create table information.employee.
Here database name is "information" and table name is "employee" . Is it possible to achieve same query in postgresql.
I searched google it says cross database reference is not possible. Please help me.
I saw pg_class table it contains the table names in the specific database, like wise databse and tables relationships are stored in any other table.
This is normally done using schemas rather than databases, which is more or less like how MySQL organizes it anyway.
Instead of
create database xyz
use
create schema xyz
When you create tables, create them:
create table xyz.myTable
you will need to update your search path to see them on the psql command line tool, or if you want to query them without using the schema explicitly. The default schema is public, so when you create a table without a schema name, it ends up in public. If you modify your search_path as below, the default schema becomes the first in the list: xyz.
set search_path=xyz,public,pg_catalog;
and you must not have spaces in that statement. You can do it globally for a user/role too:
alter role webuser set search_path=xyz,public,pg_catalog;
Also, don't forget that postgresql string matches are case sensitive by default (this one catches people out a lot).
If you want to have different physical locations for the files for each schema, you can do that with tablespaces. If you have a look at the postgresql documentation page, they have info on how to do it, it's pretty easy.
database in MySQL == schema in PostgreSQL. So you will most probably want to migrate all your mysql dbs into one postgres db. Then you will be able to do "cross-database" queries.
See my answer to this question: Relationship between catalog, schema, user, and database instance

Use of wildcards in mysql table name

The table names in my mysql database are dynamically generated. Is there some way to select data from tables which have a name matching a pattern? I guess it will look like:
select * from 'table_id_%'
No, you can't do that with MySQL. Tables in a query can't be dynamically specified - you have to build the list in your application (or do several single-table queries).
You can use INFORMATION_SCHEMA TABLES table to find tables you want, here is documentation: http://dev.mysql.com/doc/refman/5.0/en/tables-table.html . TABLES table has column NAME which represents names of tables. After finding table names you can run any sql queries you like.
That's not possible in the way you'd like to do it. However you could probably use prepared statements which are basically query-templates where you specificy the parameters (AFAIK also table names) that get replaced depending on your needs without copy and pasting the same query over and over again for different tables.