PostgreSQL
SQL
SQL (Structured Query Language) is a standard programming language used to manage and manipulate relational databases. It allows you to create, read, update, and delete data in a structured way using tables, which are made up of rows (records) and columns (fields). PostgreSQL, as a powerful open-source relational database system, uses SQL to define and manage data structures such as tables, which organize information in rows and columns for easy retrieval and management.
Here’s how tables, columns, and rows work in PostgreSQL with an example from a banking database system:
- Creating Tables:
- In PostgreSQL, tables are where the data is stored. Each table consists of columns (fields), which define the type of data stored, and rows (records), which hold the actual data.
- Example: To create a table for customers in a banking system:
CREATE TABLE customers ( customer_id SERIAL PRIMARY KEY, first_name VARCHAR(50) NOT NULL, last_name VARCHAR(50) NOT NULL, email VARCHAR(100) UNIQUE, phone VARCHAR(15), address TEXT ); - Adding Columns to a Table:
- Columns represent the type of data in the table. Each column has a specific data type (like
VARCHAR,INTEGER, orTEXT) that defines what kind of data it can hold. - In the
customerstable above, we have several columns:customer_id: A unique identifier for each customer (auto-incremented).first_nameandlast_name: Strings for storing customer names.email: A string to store the customer's email address, which must be unique.phone: A string to store the customer's phone number.address: A text field for storing the customer's address.
- Columns represent the type of data in the table. Each column has a specific data type (like
- Inserting Rows (Records):
- Rows are the actual data stored in the table. Each row represents one entry, and every column in that row contains a value.
- Example: To insert a row of data into the
customerstable:
INSERT INTO customers (first_name, last_name, email, phone, address) VALUES ('John', 'Doe', 'john.doe@example.com', '123-456-7890', '123 Main St, Anytown, USA'); - Defining a Table for Bank Accounts:
- Let’s create a table to store information about customer bank accounts.
CREATE TABLE accounts ( account_id SERIAL PRIMARY KEY, customer_id INTEGER REFERENCES customers(customer_id), account_type VARCHAR(20) NOT NULL, balance DECIMAL(15, 2) DEFAULT 0.00, opened_on DATE DEFAULT CURRENT_DATE ); - Here:
account_id: A unique identifier for the account.customer_id: A foreign key referencing thecustomerstable to link accounts to specific customers.account_type: The type of account (e.g., savings, checking).balance: The current balance in the account.opened_on: The date the account was opened, with a default value of the current date.
- Let’s create a table to store information about customer bank accounts.
- Defining a Table for Transactions:
- To track customer transactions, we need a
transactionstable.CREATE TABLE transactions ( transaction_id SERIAL PRIMARY KEY, account_id INTEGER REFERENCES accounts(account_id), transaction_date DATE DEFAULT CURRENT_DATE, amount DECIMAL(10, 2) NOT NULL, transaction_type VARCHAR(10) CHECK (transaction_type IN ('credit', 'debit')), description TEXT ); - In this table:
transaction_id: A unique identifier for each transaction.account_id: A foreign key linking each transaction to an account.transaction_date: The date of the transaction.amount: The transaction amount.transaction_type: Specifies whether the transaction is a credit or debit.description: Additional details about the transaction.
- To track customer transactions, we need a
- Retrieving Data (Using SELECT Queries):
- Once data is in the tables, you can retrieve it using the
SELECTstatement. - Example: To get all customers from the
customerstable:SELECT * FROM customers; - Example: To find all transactions for a specific account:
SELECT * FROM transactions WHERE account_id = 1;
- Once data is in the tables, you can retrieve it using the
This structured explanation introduces the fundamental concepts of SQL and PostgreSQL using simple examples that are easy for beginners to follow, particularly within the context of a banking system.
To gain complete access, login with gmail or outlook, no need of signup. click here
Introduction to SQL
SQL (Structured Query Language) is a standard language for storing, manipulating, and retrieving data in databases.
1. Creating a Table
To store data, we first create a table. Here's how to create an Authors table:
CREATE TABLE Authors (AuthorID int, FirstName varchar(255), LastName varchar(255), BirthYear int );
- CREATE TABLE Authors- starts the command to create a new table named 'Authors'.
- AuthorID int, FirstName varchar(255), etc.- defines the columns and their data types.
2. Inserting Data
Once a table is created, you can add data to it:
INSERT INTO Authors (AuthorID, FirstName, LastName, BirthYear) VALUES (1, 'Jane', 'Austen',1775);
- INSERT INTO Authors- specifies the table to insert data into.
- VALUES (1, 'Jane', 'Austen',1775)- defines the data being inserted.
3. Selecting Data
To retrieve and view data from the table:
SELECT FirstName, LastName FROM Authors;
- SELECT FirstName, LastName- indicates the columns to retrieve.
- FROM Authors- specifies the table to select data from.
4. Updating Data
If data needs correction or updating:
UPDATE Authors SET BirthYear = 1776 WHERE AuthorID = 1;
- UPDATE Authors- indicates the table where the update will occur.
- SET BirthYear = 1776- specifies the new value for a column.
- WHERE AuthorID = 1- identifies which record(s) to update.
5. Deleting Data
To remove data from the table:
DELETE FROM Authors WHERE AuthorID = 1;
- DELETE FROM Authors- specifies from which table to delete data.
- WHERE AuthorID = 1- identifies which record(s) to delete.
Each SQL statement serves a specific function, allowing for efficient management and manipulation of data within databases.
Understanding SQL Command Categories
Data Definition Language (DDL)
DDL commands define, alter, and manage the structure of database objects like tables and indexes. These commands affect the schema and architecture of the database rather than the data itself.
- CREATE: Creates new database objects.
- ALTER: Modifies existing database objects.
- DROP: Deletes objects from the database.
- TRUNCATE: Removes all records from a table, deleting the space allocated for the records.
Data Manipulation Language (DML)
DML commands are used for managing data within database tables. These commands allow adding, updating, or deleting data.
- INSERT: Adds new rows to a table.
- UPDATE: Modifies existing data within a table.
- DELETE: Removes rows from a table.
- SELECT: Queries and retrieves data based on specific criteria. Often considered part of DQL, but crucial for data manipulation.
Data Query Language (DQL)
DQL focuses on querying and retrieving data. It allows fetching and organizing data from one or more tables.
- SELECT: The primary command used to query the database for specific information, utilizing clauses like
WHERE,GROUP BY, andORDER BYto refine results.


Comments Not Found