SQL
SQL
Definition of
(Structured Query Language) is a standard language used to interact with
relational databases.
It allows for:
o Data Definition: Creating and modifying database structures.
o Data Manipulation: Inserting, updating, deleting, and retrieving data.
Key Features of
1. Declarative Nature:
o Focuses on what to do rather than how to do it.
o Example: Retrieve data from a table without specifying algorithms.
2. Standardized Language:
o Supported by most relational database systems like My , Postgre , Oracle, and
Server.
3. Comprehensive:
o Includes commands for querying, updating, and managing data.
Types of Commands
1. Data Definition Language (DDL): Define and modify database structure.
o Commands: CREATE, ALTER, DROP, TRUNCATE.
2. Data Manipulation Language (DML): Work with data in tables.
o Commands: SELECT, INSERT, UPDATE, DELETE.
3. Data Query Language (DQL): Retrieve data from databases.
o Command: SELECT.
4. Transaction Control Language (TCL): Manage database transactions.
o Commands: COMMIT, ROLLBACK, SAVEPOINT.
5. Data Control Language (DCL): Control access to data.
o Commands: GRANT, REVOKE.
.............Code........
CREATE TABLE TableName (
Column1 DataType Constraint,
Column2 DataType Constraint
);
Example:
.............Code........
CREATE TABLE Students (
StudentID INT PRIMARY KEY,
Name VARCHAR(100),
Age INT CHECK (Age > 0),
Department VARCHAR(50)
);
2. ALTER Command
Used to modify an existing table.
Syntax:
.............Code........
ALTER TABLE TableName
ADD ColumnName DataType;
Example:
.............Code........
ALTER TABLE Students
ADD Email VARCHAR(100);
3. DROP Command
Used to delete an entire database or table.
Syntax:
.............Code........
DROP TABLE TableName;
Example:
.............Code........
DROP TABLE Students;
.............Code........
SELECT Column1, Column2 FROM TableName
WHERE Condition;
Example:
.............Code........
SELECT Name, Age FROM Students WHERE Department = 'IT';
2. INSERT Command
Add new records to a table.
Syntax:
.............Code........
INSERT INTO TableName (Column1, Column2)
VALUES (Value1, Value2);
Example:
.............Code........
INSERT INTO Students (StudentID, Name, Age, Department)
VALUES (1, 'Alice', 20, 'IT');
3. UPDATE Command
Modify existing data.
Syntax:
.............Code........
UPDATE TableName
SET Column1 = Value1
WHERE Condition;
Example:
.............Code........
UPDATE Students
SET Age = 21
WHERE StudentID = 1;
4. DELETE Command
Remove specific records from a table.
Syntax:
.............Code........
DELETE FROM TableName WHERE Condition;
Example:
.............Code........
DELETE FROM Students WHERE Age < 18;
.............Code........
CREATE TABLE Books (
BookID INT PRIMARY KEY,
Title VARCHAR(100),
Author VARCHAR(100),
Price DECIMAL CHECK (Price > 0)
);
UPDATE Books
SET Price = 55.00
WHERE BookID = 1;
.............Code........
SELECT * FROM Books WHERE Price > 40;
Homework
1. Design a Students database with fields: StudentID, Name, Age, Department, and
Email.
o Include constraints: StudentID as primary key, Age greater than 0, and unique
Email.
o Write queries to insert data, update a record, and retrieve all students in a
specific department.