CREATE TABLE epictable
(
mytable_key serial primary key,
moobars VARCHAR(40) not null,
foobars DATE
);
ALTER SEQUENCE product_id_seq RESTART WITH 1453
-- SERIAL keyword auto increments the value
CREATE TABLE Colleges (
college_id INT SERIAL,
college_code VARCHAR(20) NOT NULL,
college_name VARCHAR(50),
CONSTRAINT CollegePK PRIMARY KEY (college_id)
);
-- inserting record without college_id
INSERT INTO Colleges(college_code, college_name)
VALUES ("ARD13", "Star Public School");
ALTER TABLE temp ALTER COLUMN id
ADD GENERATED BY DEFAULT AS IDENTITY
TRUNCATE TABLE someTable RESTART IDENTITY;
By simply setting our id column as SERIAL with PRIMARY KEY attached, Postgres will handle all the complicated behind-the-scenes work and automatically increment our id column with a unique, primary key value for every INSERT .
SELECT setval('test_id_seq', (SELECT MAX(id) from "test"));