![]() |
VOOZH | about |
SQL (Structured Query Language) is the standard language for managing relational databases. Two of the most widely used relational databases are PostgreSQL and MySQL.
Here is step-by-step SQL operations, using examples that work in both PostgreSQL and MySQL.
First, install PostgreSQL (which comes with pgAdmin) or MySQL (with Workbench). These GUI tools make it easier to manage databases without using command-line commands.
After installation, connect to your database server. In pgAdmin, provide username/password; in MySQL Workbench, create a new connection with host, port and credentials.
A database is a container for tables, views and other objects. Before running queries, you must create a database to store your data.
Syntax:
CREATE DATABASE database_name;
Tables are the backbone of relational databases. They store data in rows and columns. You can define the table structure using SQL commands (CREATE TABLE).
Syntax:
CREATE TABLE table_name (
column1 datatype constraint,
column2 datatype constraint,
...
);
Once the table is created, you can insert records into it using the INSERT INTO statement. This step helps you populate the table with sample data for testing queries.
Syntax:
INSERT INTO table_name (column1, column2, ...)
VALUES (value1, value2, ...);
Use SELECT to fetch data from a table.
Syntax:
SELECT column1, column2, ...
FROM table_name;
Use UPDATE to change existing records in a table. Always include a WHERE clause to avoid updating all rows.
Syntax:
UPDATE table_name
SET column1 = value1
WHERE condition;
The DELETE command removes records from a table. Use WHERE to delete specific rows instead of all.
Syntax:
DELETE FROM table_name
WHERE condition;