Oracle / PLSQL: ALIASES

website:https://www.techonthenet.com/oracle/alias.php

This Oracle tutorial explains how to use Oracle ALIASES (temporary names for columns or tables) with syntax and examples.

Description

Oracle ALIASES can be used to create a temporary name for columns or tables.

  • COLUMN ALIASES are used to make column headings in your result set easier to read.
  • TABLE ALIASES are used to shorten your SQL to make it easier to read or when you are performing a self join (ie: listing the same table more than once in the FROM clause(列出相同的表不止一次在那个from从句)).

Syntax

The syntax to ALIAS A COLUMN in Oracle/PLSQL is:

column_name AS alias_name

OR

The syntax to ALIAS A TABLE in Oracle/PLSQL is:

table_name alias_name

Parameters or Arguments

column_name
The original name of the column that you wish to alias.
table_name
The original name of the table that you wish to alias.
alias_name
The temporary name to assign.

Note

  • If the alias_name contains spaces, you must enclose(包围) the alias_name in quotes(双引号).
  • if the alias_name contains number,you must enclose then alias_name in quotes.(myself addtion)
  • It is acceptable to use spaces when you are aliasing a column name. However, it is not generally good practice to use spaces when you are aliasing a table name.
  • The alias_name is only valid within the scope of the SQL statement.

Example - ALIAS a column

Generally, aliases are used to make the column headings in your result set easier to read. For example, when concatenating fields together(连接字段), you might alias the result.

For example:

SELECT contact_id, first_name || last_name AS NAME
FROM contacts
WHERE last_name = 'Anderson';

In this example, we've aliased the second column (ie: first_name and last_name concatenated) as NAME. As a result, NAME will display as the heading for the second column when the result set is returned. Because our alias_name did not include any spaces, we are not required to enclose the alias_name in quotes.

However, it would have been perfectly acceptable to write this example using quotes as follows:

SELECT contact_id, first_name || last_name AS "NAME"
FROM contacts
WHERE last_name = 'Anderson';


Next, let's look at an example where we are required to enclose the alias_name in quotes.

For example:

SELECT contact_id, first_name || last_name AS "CONTACT NAME"
FROM contacts
WHERE last_name = 'Anderson';

In this example, we've aliased the second column (ie: first_name and last_name concatenated) as "CONTACT NAME". Since there are spaces in this alias_name, "CONTACT NAME" must be enclosed in quotes.

Example - ALIAS a Table

When you create an alias on a table, it is either(任何一个) because you plan to list the same table name more than once in the FROM clause (ie: self join), or you want to shorten the table name to make the SQL statement shorter and easier to read.

Let's look at an example of how to alias a table name in Oracle/PLSQL.

For example:

SELECT p.product_id, p.product_name, categories.category_name
FROM products p
INNER JOIN categories
ON p.category_id = categories.category_id
ORDER BY p.product_name ASC, categories.category_name ASC;

In this example, we've created an alias for the products table called p. Now within this SQL statement, we can refer to the products table as p.

When creating table aliases, it is not necessary to create aliases for all of the tables listed in the FROM clause. You can choose to create aliases on any or all of the tables.

For example, we could modify our example above and create an alias for the categories table as well.

SELECT p.product_id, p.product_name, c.category_name
FROM products p
INNER JOIN categories c
ON p.category_id = c.category_id
ORDER BY p.product_name ASC, c.category_name ASC;

Now we have an alias for categories table called c as well as the alias for the products table called p.

ORACLE_ALIAS的更多相关文章

随机推荐

  1. Codeforces 1136D Nastya Is Buying Lunch (贪心)

    题意: 给一个序列和一组交换序列(a,b),当且仅当a在b的前面(不允许有间隔),这两个数才能交换,问最后一个数最多能移动多少个位置. 分析: 这题是思路是十分的巧妙呀 , 用一个数组num[x]  ...

  2. WordPress 有关Https的设置

    开头卖萌求点击 https://www.yinghualuowu.com/ Http和Https的区别 就是多了s的区别(不是),简单点就是比http更安全了.23333.这里不打算说的太详细,知道前 ...

  3. vm中centos7磁盘扩容

      在VM虚拟平台管理客户端,将虚拟机关机后,将分配的磁盘大小30G扩至300G.如图.   调整完后,重新打开虚拟机,使用fdisk -l查看,可以看到我们刚刚扩容的空间已经可以看到,但没有分区,还 ...

  4. PHP sprintf() 函数

    PHP sprintf() 函数 先说下为什么要写这个函数的前言,这个是我在微信二次开发的一个token验证文档也就是示例文档看到的一个函数,当时非常不理解,于是查了百度,但是很多结果都很笼统,结果也 ...

  5. hybrid app开发工具

    hybrid app开发工具 1.AppCan AppCan是国内Hybrid App混合模式开发的倡导者,AppCan应用引擎支持Hybrid App的开发和运行.并且着重解决了基于HTML5的移动 ...

  6. n皇后问题--DFS输出棋盘

    N皇后问题 Problem Description 在N*N的方格棋盘放置了N个皇后,使得它们不相互攻击(即任意2个皇后不允许处在同一排,同一列,也不允许处在与棋盘边框成45角的斜线上.你的任务是,对 ...

  7. 【研究】Discuz<3.4任意文件删除漏洞

    这里以Discuz3.2为例 关键字:Powered by Discuz! X3.2 时间有限,就不一一截图了,Discuz所有页面全在Discuz_X3.2_SC_UTF8/upload/目录下 利 ...

  8. Linux下jenkins改端口、解决内存溢出、版本升级

    1.新版本的jenkins修改端口新版本jenkins的配置文件在/etc/sysconfig/jenkinsvi /etc/sysconfig/jenkins找到JENKINS_PORT=" ...

  9. GreenPlum 大数据平台--基础使用(一)

    一,操作语法 01,创建数据库 --创建用户-- [gpadmin@greenplum01 ~]$ export PGDATABASE=testDB --指定数据库名字 [gpadmin@greenp ...

  10. crontab -e

    crontab -e可以配置定时任务 0 */3 * * * cd /root/find && nohup qbittorrent-nox --webui-port=7070 & ...