![]() If unspecified, the old cache value will be maintained. The minimum value is 1 (only one value can be generated at a time, i.e., no cache). The clause CACHE cache enables sequence numbers to be preallocated and stored in memory for faster access. If that's not the desired mode of operation, setval should be used. In contrast to a setval call, a RESTART operation on a sequence is transactional and blocks concurrent transactions from obtaining numbers from the same sequence. Writing RESTART with no restart value is equivalent to supplying the start value that was recorded by CREATE SEQUENCE or last set by ALTER SEQUENCE START WITH. This is similar to calling the setval function with is_called = false: the specified value will be returned by the next call of nextval. The optional clause RESTART changes the current value of the sequence. This has no effect on the current sequence value it simply sets the value that future ALTER SEQUENCE RESTART commands will use. ![]() The optional clause START WITH start changes the recorded start value of the sequence. If neither option is specified, the current maximum value will be maintained. If NO MAXVALUE is specified, the defaults of the maximum value of the data type and -1 for ascending and descending sequences, respectively, will be used. The optional clause MAXVALUE maxvalue determines the maximum value for the sequence. If neither option is specified, the current minimum value will be maintained. If NO MINVALUE is specified, the defaults of 1 and the minimum value of the data type for ascending and descending sequences, respectively, will be used. The optional clause MINVALUE minvalue determines the minimum value a sequence can generate. If unspecified, the old increment value will be maintained. A positive value will make an ascending sequence, a negative one a descending sequence. ![]() The clause INCREMENT BY increment is optional. If the minimum and maximum values do not fit into the new data type, an error will be generated. Otherwise, the minimum and maximum values are preserved, unless new values are given as part of the same command. Valid types are smallint, integer, and bigint.Ĭhanging the data type automatically changes the minimum and maximum values of the sequence if and only if the previous minimum and maximum values were the minimum or maximum value of the old data type (in other words, if the sequence had been created using NO MINVALUE or NO MAXVALUE, implicitly or explicitly). The optional clause AS data_type changes the data type of the sequence. IF EXISTSĭo not throw an error if the sequence does not exist. insert into users values ('Jamie', 'EM3456') īy selecting from the users table again, we can see that the new row has had the id populated appropriately.The name (optionally schema-qualified) of a sequence to be altered. select * from users įinally, let's add a new user to our table. If we select from our users again, we can see that our existing users have already had their id populated. Now if we inspect our users table, we can see that there is a new column of type integer with a sequence that was generated for us by Postgres. The serial type is a Postgres shorthand for allowing us to create columns with an autoincrementing integer. alter table users add column id serial primary key To add an autoincrementing id as the primary key to our table, we can use the "alter table" command, passing our users table in, and then specifying that we want to add a column called id with the type "serial" and set it as our primary key. We can also select from this table and see that we already have data. We can inspect this table using the "\d" command. In this example, we're going to be dealing with a dummy database that has a table users, with no primary key currently set.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |