PostgreSQL allows users to perform different operations to manage databases and their data.
These operations are commonly performed using SQL commands.
The CREATE DATABASE command creates a new database.
CREATE DATABASE university;
This command creates a database named university.
The CREATE TABLE command creates a new table inside a database.
CREATE TABLE students (
student_id INT,
student_name VARCHAR(100),
age INT
);
This creates a table named students with three columns.
The INSERT INTO command adds new records to a table.
INSERT INTO students
(student_id, student_name, age)
VALUES
(101, 'Ali', 20);
This inserts one student record.
The SELECT command retrieves data from a table.
SELECT * FROM students;
The asterisk * means all columns.
The UPDATE command modifies existing records.
UPDATE students
SET age = 21
WHERE student_id = 101;
This changes the age of the student whose ID is 101.
The DELETE command removes records from a table.
DELETE FROM students
WHERE student_id = 101;
This deletes the student record with ID 101.
| Task | SQL Command |
|---|---|
| Create database | CREATE DATABASE |
| Delete database | DROP DATABASE |
| Create table | CREATE TABLE |
| Add records | INSERT INTO |
| Retrieve records | SELECT |
| Update records | UPDATE |
| Delete records | DELETE |
| Modify table structure | ALTER TABLE |
| Create a view | CREATE VIEW |
| Create an index | CREATE INDEX |
The examples in this lesson are introductory examples. Later modules will explain each command in detail.
Also, commands such as UPDATE and DELETE can affect multiple records if the WHERE condition is omitted. Always check the condition before executing them.
students.