ORACLE_ALIAS
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的更多相关文章
随机推荐
- P3233 [HNOI2014]世界树
传送门 看到指定的总节点数小于等于 300000 就知道要搞虚树了 考虑如何在虚树确定每个议事处控制的节点数量 可以两遍dfs 第一遍求儿子对父亲的影响,第二遍求父亲对儿子影响 注意搜索顺序,这样就可 ...
- express运行www后,在http://localhost:3000/查看返回会报 Cannot find module 'jade'
解决方法:npm install --save express jade
- Randy Pausch’s Last Lecture
he University of Virginia American Studies Program 2002-2003. Randy Pausch ...
- SpringMVC 商城项目
1. 商城视频中有word 笔记文档
- Oracle PLSQL INDEX BY Binary_Integer 测试
[转自] http://blog.chinaunix.net/uid-14669803-id-2921539.html DECLARE TYPE t_list_1 IS TABLE OF VARCHA ...
- 网络那点事之socket队列
1.socket 在内核中是怎么表示的,怎么与文件系统建立关系 2.socket在什么时候创建了接收队列,接收队列的长度是怎么确定的 3.接收到一个数据包根据怎么找到到接收队列的 使用hash算法,分 ...
- 转 .net数据类型
.net 数据类型 short s=0; s = s + 1; 和short s=0; s += 1; 这两个表达式有什么区别,会报什么错误? 有区别吗??大家要想想呢还是要测试一下啊,我选择测试 ...
- Oracle RAC集群删除节点
一,节点环境 [root@node1 ~]# cat /etc/hosts 127.0.0.1 localhost localhost.localdomain localhost4 localhost ...
- Murano Weekly Meeting 2016.08.16
Meeting time: 2016.August.16 1:00~2:00 Chairperson: Kirill Zaitsev, from Mirantis Meeting summary: ...
- JavaScript设计模式(二) - 单例模式
什么是单例模式? 单例模式从字面上的理解是不困难的,js上就是指只有一个对象实例. 为什么需要单例模式? 我们可以将一些成员变量封装在一个单例对象中,每次访问这些变量都只能从这个单例对象进行访问,这样 ...