PostgreSQL Create Table

Previously "create" is used to create database, but in this the Create Table is used to create new tables or other database objects.

Create Table Syntax :

The syntax of the Create Table is −

\\Note: Syntax enclosed in a [ ] bracket is optional.
CREATE TABLE table_name                 \\The name of the table should be unique
(col_name datatype [column constraint],  \\Defines rules for the data in the table
[table_constraint table constraint]
)[INHERITS (existing_table_name)]

Line 1 :- In creating a basic table involves, define the naming of the table (table-name)
Line 2 :- multiple columns in a table, each column is separated by a comma (column 1, column 2 ..... column).
Line 3 :- The datatype of each column (column 1 datatype ...... column 2 data Type).
Line 4 :- Finally we inherit the existing table, so the new table will contain the columns of the existing table and the columns of the new table.



Create Table Example :

The following is an example, which creates a Student table with ID as the primary key NOT NULL are the constraints showing that these fields cannot be NULL while creating records in this table.

CREATE TABLE Student(
ID INT PRIMARY KEY     NOT NULL,
NAME           TEXT    NOT NULL,
AGE            INT     NOT NULL,
ADDRESS        CHAR(50)
);



The empty "Student" table will now look like this:

ID NAME AGE ADDRESS





Visit :


Discussion



* You must be logged in to add comment.