上一篇介绍了 create type,以及 typed table.
本篇介绍应用,这次就不做複合型态的了.就是直接建立type,建立typed table,
然后删除属性,新增属性.
create type ty_gal as ( id int, name text, stuff text);create table gal1 of ty_gal;create table gal2 of ty_gal;insert into gal1 values(1, '小岛南', '测试用'),(2, '初川南', '测试用');insert into gal2 values(3, '相沢南', '测试用'),(4, '小宵虎南', '测试用');alter type ty_gal drop attribute stuff cascade;alter type ty_gal add attribute products text[] cascade;commit;update gal1 set products = array['SSIS-340', 'SSIS-315'] where id = 1; update gal1 set products = array['BBAN-359', 'SHKD-987'] where id = 2; update gal2 set products = array['IPX-819', 'IPX-801'] where id = 3; update gal2 set products = array['SSIS-309', 'SSIS-281'] where id = 4;select * from gal1union allselect * from gal2order by id; id | name | products----+----------+--------------------- 1 | 小岛南 | {SSIS-340,SSIS-315} 2 | 初川南 | {BBAN-359,SHKD-987} 3 | 相沢南 | {IPX-819,IPX-801} 4 | 小宵虎南 | {SSIS-309,SSIS-281}(4 rows)
线上展示连结:
https://dbfiddle.uk/?rdbms=postgres_14&fiddle=56f53842156bf29508fa0e853e88538e
上面的简单範例中,可以看到两个typed table 都不需要去使用 alter table 来修改栏位,
我们只要直接alter type ... cascade 就可以了.
相信聪明的你,应该有想到这对于系统发展,构型的一致性,有一定的帮助了.