8.1.4. Serial Types

8.1.4. Serial Types
8.1.4.序列类型
Note
This section describes a PostgreSQL-specific way to create an autoincrementing column.  Another way is to use the SQL-standard identity column feature, described at  CREATE TABLE.
此节介绍以PostgreSQL特定的方式创建一个自动递增的列。另一种方式是SQL标准中定义的列特性,参见 CREATE TABLE
 
The data types smallserial , serial and bigserial are not true types, but merely a notational  convenience for creating unique identifier columns (similar to the AUTO_INCREMENT property  supported by some other databases). In the current implementation, specifying:
数据类型smallserial,serial和bigserial不是真实类型,而仅仅是为了创建唯一标识列的符号方便(类似于某些其他数据库支持的AUTO_INCREMENT属性)。在当前的实现中,指定:
 
CREATE TABLE tablename (
colname SERIAL
);
is equivalent to specifying:
CREATE SEQUENCE tablename_colname_seq;
CREATE TABLE tablename (
colname integer NOT NULL DEFAULT nextval('tablename_colname_seq')
);
ALTER SEQUENCE tablename_colname_seq OWNED BY tablename.colname;
Thus, we have created an integer column and arranged for its default values to be assigned from a  sequence generator. A NOT NULL constraint is applied to ensure that a null value cannot be inserted. (In most cases you would also want to attach a UNIQUE or PRIMARY KEY constraint to prevent  duplicate values from being inserted by accident, but this is not automatic.) Lastly, the sequence is  marked as “owned by” the column, so that it will be dropped if the column or table is dropped.
也就是,我们创建了一个integer类型的列,并为其赋予来自序列的默认值。非空约束限定该列不能插入空值。(有些场景下,也需要创建唯一约束或主键约束以防止不小心插入重复行。)最后,将序列标记为列所有,这样可以确保,在删掉该列或该表的时候,该序列也可以被删掉。
 
Note
Because smallserial , serial and bigserial are implemented using sequences, there may be "holes" or gaps in the sequence of values which appears in the  column, even if no rows are ever deleted. A value allocated from the sequence is still  "used up" even if a row containing that value is never successfully inserted into the  table column. This may happen, for example, if the inserting transaction rolls back. See nextval() in Section 9.16 for details.
因为smallserial、serial和bigserial是通过序列实现的,所以列中的值,即使没有删过,也有可能出现不连续的情况。 即使从未成功将包含序列分配值的行插入表列,也仍会消耗从序列中分配的值。例如,如果插入事务回滚,可能会发生这种情况。有关详细信息,请参见第9.16节中的nextval()。
 
To insert the next value of the sequence into the serial column, specify that the serial column  should be assigned its default value. This can be done either by excluding the column from the list of  columns in the INSERT statement, or through the use of the DEFAULT key word.
将序列的下一个值插入到serial列中,可以指定serial列取其默认值。这可以通过在插入时排除该列实现,或者通过使用default关键词实现。
 
The type names serial and serial4 are equivalent: both create integer columns. The type  names bigserial and serial8 work the same way, except that they create a bigint column. bigserial should be used if you anticipate the use of more than 2 31 identifiers over the lifetime of  the table. The type names smallserial and serial2 also work the same way, except that they  create a smallint column.
serial与serial4等价:均创建integer类型列。bigserial与serial8工作方式也差不多,不过它们创建的是bigint列。如果在表的整个生命周期中,会使用超过2的31次方个数据,那么请使用bigserial数据类型。smallserial和serial2也是这么工作的,不过它们创建的是smallint列。
 
The sequence created for a serial column is automatically dropped when the owning column is  dropped. You can drop the sequence without dropping the column, but this will force removal of the  column default expression.
当由序列创建的serial列删除时,相对应的序列也会自动随之删掉。也可以手动删掉该序列,不过这会去掉该列的默认值。
 
发布了341 篇原创文章 · 获赞 54 · 访问量 88万+

猜你喜欢

转载自blog.csdn.net/ghostliming/article/details/104613058