[go: up one dir, main page]

0% found this document useful (0 votes)
31 views14 pages

DDL Postgresql

The document outlines key Data Definition Language (DDL) commands in PostgreSQL for managing tables, including CREATE TABLE, ALTER TABLE, DROP TABLE, RENAME TABLE, and TRUNCATE TABLE. Each command is explained with its purpose and examples of usage. DDL commands are essential for defining and modifying database schema objects.

Uploaded by

anushakb
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
31 views14 pages

DDL Postgresql

The document outlines key Data Definition Language (DDL) commands in PostgreSQL for managing tables, including CREATE TABLE, ALTER TABLE, DROP TABLE, RENAME TABLE, and TRUNCATE TABLE. Each command is explained with its purpose and examples of usage. DDL commands are essential for defining and modifying database schema objects.

Uploaded by

anushakb
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 14

MANAGING TABLES

DDL(data Definition Language)


DML(data Manipulation Language)

Data Definition Language (DDL) commands in


PostgreSQL are used to define and manage
database schema objects such as tables,
indexes, and views. Here are some key DDL
commands for managing tables:
1. CREATE TABLE
The CREATE TABLE command is used to
create a new table in the database.
 CREATE TABLE table_name (
 column1 datatype constraints,
 column2 datatype constraints,
 ...);
 EXAMPLE:
CREATE TABLE employees (
 employee_id SERIAL PRIMARY KEY,
 first_name VARCHAR(50),
 last_name VARCHAR(50),
 hire_date DATE,
 salary NUMERIC(10, 2)
 );
OR
primary can give as
CREATE TABLE employees (
 employee_id,
 first_name VARCHAR(50),
 last_name VARCHAR(50),
 hire_date DATE,
 salary NUMERIC(10, 2),
Primary key (employee_id)
);
2.ALTER TABLE
The ALTER TABLE command is used to modify
the structure of an existing table. This can
include adding, deleting, or modifying
columns, as well as adding constraints.
Add a column:
ALTER TABLE table_name ADD
column_namedatatype;
EXAMPLE:
ALTER TABLE employees ADD department_id
INTEGER;
 Drop a column:
 ALTER TABLE table_name DROP COLUMN
column_name;
 EXAMPLE
 ALTER TABLE employees DROP COLUMN
department_id;
 Modify a column:
 ALTER TABLE table_name ALTER COLUMN
column_name TYPE new_datatype;
 EXAMPLE
 ALTER TABLE employees ALTER COLUMN salary
TYPE NUMERIC(12, 2);
 3. DROP TABLE
 The DROP TABLE command is used to delete a table and
its data from the database.
DROP TABLE table_name;
EXAMPLE
DROP TABLE employees;
4. RENAME TABLE
The ALTER TABLE ... RENAME TO command is used to
rename an existing table.
ALTER TABLE old_table_name RENAME TO
new_table_name;
EXAMPLE
ALTER TABLE employees RENAME TO staff;
5. TRUNCATE TABLE
The TRUNCATE TABLE command is used to
remove all rows from a table, but the table
structure remains intact. It is faster than the
DELETE command because it doesn't
generate individual row deletions.
TRUNCATE TABLE table_name;

You might also like