1 Named row types
If you define a table column with the name of a named row type, this column can only hold data of that row type. This is the reason why explicit row type cast is required in all row types assignation in insert or update sql statementes.
1.1 Create table with named row type columns
To create a table with columns using named row type columns, you can use the following syntax:
CREATE ROW TYPE nametype (lname CHAR(15), initial CHAR(1), fname CHAR(15)); CREATE TABLE person( code char(10), name nametype ); INSERT INTO person VALUES("001", ROW('Kimberland', 'I', 'Andrew')::nametype); SELECT * FROM person;
code 001
name ROW('Kimberland ','I','Andrew ')
1.2 Update table with named row types
To update a named ROW type, specify the ROW constructor before the list (in parentheses) of field values, and use the cast ( :: ) operator to cast the ROW value as a named ROW type.
The following statement updates the address column (a named ROW type) of the empinfo table:
UPDATE person SET person.name = ROW('Kumberland', 'I', 'Andrew')::nametype WHERE name.fname = 'Andrew'; UPDATE person SET name = NULL WHERE name.fname = 'Andrew';
1.3 Alter row type column schema
This is an example of a method to change row type structure in tables. Exclusive method allowed is to create a new row type, add a new column of this new row type, update this column with data stored in old column and drop old column.
This procedure can be visualized next:
CREATE ROW TYPE nametype_v2 (lname CHAR(15), initial CHAR(1), fname CHAR(15), nickname varchar(15)); RENAME COLUMN person.name TO name_old; ALTER TABLE person ADD(name nametype_v2 BEFORE name_old); UPDATE person SET name = ROW(name_old.lname, name_old.initial, name_old.fname, NULL::VARCHAR(15))::nametype_v2; ALTER TABLE person DROP(name_old); DROP ROW TYPE IF EXISTS nametype RESTRICT;
2 Unnamed row types
Using unnamed row types in columns are a method allowing more open structures. Columns storing data in an unnamed row type can be defined directly in "create table" statement
Because unnamed row types are type-checked for structural equivalence only, a variable defined with an unnamed row type can hold data from any unnamed row type that has the same number of fields and the same type definitions.
2.1 Create table with unnamed row type columns
CREATE TABLE person_u( code char(10), name ROW(lname CHAR(15), initial CHAR(1), fname CHAR(15)) ); INSERT INTO person_u VALUES("001", ROW('Kimberland', 'I', 'Andrew')); SELECT * FROM person_u;
code 001
name ROW('Kimberland ','I','Andrew ')
2.2 Update table with unnamed row types
To update an unnamed ROW type, specify the ROW constructor before the parenthesized list of field values.
The following statement updates the name column (an unnamed ROW type) of the person_u table:
UPDATE person_u SET person_u.name = ROW('Kumberland', 'I', 'Andrew') WHERE name% UPDATE person_u SET name = NULL WHERE name.fname = 'Andrew';
2.3 Alter unnamed row type in table schema
This is an example of a method to change unnamed row type structure in tables. Allowed method is create a new column with new unnamed row type structure, update this column with data stored in old column and drop old column.
This procedure can be visualized next:
RENAME COLUMN person_u.name to name_old; ALTER TABLE person_u ADD(name ROW(fname VARCHAR(20), initial char(1), lname VARCHAR(20), nickname VARCHAR(20)) BEFORE name_old); UPDATE person_u set name = ROW(name_old.fname, name_old.initial, name_old.lname, NULL::CHAR); ALTER TABLE person_u DROP (name_old);