PostgreSQL 序列绑定字段与不绑定字段的区别说明

发布时间:2022-04-21 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了PostgreSQL 序列绑定字段与不绑定字段的区别说明脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。

序列绑定字段与不绑定字段的区别

绑定字段

构造数据

drop sequence if exists test_id_seq;
create sequence test_id_seq;
drop table if exists test;
create table test(id int default nextval('test_id_seq'), name text);
alter sequence test_id_seq owned by test.id;

测试

test=# drop table test;
DROP TABLE
test=# \d
Did not find any relations.
test=# 

不绑定字段

构造数据

drop sequence if exists test_id_seq;
create sequence test_id_seq;
drop table if exists test;
create table test(id int default nextval('test_id_seq'), name text);

测试

test=# drop table test;
DROP TABLE
test=# \d
       List of relations
 Schema |  Name   |  TyPE  | Owner  
--------+-------------+----------+----------
 public | test_id_seq | sequence | postgres
(1 row)

test=# 

总结

序列绑定字段,则删除表的时候,序列会被一并删除

序列不绑定字段,则序列与表是独立的,删除表不会将序列一并删除

补充:PG表中字段使用序列类型以及绑定序列实例

两种方法效果是一样的

直接看代码

以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本宝典。如有错误或未考虑完全的地方,望不吝赐教。

脚本宝典总结

以上是脚本宝典为你收集整理的PostgreSQL 序列绑定字段与不绑定字段的区别说明全部内容,希望文章能够帮你解决PostgreSQL 序列绑定字段与不绑定字段的区别说明所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。