PostgreSql sequence

What is PostgreSql: https: //www.postgresql.org/about/
PostgreSql related sequences using: https: //www.cnblogs.com/alianbog/p/5654604.html
Oracle built table in accordance with past processes, we have to create a new table, and import the data into the table.
1. Create a Book table
CREATE TABLE book(  id INTEGER PRIMARY KEY ,  name CHARACTER VARYING(50),  price DOUBLE PRECISION,  author CHARACTER VARYING(20));
The PRIMARY KEY id as the primary key;
 
2. Create an automatic sequence of growth
CREATE SEQUENCE book_id_seq  START WITH 1  INCREMENT BY 1  NO MINVALUE  NO MAXVALUE  CACHE 1;
Parameter
   Description
  START WITH
   set the starting value, allowing the sequence to start from anywhere
  INCREMENT BY
   setting increments, specify in which to create a new value based on the worth, value will generate increasing sequence, negative values will produce a descending sequence; the default is 1.
  NO MINVALUE
   set minimum sequence can generate, if NO MINVALUE not specified, for ascending and descending sequence, and default values are 1.
  NO MAXVALUE
   set the maximum sequence may be generated if this clause is not specified, the default values for the ascending and descending sequence, and the default value is -1.
  CACHE
   Sets the cache, how much you want to assign a serial number and convenient storage in memory for faster access, minimum is 1, the default value is 1.
  3. Add grow automatically sequence table book
ALTER TABLE book ALTER COLUMN id SET DEFAULT nextval('book_id_seq');
4. Insert the table data to the book
INSERT INTO public.book VALUES (nextval ( 'book_id_seq'), ' "character"', 30.5 'Roca');
INSERT INTO public.book VALUES (nextval ( 'book_id_seq'), ' "Ordinary World' ', 90.5' Lu Yao ');
INSERT INTO public.book VALUES (nextval ( 'book_id_seq'), ' "Java programming ideas'', 60.5 'James Gosling');
INSERT INTO public.book VALUES (nextval ( 'book_id_seq'), ' "Web Advanced Programming"', 50.5 'Lee III');
The book table query data
SELECT * FROM public.book;
search result:
 
6. operations related sequences:
- query sequence (every time the query sequence to do a +1 operation, that is the value at query time)
SELECT nextval('book_id_seq');
- Remove sequence
DROP SEQUENCE 'book_id_seq';

Original link: https: //blog.csdn.net/qq_37464248/article/details/82769868

Guess you like

Origin www.cnblogs.com/xibuhaohao/p/12166960.html