SQL For Database Management
SQL For Database Management
What is SQL?
SQL (Structured Query Language) is a standard programming language specifically designed for
managing and manipulating databases. It allows you to create, read, update, and delete database
records.
Key Concepts
SQL statements are composed of various clauses, expressions, and predicates. Here is a simple
example of a SQL query:
sql
Copy code
SELECT column1, column2
FROM table_name
WHERE condition;
Setting Up a Database
To practice SQL, you need a database management system (DBMS). Popular DBMSs include
MySQL, PostgreSQL, SQLite, and Microsoft SQL Server. For simplicity, we'll use SQLite, a
lightweight, file-based database.
1. Install SQLite:
o Download and install SQLite from sqlite.org.
2. Create a Database:
o Open a terminal or command prompt.
o Run the following command to create a new database file:
sh
Copy code
sqlite3 mydatabase.db
3. Create a Table:
o Within the SQLite prompt, create a table named users:
sql
Copy code
CREATE TABLE users (
id INTEGER PRIMARY KEY,
name TEXT,
email TEXT
);
sql
Copy code
INSERT INTO users (name, email)
VALUES ('Alice', 'alice@example.com');
sql
Copy code
INSERT INTO users (name, email)
VALUES
('Bob', 'bob@example.com'),
('Charlie', 'charlie@example.com');
Querying Data
sql
Copy code
SELECT * FROM users;
This will return all columns and rows from the users table. You can specify particular columns
to retrieve:
sql
Copy code
SELECT name, email FROM users;
Filtering Data
Updating Data
sql
Copy code
UPDATE users
SET email = 'alice.new@example.com'
WHERE name = 'Alice';
Deleting Data
sql
Copy code
DELETE FROM users WHERE name = 'Charlie';
SQL Functions
Aggregate Functions
sql
Copy code
SELECT COUNT(*) FROM users;
sql
Copy code
SELECT SUM(salary) FROM employees;
sql
Copy code
SELECT AVG(salary) FROM employees;
sql
Copy code
SELECT MAX(salary) FROM employees;
sql
Copy code
SELECT MIN(salary) FROM employees;
String Functions
sql
Copy code
SELECT UPPER(name) FROM users;
sql
Copy code
SELECT LOWER(name) FROM users;
sql
Copy code
SELECT LENGTH(name) FROM users;
To combine rows from two or more tables, use the JOIN clause.
INNER JOIN
sql
Copy code
SELECT users.name, orders.order_date
FROM users
INNER JOIN orders ON users.id = orders.user_id;
LEFT JOIN
Returns all records from the left table and the matched records from the right table. The result is
NULL from the right side if there is no match.
sql
Copy code
SELECT users.name, orders.order_date
FROM users
LEFT JOIN orders ON users.id = orders.user_id;
sql
Copy code
CREATE INDEX idx_user_name ON users (name);
Transactions
Transactions ensure that a series of SQL operations are executed as a single unit of work. Use the
BEGIN TRANSACTION, COMMIT, and ROLLBACK statements.
sql
Copy code
BEGIN TRANSACTION;
UPDATE accounts
SET balance = balance - 100
WHERE user_id = 1;
UPDATE accounts
SET balance = balance + 100
WHERE user_id = 2;
COMMIT;
sql
Copy code
ROLLBACK;
Stored Procedures
Stored procedures are a collection of SQL statements that can be executed as a single procedure.
sql
Copy code
CREATE PROCEDURE AddUser (
IN userName VARCHAR(50),
IN userEmail VARCHAR(50)
)
BEGIN
INSERT INTO users (name, email)
VALUES (userName, userEmail);
END;
Conclusion
You've now learned the basics of SQL, including how to create and manipulate databases, insert
and query data, and use advanced concepts like joins, indexes, transactions, and stored
procedures. SQL is a powerful language that forms the backbone of database management and
manipulation. Continue practicing and exploring more advanced SQL features to enhance your
database management skills.
Happy querying!