PostgreSQL – Create Table

Below is the sql command syntax to create a table in PostgreSQL database:

CREATE TABLE table_name (
   column1 datatype1 constraint1,
   column2 datatype2 constraint2,
   column3 datatype3 constraint3,
   ...
);
  • CREATE TABLE: This is the command that tells PostgreSQL to create a new table.
  • table_name: This is the name of your new table.
  • column1, column2, column3, etc.: These are the names of the columns in your table. You can choose any name you like. It is a good idea to use descriptive names that will make sense to anyone who needs to work with your table.
  • datatype1, datatype2, datatype3, etc.: These are the data types of the columns. You must specify a data type for each column, such as integer, text, date, etc.
  • constraint1, constraint2, constraint3, etc.: These are optional constraints that you can add to your columns to enforce rules such as unique values or not null values. You can also add constraints to the table as a whole to enforce rules such as primary keys or foreign keys.

Below is an example of how to create a table in PostgreSQL database with two columns:

CREATE TABLE employees (
   employee_id integer,
   employee_name text
);

Above sql creates a table called employees with two columns: employee_id of data type integer and employee_name of data type text.

Similar Posts