当前位置: 首页 > news >正文

手机上如何制作app/安卓优化大师清理

手机上如何制作app,安卓优化大师清理,网站分析软件,电子商务怎么样标签 PostgreSQL , 10 , 特性 , identify , 自增 , 覆盖 , SQL Server IDENTITY兼容 , SQL标准 背景 自增列是数据库的一个常用功能,PostgreSQL的自增列在10的版本出来前,有两种非常简单的方法来实现: 1、serial类型,自动创建一个…

标签

PostgreSQL , 10 , 特性 , identify , 自增 , 覆盖 , SQL Server IDENTITY兼容 , SQL标准


背景

自增列是数据库的一个常用功能,PostgreSQL的自增列在10的版本出来前,有两种非常简单的方法来实现:

1、serial类型,自动创建一个序列,同时将列设置为INT,默认值设置为nextval('序列')。

create table test(id serial, info text);  postgres=# \d+ test  Table "public.test"  Column |  Type   | Collation | Nullable |             Default              | Storage  | Stats target | Description   
--------+---------+-----------+----------+----------------------------------+----------+--------------+-------------  id     | integer |           | not null | nextval('test_id_seq'::regclass) | plain    |              |   info   | text    |           |          |                                  | extended |              |   

2、serial8类型,,自动创建一个序列,同时将列设置为INT8,默认值设置为nextval('序列')。

create table test(id serial8, info text);  

3、序列+默认值设置为序列,

create sequence seq1;  create table test (id int default nextval('seq1'), info text);  

为了兼容SQL Server或SQL标准,PostgreSQL 10加入了IDENTITY列的支持。实际上功效类似,都是为了生成默认值。

但是IDENTITY加入了一个新的功能,可以允许用户选择是否覆盖这个列的默认值。

PostgreSQL IDENTITY列语法

1、创建IDENTITY列。

create table语法中,在列的类型后使用如下语法定义identity列。

ALWAYS,表示优先使用系统列生成的自增值。

BY DEFAULT,表示优先使用用户输入的值。

使用COPY导入数据时,输入的值会强行覆盖IDENTITY的设置。不管使用always还是by default。

GENERATED { ALWAYS | BY DEFAULT } AS IDENTITY [ ( sequence_options ) ]  This clause creates the column as an identity column.   It will have an implicit sequence attached to it and the column   
in new rows will automatically have values from the sequence assigned to it.  The clauses ALWAYS and BY DEFAULT determine how the sequence   
value is given precedence over a user-specified value in an INSERT statement.   If ALWAYS is specified, a user-specified value is only accepted if the   
INSERT statement specifies OVERRIDING SYSTEM VALUE.   If BY DEFAULT is specified, then the user-specified value takes precedence.   See INSERT for details. (In the COPY command, user-specified values are always used regardless of this setting.)  The optional sequence_options clause can be used to override the options of the sequence.   
See CREATE SEQUENCE for details.  

例子

postgres=# create table test (id int GENERATED ALWAYS AS IDENTITY (cache 100), info text);  
CREATE TABLE  postgres=# create table test1 (id int GENERATED BY DEFAULT AS IDENTITY (cache 100), info text);  
CREATE TABLE  postgres=# \d test  Table "public.test"  Column |  Type   | Collation | Nullable |           Default              
--------+---------+-----------+----------+------------------------------  id     | integer |           | not null | generated always as identity  info   | text    |           |          |   postgres=# \d test1  Table "public.test1"  Column |  Type   | Collation | Nullable |             Default                
--------+---------+-----------+----------+----------------------------------  id     | integer |           | not null | generated by default as identity  info   | text    |           |          |   

实际上identify列,也使用了序列,如下:

postgres=# \ds  List of relations  Schema |     Name     |   Type   |  Owner     
--------+--------------+----------+----------  public | test1_id_seq | sequence | postgres  public | test_id_seq  | sequence | postgres  postgres=# drop sequence test1_id_seq;  
错误:  无法删除 序列 test1_id_seq, 因为 表 test1 字段 id 需要它  
HINT:  您也可以删除 表 test1 字段 id 代替.  

2、插入,如何覆盖默认值或覆盖用户提供值。

当identity列被定义为GENERATED ALWAYS AS IDENTITY时,如果要覆盖系统产生的值,需要使用OVERRIDING SYSTEM VALUE,否则会报错。

OVERRIDING SYSTEM VALUE  Without this clause, it is an error to specify an explicit value   
(other than DEFAULT) for an identity column defined as GENERATED ALWAYS.   This clause overrides that restriction.  

当identity列被定义为GENERATED BY DEFAULT AS IDENTITY时,如果要使用系统产生的值(即覆盖用户提交的值),需要使用OVERRIDING USER VALUE,否则会使用用户提交的值。

OVERRIDING USER VALUE  If this clause is specified, then any values supplied for   
identity columns defined as GENERATED BY DEFAULT are ignored   
and the default sequence-generated values are applied.  This clause is useful for example when copying values between tables.   
Writing INSERT INTO tbl2 OVERRIDING USER VALUE SELECT * FROM tbl1 will   
copy from tbl1 all columns that are not identity columns in tbl2   
while values for the identity columns in tbl2 will be generated by the   
sequences associated with tbl2.  

例子:

1、覆盖IDENTITY列,系统自动生成的自增值。

OVERRIDING SYSTEM VALUE

postgres=# insert into test (id, info) values (1,'test');  
错误:  cannot insert into column "id"  
DETAIL:  Column "id" is an identity column defined as GENERATED ALWAYS.  
HINT:  Use OVERRIDING SYSTEM VALUE to override.  postgres=# insert into test (id, info) OVERRIDING SYSTEM VALUE values (1,'test');  
INSERT 0 1  postgres=# select * from test;  id | info   
----+------  1 | test  
(1 row)  

2、覆盖用户提供的值。

postgres=# insert into test1 values (1,'test');  -- 用户输入的值优先  
INSERT 0 1  
postgres=# insert into test1 (id, info) OVERRIDING user VALUE values (1000,'test');  -- 覆盖用户输入的值(使用系统列定义的自增值)  
INSERT 0 1  
postgres=# select * from test1;  id | info   
----+------  1 | test  1 | test  
(2 rows)  

3、COPY,不管always还是by default,总是使用用户提供的值。

postgres=# copy test from stdin  
postgres-# ;  
Enter data to be copied followed by a newline.  
End with a backslash and a period on a line by itself, or an EOF signal.  
>> 1999 abc  
>> 2999 cde      
>> \.  
COPY 2  
postgres=# select * from test;  id  | info   
------+------  1 | test  1999 | abc  2999 | cde  
(3 rows)  

小结

现在你应该知道,在PostgreSQL中有几种定义自增列的方法了吧。

1、serial或serial8类型。

2、identity列定义。

参考

https://www.postgresql.org/docs/10/static/sql-createtable.html

https://www.postgresql.org/docs/10/static/sql-insert.html

https://www.postgresql.org/docs/10/static/sql-createsequence.html

http://www.lbrq.cn/news/1271161.html

相关文章:

  • 新疆建设厅网站房屋租赁合同/企业seo职位
  • 沈阳网站建设公司电话/流量网站
  • 建网站的公司广州排名/运营培训班学费大概多少
  • 用动易做的校园网站/推广软文发布平台
  • 如何留住网站用户/微信代运营
  • 网站发外链/谷歌浏览器官网下载
  • 校园网站集群建设/百度广告收费表
  • 网站实现中英文/windows7优化大师
  • 网站宽屏/什么叫网络营销
  • 网络问卷制作平台/厦门seo厦门起梦
  • 民宅挂在民宿网站上 保洁谁做/seo优化排名工具
  • 手机网站需要多少钱/爱站网反链查询
  • 网站优化成本/开通网站需要多少钱
  • 有什么可以做兼职的网站/广告外链购买交易平台
  • 河南网站建设服务公司/网络推广策划
  • 超级seo助手/百度关键词优化排名
  • 我想做个旅游网站怎么做/如何营销推广
  • 东莞自适应网站建设/安卓优化大师最新版下载
  • 长沙感染人数最新消息/潜江seo
  • 全部网站/鸿星尔克网络营销案例分析
  • 邯郸网站建设咨询安联网络/推广软文范文
  • 北京网站建设 案例/seo网站排名优化快速排
  • 网站修改影响做百度竞价吗/宁波网络营销策划公司
  • 哈尔滨seo优化/优化流程
  • 南昌做网站建设哪家好/b2b免费发布信息平台
  • 手机程序编程/外贸seo是什么意思
  • 平面设计能干到老吗/武汉建站优化厂家
  • 一个旅游网站建设需求分析/苏州排名搜索优化
  • 网站建设案例步骤/关键词优化技巧有哪些
  • 手机免费创网站/企业网站建设的基本流程
  • U-Net vs. 传统CNN:为什么医学图像分割需要跳过连接?
  • 60 GHz DreamHAT+ 雷达已被正式批准为“Powered by Raspberry Pi”产品
  • WPF TreeView自带自定义滚动条
  • Qt 开发自动化测试框架搭建
  • 3D 建模核心术语扫盲:拓扑、UV 展开、烘焙与 AO 贴图解析
  • ORACLE的表维护