0% found this document useful (0 votes)
9 views5 pages

Chapter 4

Chapter 4 covers SQL, a language for managing relational databases, detailing its command types: DDL for structure definition, DML for data manipulation, and DQL for data retrieval. It explains SQL functions, joins for combining tables, subqueries for nested queries, constraints for data integrity, and the importance of transactions and ACID properties for reliability. The chapter provides examples for each command and concept to illustrate their usage.

Uploaded by

ashim05birbhum
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views5 pages

Chapter 4

Chapter 4 covers SQL, a language for managing relational databases, detailing its command types: DDL for structure definition, DML for data manipulation, and DQL for data retrieval. It explains SQL functions, joins for combining tables, subqueries for nested queries, constraints for data integrity, and the importance of transactions and ACID properties for reliability. The chapter provides examples for each command and concept to illustrate their usage.

Uploaded by

ashim05birbhum
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Chapter 4: SQL (Structured Query

Language) - Class 12 Computer Science


4.1 Introduction to SQL
SQL (Structured Query Language) is a language used to store, retrieve, and manipulate
data in relational databases.

4.1.1 Types of SQL Commands

1. DDL (Data Definition Language) → Defines the database structure.


o Commands: CREATE, ALTER, DROP, TRUNCATE
2. DML (Data Manipulation Language) → Modifies data.
o Commands: INSERT, UPDATE, DELETE, SELECT
3. DCL (Data Control Language) → Controls access to the database.
o Commands: GRANT, REVOKE
4. TCL (Transaction Control Language) → Manages transactions.
o Commands: COMMIT, ROLLBACK, SAVEPOINT

4.2 DDL (Data Definition Language) Commands


4.2.1 CREATE Command

Creates a new table in the database.

sql
Copy
CREATE TABLE Students (
ID INT PRIMARY KEY,
Name VARCHAR(50),
Age INT,
Grade CHAR(1)
);

4.2.2 ALTER Command

Modifies an existing table (adds, modifies, or deletes columns).

sql
Copy
ALTER TABLE Students ADD Email VARCHAR(100); -- Adds a new column
ALTER TABLE Students MODIFY Age FLOAT; -- Modifies column datatype
ALTER TABLE Students DROP COLUMN Grade; -- Deletes a column

4.2.3 DROP & TRUNCATE Command

 DROP removes the entire table.


sql
Copy
DROP TABLE Students;

 TRUNCATE deletes all records but keeps the table structure.

sql
Copy
TRUNCATE TABLE Students;

4.3 DML (Data Manipulation Language) Commands


4.3.1 INSERT Command

Adds new records to a table.

sql
Copy
INSERT INTO Students (ID, Name, Age, Grade) VALUES (1, 'Alice', 18, 'A');

4.3.2 UPDATE Command

Modifies existing records.

sql
Copy
UPDATE Students SET Age = 19 WHERE ID = 1;

4.3.3 DELETE Command

Removes records from a table.

sql
Copy
DELETE FROM Students WHERE ID = 1;

4.4 DQL (Data Query Language) - SELECT Command


The SELECT statement is used to retrieve data from tables.

4.4.1 Basic SELECT Query


sql
Copy
SELECT * FROM Students;

This retrieves all columns from the Students table.

4.4.2 Selecting Specific Columns


sql
Copy
SELECT Name, Age FROM Students;

4.4.3 Using WHERE Clause (Filtering Data)


sql
Copy
SELECT * FROM Students WHERE Age > 18;

4.4.4 Using ORDER BY (Sorting Results)


sql
Copy
SELECT * FROM Students ORDER BY Name ASC; -- Sort in ascending order
SELECT * FROM Students ORDER BY Age DESC; -- Sort in descending order

4.4.5 Using DISTINCT (Removing Duplicates)


sql
Copy
SELECT DISTINCT Grade FROM Students;

4.5 SQL Functions


SQL provides built-in functions to perform operations on data.

4.5.1 Aggregate Functions

Function Description Example


COUNT() Counts rows SELECT COUNT(*) FROM Students;
SUM() Adds values SELECT SUM(Age) FROM Students;
AVG() Calculates average SELECT AVG(Age) FROM Students;
MAX() Returns max value SELECT MAX(Age) FROM Students;
MIN() Returns min value SELECT MIN(Age) FROM Students;

4.5.2 String Functions

Function Description Example


LENGTH() Finds length of string SELECT LENGTH(Name) FROM Students;
UPPER() Converts to uppercase SELECT UPPER(Name) FROM Students;
LOWER() Converts to lowercase SELECT LOWER(Name) FROM Students;
SUBSTRING() Extracts part of a string SELECT SUBSTRING(Name, 1, 3) FROM Students;

4.6 SQL Joins


SQL joins are used to combine data from multiple tables based on a related column.

4.6.1 Types of Joins

Type Description
INNER JOIN Returns matching records from both tables.
Returns all records from the left table and matching records from the right
LEFT JOIN
table.
RIGHT Returns all records from the right table and matching records from the left
JOIN table.
FULL JOIN Returns all records from both tables, filling gaps with NULL.

4.6.2 Example of INNER JOIN


sql
Copy
SELECT Students.Name, Marks.Subject, Marks.Score
FROM Students
INNER JOIN Marks ON Students.ID = Marks.StudentID;

4.7 Subqueries in SQL


A subquery is a query inside another query.

4.7.1 Example of Subquery

Find students older than the average age:

sql
Copy
SELECT Name FROM Students WHERE Age > (SELECT AVG(Age) FROM Students);

4.8 SQL Constraints


SQL constraints ensure data integrity in tables.

Constraint Description
PRIMARY KEY Ensures uniqueness for each record.
FOREIGN KEY Establishes relationships between tables.
NOT NULL Ensures a column cannot have NULL values.
UNIQUE Ensures all values in a column are unique.
CHECK Restricts values in a column.
DEFAULT Assigns a default value if none is provided.

Example of Constraints
sql
Copy
CREATE TABLE Students (
ID INT PRIMARY KEY,
Name VARCHAR(50) NOT NULL,
Age INT CHECK (Age >= 18),
Grade CHAR(1) DEFAULT 'B'
);

4.9 Transactions & ACID Properties in SQL


A transaction is a set of SQL commands executed together.

ACID Properties:

1. Atomicity → A transaction is either completed or not executed at all.


2. Consistency → The database remains valid before & after a transaction.
3. Isolation → Transactions do not affect each other.
4. Durability → Once committed, data is permanently saved.

Example of a Transaction
sql
Copy
START TRANSACTION;
UPDATE Accounts SET Balance = Balance - 500 WHERE AccountID = 1;
UPDATE Accounts SET Balance = Balance + 500 WHERE AccountID = 2;
COMMIT; -- Finalizes the transaction

Summary of Chapter 4
 SQL is used to interact with databases.
 DDL Commands (CREATE, ALTER, DROP) define structure.
 DML Commands (INSERT, UPDATE, DELETE) modify data.
 DQL Commands (SELECT, WHERE, ORDER BY) retrieve data.
 Functions (Aggregate, String) help in data manipulation.
 Joins combine data from multiple tables.
 Subqueries allow nested queries.
 Constraints ensure data integrity.
 Transactions & ACID properties maintain database reliability.

You might also like