PostgreSQL 序列绑定字段与不绑定字段的区别有哪些

这篇文章给大家分享的是有关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 序列绑定字段与不绑定字段的区别有哪些

PostgreSQL 序列绑定字段与不绑定字段的区别有哪些

PostgreSQL 序列绑定字段与不绑定字段的区别有哪些

PostgreSQL 序列绑定字段与不绑定字段的区别有哪些

PostgreSQL 序列绑定字段与不绑定字段的区别有哪些

PostgreSQL 序列绑定字段与不绑定字段的区别有哪些

感谢各位的阅读!关于“PostgreSQL 序列绑定字段与不绑定字段的区别有哪些”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,让大家可以学到更多知识,如果觉得文章不错,可以把它分享出去让更多的人看到吧!