- PostgreSQL - Distinct Keyword
- PostgreSQL - Having Clause
- PostgreSQL - With Clause
- PostgreSQL - Group By
- PostgreSQL - Order By Clause
- PostgreSQL - Limit Clause
- PostgreSQL - Like Clause
- PostgreSQL - Delete Query
- PostgreSQL - Update Query
- PostgreSQL - AND & OR Clauses
- PostgreSQL - Where Clause
- PostgreSQL - Expressions
- PostgreSQL - Operators
- PostgreSQL - Select Query
- PostgreSQL - Insert Query
- PostgreSQL - Schema
- PostgreSQL - Drop Table
- PostgreSQL - Create Table
- PostgreSQL - Drop Database
- PostgreSQL - Select Database
- PostgreSQL - Create Database
- PostgreSQL - Data Types
- PostgreSQL - Syntax
- PostgreSQL - Environment Setup
- PostgreSQL - Overview
- PostgreSQL - Home
Advanced PostgreSQL
- PostgreSQL - Useful Functions
- PostgreSQL - Functions
- Date/Time Functions & Operators
- PostgreSQL - Privileges
- PostgreSQL - Auto Increment
- PostgreSQL - Sub Queries
- PostgreSQL - Locks
- PostgreSQL - Transactions
- PostgreSQL - Views
- Truncate Table Command
- PostgreSQL - Alter Table Command
- PostgreSQL - Indexes
- PostgreSQL - Triggers
- PostgreSQL - Alias Syntax
- PostgreSQL - NULL Values
- PostgreSQL - Unions Clause
- PostgreSQL - Joins
- PostgreSQL - Constraints
PostgreSQL Interfaces
PostgreSQL Useful Resources
Selected Reading
- Who is Who
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
PostgreSQL - CREATE Table
The PostgreSQL CREATE TABLE statement is used to create a new table in any of the given database.
Syntax
Basic syntax of CREATE TABLE statement is as follows −
CREATE TABLE table_name( column1 datatype, column2 datatype, column3 datatype, ..... columnN datatype, PRIMARY KEY( one or more columns ) );
CREATE TABLE is a keyword, telpng the database system to create a new table. The unique name or identifier for the table follows the CREATE TABLE statement. Initially, the empty table in the current database is owned by the user issuing the command.
Then, in brackets, comes the pst, defining each column in the table and what sort of data type it is. The syntax will become clear with an example given below.
Examples
The following is an example, which creates a COMPANY table with ID as primary key and NOT NULL are the constraints showing that these fields cannot be NULL while creating records in this table −
CREATE TABLE COMPANY( ID INT PRIMARY KEY NOT NULL, NAME TEXT NOT NULL, AGE INT NOT NULL, ADDRESS CHAR(50), SALARY REAL );
Let us create one more table, which we will use in our exercises in subsequent chapters −
CREATE TABLE DEPARTMENT( ID INT PRIMARY KEY NOT NULL, DEPT CHAR(50) NOT NULL, EMP_ID INT NOT NULL );
You can verify if your table has been created successfully using d command, which will be used to pst down all the tables in an attached database.
testdb-# d
The above given PostgreSQL statement will produce the following result −
List of relations Schema | Name | Type | Owner --------+------------+-------+---------- pubpc | company | table | postgres pubpc | department | table | postgres (2 rows)
Use d tablename to describe each table as shown below −
testdb-# d company
The above given PostgreSQL statement will produce the following result −
Table "pubpc.company" Column | Type | Modifiers -----------+---------------+----------- id | integer | not null name | text | not null age | integer | not null address | character(50) | salary | real | join_date | date | Indexes: "company_pkey" PRIMARY KEY, btree (id)Advertisements