以Postgresql为主,再聊聊资料库 typed table的应用

上一篇介绍了 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 gal1
union all
select *
  from gal2
order 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 就可以了.
相信聪明的你,应该有想到这对於系统发展,构型的一致性,有一定的帮助了.


<<:  以Postgresql为主,再聊聊资料库 利用自定义型态 create type 建立 typed table

>>:  彻底卸载 Mac 应用程序

搜寻引擎优化入门篇:你不能不知道的响应式网页设计

随着行动浏览成为搜寻引擎主流,响应式网页设计也越来越重要,甚至能说在这几年,假设你的网页没有响应式设...

Day 13 - Kotlin的集合(2)

Day 11 - Kotlin的函式(2) 昨天我们讲了list集合,以及如何取得数值,今天我们要继...

[Day4] 安装Django

夥伴们大家好,今天要来说明如何安装Django啦~~~ 但是在安装前我们要先查看一下,我们使用的py...

Day 27 : Python - 什麽是列表推导式?又该如何将它和if、if-else一起做使用?

如标题,这篇想和大家聊聊「列表推导式」是什麽东西 我们先看看范例再说明,这样大家会比较好理解 Ex ...

TypeScript 能手养成之旅 Day 5 原始型别

前言 前一天大致上了解一下,TypeScript 有支援哪些型别,从今天开始,将一一来每一个型别的定...