<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
  <channel>
    <title>OpenText Analytics Database 26.2.x – Managing table columns</title>
    <link>/en/admin/working-with-native-tables/managing-table-columns/</link>
    <description>Recent content in Managing table columns on OpenText Analytics Database 26.2.x</description>
    <generator>Hugo -- gohugo.io</generator>
    
	  <atom:link href="/en/admin/working-with-native-tables/managing-table-columns/index.xml" rel="self" type="application/rss+xml" />
    
    
      
        
      
    
    
    <item>
      <title>Admin: Renaming columns</title>
      <link>/en/admin/working-with-native-tables/managing-table-columns/renaming-columns/</link>
      <pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate>
      
      <guid>/en/admin/working-with-native-tables/managing-table-columns/renaming-columns/</guid>
      <description>
        
        
        &lt;p&gt;You rename a column with &lt;code&gt;ALTER TABLE&lt;/code&gt; as follows:&lt;/p&gt;
&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;ALTER TABLE [&lt;span class=&#34;code-variable&#34;&gt;schema.&lt;/span&gt;]&lt;span class=&#34;code-variable&#34;&gt;table-name&lt;/span&gt;  RENAME [ COLUMN ] &lt;span class=&#34;code-variable&#34;&gt;column-name&lt;/span&gt; TO &lt;span class=&#34;code-variable&#34;&gt;new-column-name&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;The following example renames a column in the &lt;code&gt;Retail.Product_Dimension&lt;/code&gt; table from &lt;code&gt;Product_description&lt;/code&gt; to &lt;code&gt;Item_description&lt;/code&gt;:&lt;/p&gt;
&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;=&amp;gt; ALTER TABLE Retail.Product_Dimension
    RENAME COLUMN Product_description TO Item_description;
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;If you rename a column that is referenced by a view, the column does not appear in the result set of the view even if the view uses the wild card (*) to represent all columns in the table. Recreate the view to incorporate the column&#39;s new name.&lt;/p&gt;

      </description>
    </item>
    
    <item>
      <title>Admin: Changing scalar column data type</title>
      <link>/en/admin/working-with-native-tables/managing-table-columns/changing-scalar-column-data-type/</link>
      <pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate>
      
      <guid>/en/admin/working-with-native-tables/managing-table-columns/changing-scalar-column-data-type/</guid>
      <description>
        
        
        &lt;p&gt;In general, you can change a column&#39;s data type with &lt;a href=&#34;../../../../en/sql-reference/statements/alter-statements/alter-table/#&#34;&gt;ALTER TABLE&lt;/a&gt; if doing so does not require storage reorganization. After you modify a column&#39;s data type, data that you load conforms to the new definition.&lt;/p&gt;
&lt;p&gt;The sections that follow describe requirements and restrictions associated with changing a column with a scalar (primitive) data type. For information on modifying complex type columns, see &lt;a href=&#34;../../../../en/admin/working-with-native-tables/managing-table-columns/adding-new-field-to-complex-type-column/#&#34;&gt;Adding a new field to a complex type column&lt;/a&gt;.&lt;/p&gt;
&lt;h2 id=&#34;supported-data-type-conversions&#34;&gt;Supported data type conversions&lt;/h2&gt;
&lt;p&gt;OpenText™ Analytics Database supports conversion for the following data types:

&lt;table class=&#34;table table-bordered&#34; &gt;



&lt;tr&gt; 

&lt;th &gt;
Data Types&lt;/th&gt; 

&lt;th &gt;
Supported Conversions&lt;/th&gt;&lt;/tr&gt;

&lt;tr&gt; 

&lt;td &gt;
Binary&lt;/td&gt; 

&lt;td &gt;
Expansion and contraction.&lt;/td&gt;&lt;/tr&gt;

&lt;tr&gt; 

&lt;td &gt;
Character&lt;/td&gt; 

&lt;td &gt;
All conversions between CHAR, VARCHAR, and LONG VARCHAR.&lt;/td&gt;&lt;/tr&gt;

&lt;tr&gt; 

&lt;td &gt;
Exact numeric&lt;/td&gt; 

&lt;td &gt;




&lt;p&gt;All conversions between the following &lt;a href=&#34;../../../../en/sql-reference/data-types/numeric-data-types/&#34;&gt;numeric&lt;/a&gt; data types: &lt;a href=&#34;../../../../en/sql-reference/data-types/numeric-data-types/integer/&#34;&gt;integer&lt;/a&gt; data types—INTEGER, INT, BIGINT, TINYINT, INT8, SMALLINT—and &lt;a href=&#34;../../../../en/sql-reference/data-types/numeric-data-types/numeric/&#34;&gt;NUMERIC&lt;/a&gt; values of scale &amp;lt;=18 and precision 0.&lt;/p&gt;
&lt;p&gt;You cannot modify the scale of NUMERIC data types; however, you can change precision in the ranges (0-18), (19-37), and so on.&lt;/p&gt;
&lt;/td&gt;&lt;/tr&gt;

&lt;tr&gt; 

&lt;td &gt;
Collection&lt;/td&gt; 

&lt;td &gt;








&lt;p&gt;The following conversions are supported:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Collection of one element type to collection of another element type, if the source element type can be coerced to the target element type.&lt;/li&gt;
&lt;li&gt;Between arrays and sets.&lt;/li&gt;
&lt;li&gt;Collection type to the same type (array to array or set to set), to change bounds or binary size.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;For details, see &lt;a href=&#34;../../../../en/admin/working-with-native-tables/managing-table-columns/changing-scalar-column-data-type/changing-column-width/#Changing&#34;&gt;Changing Collection Columns&lt;/a&gt;.&lt;/p&gt;
&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;
&lt;/p&gt;
&lt;h2 id=&#34;unsupported-data-type-conversions&#34;&gt;Unsupported data type conversions&lt;/h2&gt;
&lt;p&gt;The database does not allow data type conversion on types that require storage reorganization:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;Boolean&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;DATE/TIME&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Approximate numeric type&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;BINARY to VARBINARY and vice versa&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;You also cannot change a column&#39;s data type if the column is one of the following:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;Primary key&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Foreign key&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Included in the SEGMENTED BY clause of any projection for that table.&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;You can work around some of these restrictions. For details, see &lt;a href=&#34;../../../../en/admin/working-with-native-tables/managing-table-columns/changing-scalar-column-data-type/working-with-column-data-conversions/#&#34;&gt;Working with column data conversions&lt;/a&gt;.&lt;/p&gt;

      </description>
    </item>
    
    <item>
      <title>Admin: Adding a new field to a complex type column</title>
      <link>/en/admin/working-with-native-tables/managing-table-columns/adding-new-field-to-complex-type-column/</link>
      <pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate>
      
      <guid>/en/admin/working-with-native-tables/managing-table-columns/adding-new-field-to-complex-type-column/</guid>
      <description>
        
        
        &lt;p&gt;You can add new fields to columns of complex types (any combination or nesting of arrays and structs) in native tables. To add a field to an existing table&#39;s column, use a single &lt;a href=&#34;../../../../en/sql-reference/statements/alter-statements/alter-table/#&#34;&gt;ALTER TABLE&lt;/a&gt; statement.&lt;/p&gt;
&lt;h2 id=&#34;requirements-and-restrictions&#34;&gt;Requirements and restrictions&lt;/h2&gt;
&lt;p&gt;The following are requirements and restrictions associated with adding a new field to a complex type column:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;New fields can only be added to rows/structs.&lt;/li&gt;
&lt;li&gt;The new type definition must contain all of the existing fields in the complex type column. Dropping existing fields from the complex type is not allowed. All of the existing fields in the new type must exactly match their definitions in the old type.This requirement also means that existing fields cannot be renamed.&lt;/li&gt;
&lt;li&gt;New fields can only be added to columns of native (non-external) tables.&lt;/li&gt;
&lt;li&gt;New fields can be added at any level within a nested complex type. For example, if you have a column defined as &lt;code&gt;ROW(id INT, name ROW(given_name VARCHAR(20), family_name VARCHAR(20))&lt;/code&gt;, you can add a middle_name field to the nested ROW.&lt;/li&gt;
&lt;li&gt;New fields can be of any type, either complex or primitive.&lt;/li&gt;
&lt;li&gt;Blank field names are not allowed when adding new fields. Note that blank field names in complex type columns are allowed when creating the table. The database automatically assigns a name to each unnamed field.&lt;/li&gt;
&lt;li&gt;If you change the ordering of existing fields using ALTER TABLE, the change affects existing data in addition to new data. This means it is possible to reorder existing fields.&lt;/li&gt;
&lt;li&gt;When you call ALTER COLUMN ... SET DATA TYPE to add a field to a complex type column, the database will place an O lock on the table preventing DELETE, UPDATE, INSERT, and COPY statements from accessing the table and blocking SELECT statements issued at SERIALIZABLE isolation level, until the operation completes.&lt;/li&gt;
&lt;li&gt;Performance is slower when adding a field to an array element than when adding a field to an element not nested in an array.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&#34;examples&#34;&gt;Examples&lt;/h2&gt;
&lt;h3 id=&#34;adding-a-field&#34;&gt;Adding a field&lt;/h3&gt;
&lt;p&gt;Consider a company storing customer data:&lt;/p&gt;
&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;=&amp;gt; CREATE TABLE customers(id INT, name VARCHAR, address ROW(street VARCHAR, city VARCHAR, zip INT));
CREATE TABLE
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;The company has just decided to expand internationally, so now needs to add a country field:&lt;/p&gt;
&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;=&amp;gt; ALTER TABLE customers ALTER COLUMN address
SET DATA TYPE ROW(street VARCHAR, city VARCHAR, zip INT, country VARCHAR);
ALTER TABLE
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;You can view the table definition to confirm the change:&lt;/p&gt;
&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;
=&amp;gt; \d customers
List of Fields by Tables
 Schema |   Table   | Column  |                                 Type                                 | Size | Default | Not Null | Primary Key | Foreign Key
--------+-----------+---------+----------------------------------------------------------------------+------+---------+----------+-------------+-------------
 public | customers | id      | int                                                                  |    8 |         | f        | f           |
 public | customers | name    | varchar(80)                                                          |   80 |         | f        | f           |
 public | customers | address | ROW(street varchar(80),city varchar(80),zip int,country varchar(80)) |   -1 |         | f        | f           |
 (3 rows)
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;You can also see that the country field remains null for existing customers:&lt;/p&gt;
&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;=&amp;gt; SELECT * FROM customers;
 id | name |                                    address
----+------+--------------------------------------------------------------------------------
  1 | mina | {&amp;#34;street&amp;#34;:&amp;#34;1 allegheny square east&amp;#34;,&amp;#34;city&amp;#34;:&amp;#34;hamden&amp;#34;,&amp;#34;zip&amp;#34;:6518,&amp;#34;country&amp;#34;:null}
 (1 row)
&lt;/code&gt;&lt;/pre&gt;&lt;h3 id=&#34;common-error-messages&#34;&gt;Common error messages&lt;/h3&gt;
&lt;p&gt;While you can add one or more fields with a single ALTER TABLE statement, existing fields cannot be removed. The following example throws an error because the city field is missing:&lt;/p&gt;
&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;=&amp;gt; ALTER TABLE customers ALTER COLUMN address SET DATA TYPE ROW(street VARCHAR, state VARCHAR, zip INT, country VARCHAR);
ROLLBACK 2377:  Cannot convert column &amp;#34;address&amp;#34; from &amp;#34;ROW(varchar(80),varchar(80),int,varchar(80))&amp;#34; to type &amp;#34;ROW(varchar(80),varchar(80),int,varchar(80))&amp;#34;
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;Similarly, you cannot alter the type of an existing field. The following example will throw an error because the zip field&#39;s type cannot be altered:&lt;/p&gt;
&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;=&amp;gt; ALTER TABLE customers ALTER COLUMN address SET DATA TYPE ROW(street VARCHAR, city VARCHAR, zip VARCHAR, country VARCHAR);
ROLLBACK 2377:  Cannot convert column &amp;#34;address&amp;#34; from &amp;#34;ROW(varchar(80),varchar(80),int,varchar(80))&amp;#34; to type &amp;#34;ROW(varchar(80),varchar(80),varchar(80),varchar(80))&amp;#34;
&lt;/code&gt;&lt;/pre&gt;&lt;h3 id=&#34;additional-properties&#34;&gt;Additional properties&lt;/h3&gt;
&lt;p&gt;A complex type column&#39;s field order follows the order specified in the ALTER command, allowing you to reorder a column&#39;s existing fields. The following example reorders the fields of the address column:&lt;/p&gt;
&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;=&amp;gt; ALTER TABLE customers ALTER COLUMN address
SET DATA TYPE ROW(street VARCHAR, country VARCHAR, city VARCHAR, zip INT);
ALTER TABLE
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;The table definition shows the address column&#39;s fields have been reordered:&lt;/p&gt;
&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;
=&amp;gt; \d customers
List of Fields by Tables
 Schema |   Table   | Column  |                                 Type                                 | Size | Default | Not Null | Primary Key | Foreign Key
--------+-----------+---------+----------------------------------------------------------------------+------+---------+----------+-------------+-------------
 public | customers | id      | int                                                                  |    8 |         | f        | f           |
 public | customers | name    | varchar(80)                                                          |   80 |         | f        | f           |
 public | customers | address | ROW(street varchar(80),country varchar(80),city varchar(80),zip int) |   -1 |         | f        | f           |
 (3 rows)
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;Note that you cannot add new fields with empty names. When creating a complex table, however, you can omit field names, and the database automatically assigns a name to each unnamed field:&lt;/p&gt;
&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;=&amp;gt; CREATE TABLE products(name VARCHAR, description ROW(VARCHAR));
CREATE TABLE
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;Because the field created in the &lt;code&gt;description&lt;/code&gt; column has not been named, the database assigns it a default name. This default name can be checked in the table definition:&lt;/p&gt;
&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;=&amp;gt; \d products
List of Fields by Tables
 Schema |  Table   |   Column    |        Type         | Size | Default | Not Null | Primary Key | Foreign Key
--------+----------+-------------+---------------------+------+---------+----------+-------------+-------------
 public | products | name        | varchar(80)         |   80 |         | f        | f           |
 public | products | description | ROW(f0 varchar(80)) |   -1 |         | f        | f           |
(2 rows)
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;Above, we see that the VARCHAR field in the &lt;code&gt;description&lt;/code&gt; column was automatically assigned the name &lt;code&gt;f0&lt;/code&gt;. When adding new fields, you must specify the existing database-assigned field name:&lt;/p&gt;
&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;=&amp;gt; ALTER TABLE products ALTER COLUMN description
SET DATA TYPE ROW(f0 VARCHAR(80), expanded_description VARCHAR(200));
ALTER TABLE
&lt;/code&gt;&lt;/pre&gt;
      </description>
    </item>
    
    <item>
      <title>Admin: Defining column values</title>
      <link>/en/admin/working-with-native-tables/managing-table-columns/defining-column-values/</link>
      <pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate>
      
      <guid>/en/admin/working-with-native-tables/managing-table-columns/defining-column-values/</guid>
      <description>
        
        
        &lt;p&gt;You can define a column so the database automatically sets its value from an expression through one of the following clauses:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;DEFAULT&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;SET USING&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;DEFAULT USING&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&#34;default&#34;&gt;DEFAULT&lt;/h2&gt;
&lt;p&gt;The DEFAULT option sets column values to a specified value. It has the following syntax:&lt;/p&gt;
&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;DEFAULT &lt;span class=&#34;code-variable&#34;&gt;default-expression&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;Default values are set when you:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;Load new rows into a table, for example, with &lt;a href=&#34;../../../../en/sql-reference/statements/insert/#&#34;&gt;INSERT&lt;/a&gt; or &lt;a href=&#34;../../../../en/sql-reference/statements/copy/#&#34;&gt;COPY&lt;/a&gt;. The database populates DEFAULT columns in new rows with their default values. Values in existing rows, including columns with DEFAULT expressions, remain unchanged.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Execute &lt;a href=&#34;../../../../en/sql-reference/statements/update/#&#34;&gt;UPDATE&lt;/a&gt; on a table and set the value of a DEFAULT column to &lt;code&gt;DEFAULT&lt;/code&gt;:&lt;/p&gt;
&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;=&amp;gt; UPDATE &lt;span class=&#34;code-variable&#34;&gt;table-name&lt;/span&gt; SET &lt;span class=&#34;code-variable&#34;&gt;column-name&lt;/span&gt;=DEFAULT;
&lt;/code&gt;&lt;/pre&gt;&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Add a column with a DEFAULT expression to an existing table. The database populates the new column with its default values when it is added to the table.&lt;/p&gt;

&lt;div class=&#34;alert admonition note&#34; role=&#34;alert&#34;&gt;
&lt;h4 class=&#34;admonition-head&#34;&gt;Note&lt;/h4&gt;

Altering an existing table column to specify a DEFAULT expression has no effect on existing values in that column. The database applies the DEFAULT expression only on new rows when they are added to the table, through load operations such as INSERT and COPY. To refresh all values in a column with the column&#39;s DEFAULT expression, update the column as shown above.

&lt;/div&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 id=&#34;restrictions&#34;&gt;Restrictions&lt;/h3&gt;
&lt;p&gt;DEFAULT expressions cannot specify volatile functions with ALTER TABLE...ADD COLUMN. To specify volatile functions, use CREATE TABLE or ALTER TABLE...ALTER COLUMN statements.&lt;/p&gt;
&lt;p&gt;&lt;a name=&#34;SET_USING&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2 id=&#34;set-using&#34;&gt;SET USING&lt;/h2&gt;
&lt;p&gt;The SET USING option sets the column value to an expression when the function &lt;a href=&#34;../../../../en/sql-reference/functions/management-functions/projection-functions/refresh-columns/#&#34;&gt;REFRESH_COLUMNS&lt;/a&gt; is invoked on that column. This option has the following syntax:&lt;/p&gt;
&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;SET USING &lt;span class=&#34;code-variable&#34;&gt;using-expression&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;This approach is useful for large denormalized (&lt;a href=&#34;../../../../en/data-analysis/flattened-tables/&#34;&gt;flattened&lt;/a&gt;) tables, where multiple columns get their values by querying other tables.&lt;/p&gt;
&lt;p&gt;&lt;a name=&#34;SetUsingRestrictions&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h3 id=&#34;restrictions-1&#34;&gt;Restrictions&lt;/h3&gt;
&lt;p&gt;SET USING has the following restrictions:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;Volatile functions are not allowed.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;The expression cannot specify a sequence.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;The database limits the use of several meta-functions that copy table data: &lt;a href=&#34;../../../../en/sql-reference/functions/management-functions/table-functions/copy-table/#&#34;&gt;COPY_TABLE&lt;/a&gt;, &lt;a href=&#34;../../../../en/sql-reference/functions/management-functions/partition-functions/copy-partitions-to-table/#&#34;&gt;COPY_PARTITIONS_TO_TABLE&lt;/a&gt;, &lt;a href=&#34;../../../../en/sql-reference/functions/management-functions/partition-functions/move-partitions-to-table/#&#34;&gt;MOVE_PARTITIONS_TO_TABLE&lt;/a&gt;, and &lt;a href=&#34;../../../../en/sql-reference/functions/management-functions/partition-functions/swap-partitions-between-tables/#&#34;&gt;SWAP_PARTITIONS_BETWEEN_TABLES&lt;/a&gt;:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;If the source and target tables both have SET USING columns, the operation is permitted only if each source SET USING column has a corresponding target SET USING column.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;If only the source table has SET USING columns, &lt;a href=&#34;../../../../en/sql-reference/functions/management-functions/partition-functions/swap-partitions-between-tables/#&#34;&gt;SWAP_PARTITIONS_BETWEEN_TABLES&lt;/a&gt; is disallowed.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;If only the target table has SET USING columns, the operation is disallowed.&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&#34;default-using&#34;&gt;DEFAULT USING&lt;/h2&gt;
&lt;p&gt;&lt;a name=&#34;DefaultUsing&#34;&gt;&lt;/a&gt;The DEFAULT USING option sets DEFAULT and SET USING constraints on a column, equivalent to using DEFAULT and SET USING separately with the same expression on the same column. It has the following syntax:&lt;/p&gt;
&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;DEFAULT USING &lt;span class=&#34;code-variable&#34;&gt;expression&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;For example, the following column definitions are effectively identical:&lt;/p&gt;
&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;=&amp;gt; ALTER TABLE public.orderFact ADD COLUMN cust_name varchar(20)
     DEFAULT USING (SELECT name FROM public.custDim WHERE (custDim.cid = orderFact.cid));
&lt;/code&gt;&lt;/pre&gt;&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;=&amp;gt; ALTER TABLE public.orderFact ADD COLUMN cust_name varchar(20)
     DEFAULT (SELECT name FROM public.custDim WHERE (custDim.cid = orderFact.cid))
     SET USING (SELECT name FROM public.custDim WHERE (custDim.cid = orderFact.cid));
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;DEFAULT USING supports the same expressions as SET USING and is subject to the same &lt;a href=&#34;#SetUsingRestrictions&#34;&gt;restrictions&lt;/a&gt;.&lt;/p&gt;
&lt;h2 id=&#34;supported-expressions&#34;&gt;Supported expressions&lt;/h2&gt;
&lt;p&gt;DEFAULT and SET USING generally support the same expressions. These include:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;Queries&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Other columns in the same table&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;a href=&#34;../../../../en/sql-reference/language-elements/literals/&#34;&gt;Literals&lt;/a&gt; (constants)&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;All &lt;a href=&#34;../../../../en/sql-reference/language-elements/operators/&#34;&gt;operators&lt;/a&gt; supported by OpenText™ Analytics Database&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;The following categories of functions:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;&lt;a href=&#34;../../../../en/sql-reference/functions/null-handling-functions/&#34;&gt;Null-handling&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;a href=&#34;../../../../en/extending/developing-udxs/scalar-functions-udsfs/&#34;&gt;User-defined scalar&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;a href=&#34;../../../../en/sql-reference/functions/system-information-functions/&#34;&gt;System information&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;a href=&#34;../../../../en/sql-reference/functions/data-type-specific-functions/string-functions/&#34;&gt;String&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;a href=&#34;../../../../en/sql-reference/functions/mathematical-functions/&#34;&gt;Mathematical&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;a href=&#34;../../../../en/sql-reference/functions/formatting-functions/&#34;&gt;Formatting&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&#34;expression-restrictions&#34;&gt;Expression restrictions&lt;/h2&gt;
&lt;p&gt;The following restrictions apply to DEFAULT and SET USING expressions:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;The return value data type must match or be cast to the column data type.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;The expression must return a value that conforms to the column bounds. For example, a column that is defined as a &lt;code&gt;VARCHAR(1)&lt;/code&gt; cannot be set to a default string of &lt;code&gt;abc&lt;/code&gt;.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;In a temporary table, DEFAULT and SET USING do not support subqueries. If you try to create a temporary table where DEFAULT or SET USING use subquery expressions, the database returns an error.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;A column&#39;s SET USING expression cannot specify another column in the same table that also sets its value with SET USING. Similarly, a column&#39;s DEFAULT expression cannot specify another column in the same table that also sets its value with DEFAULT, or whose value is automatically set to a &lt;a href=&#34;../../../../en/admin/working-with-native-tables/sequences/&#34;&gt;sequence&lt;/a&gt;. However, a column&#39;s SET USING expression can specify another column that sets its value with DEFAULT.&lt;/p&gt;

&lt;div class=&#34;alert admonition note&#34; role=&#34;alert&#34;&gt;
&lt;h4 class=&#34;admonition-head&#34;&gt;Note&lt;/h4&gt;

You can set a column&#39;s DEFAULT expression from another column in the same table that sets its value with SET USING. However, the DEFAULT column is typically set to &lt;code&gt;NULL&lt;/code&gt;, as it is only set on load operations that initially set the SET USING column to &lt;code&gt;NULL&lt;/code&gt;.

&lt;/div&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;DEFAULT and SET USING expressions only support one SELECT statement; attempts to include multiple SELECT statements in the expression return an error. For example, given table &lt;code&gt;t1&lt;/code&gt;:&lt;/p&gt;
&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;=&amp;gt; SELECT * FROM t1;
 a |    b
---+---------
 1 | hello
 2 | world
(2 rows)
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;Attempting to create table &lt;code&gt;t2&lt;/code&gt; with the following DEFAULT expression returns with an error:&lt;/p&gt;
&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;=&amp;gt; CREATE TABLE t2 (aa int, bb varchar(30) DEFAULT (SELECT &amp;#39;I said &amp;#39;)||(SELECT b FROM t1 where t1.a = t2.aa));
ERROR 9745:  Expressions with multiple SELECT statements cannot be used in &amp;#39;set using&amp;#39; query definitions
&lt;/code&gt;&lt;/pre&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&#34;disambiguating-predicate-columns&#34;&gt;Disambiguating predicate columns&lt;/h2&gt;
&lt;p&gt;If a SET USING or DEFAULT query expression joins two columns with the same name, the column names must include their table names. Otherwise, the database assumes that both columns reference the dimension table, and the predicate always evaluates to true.&lt;/p&gt;
&lt;p&gt;For example, tables orderFact and custDim both include column cid. Flattened table orderFact defines column cust_name with a SET USING query expression. Because the query predicate references columns cid from both tables, the column names are fully qualified:&lt;/p&gt;
&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;=&amp;gt; CREATE TABLE public.orderFact
(
    ...
    cid int REFERENCES public.custDim(cid),
    cust_name varchar(20) SET USING (
        SELECT name FROM public.custDim WHERE (custDIM.cid = orderFact.cid)),
    ...
)
&lt;/code&gt;&lt;/pre&gt;&lt;h2 id=&#34;examples&#34;&gt;Examples&lt;/h2&gt;
&lt;p&gt;&lt;a name=&#34;DeriveColDefaultValueFromColumn&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h3 id=&#34;derive-a-columns-default-value-from-another-column&#34;&gt;Derive a column&#39;s default value from another column&lt;/h3&gt;
&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;Create table &lt;code&gt;t&lt;/code&gt; with two columns, &lt;code&gt;date&lt;/code&gt; and &lt;code&gt;state&lt;/code&gt;, and insert a row of data:&lt;/p&gt;
&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;=&amp;gt; CREATE TABLE t (date DATE, state VARCHAR(2));
CREATE TABLE
=&amp;gt; INSERT INTO t VALUES (CURRENT_DATE, &amp;#39;MA&amp;#39;);
 OUTPUT
--------
      1
(1 row)

=&amp;gt; COMMIT;
COMMMIT
SELECT * FROM t;
    date    | state
------------+-------
 2017-12-28 | MA
(1 row)
&lt;/code&gt;&lt;/pre&gt;&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Use ALTER TABLE to add a third column that extracts the integer month value from column &lt;code&gt;date&lt;/code&gt;:&lt;/p&gt;
&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;=&amp;gt; ALTER TABLE t ADD COLUMN month INTEGER DEFAULT date_part(&amp;#39;month&amp;#39;, date);
ALTER TABLE
&lt;/code&gt;&lt;/pre&gt;&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;When you query table &lt;code&gt;t&lt;/code&gt;, the database returns the number of the month in column &lt;code&gt;date&lt;/code&gt;:&lt;/p&gt;
&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;=&amp;gt; SELECT * FROM t;
    date    | state | month
------------+-------+-------
 2017-12-28 | MA    |    12
(1 row)
&lt;/code&gt;&lt;/pre&gt;&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;&lt;a name=&#34;UpdateDefaultColValues&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h3 id=&#34;update-default-column-values&#34;&gt;Update default column values&lt;/h3&gt;
&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;Update table &lt;code&gt;t&lt;/code&gt; by subtracting 30 days from &lt;code&gt;date&lt;/code&gt;:&lt;/p&gt;
&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;=&amp;gt; UPDATE t SET date = date-30;
 OUTPUT
--------
      1
(1 row)

=&amp;gt; COMMIT;
COMMIT
=&amp;gt; SELECT * FROM t;
    date    | state | month
------------+-------+-------
 2017-11-28 | MA    |    12
(1 row)
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;The value in &lt;code&gt;month&lt;/code&gt; remains unchanged.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Refresh the default value in &lt;code&gt;month&lt;/code&gt; from column &lt;code&gt;date&lt;/code&gt;:&lt;/p&gt;
&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;=&amp;gt; UPDATE t SET month=DEFAULT;
 OUTPUT
--------
      1
(1 row)

=&amp;gt; COMMIT;
COMMIT
=&amp;gt; SELECT * FROM t;
    date    | state | month
------------+-------+-------
 2017-11-28 | MA    |    11
(1 row)
&lt;/code&gt;&lt;/pre&gt;&lt;/li&gt;
&lt;/ol&gt;
&lt;h3 id=&#34;derive-a-default-column-value-from-user-defined-scalar-function&#34;&gt;Derive a default column value from user-defined scalar function&lt;/h3&gt;
&lt;p&gt;This example shows a user-defined scalar function that adds two integer values. The function is called &lt;code&gt;add2ints&lt;/code&gt; and takes two arguments.&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;Develop and deploy the function, as described in &lt;a href=&#34;../../../../en/extending/developing-udxs/scalar-functions-udsfs/#&#34;&gt;Scalar functions (UDSFs)&lt;/a&gt;.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Create a sample table, &lt;code&gt;t1&lt;/code&gt;, with two integer columns:&lt;/p&gt;
&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;=&amp;gt; CREATE TABLE t1 ( x int, y int );
CREATE TABLE
&lt;/code&gt;&lt;/pre&gt;&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Insert some values into t1:&lt;/p&gt;
&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;=&amp;gt; insert into t1 values (1,2);
OUTPUT
--------
      1
(1 row)
=&amp;gt; insert into t1 values (3,4);
 OUTPUT
--------
      1
(1 row)
&lt;/code&gt;&lt;/pre&gt;&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Use ALTER TABLE to add a column to &lt;code&gt;t1&lt;/code&gt;, with the default column value derived from the UDSF &lt;code&gt;add2ints&lt;/code&gt;:&lt;/p&gt;
&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;alter table t1 add column z int default add2ints(x,y);
ALTER TABLE
&lt;/code&gt;&lt;/pre&gt;&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;List the new column:&lt;/p&gt;
&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;select z from t1;
 z
----
  3
  7
(2 rows)
&lt;/code&gt;&lt;/pre&gt;&lt;/li&gt;
&lt;/ol&gt;
&lt;h3 id=&#34;table-with-a-set-using-column-that-queries-another-table-for-its-values&#34;&gt;Table with a SET USING column that queries another table for its values&lt;/h3&gt;
&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;Define tables &lt;code&gt;t1&lt;/code&gt; and &lt;code&gt;t2&lt;/code&gt;. Column &lt;code&gt;t2.b&lt;/code&gt; is defined to get its data from column &lt;code&gt;t1.b&lt;/code&gt;, through the query in its SET USING clause:&lt;/p&gt;
&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;=&amp;gt; CREATE TABLE t1 (a INT PRIMARY KEY ENABLED, b INT);
CREATE TABLE

=&amp;gt; CREATE TABLE t2 (a INT, alpha VARCHAR(10),
      b INT SET USING (SELECT t1.b FROM t1 WHERE t1.a=t2.a))
      ORDER BY a SEGMENTED BY HASH(a) ALL NODES;
CREATE TABLE
&lt;/code&gt;&lt;/pre&gt;
&lt;div class=&#34;admonition important&#34; role=&#34;alert&#34;&gt;
&lt;h4 class=&#34;admonition-head&#34;&gt;Important&lt;/h4&gt;
&lt;p&gt;The definition for table &lt;code&gt;t2&lt;/code&gt; includes SEGMENTED BY and ORDER BY clauses that exclude SET USING column &lt;code&gt;b&lt;/code&gt;. If these clauses are omitted, the database creates an &lt;a href=&#34;../../../../en/admin/projections/auto-projections/&#34;&gt;auto-projection&lt;/a&gt; for this table that specifies column &lt;code&gt;b&lt;/code&gt; in its SEGMENTED BY and ORDER BY clauses . Inclusion of a SET USING column in any projection&#39;s segmentation or sort order prevents function REFRESH_COLUMNS from populating this column. Instead, it returns with an error.&lt;/p&gt;
&lt;p&gt;For details on this and other restrictions, see &lt;a href=&#34;../../../../en/sql-reference/functions/management-functions/projection-functions/refresh-columns/#&#34;&gt;REFRESH_COLUMNS&lt;/a&gt;.&lt;/p&gt;

&lt;/div&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Populate the tables with data:&lt;/p&gt;
&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;=&amp;gt; INSERT INTO t1 VALUES(1,11),(2,22),(3,33),(4,44);
=&amp;gt; INSERT INTO t2 VALUES (1,&amp;#39;aa&amp;#39;),(2,&amp;#39;bb&amp;#39;);
=&amp;gt; COMMIT;
COMMIT
&lt;/code&gt;&lt;/pre&gt;&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;View the data in table &lt;code&gt;t2&lt;/code&gt;: Column in SET USING column &lt;code&gt;b&lt;/code&gt; is empty, pending invocation of function REFRESH_COLUMNS:&lt;/p&gt;
&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;=&amp;gt; SELECT * FROM t2;
 a | alpha | b
---+-------+---
 1 | aa    |
 2 | bb    |
(2 rows)
&lt;/code&gt;&lt;/pre&gt;&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Refresh the column data in table &lt;code&gt;t2&lt;/code&gt; by calling function REFRESH_COLUMNS:&lt;/p&gt;
&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;=&amp;gt; SELECT REFRESH_COLUMNS (&amp;#39;t2&amp;#39;,&amp;#39;b&amp;#39;, &amp;#39;REBUILD&amp;#39;);
      REFRESH_COLUMNS
---------------------------
 refresh_columns completed
(1 row)
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;In this example, REFRESH_COLUMNS is called with the optional argument REBUILD. This argument specifies to replace all data in SET USING column &lt;code&gt;b&lt;/code&gt;. It is generally good practice to call REFRESH_COLUMNS with REBUILD on any new SET USING column. For details, see &lt;a href=&#34;../../../../en/sql-reference/functions/management-functions/projection-functions/refresh-columns/#&#34;&gt;REFRESH_COLUMNS&lt;/a&gt;.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;View data in refreshed column &lt;code&gt;b&lt;/code&gt;, whose data is obtained from table &lt;code&gt;t1&lt;/code&gt; as specified in the column&#39;s SET USING query:&lt;/p&gt;
&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;=&amp;gt; SELECT * FROM t2 ORDER BY a;
  a | alpha | b
---+-------+----
 1 | aa    | 11
 2 | bb    | 22
(2 rows)
&lt;/code&gt;&lt;/pre&gt;&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;&lt;a name=&#34;Expressi&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h3 id=&#34;expressions-with-correlated-subqueries&#34;&gt;Expressions with correlated subqueries&lt;/h3&gt;
&lt;p&gt;DEFAULT and SET USING expressions support subqueries that can obtain values from other tables, and use those with values in the current table to compute column values. The following example adds a column &lt;code&gt;gmt_delivery_time&lt;/code&gt; to fact table &lt;code&gt;customer_orders&lt;/code&gt;. The column specifies a DEFAULT expression to set values in the new column as follows:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;Calls meta-function &lt;a href=&#34;../../../../en/sql-reference/functions/data-type-specific-functions/datetime-functions/new-time/#&#34;&gt;NEW_TIME&lt;/a&gt;, which performs the following tasks:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;Uses customer keys in &lt;code&gt;customer_orders&lt;/code&gt; to query the &lt;code&gt;customers&lt;/code&gt; dimension table for customer time zones.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Uses the queried time zone data to convert local delivery times to GMT.&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Populates the &lt;code&gt;gmt_delivery_time&lt;/code&gt; column with the converted values.&lt;/p&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;=&amp;gt; CREATE TABLE public.customers(
    customer_key int,
    customer_name varchar(64),
    customer_address varchar(64),
    customer_tz varchar(5),
    ...);

=&amp;gt; CREATE TABLE public.customer_orders(
    customer_key int,
    order_number int,
    product_key int,
    product_version int,
    quantity_ordered int,
    store_key int,
    date_ordered date,
    date_shipped date,
    expected_delivery_date date,
    local_delivery_time timestamptz,
    ...);

=&amp;gt; ALTER TABLE customer_orders ADD COLUMN gmt_delivery_time timestamp
   DEFAULT NEW_TIME(customer_orders.local_delivery_time,
      (SELECT c.customer_tz FROM customers c WHERE (c.customer_key = customer_orders.customer_key)),
      &amp;#39;GMT&amp;#39;);
&lt;/code&gt;&lt;/pre&gt;
      </description>
    </item>
    
  </channel>
</rss>
