PostgreSQL DBA (117) - pgAdmin(不要# 039;t这么做:不要# 039;t使用串行)

  

没有左死系列,来自于pg的wiki。   
这一节的内容是:不要使用串行。   
理由是:   

  

系列类型有一些奇怪的行为,使模式,依赖和许可管理不必要的麻烦。

     

该类型有某些行为会给模式,依赖和权限管理带来不必要的麻烦。

  

  <>强基本用法

  <前>   <代码>(本地):5432 pg12@testdb=#删除表如果存在t_serial;   删除表   时间:158.910毫秒   (本地):5432 pg12@testdb=#创建表t_serial (   pg12@testdb (# id序列主键,   pg12@testdb (# c1 varchar   pg12@testdb (#);   创建表   时间:9.424毫秒   (本地):5432 pg12@testdb=#   (本地):5432 pg12@testdb=#插入t_serial (c1)值(a)、(b)、(“c”)返回*;   id | c1   - - - - - + - - - - -   1 |   2 | b   3 | c   (3行)   插入0 3   时间:3.076毫秒   (本地):5432 pg12@testdb=#   (本地):5432 pg12@testdb=# select * from t_serial;   id | c1   - - - - - + - - - - -   1 |   2 | b   3 | c   (3行)   时间:0.847毫秒   (本地):5432 pg12@testdb=#      

系列与生成的默认情况下作为身份主键的作用很相似

  <前>   <代码>(本地):5432 pg12@testdb=#创建表t_identify (   pg12@testdb (# int id生成默认为身份主键,   pg12@testdb (# c1文本   pg12@testdb (#);   创建表   时间:5.215毫秒   (本地):5432 pg12@testdb=#   (本地):5432 pg12@testdb=#插入t_identify (c1)值(a)、(b)、(“c”)返回*;   id | c1   - - - - - + - - - - -   1 |   2 | b   3 | c   (3行)   插入0 3   时间:1.127毫秒   (本地):5432 pg12@testdb=#      

实际上,连续符合SQL标准具备兼容性,而生成的默认情况下作为身份是PG的语法不具备兼容性。

  

  <强>权限   
系列类型的第一个问题是与串行列相关的顺序需要单独处理   <前>   <代码>(本地):5432 pg12@testdb=#用户如果存在user1029下降;   注意:角色“user1029”并不存在,跳过   下降的作用   时间:0.422毫秒   (本地):5432 pg12@testdb=#创建用户user1029密码“测试”;   创建角色   时间:0.543毫秒   (本地):5432 pg12@testdb=#格兰特插入t_serial user1029;   格兰特   时间:1.297毫秒   (本地):5432 pg12@testdb=#格兰特插入t_identify user1029;   格兰特   时间:3.729毫秒   (本地):5432 pg12@testdb=#设置会话授权user1029;   集   时间:1.243毫秒   (本地):5432 user1029@testdb=比;插入t_serial (c1)值(' d ');   错误:没有权限对t_serial_id_seq序列   时间:2.705毫秒   (本地):5432 user1029@testdb=比;插入t_identify (c1)值(' d ');   插入0 1   时间:3.340毫秒   (本地):5432 user1029@testdb=比;      

可以看的到,类型序列的实现底层依赖于序列,id列对应的序列是t_serial_id_seq。   
而生成的默认情况下作为身份则不需要依赖,因此执行不会出的错。   
通过授权可以解决此问题   <前>   <代码>——pg12   (本地):5432 pg12@testdb=#格兰特使用序列t_serial_id_seq user1029;   格兰特   时间:5.291毫秒   (本地):5432 pg12@testdb=#   ——user1029   (本地):5432 user1029@testdb=比;插入t_serial (c1)值(' d ');   插入0 1   时间:3.791毫秒   (本地):5432 user1029@testdb=比;      

由于串行类型依赖于序列,如果我们对序列进行相关操作,那会出现什么情况?

  <前>   <代码>(本地):5432 pg12@testdb=#序列t_serial_id_seq下降;   错误:无法删除序列t_serial_id_seq因为其他对象依赖于它   表的id细节:默认值列t_serial取决于t_serial_id_seq序列   提示:使用下降……级联删除依赖对象。   时间:1.056毫秒      

存在依赖,删除时会报错,添加级联选项。

  <前>   <代码>(本地):5432 pg12@testdb=#下降序列t_serial_id_seq级联;   注意:瀑布下降为表列id t_serial默认值   下降序列   时间:10.075毫秒   (本地):5432 pg12@testdb=# \ d t_serial   表“public.t_serial”   专栏| |类型排序| Nullable |违约   - - - - - - - - - - + - - - - - - - - - - - - - - - - - - - + - - - - - - - - - - - - - - - - - - - - - - - + - - - - - - - - - - - -   id的整数| | |不是零|   c1 |字符不同| | |   索引数量:   “t_serial_pkey”主键,btree (id)   (本地):5432 pg12@testdb=#      

t_serial列变成了普通的int字段。

PostgreSQL DBA (117) - pgAdmin(不要# 039;t这么做:不要# 039;t使用串行)