Nate
Nate

Reputation: 28384

Should table names be prefixed with the database name and period when using MySQL/PDO?

The vast majority of queries I've seen have the database name followed by a period before the table name. Example:

SELECT * FROM mydatabase.mytable;

However, this seems to work just as well:

SELECT * FROM mytable;

Is there a reason to have the mydatabase. before each table name?

I am using MySQL via PDO in PHP.

Upvotes: 1

Views: 339

Answers (2)

Kyle
Kyle

Reputation: 1733

If you are only ever going to use one database, then there is no need to define it in every query. The only reason to use mydatabase.mytable is if you are connecting to multiple databases.

Upvotes: 2

Rahul
Rahul

Reputation: 77876

Is there a reason to have the mydatabase. before each table name

Yes, if you are performing a cross database query; where you are actually accessing and joining tables from different database. An example below where db1 and db2 are different databases.

select t1.*,t2.some_column
from db1.table1 t1  
inner join db2.table2 t2 on t1.some_id_column = t2.some_id_column;

But if you are accessing from tables from same database and running the query against that database then no need specifying a fully qualified name (DB_NAME.SCHEMA_NAME.TABLE_NAME)

Upvotes: 3

Related Questions