Db2 简明教程

DB2 - Alias

本章描述了使用数据库对象的别名创建别名和在数据中使用数据库对象的别名。

This chapter describes the creation of alias and retrieving data using alias of database objects.

Introduction

别名是数据库对象的其他名称。它可用于引用数据库对象。您可以说,它是数据库对象的昵称。定义别名是为了使数据库对象的名称简短,从而减小查询大小并增加查询的可读性。

Alias is an alternative name for database objects. It can be used to reference the database object. You can say, it is a nick name for database objects. Alias are defined for the objects to make their name short, thereby reducing the query size and increasing readability of the query.

Creating database object aliases

您可以按如下方式创建数据库对象别名:

You can create database object alias as shown below:

Syntax

Syntax:

db2 create alias <alias_name> for <table_name>

Example : 为“professional.customer”表创建别名名称

Example: Creating alias name for table “professional.customer” table

db2 create alias pro_cust for professional.customer

如果您传递“SELECT * FROM PRO_CUST”或“SELECT * FROM PROFESSIONAL.CUSTOMER”,则数据库服务器将显示相同的结果。

If you pass “SELECT * FROM PRO_CUST” or “SELECT * FROM PROFESSIONAL.CUSTOMER” the database server will show the same result.

Syntax :[直接使用模式名称从表中获取值]

Syntax: [To retrieve values from a table directly with schema name]

db2 select * from <schema_name>.<table_name>

Example :[从 customer 表中获取值]

Example: [To retrieve values from table customer]

db2 select * from professional.customer

Output:

Output:

CUSTID  FULLNAME    PHONE
------- ---------   ------------
100     ravi        9898989
101     krathi      87996659
102     gopal       768678687

  3 record(s) selected.

Retrieving values using alias name of the table

您可以按如下方式使用数据库中的别名来获取值:

You can retrieve values from database using alias name as shown below:

Syntax :[通过调用表的别名从表中获取值]

Syntax: [To retrieve values from table by calling alias name of the table]

db2 select * from <alias_name>

Example :[使用别名从 customer 表中获取值]

Example: [To retrieve values from table customer using alias name]

db2 select * from pro_cust

Output:

Output:

CUSTID  FULLNAME    PHONE
------- ---------   ------------
100     ravi        9898989
101     krathi      87996659
102     gopal       768678687

  3 record(s) selected.