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. loj 2038 / 洛谷 P4345 [SHOI2015] 超能粒子炮・改 题解

    好玩的推式子 题目描述 曾经发明了脑洞治疗仪与超能粒子炮的发明家 SHTSC 又公开了他的新发明:超能粒子炮・改--一种可以发射威力更加强大的粒子流的神秘装置. 超能粒子炮・改相比超能粒子炮,在威力上 ...

  2. Codeforces - 912B 位运算

    求[1,n]内k的值的异或和使其值最大 k=1是肯定是n k>1,设pos是n的最高位,那答案就是(1ll<<(pos+1))-1 这里用到一个性质是当S=2^i-1时,a xor ...

  3. python爬虫之趟雷

    python爬虫之趟雷整理 雷一:URLError 问题具体描述:urllib.error.URLError: <urlopen error [Errno 11004] getaddrinfo ...

  4. centos7升级firefox的flash插件

    1. 在https://get.adobe.com/flashplayer/下载文件.选择.tar.gz. 2. 下载的文件名为flash_player_npapi_linux.x86_64.tar. ...

  5. linux 第八章 高级键盘

    1.clear:清屏 2.history:显示历史命令列表 3.Ctrl+A:移动光标到行首 4.Ctrl+E:移动光标到行尾 5.Ctrl+F:光标向前移动一个字符 6.Ctrl+B:光标向h后移动 ...

  6. 笔记_JSON

    解析 JSON 步骤 如果没有自带 , 就添加 第三方包 (JavaScript编程语言本身自带解析JSON的能力) 一般是要手写 : 实体类 JSON -> 实体类  中间映射 Gson的话 ...

  7. linux-lnmp 搭建报错

    一, 大概就是 没有php-nysql拓展 yum -y install php-mysql 二, 编辑php.ini php.ini中 添加extension=mysql.so PHP.ini 中找 ...

  8. (转)10 分钟内快速构建能够承载海量数据的 nginx 日志分析与报警平台

    10 分钟内快速构建能够承载海量数据的 nginx 日志分析与报警平台 原文:https://blog.qiniu.com/archives/8713

  9. 详解http之post

    详解http之post 首先,我们先看看jquery中的post方法的使用: $.ajax({ url:'api/bbg/goods/get_goods_list_wechat', data:{ , ...

  10. 九度oj题目1027:欧拉回路

    题目1027:欧拉回路 时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:2844 解决:1432 题目描述:     欧拉回路是指不令笔离开纸面,可画过图中每条边仅一次,且可以回到起点的一条 ...