📜  Teradata表类型

📅  最后修改于: 2021-01-11 11:29:31             🧑  作者: Mango

Teradata表类型

Teradata支持以下类型的表来保存临时数据。

S. No. Table Types Description
1 ANSI Temporal ANSI-compliant support for temporal tables. Using temporal tables, Teradata Database can process statements and queries that include time-based reasoning.
Temporal tables record both system time (the time when the information was recorded in the database) and valid time (when the information is in effect or correct in a real-world application).
2 Derived A derived table is a type of temporary table obtained from one or more other tables as the result of a SubQuery.
It is specified in an SQL SELECT statement.
It avoids the need to use the CREATE and DROP TABLE statements for storing retrieved information.
It is useful when we are more sophisticated and complex query codes.
3 Error Logging Error logging tables store the information about errors on an associated permanent table.
And it also stores Log information about insert and updates errors.
4 Global Temporary Global temporary tables are private to the session.
It is dropped automatically at the end of a session.
It has a persistent table definition stored in the Data Dictionary. The saved description may be shared by multiple users and sessions, with each session getting its instance of the table.
5 Global Temporary Trace Global temporary trace tables are store trace output for the length of the session.
It has a persistent table definition stored in the Data Dictionary.
It is useful for debugging SQL stored procedures (via a call to an external stored procedure written to the trace output) and external routines (UDFs, UDMs, and external stored procedures).
6 NoPI NoPI tables are permanent tables that do not have primary indexes defined on them.
They provide a performance advantage when used as staging tables to load data from FastLoad or TPump Array INSERT.
They can have secondary indexes defined on them to avoid full-table scans during row access.
7 Permanent Permanent tables allow different sessions and users to share table content.
8 Queue Queue tables are permanent tables with a timestamp column. The timestamp indicates when each row was inserted into the table.
It is established first-in-first-out (FIFO) ordering of table contents, which is needed for customer applications requiring event processing.
9 Volatile Volatile tables are used only when
  • One session needs the table.
  • The creator needs to access the table.
  • We want better performance than a global temporary table.
  • We do not need the table definition after the session ends.

Note: The definition of a volatile table can survive across a system restart if it is contained in a macro.

派生表

在查询中创建,使用和删除派生表。这些用于在查询中存储中间结果。

考虑以下两个表形式的员工记录。

Emp表:

Employee_Id First_Name Last_Name Department_No
202001 Mike Richard 1
202002 Robert Williams 2
202003 Peter Collin 2
202004 Alexa Stuart 1
202005 Robert Peterson 1

工资表:

Employee_Id First_Name Last_Name Department_No
202001 40,000 4,000 36,000
202002 80,000 6,000 74,000
202003 90,000 7,000 83,000
202004 75,000 5,000 70,000
202005 80,000 00 80,000

以下查询创建一个派生表EmpSal,其中包含薪水高于80,000的员工记录。

SELECT 
Emp.Employee_Id, 
Emp.First_Name, 
EmpSal.NetPay 
FROM 
Emp, 
(select Employee_Id , NetPay 
from Salary
where NetPay >= 80000) EmpSal 
where Emp.Employee_Id = EmpSal.Employee_Id;

执行上述查询后,它返回薪水高于或等于80,000的员工记录。

Employee_Id       First_Name       NetPay
202003            Peter            83,000
202005            Robert           80,000

挥发表

在用户会话中创建并删除易失表。它们的定义未存储在数据字典中。它们保存查询的中间数据,该数据是经常使用的。

句法

CREATE [SET|MULTISET] VOALTILE TABLE tablename 
 
 
ON COMMIT [DELETE|PRESERVE] ROWS


在以下示例中,我们创建一个名为dept_stat的VOLATILE表。

CREATE VOLATILE TABLE dept_stat ( 
   dept_no INTEGER, 
   avg_salary INTEGER, 
   max_salary INTEGER, 
   min_salary INTEGER 
) 
PRIMARY INDEX(dept_no) 
ON COMMIT PRESERVE ROWS;

执行上述代码后,它将根据输出中的部门返回最低,平均和最高薪水。

Dept_no     avg_salary     max_salary     min_salary 
 1           186,000         80,000         36,000
 2           157,000         83,000         74,000

全球临时表

全局临时表的定义存储在数据字典中,并且可以被许多用户和会话使用。但是加载到全局临时表中的数据仅在会话期间保留。

每个会话最多可以实现2000个全局临时表。

句法

以下是全局临时表的语法。

CREATE [SET|MULTISET] GLOBAL TEMPORARY TABLE tablename 

下面的查询创建Global Temporary表,例如:

CREATE SET GLOBAL TEMPORARY TABLE dept_stat ( 
   dept_no INTEGER, 
   avg_salary INTEGER, 
   max_salary INTEGER, 
   min_salary INTEGER 
) 
PRIMARY INDEX(dept_no);

执行上述查询后,将返回以下输出。

A table has been created.