SQL (Structured Query Language) is a standardized programming language used to manage and manipulate relational databases. It is used for querying, updating, and managing data stored in a relational database management system (RDBMS).
Key Features of SQL
Data Query: Retrieve data from a database.
Data Manipulation: Insert, update, delete, and manage data.
Data Definition: Define and modify database schema.
Data Control: Control access to data and database objects.
Basic SQL Commands
SELECT: Retrieve data from a database.
INSERT: Add new data into a database.
UPDATE: Modify existing data in a database.
DELETE: Remove data from a database.
CREATE: Create new database objects (like tables).
ALTER: Modify existing database objects.
DROP: Delete database objects.
Example: Basic SQL Operations
1. Creating a Database and Table
-- Create a database
CREATE DATABASE mydatabase;
-- Use the database
USE mydatabase;
-- Create a table
CREATE TABLE users (
id INT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(100),
email VARCHAR(100)
);
2. Inserting Data
-- Insert data into the table
INSERT INTO users (name, email) VALUES ('Alice', 'alice@example.com');
INSERT INTO users (name, email) VALUES ('Bob', 'bob@example.com');
3. Querying Data
-- Retrieve all data from the table
SELECT * FROM users;
-- Retrieve specific columns
SELECT name, email FROM users;
-- Retrieve data with a condition
SELECT * FROM users WHERE name = 'Alice';
4. Updating Data
-- Update data in the table
UPDATE users SET email = 'alice@newdomain.com' WHERE name = 'Alice';
5. Deleting Data
-- Delete data from the table
DELETE FROM users WHERE name = 'Bob';
Explanation of the Example
Creating a Database and Table:
CREATE DATABASE mydatabase; creates a new database named mydatabase.
USE mydatabase; selects the mydatabase for subsequent operations.
CREATE TABLE users (...); creates a table named users with columns id, name, and email.
Inserting Data:
INSERT INTO users (name, email) VALUES (...); inserts new records into the users table.
Querying Data:
SELECT * FROM users; retrieves all columns from all rows in the users table.
SELECT name, email FROM users; retrieves only the name and email columns.
SELECT * FROM users WHERE name = 'Alice'; retrieves rows where the name column is 'Alice'.
Updating Data:
UPDATE users SET email = 'alice@newdomain.com' WHERE name = 'Alice'; updates the email address of the user named 'Alice'.
Deleting Data:
DELETE FROM users WHERE name = 'Bob'; removes the user named 'Bob' from the users table.
Advanced SQL Concepts
Joins: Combine rows from two or more tables based on a related column.
INNER JOIN, LEFT JOIN, RIGHT JOIN, FULL JOIN
Indexes: Improve the speed of data retrieval.
CREATE INDEX, DROP INDEX
Transactions: Ensure a series of SQL statements are executed as a single unit.
BEGIN, COMMIT, ROLLBACK
Stored Procedures and Functions: Reusable SQL code that can be executed with a single call.
CREATE PROCEDURE, CREATE FUNCTION
Example: Using a Join
-- Create another table
CREATE TABLE orders (
order_id INT AUTO_INCREMENT PRIMARY KEY,
user_id INT,
order_date DATE,
FOREIGN KEY (user_id) REFERENCES users(id)
);
-- Insert data into the orders table
INSERT INTO orders (user_id, order_date) VALUES (1, '2024-01-01');
INSERT INTO orders (user_id, order_date) VALUES (2, '2024-01-02');
-- Retrieve data using a join
SELECT users.name, orders.order_date
FROM users
INNER JOIN orders ON users.id = orders.user_id;
Explanation of Join Example
Creating the Orders Table:
The orders table is created with columns order_id, user_id, and order_date.
The user_id column is a foreign key that references the id column in the users table.
Inserting Data into Orders Table:
New records are added to the orders table, linking users by user_id.
Retrieving Data with a Join:
The SELECT statement retrieves user names and their corresponding order dates by joining the users and orders tables on the user_id column.