DDL(Data Definition Language) Statements in PostgreSQL

Vishal Pathak
Abhima Database Technology
2 min readMay 1, 2022

--

Today we will show you basics of DDL Statements in PostgreSQL like Creating table, Editing a table, Truncating a table, Dropping a table, etc.

Nalanda University(World’s first university) Destroyed by Mughals

First let’s understand what is DDL and then we will discuss about DDL Statements. So DDL is used to handle database description and schemas meaning it is used to modify or create the data structure.

There are four most used DDL Statements in PostgreSQL.

  1. Create
  2. Alter
  3. Truncate
  4. Drop

Create:-

Create statement is used to create table with their columns along with their data type. Below is the basic syntax to create a table.

Syntax:-

CREATE TABLE <table_name>(
<Column1> <DataType>,
<Column2> <DataType>
)

As you can see we have specify the table name after CREATE TABLE and then inside parenthesis Column name and it’s datatype. Below is the example of creating a table.

As you can see in the above example we are creating table Users.

Alter:-

Alter statement is used to modify the table structure like changing the data type of a column, changing name the of the column, renaming the column. Below is the syntax and example of adding a column.

Syntax:-

ALTER TABLE <table_name>

<ADD/DROP/ALTER/ALTER> COLUMN <column_name> <datatype>

Example:-

In the above example we are adding new column to Users table.

Truncate:-

Truncate is a part of DDL query as it affects the schema of the table. It is same in every SQL based RDBMS. We have detailed Article on Truncate and Delete.

Syntax:-

TRUNCATE TABLE <table_name>;

Example:-

Drop:-

Drop statement is used to remove a table from the database, remove the database, etc. Below is the syntax and example of it.

Syntax:-

DROP TABLE <table_name/database_name>.

Example:-

Thank you for reading please comment your suggestions, share the article, follow me and Abhima Database Technology publication.

Bhagavad Gita Verse of the Day

प्रकृत्यैव च कर्माणि क्रियमाणानि सर्वश: |
य: पश्यति तथात्मानमकर्तारं स पश्यति || 30||

prakṛityaiva cha karmāṇi kriyamāṇāni sarvaśhaḥ
yaḥ paśhyati tathātmānam akartāraṁ sa paśhyati

BG 13.30: They alone truly see who understand that all actions (of the body) are performed by material nature, while the embodied soul actually does nothing.

--

--

Vishal Pathak
Abhima Database Technology

love ❤ coding, solving some industry problems technologies: JavaScript, C#, Angular, PLSQL, Docker Want to learn: Python, Go language, AI, ML and Cloud