PostgreSQL – TABLES

Tables are fundamental database objects used to store structured data in rows and columns. They are the primary means of organizing and managing data within a relational database. Tables define the structure of data and provide a framework for storing, retrieving, and manipulating information.

Creating a Table: To create a table in PostgreSQL, you use the CREATE TABLE statement. You specify the table name, column names, and data types for each column.

CREATE TABLE table_name 
( column1_name data_type, 
column2_name data_type, ... );

For example, creating a simple employees table:

CREATE TABLE employees 
( employee_id serial PRIMARY KEY, 
first_name varchar(50), 
last_name varchar(50), 
hire_date date );

Inserting Data: You use the INSERT INTO statement to add data to a table.

INSERT INTO table_name (column1_name, column2_name, ...) VALUES (value1, value2, ...);

For example:

INSERT INTO employees (first_name, last_name, hire_date) 
VALUES ('John', 'Doe', '2023-01-15');

Querying Data: You can retrieve data from a table using the SELECT statement.

SELECT column1_name, column2_name, ... FROM table_name WHERE condition;

For example:

SELECT first_name, last_name FROM employees WHERE hire_date > '2023-01-01';

Updating Data: To modify existing data, you use the UPDATE statement.

UPDATE table_name SET column1_name = new_value1, column2_name = new_value2, ... WHERE condition;

Deleting Data: To remove rows from a table, you use the DELETE statement.

DELETE FROM table_name WHERE condition;

Dropping a Table: To remove a table and its data, you use the DROP TABLE statement.

DROP TABLE table_name;

These are some of the basic operations you can perform with tables in PostgreSQL. Tables are central to organizing, storing, and manipulating data within a database, and they form the foundation for building relational database structures.

Similar Posts