SQL Learning Guide
1. Introduction to SQL
SQL (Structured Query Language) is a standard programming language used to manage relational databases. It allows
users to create, read, update, and delete data in databases.
Types of SQL Commands:
1. DDL (Data Definition Language): Commands that define the structure of a database (e.g., CREATE, ALTER).
2. DML (Data Manipulation Language): Commands to manipulate data (e.g., INSERT, UPDATE, DELETE).
3. DQL (Data Query Language): Commands to query data (e.g., SELECT).
4. TCL (Transaction Control Language): Commands to manage transactions (e.g., COMMIT, ROLLBACK).
5. DCL (Data Control Language): Commands to control access (e.g., GRANT, REVOKE).
2. Database Creation and Usage
To create and use a database, follow these steps:
Command: CREATE DATABASE DBMS;
Step-by-Step Example:
1. Create a database named DBMS.
2. Use the database with the command: USE DBMS;
Tip: Always use descriptive database names to ensure clarity.
3. Creating a Table
To create a table, use the CREATE TABLE command:
SQL Learning Guide
Command:
CREATE TABLE student (
id INT PRIMARY KEY,
name VARCHAR(50),
age INT,
course VARCHAR(50),
admission_date DATE,
city VARCHAR(50)
);
This command creates a student table with columns for ID, name, age, course, admission date, and city.
4. Inserting Data
To add data to a table, use the INSERT INTO command:
Command:
INSERT INTO student (id, name, age, course, admission_date, city) VALUES
(1, 'Amit Sharma', 20, 'Computer Science', '2023-06-15', 'Mumbai');
This command adds a record to the student table. Tip: Always validate your data before inserting it.
5. Querying Data (DQL)
To retrieve data from a table, use the SELECT command:
Command:
SELECT * FROM student;
SQL Learning Guide
Examples with Clauses:
1. WHERE: SELECT * FROM student WHERE city = 'Mumbai';
2. ORDER BY: SELECT * FROM student ORDER BY age DESC;
3. GROUP BY: SELECT city, COUNT(*) FROM student GROUP BY city;
6. Updating Data (DML)
To update existing data, use the UPDATE command:
Command:
UPDATE student SET city = 'Delhi' WHERE id = 1;
This command updates the city for the student with ID 1 to 'Delhi'.
7. Deleting Data (DML)
To delete data from a table, use the DELETE command:
Command:
DELETE FROM student WHERE id = 2;
This command deletes the record where the student ID is 2.
8. Modifying Table Structure (DDL)
To modify a table, use the ALTER TABLE command:
Command:
SQL Learning Guide
ALTER TABLE student ADD COLUMN email VARCHAR(100);
This command adds an email column to the student table.
9. Managing Transactions (TCL)
To manage transactions, use the TCL commands:
Commands:
BEGIN TRANSACTION;
INSERT INTO student (id, name, age) VALUES (3, 'Neha Gupta', 22);
ROLLBACK;
COMMIT;
This example demonstrates how to add a record but rollback the transaction.
10. Granting Permissions (DCL)
To manage user access, use GRANT and REVOKE commands:
Commands:
GRANT SELECT ON student TO user_name;
REVOKE SELECT ON student FROM user_name;
These commands grant and revoke the SELECT permission on the student table.