Benefits of using database aliases

Database aliases are temporary names used in query statements to hide real database table or column names. It makes queries more concise and readable, and prevents disclosure of sensitive information. Database aliases can be prefixed or suffixed with underscores, dollar signs, numbers, etc., but cannot start or end with a number.

SQL supports the use of aliases to assign temporary names to tables or columns. Aliases not only reduce typing, but also make queries easier to read and understand. In today's article, we will learn how to   use aliases in queries using Navicat Premium 16.2 .

Overview of SQL aliases

As mentioned in the introduction, both table and column names can be aliased. The syntax for each alias is as follows:

Column alias syntax

SELECT 
  column_name [AS] alias_name,
  column_name AS 'Alias Name' -- for names with spaces
FROM table_name;

table alias syntax

SELECT column_name(s)
FROM table_name [AS] alias_name;

There are two things to consider about using aliases:

  • Aliases are usually specified using the AS keyword, but can optionally be omitted.
  • The alias only exists for that query time.

Table aliases in JOIN queries

Here is a query against the Sakila sample database to get information about all copies of a particular movie:

SELECT *
FROM film f 
  INNER JOIN inventory i ON i.film_id = f.film_id
WHERE i.store_id = 1 AND f.title = "Academy Dinosaur";

In the above query, since both the film and inventory tables contain a film_id column, the name must be fully qualified, ie, the column name preceded by the table name. In this case, aliases can be used to shorten the statement.

Following are the queries and their results in Navicat:

film_query (80K)

column alias

When it comes to column names, abbreviations are often used when designing database tables to simplify column names. For example:

  • "so_no" 代表 "sales order number"。
  • "qty" 代表 "quantity"。

In such cases, column aliases can be used to make the column content more intuitive. Here is an example:

SELECT
	inv_no AS invoice_no,
	amount,
	due_date AS 'Due date',
	cust_no 'Customer No'
FROM
	invoices;

Of course, you can also specify a column alias for the expression, as shown in the following figure:

expression_alias (113K)

The above query selects the current price of the product and the price after the price increase.

Column alias restrictions

Since column aliases are specified in the SELECT clause, you can only reference column aliases in clauses that are evaluated after the SELECT clause. Therefore, you cannot use aliases in the WHERE clause, otherwise an error will be raised:

alias_error (42K)

This is because the database evaluates the WHERE clause before evaluating the SELECT clause. Therefore, when evaluating the WHERE clause, the database has no information about the NewPrice column alias.

However, it is okay to use a column alias in the ORDER BY clause because it is evaluated after the SELECT clause:

alias_in_order_by (113K)

The database evaluates the clauses of a query in the following order:

FROM > SELECT > ORDER BY

Table aliases with Navicat

In Navicat, once a table alias is defined, it will appear in the autocompletion list.

alias_in_navicat (65K)

Therefore, using an alias is more time-saving and convenient!

Conclusion on using database aliases

In today's article, we learned how to   use aliases in queries using Navicat Premium 16.2 . It's an easy way to make queries more readable and understandable, which is important because code is not just for execution, it's a communication mechanism.

 

Past review 

  1. Navicat now officially supports Redis
  2. Navicat 16 supports OceanBase full-line database
  3. Recruit Navicat Monitor 3.0 monitoring tool experience officer |
  4. Nanny level tutorial | Navicat manual backup and automatic backup
  5. Navicat was selected into the "China Database Industry Map" released by the China Academy of Information and Communications Technology in 2023
  6. Navicat's 20-year development history | Founded in Hong Kong, China in 1999
  7. The interactive gift event is in progress | The prize is Navicat Premium worth 819 yuan
  8. Fake websites cause multiple security risks | Official solemn statement: Do not buy or download Navicat software from unofficial channels

Guess you like

Origin blog.csdn.net/weixin_53935287/article/details/131847850