序列綁定字段與不綁定字段的區(qū)別
綁定字段
構(gòu)造數(shù)據(jù)
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;
測(cè)試
test=# drop table test;
DROP TABLE
test=# \d
Did not find any relations.
test=#
不綁定字段
構(gòu)造數(shù)據(jù)
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);
測(cè)試
test=# drop table test;
DROP TABLE
test=# \d
List of relations
Schema | Name | Type | Owner
--------+-------------+----------+----------
public | test_id_seq | sequence | postgres
(1 row)
test=#
總結(jié)
序列綁定字段,則刪除表的時(shí)候,序列會(huì)被一并刪除
序列不綁定字段,則序列與表是獨(dú)立的,刪除表不會(huì)將序列一并刪除
補(bǔ)充:PG表中字段使用序列類型以及綁定序列實(shí)例
兩種方法效果是一樣的
直接看代碼
![](/d/20211018/ede865915ab244372cc6577e417956c3.gif)
![](/d/20211018/e155b647ae911b54672dbc8e1eb48cfd.gif)
![](/d/20211018/08e855e87bcab5b2dc0a708b7c12d8b6.gif)
![](/d/20211018/05bba90ed8fe72dbfcff1928872bd2db.gif)
![](/d/20211018/6d8caee047fd1d14d72f409c6e30ef32.gif)
![](/d/20211018/d593a6a8c045bd64f0018370e6569537.gif)
![](/d/20211018/75e28c77a58b696ec3d3c6ba2c422f59.gif)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教。
您可能感興趣的文章:- PostgreSQL Sequence序列的使用詳解
- postgresql 中的序列nextval詳解
- PostgreSQL 序列增刪改案例
- postgresql重置序列起始值的操作
- postgresql 實(shí)現(xiàn)更新序列的起始值
- postgresql修改自增序列操作
- Postgresql數(shù)據(jù)庫(kù)之創(chuàng)建和修改序列的操作