Loading...
X

What do dots mean in MySQL and MariaDB SQL queries. How to refer from a table to a column in another table

Contents

  1. What do dots mean in MySQL and MariaDB SQL queries
  2. Identifier qualifiers in MySQL and MariaDB
  3. When to qualify a column name and when not to
  4. “ERROR 1046 (3D000): No database selected” / “#1046 - No database selected” even if qualified column names are used
  5. Conclusion
  6. Sources

What do dots mean in MySQL and MariaDB SQL queries

Consider the following SQL query:

SELECT Staff.surname, Staff.position, Salary.wages FROM Staff, Salary WHERE Staff.surname = Salary.surname;

In this query, in places where column names should be located, double names separated by a dot are specified, for example:

  • Staff.surname
  • Staff.position
  • Salary.wages

Dots in column names indicate the location of the element in the database tree. For example, the following names are possible:

  • DATABASE.TABLE.COLUMN
  • TABLE.COLUMN
  • DATABASE.TABLE

That is, in the above SQL query, “Staff.surname” means the column “surname”, which is in the table “Staff”.

Let's look at another example:

SELECT DB_Test_JOIN.Staff.name FROM DB_Test_JOIN.Staff

In this case, the entry “DB_Test_JOIN.Staff.name” means that we are talking about a column named “name” that is in the table “Staff” that is in the database “DB_Test_JOIN”.

Qualifying names with dots that contain table and database names are called qualifiers.

Identifier qualifiers in MySQL and MariaDB

Object names may be unqualified or qualified. An unqualified name is permitted in contexts where interpretation of the name is unambiguous. A qualified name includes at least one qualifier to clarify the interpretive context by overriding a default context or providing missing context.

For example, this statement creates a table using the unqualified name t1:

CREATE TABLE t1 (i INT);

Because t1 includes no qualifier to specify a database, the statement creates the table in the default database. If there is no default database, an error occurs.

This statement creates a table using the qualified name db1.t1:

CREATE TABLE db1.t1 (i INT);

Because db1.t1 includes a database qualifier db1, the statement creates t1 in the database named db1, regardless of the default database. The qualifier must be specified if there is no default database. The qualifier may be specified if there is a default database, to specify a database different from the default, or to make the database explicit if the default is the same as the one specified.

Qualifiers have these characteristics:

  • An unqualified name consists of a single identifier. A qualified name consists of multiple identifiers.

  • The components of a multiple-part name must be separated by period (.) characters. The initial parts of a multiple-part name act as qualifiers that affect the context within which to interpret the final identifier.

  • The qualifier character is a separate token and need not be contiguous with the associated identifiers. For example, tbl_name.col_name and tbl_name . col_name are equivalent.

  • If any components of a multiple-part name require quoting, quote them individually rather than quoting the name as a whole. For example, write `my-table`.`my-column`, not `my-table.my-column`.

  • A reserved word that follows a period in a qualified name must be an identifier, so in that context it need not be quoted.

The permitted qualifiers for object names depend on the object type:

  • A database name is fully qualified and takes no qualifier:

    CREATE DATABASE db1;
  • A table, view, or stored program name may be given a database-name qualifier. Examples of unqualified and qualified names in CREATE statements:

    CREATE TABLE mytable ...;
    CREATE VIEW myview ...;
    CREATE PROCEDURE myproc ...;
    CREATE FUNCTION myfunc ...;
    CREATE EVENT myevent ...;
    
    CREATE TABLE mydb.mytable ...;
    CREATE VIEW mydb.myview ...;
    CREATE PROCEDURE mydb.myproc ...;
    CREATE FUNCTION mydb.myfunc ...;
    CREATE EVENT mydb.myevent ...;
  • A trigger is associated with a table, so any qualifier applies to the table name:

    CREATE TRIGGER mytrigger ... ON mytable ...;
    
    CREATE TRIGGER mytrigger ... ON mydb.mytable ...;
  • A column name may be given multiple qualifiers to indicate context in statements that reference it, as shown in the following table.

    Column Reference Meaning
    col_name Column col_name from whichever table used in the statement contains a column of that name
    tbl_name.col_name Column col_name from table tbl_name of the default database
    db_name.tbl_name.col_name Column col_name from table tbl_name of the database db_name

    In other words, a column name may be given a table-name qualifier, which itself may be given a database-name qualifier. Examples of unqualified and qualified column references in SELECT statements:

    SELECT c1 FROM mytable
    WHERE c2 > 100;
    
    SELECT mytable.c1 FROM mytable
    WHERE mytable.c2 > 100;
    
    SELECT mydb.mytable.c1 FROM mydb.mytable
    WHERE mydb.mytable.c2 > 100;

You need not specify a qualifier for an object reference in a statement unless the unqualified reference is ambiguous. Suppose that column c1 occurs only in table t1c2 only in t2, and c in both t1 and t2. Any unqualified reference to c is ambiguous in a statement that refers to both tables and must be qualified as t1.c or t2.c to indicate which table you mean:

SELECT c1, c2, t1.c FROM t1 INNER JOIN t2
WHERE t2.c > 100;

Similarly, to retrieve from a table t in database db1 and from a table t in database db2 in the same statement, you must qualify the table references: For references to columns in those tables, qualifiers are required only for column names that appear in both tables. Suppose that column c1 occurs only in table db1.tc2 only in db2.t, and c in both db1.t and db2.t. In this case, c is ambiguous and must be qualified but c1 and c2 need not be:

SELECT c1, c2, t1.c FROM t1 INNER JOIN t2
WHERE t2.c > 100;

Table aliases enable qualified column references to be written more simply:

SELECT c1, c2, db1.t.c FROM db1.t INNER JOIN db2.t
WHERE db2.t.c > 100;

You can refer to a table within the default database as tbl_name, or as db_name.tbl_name to specify a database explicitly. You can refer to a column as col_nametbl_name.col_name, or db_name.tbl_name.col_name. You need not specify a tbl_name or db_name.tbl_name prefix for a column reference unless the reference would be ambiguous.

When to qualify a column name and when not to

1) When no default database is selected

Consider the following example:

SELECT DB_Test_JOIN.Staff.name FROM DB_Test_JOIN.Staff;

As we already know, the entry DB_Test_JOIN.Staff.name means a column named “name” which is in the table “Staff” which is in the database “DB_Test_JOIN”.

Using unqualified names, this SQL query can be written as follows:

SELECT name FROM Staff;

Are these two SQL queries equivalent? In certain situations, these queries can produce the same result, but they are not identical.

For example, if you select the default database, such as with the following SQL query:

USE DB_Test_JOIN;

Then both queries will produce the same result.

Note: in phpMyAdmin, the default database is the database selected in the web interface:

Similarly, in the command line when using the mysql/mariadb utility to work with databases:

But if the database is not pre-selected, then instead of the result we will get an error:

#1046 - No database selected

Using qualified names will give a result without an error:

SELECT DB_Test_JOIN.Staff.name FROM DB_Test_JOIN.Staff;

Similarly, in the command line when using the mysql or mariadb utility to work with databases. Using unqualified names without selecting a default database will result in an error:

ERROR 1046 (3D000): No database selected

And using qualified names even without selecting a default database will not result in an error:

2) When working with columns from different tables or databases in a single SELECT statement

If you need to combine and process data from different tables or even databases in a single SQL query, then in this case you need to use qualified names, for example:

SELECT Staff.surname, Staff.position, Salary.wages FROM Staff, Salary WHERE Staff.surname = Salary.surname;

3) In JOIN constructs

Since JOIN statements usually query two or more tables, then in this case you also need to specify qualified names, for example:

SELECT Staff.surname, Staff.position, Salary.wages FROM Staff JOIN Salary ON Staff.surname = Salary.surname;

“ERROR 1046 (3D000): No database selected” / “#1046 - No database selected” even if qualified column names are used

Consider the following example:

SELECT `DB_Test_JOIN.Staff.name` FROM `DB_Test_JOIN.Staff`;

Although the database is not selected, qualified names with databases are used, but an error occurs:

#1046 - No database selected

The reason for the error is that if you need to put names (columns, tables, databases) in quotation marks (`), then you need to put each name in quotation marks individually. That is, it should be correct like this:

SELECT `DB_Test_JOIN`.`Staff`.`name` FROM `DB_Test_JOIN`.`Staff`;

Note: You can omit the quotes entirely in this example, since these Schema Object Names do not use special characters or reserved words. This SQL query is written in this form for educational purposes only.

Conclusion

So, qualified names should be used when a single query accesses two (or more) tables or databases at once, and also when the default database is not selected.

In other cases (when it is obviously clear which column in which table is meant), qualified names should not be used.

Sources:


Leave Your Observation

Your email address will not be published. Required fields are marked *