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. Oracle PL/SQL之GROUP BY GROUPING SETS

    [转自] http://blog.csdn.net/t0nsha/article/details/6538838 使用GROUP BY GROUPING SETS相当于把需要GROUP的集合用UNIO ...

  2. 什么是javascript的中间件?

    第一次写博客,有点想在博客园试水的感觉,也分享下觉得有用的东西(源码自己写的) 什么是javascript中间件呢?函数middle就是用来构建中间件的,我用例子说明下 下面我定义了一个函数use,在 ...

  3. nfs 问题总结

    1. [root@backup read]# touch r01.txt   touch: cannot touch `r01.txt': Stale file handle   使用共享目录创建文件 ...

  4. C++的一些编程规范

    新规范的目标: 让代码排错更加简单 程序员专心于业务逻辑 将一些错误交给编译器处理 提高代码可维护性 逐步实现插件化 编码 使用array(QT下用QVarLengthArray)代替和vector代 ...

  5. C#知识点提要

    本篇博文主要对asp.net mvc开发需要撑握的C#语言知识点进行简单回顾,尤其是C# 3.0才有的一些C#语言特性.对于正在学asp.net mvc的童鞋,不防花个几分钟浏览一下.本文要回顾的C# ...

  6. cloudermanger安装时需要安装或彻底正确卸载再安装orcal-java7-installer、oracle-java7-set-default(ubuntu14.04版本)(图文详解)

    不多说,直接上干货! 安装orcal-java7-installer和oracle-java7-set-default 安装JDK1.7 (所有节点)CDH要求至少是Oracle JDK7,Ubunt ...

  7. zookeeper JAVA API 简单操作

    package org.admln.program.Zoo_Test; import java.io.IOException; import java.security.NoSuchAlgorithm ...

  8. Coursera 机器学习 第8章(上) Unsupervised Learning 学习笔记

    8 Unsupervised Learning8.1 Clustering8.1.1 Unsupervised Learning: Introduction集群(聚类)的概念.什么是无监督学习:对于无 ...

  9. bzoj 4543: [POI2014]Hotel加强版

    Description 给出一棵树求三元组 \((x,y,z)\,,x<y<z\) 满足三个点两两之间距离相等,求三元组的数量 Solution 考虑暴力 \(DP\) 设 \(f[i][ ...

  10. 关于两个 IQueryable 合并

    原先根据需求要对数据进行两种筛选,起初写过滤条件,但是过滤后发现有的数据重叠.因此改为查询两次. 因为查询后返回的是两个相同的.匿名的 IQueryable ,最终的目的是想两个 类型结合成一个. 参 ...