The SERIAL type in PostgreSQL is a convenient way to define an auto-incrementing column. It is essentially a shorthand for creating a sequence object and setting a default value for the column.
When you define a column with SERIAL, PostgreSQL automatically creates a sequence and sets the column's default value to the next value of the sequence.
PostgreSQL provides three types of SERIAL: SERIAL, BIGSERIAL, and SMALLSERIAL. These correspond to different integer sizes.
The SERIAL type is commonly used for primary keys, where a unique identifier is needed for each row in a table.
CREATE TABLE users (
id SERIAL PRIMARY KEY,
username VARCHAR(50) NOT NULL
);
Using SERIAL simplifies the process of creating unique identifiers and reduces the likelihood of errors associated with manual sequence management.
While SERIAL uses sequences, identity columns introduced in PostgreSQL 10 offer a more standardized SQL way of handling auto-incrementing fields.
Console Output:
Table "users" created successfully.
The BIGSERIAL type is used when you anticipate needing more than 2 billion unique identifiers, which is the limit for the standard SERIAL type.
To define a column as BIGSERIAL, simply replace SERIAL with BIGSERIAL in your table definition.
CREATE TABLE orders (
order_id BIGSERIAL PRIMARY KEY,
order_date DATE NOT NULL
);
BIGSERIAL provides a larger range for identifiers, making it suitable for applications with high insertion rates.
Console Output:
Table "orders" created successfully.
The SMALLSERIAL type is used for smaller ranges of numbers, up to 32,767, making it ideal for small datasets.
To use SMALLSERIAL, declare the column with this type in your table definition.
CREATE TABLE small_data (
id SMALLSERIAL PRIMARY KEY,
description TEXT
);
SMALLSERIAL uses less storage space, which can be beneficial for optimizing database performance in certain scenarios.
Console Output:
Table "small_data" created successfully.
Although SERIAL handles sequences automatically, you can manually create sequences using the CREATE SEQUENCE
command.
CREATE SEQUENCE custom_seq
START 1
INCREMENT 1;
Once a sequence is created, it can be used to set default values for columns, similar to how SERIAL works.
Console Output:
Sequence "custom_seq" created successfully.
If you need to change a SERIAL column to BIGSERIAL, you must manually alter the column and update its sequence.
ALTER TABLE users
ALTER COLUMN id TYPE BIGINT,
ALTER COLUMN id SET DEFAULT nextval('users_id_seq'::regclass);
Altering a column type can have implications on existing data and indexes, so it should be done with caution.
Console Output:
Table "users" altered successfully.
Dropping a SERIAL column involves removing the column and optionally the associated sequence if it's no longer needed.
ALTER TABLE users
DROP COLUMN id;
DROP SEQUENCE IF EXISTS users_id_seq;
Removing a column will result in data loss for that column, so ensure that any necessary data is backed up or migrated before performing this operation.
Console Output:
Column "id" and sequence "users_id_seq" dropped successfully.
Identity columns, introduced in PostgreSQL 10, provide a SQL-standard way of defining auto-incrementing columns, offering an alternative to SERIAL.
CREATE TABLE products (
product_id INT GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
product_name VARCHAR(100)
);
Identity columns are more flexible in terms of sequence management and are part of the SQL standard, making them preferable for new applications.
Console Output:
Table "products" created successfully.
Resetting a sequence might be necessary if you want to restart the numbering, for instance, after deleting all rows from a table.
ALTER SEQUENCE users_id_seq RESTART WITH 1;
Resetting a sequence should be done with caution as it can lead to potential conflicts with existing data if not handled properly.
Console Output:
Sequence "users_id_seq" reset successfully.
Newsletter
Subscribe to our newsletter for weekly updates and promotions.
Wiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWikiCode
Programming LanguagesWeb DevelopmentMobile App DevelopmentData Science and Machine LearningDatabase ManagementDevOps and Cloud ComputingSoftware EngineeringCybersecurityGame DevelopmentWikiCode
Programming LanguagesWeb DevelopmentMobile App DevelopmentData Science and Machine LearningDatabase ManagementDevOps and Cloud ComputingSoftware EngineeringCybersecurityGame DevelopmentWikiCode
Programming LanguagesWeb DevelopmentMobile App DevelopmentData Science and Machine LearningDatabase ManagementDevOps and Cloud ComputingSoftware EngineeringCybersecurityGame DevelopmentWikiCode
Programming LanguagesWeb DevelopmentMobile App DevelopmentData Science and Machine LearningDatabase ManagementDevOps and Cloud ComputingSoftware EngineeringCybersecurityGame DevelopmentWikiCode
Programming LanguagesWeb DevelopmentMobile App DevelopmentData Science and Machine LearningDatabase ManagementDevOps and Cloud ComputingSoftware EngineeringCybersecurityGame DevelopmentWikiCode
Programming LanguagesWeb DevelopmentMobile App DevelopmentData Science and Machine LearningDatabase ManagementDevOps and Cloud ComputingSoftware EngineeringCybersecurityGame DevelopmentWiki News
World NewsPolitics NewsBusiness NewsTechnology NewsHealth NewsScience NewsSports NewsEntertainment NewsEducation NewsWiki News
World NewsPolitics NewsBusiness NewsTechnology NewsHealth NewsScience NewsSports NewsEntertainment NewsEducation NewsWiki News
World NewsPolitics NewsBusiness NewsTechnology NewsHealth NewsScience NewsSports NewsEntertainment NewsEducation NewsWiki News
World NewsPolitics NewsBusiness NewsTechnology NewsHealth NewsScience NewsSports NewsEntertainment NewsEducation NewsWiki News
World NewsPolitics NewsBusiness NewsTechnology NewsHealth NewsScience NewsSports NewsEntertainment NewsEducation NewsWiki News
World NewsPolitics NewsBusiness NewsTechnology NewsHealth NewsScience NewsSports NewsEntertainment NewsEducation NewsWiki Tools
JPEG/PNG Size ReductionPDF Size CompressionPDF Password RemoverSign PDFPower Point to PDFPDF to Power PointJPEG to PDF ConverterPDF to JPEG ConverterWord to PDF ConverterWiki Tools
JPEG/PNG Size ReductionPDF Size CompressionPDF Password RemoverSign PDFPower Point to PDFPDF to Power PointJPEG to PDF ConverterPDF to JPEG ConverterWord to PDF ConverterWiki Tools
JPEG/PNG Size ReductionPDF Size CompressionPDF Password RemoverSign PDFPower Point to PDFPDF to Power PointJPEG to PDF ConverterPDF to JPEG ConverterWord to PDF ConverterWiki Tools
JPEG/PNG Size ReductionPDF Size CompressionPDF Password RemoverSign PDFPower Point to PDFPDF to Power PointJPEG to PDF ConverterPDF to JPEG ConverterWord to PDF ConverterWiki Tools
JPEG/PNG Size ReductionPDF Size CompressionPDF Password RemoverSign PDFPower Point to PDFPDF to Power PointJPEG to PDF ConverterPDF to JPEG ConverterWord to PDF ConverterWiki Tools
JPEG/PNG Size ReductionPDF Size CompressionPDF Password RemoverSign PDFPower Point to PDFPDF to Power PointJPEG to PDF ConverterPDF to JPEG ConverterWord to PDF ConverterCompany
About usCareersPressCompany
About usCareersPressCompany
About usCareersPressLegal
TermsPrivacyContactAds PoliciesLegal
TermsPrivacyContactAds PoliciesLegal
TermsPrivacyContactAds PoliciesCompany
About usCareersPressCompany
About usCareersPressCompany
About usCareersPressLegal
TermsPrivacyContactAds PoliciesLegal
TermsPrivacyContactAds PoliciesLegal
TermsPrivacyContactAds PoliciesLegal
TermsPrivacyContactAds PoliciesAds Policies