Database Notes by Hassaan
Database Notes by Hassaan
Data Definition Language (DDL) is a subset of SQL (Structured Query Language) used to define, manage, and
manipulate database structures. DDL commands are used to create, alter, and delete database objects like
tables, indexes, views, and schemas. Here’s a comprehensive guide covering the theory, syntax, and examples
of each DDL command.
2. Table: A collection of related data entries that consists of columns and rows.
4. TRUNCATE: Used to remove all records from a table, including all spaces allocated for the records.
1. CREATE
The CREATE command is used to create a new table, index, view, or database.
Syntax:
);
Example:
BirthDate date,
HireDate date
);
2. ALTER
The ALTER command is used to modify an existing database object, like a table.
Syntax:
Example:
3. DROP
The DROP command is used to delete an existing database object.
Syntax:
Example:
4. TRUNCATE
The TRUNCATE command is used to remove all records from a table, including all spaces allocated for the
records.
Syntax:
Example:
5. RENAME
Syntax:
Example:
Constraints
Constraints are used to specify rules for data in a table. The following are commonly used constraints:
3. PRIMARY KEY: A combination of a NOT NULL and UNIQUE. Uniquely identifies each row in a table.
BirthDate date,
HireDate date,
DepartmentID int,
);
Views
A view is a virtual table based on the result-set of an SQL statement. It contains rows and columns, just like a
real table. The fields in a view are fields from one or more real tables in the database.
Syntax:
FROM table_name
WHERE condition;
Example:
FROM Employees
Indexes
Indexes are used to retrieve data from the database more quickly than otherwise. The users cannot see the
indexes; they are just used to speed up searches/queries.
Syntax:
Example:
ON Employees (LastName);
Summary
DDL is a crucial part of database management as it defines the schema and structure of the data within the
database. Proper use of DDL commands ensures efficient data organization, integrity, and retrieval.
1. SELECT
The SELECT command is used to retrieve data from one or more tables. It can select specific columns, filter
records, sort results, and join tables.
Syntax:
FROM table_name
WHERE condition
Example:
FROM Employees
WHERE DepartmentID = 1
ORDER BY LastName;
2. INSERT
Syntax:
INSERT INTO table_name (column1, column2, ...)
Example:
INSERT INTO table_name (column1, column2, ...) VALUES (value1, value2, ...): Specifies the columns
and values for the new record.
INSERT INTO table_name VALUES (value1, value2, ...): If all columns are provided in order.
3. UPDATE
Syntax:
UPDATE table_name
WHERE condition;
Example:
UPDATE Employees
WHERE EmployeeID = 1;
SET column1 = value1, column2 = value2, ...: Specifies the columns and new values.
WHERE condition: Specifies which records to update (omit for all records).
4. DELETE
Syntax:
WHERE condition;
Example:
DELETE FROM table_name WHERE condition: Deletes records matching the condition.
1. Joins
Joins are used to combine rows from two or more tables, based on a related column between them.
Types of Joins:
LEFT JOIN (or LEFT OUTER JOIN): Returns all records from the left table and matched records from the
right table.
RIGHT JOIN (or RIGHT OUTER JOIN): Returns all records from the right table and matched records from
the left table.
FULL JOIN (or FULL OUTER JOIN): Returns records when there is a match in either left or right table.
FROM Employees
FROM Employees
2. Aggregate Functions
Aggregate functions perform a calculation on a set of values and return a single value. Common aggregate
functions include COUNT, SUM, AVG, MAX, and MIN.
FROM Employees
WHERE DepartmentID = 1;
SELECT DepartmentID, COUNT(*), AVG(Salary)
FROM Employees
GROUP BY DepartmentID;
GROUP BY: Groups rows that have the same values into summary rows.
3. Subqueries
A subquery is a query within another query. The subquery is executed first, and its result is used by the main
query.
FROM Employees
FROM Employees
Summary
DML commands are crucial for interacting with and manipulating the data stored within a database. Proper
understanding and usage of these commands allow for efficient and accurate data management.
1. Data Selection: Choosing specific data to retrieve from one or more tables.
5. GROUP BY: Groups rows sharing a property so that aggregate functions can be applied.
7. JOIN: Combines rows from two or more tables based on related columns.
The SELECT statement is the cornerstone of SQL queries, used to specify the columns to be retrieved from the
database.
Syntax:
FROM table_name;
Example:
FROM Employees;
SELECT * FROM table_name: Retrieves all columns from the specified table.
2. FROM
The FROM clause specifies the table(s) from which to retrieve data.
Example:
FROM Employees;
3. WHERE
Syntax:
FROM table_name
WHERE condition;
Example:
FROM Employees
WHERE DepartmentID = 1;
Operators: =, <>, >, <, >=, <=, BETWEEN, LIKE, IN, IS NULL.
4. ORDER BY
The ORDER BY clause sorts the result set by one or more columns.
Syntax:
FROM table_name
Example:
FROM Employees
5. GROUP BY
The GROUP BY clause groups rows that have the same values in specified columns into summary rows.
Syntax:
FROM table_name
GROUP BY column1;
Example:
FROM Employees
GROUP BY DepartmentID;
6. HAVING
The HAVING clause filters groups based on specified conditions (similar to WHERE but for groups).
Syntax:
FROM table_name
GROUP BY column1
HAVING condition;
Example:
FROM Employees
GROUP BY DepartmentID
7. JOIN
The JOIN clause combines rows from two or more tables based on related columns.
Types of Joins:
LEFT JOIN (or LEFT OUTER JOIN): Returns all records from the left table and matched records from the
right table.
RIGHT JOIN (or RIGHT OUTER JOIN): Returns all records from the right table and matched records from
the left table.
FULL JOIN (or FULL OUTER JOIN): Returns records when there is a match in either left or right table.
FROM Employees
FROM Employees
8. Subqueries
A subquery is a query nested within another query. It can be used in SELECT, FROM, WHERE, and HAVING
clauses.
FROM Employees
FROM Employees
9. Functions
Examples:
FROM Employees;
SELECT ROUND(Salary, 2)
FROM Employees;
FROM Employees;
o Syntax (MySQL):
FROM table_name
FROM Employees
LIMIT 0, 10;
o Syntax:
o Example:
FROM Employees AS e
o Syntax:
SELECT column1,
CASE
ELSE result3
END AS alias
FROM table_name;
o Example:
CASE
ELSE 'Low'
END AS SalaryLevel
FROM Employees;
Summary
Proper understanding and usage of these components and techniques are crucial for efficient and accurate
data retrieval from databases.
2. Privileges: Specific rights granted to users or roles, such as SELECT, INSERT, UPDATE, DELETE.
3. Roles: Named groups of related privileges that can be granted to users or other roles.
1. GRANT
Syntax:
ON object
privilege: The specific privilege to be granted (e.g., SELECT, INSERT, UPDATE, DELETE).
object: The database object on which the privilege is to be granted (e.g., table, view).
WITH GRANT OPTION: Allows the user or role to grant the specified privilege to others.
Example:
-- Grant SELECT and INSERT privileges on the Employees table to user 'JohnDoe'
ON Employees
TO JohnDoe;
GRANT SELECT
ON Employees
TO HR_Role;
-- Grant ALL privileges on the Employees table to user 'AdminUser' with the ability to grant these privileges to
others
ON Employees
TO AdminUser
2. REVOKE
The REVOKE command is used to remove specific privileges from users or roles.
Syntax:
ON object
privilege: The specific privilege to be revoked (e.g., SELECT, INSERT, UPDATE, DELETE).
object: The database object from which the privilege is to be revoked (e.g., table, view).
Example:
-- Revoke SELECT and INSERT privileges on the Employees table from user 'JohnDoe'
ON Employees
FROM JohnDoe;
REVOKE SELECT
ON Employees
FROM HR_Role;
-- Revoke ALL privileges on the Employees table from user 'AdminUser'
ON Employees
FROM AdminUser;
Roles simplify the management of user permissions by grouping related privileges together. Instead of
granting and revoking privileges individually for each user, you can grant and revoke roles.
Creating Roles:
Granting Roles:
sql
Copy code
GRANT role_name
TO user_name;
Revoking Roles:
REVOKE role_name
FROM user_name;
Examples:
-- Grant SELECT and UPDATE privileges on the Employees table to the 'HR_Manager' role
ON Employees
TO HR_Manager;
TO JaneDoe;
REVOKE HR_Manager
FROM JaneDoe;
Additional Considerations
Database Security: DCL commands are essential for database security, ensuring that only authorized
users have access to perform specific actions on database objects.
Granting Privileges with Hierarchies: Privileges can be granted hierarchically, meaning a user with the
WITH GRANT OPTION can grant the privilege to other users.
Cascading Revokes: When a privilege is revoked, all subsequent privileges that were granted based on
the revoked privilege are also revoked.
Summary
Roles: Named groups of related privileges that simplify user permission management.
Database Security: Ensures that only authorized users have access to specific database objects and
actions.
DCL commands are crucial for managing database security and access control, ensuring that data is protected
and only accessible to authorized users. Proper use of GRANT and REVOKE commands allows for effective
management of user permissions and roles in a database environment.