I've a little problem with serial : From a file, I filled my database in which I have a client ID (it is a serial and it is my primary key). I have 300 clients so 300 client ID (1 to 300). Now my problem is, I've a form for new clients.I cannot add them because when I add a client, my program adds the client with ID 1 or the ID 1 is already assigned to another client.
So my question is : is it possible to change the starting value of a serial for to resolve this problem ?
To change the primary key of a table, delete the existing key using a DROP clause in an ALTER TABLE statement and add the new primary key. Note You must be logged in to the database using a database name before you can add a primary key or conduct any other referential integrity (RI) operation.
There is essentially no difference.
Postgres currently defines column order based on the attnum column of the pg_attribute table. The only way to change column order is either by recreating the table, or by adding columns and rotating data until you reach the desired layout.
You can alter a sequence using RESTART WITH
to change the current sequence number;
ALTER SEQUENCE test_seq RESTART WITH 300;
To get the sequence name if you created it using the serial keyword, use
SELECT adsrc FROM pg_attrdef WHERE adrelid = (SELECT oid FROM pg_class WHERE relname = 'table name goes here');
An SQLfiddle to test with.
PostgreSQL
ALTER SEQUENCE tablename_columnname_seq RESTART WITH anynumber;
Example:
ALTER SEQUENCE test_table_rec_id_seq RESTART WITH 4615793;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With