SQL Basics: Table Names & Column Names

The objects in a SQL-based database are identified by assigning them unique names. Names are used in SQL statements to identify the database object on which the statement should act. The most fundamental named objects in a relational database are table names (which identify tables), column names (which identify columns), and user names (which identify users of the database); conventions for naming these objects were specified in the original SQL1 standard. The ANSI/ISO SQL2 standard significantly expanded the list of named entities, to include schemas (collections of tables), constraints (restrictions on the contents of tables and their relationships), domains (sets of legal values that may be assigned to a column), and several other types of objects. Many SQL implementations support additional named objects, such as stored procedures, primary key/foreign key relationships, data entry forms, and data replication schemes.

The original ANSI/ISO standard specified that SQL names must contain 1 to 18 characters, must begin with a letter, and may not contain any spaces or special punctuation characters. The SQL2 standard increased the maximum to 128 characters. In practice, the names supported by SQL-based DBMS products vary significantly. It’s common to see tighter restrictions on names that are connected to other software outside of the database (such as user names, which may correspond to login names used by an operating system), and looser restrictions on names that are private to the database. The various products also differ in the special characters they permit in table names. For portability, it’s best to keep names relatively short and to avoid the use of special characters.

1. Table Names

When you specify a table name in a SQL statement, SQL assumes that you are referring to one of your own tables (that is, a table that you created). Usually, you will want to choose table names that are short but descriptive. The table names in the sample database (ORDERS, CUSTOMERS, OFFICES, SALESREPS) are good examples. In a personal or departmental database, the choice of table names is usually up to the database developer or designer.

In a larger, shared-use corporate database, there may be corporate standards for naming tables, to insure that table names do not conflict. In addition, most DBMS brands allow different users to create tables with the same name (that is, both Joe and Sam can create a table named BIRTHDAYS). The DBMS uses the appropriate table, depending on which user is requesting data. With the proper permission, you can also refer to tables owned by other users, by using a qualified table name. A qualified table name specifies both the name of the table’s owner and the name of the table, separated by a period (.). For example, Joe could access the BIRTHDAYS table owned by Sam by using the qualified table name:

SAM.BIRTHDAYS

A qualified table name generally can be used in a SQL statement wherever a table name can appear.

The ANSI/ISO SQL2 standard generalizes the notion of a qualified table name even further. It allows you to create a named collection of tables, called a schema. You can refer to a table in a specific schema using a qualified table name. For example, the BIRTHDAYS table in the EMPLOYEEINFO schema would be referenced as:

EMPLOYEEINFO.BIRTHDAYS

Chapter 13 provides more information about schemas, users, and other aspects of SQL database structure.

2. Column Names

When you specify a column name in a SQL statement, SQL can normally determine from the context which column you intend. However, if the statement involves two columns with the same name from two different tables, you must use a qualified column name to unambiguously identify the column you intend. A qualified column name specifies both the name of the table containing the column and the name of the column, separated by a period (.). For example, the column named SALES in the SALESREPS table has the qualified column name:

SALESREPS.SALES

If the column comes from a table owned by another user, a qualified table name is used in the qualified column name. For example, the BIRTHDATE column in the BIRTHDAYS table owned by the user SAM is specified by the fully qualified column name:

SAM.BIRTHDAYS.BIRTH_DATE

Qualified column names can generally be used in a SQL statement wherever a simple (unqualified) column name can appear; exceptions are noted in the descriptions of the individual SQL statements.

Source: Liang Y. Daniel (2013), Introduction to programming with SQL, Pearson; 3rd edition.

Leave a Reply

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