DBS 3rd Module Notes
DBS 3rd Module Notes
UNIT-III Syllabus
Relational Data Model: Relation, Integrity constraints-domain, entity and Referential integrity
constraints, Basic Relational Algebra operations, select, project and join operations
(Textbook1:Chapter6: 6.1 to 6.3). Functional dependencies and Normalization for Relational
Databases, Normalization concepts, first, second, third normal forms, Boyce-Codd normal
form.(Text book1:Chapter15:15.1to15.6) SQL: Queries, sub queries, correlated sub query, views,
updating of a database through views, Update, Delete.(Textbook1:Chapter4:4.1to4.5)
Relational Model:
Constraints:
o Domain Constraints
o Key (Uniqueness) Constraints
o Entity Integrity Constraints
o Referential Integrity Constraints
Domain Constraint:
Domain constraints make sure each column has simple, single values.
-valued data is not allowed.
123456789
01 Bikash Dutta
234456678
Explanation: In the above relation, Name is a composite attribute and Phone is a multi-values attribute,
so it is violating domain constraint.
Key Constraints:
Key constraints make sure each row in a table is unique.
A table can have many candidate keys.
We choose one of the candidate keys as the primary key.
It’s better to choose a key with fewer columns as the primary key.
The primary key cannot have null values.
So, the Not Null rule is also part of the key constraint.
Example:
Ms. Smita VK, Asst. Prof. , DSU Page 2
DATABASE MANAGEMENT SYSTEM (23CA2403)
01 Bikash 6000000009
02 Paul 9000090009
01 Tuhin 9234567892
Explanation: In the above table, EID is the primary key, and the first and the last tuple have the same value
in EID ie 01, so it is violating the key constraint.
01 Bikash 9000900099
02 Paul 600000009
Explanation: In the above relation, EID is made the primary key, and the primary key can’t take NULL
values but in the third tuple, the primary key is null, so it is violating Entity Integrity constraints.
01 Divine 12
02 Dino 22
04 Vivian 14
Explanation: In the above tables, the DNO of Table 1 is the foreign key, and
DNO Place DNO in Table 2 is the primary key. DNO = 22 in the foreign key of Table 1 is
not allowed because DNO = 22 is not defined in the primary key of table 2.
Therefore, Referential integrity constraints are violated here.
12 Jaipur
13 Mumbai
14 Delhi
Relational Algebra is a formal system for manipulating and querying relational databases. It
provides a set of operations that take one or more relations (tables) as input and produce a new
relation as output. Relational algebra is the theoretical foundation of relational databases and is used
for querying and managing data.
It was introduced by Edgar F. Codd in 1970 as part of his relational model for databases. The main
purpose of relational algebra is to offer a theoretical framework for querying databases, ensuring
that operations on data follow a set of principles that make querying efficient and logical.
Basic Concepts
In relational algebra, operations are used to manipulate relations (tables). These operations can be
classified into two types based on the number of relations they operate on: unary and binary
operations.
1. Unary Operations
Unary operations in relational algebra are operations that operate on only one relation at a time.
These operations are applied to a single relation and produce a new relation as the result.
a. SELECTION (σ):
o The selection operation is used to filter rows from a relation based on a given
condition.
o It selects a subset of rows that satisfy a specified predicate.
o Syntax: σ <selection condition> (Relation)
where,
The symbol σ(sigma) is used to denote the SELECT operator
The selection condition is a Boolean (conditional) expression specified on the attributes of
relation R
Tuples that make the condition true are selected
appear in the result of the operation
Tuples that make the condition false are filtered out
discarded from the result of the operation
Examples:
1. Select the EMPLOYEE tuples whose department number is 4.
σDNO = 4 (EMPLOYEE)
3. Select the tuples for all employees who either work in department 4 and make over
$25,000 per year, or work in department 5 and make over $30,000
σ (Dno=4 AND Salary>25000) (EMPLOYEE)
b. PROJECTION (π):
o The projection operation is used to select specific columns from a relation.
o It removes duplicate rows and keeps only the specified attributes.
o Syntax: π <attribute list> (Relation)
where
symbol π (pi)used to represent the PROJECT operation
<attributelist> - desired sublist of attributes from the attributes of relation R.
The result of the PROJECT operation has only the attributes specified in <attribute
list> in the same order as they appear in the list. Hence, its degree is equal to the
number of attributes in <attribute list>
Example:
π Name,Age(Employees)
would to the following SQL query:
SELECT Name, Age FROM Employees;
This means we are selecting the Name and Age columns from the Employees relation
and removing any duplicate rows.
Result of Projection:
Name Age
Alice 30
Bob 25
Charlie 28
David 35
Eve 29
c. RENAMING (ρ):
o The renaming operation is used to rename the attributes of a relation or even the
entire relation.
o This is useful when we need to avoid ambiguity or for clarity in further operations.
o Syntax: ρ_new_relation_name(new_attribute1, new_attribute2)(Relation)
Where:
Example:
2. Binary Operations
Binary operations in relational algebra involve two relations. These operations combine, compare,
or relate two different relations to produce a new relation as the result.
The union operation combines the tuples of two relations, eliminating duplicate
tuples. Both relations must have the same number of attributes and corresponding
attributes must have the same domain.
Syntax: Relation1 ∪ Relation2
B. INTERSECTION (∩):
The intersection operation returns the tuples that appear in both relations. Similar to
union, the two relations must have the same number of attributes.
Syntax: Relation1 ∩ Relation2
C. DIFFERENCE/MINUS (−):
The difference operation returns the tuples that are in the first relation but not in the
second relation.
Syntax: Relation1 − Relation2
The Cartesian product operation combines every tuple from the first relation with every
tuple from the second relation. This results in a relation that contains all possible pairs of
tuples from the two relations.
Syntax: Relation1 × Relation2
Employees: Departments:
E. JOIN (𝔚):
In relational algebra, the join operation combines two relations based on a common attribute
or condition. This operation allows you to merge tuples from different relations in a meaningful
way, creating new relations. There are several types of joins in relational algebra, each serving
different purposes for combining data from multiple relations.
A Theta Join is the most general type of join. It allows you to combine tuples from two relations
based on a condition (θ) that can be any comparison operator, such as =, >, <, !=, etc.
The condition can be any boolean expression that compares attributes from the two relations, not
necessarily equality.
Example:
Employees: Departments:
102 Bob 40 2 35
An Equi Join is a special case of the Theta Join where the condition is specifically an equality
condition (=) between attributes from both relations.
The condition used in an equi join is always equality (=) between attributes from the two
relations.
Example:
Employees: Departments:
Employees.Department_ID = Departments.Department_ID:
102 Bob 2 IT
103 Charlie 1 HR
A Natural Join is a type of Equi Join, but with two key differences:
1. It automatically joins the two relations on all attributes with the same name.
2. It removes duplicate columns that are common between the two relations.
The condition is based on equality of all common attributes between the two relations, and it
automatically eliminates duplicate columns in the result.
Example:
Employees: Departments:
101 Alice 1 HR
102 Bob 2 IT
103 Charlie 1 HR
4. Outer Joins
Outer joins are extensions of the join operation that include unmatched tuples from one or both
relations in the result. There are three types of outer joins:
A left outer join returns all tuples from the left relation (Relation1), and the matching tuples from
the right relation (Relation2). If there is no match, the result will contain NULL values for
attributes of the right relation.
Employees: Departments:
A right outer join returns all tuples from the right relation (Relation2), and the matching tuples
from the left relation (Relation1). If there is no match, the result will contain NULL values for
attributes of the left relation.
Let's combine selection (σ) and projection (π) operations with the binary operations (union,
intersection, difference) in relational algebra to see how these can be applied together. This will
allow us to filter data (selection) and choose specific columns (projection) while combining or
manipulating relations.
Employees: Contractors:
104 David 35 HR
103 Charlie 28 IT
Example: Let’s select employees older than 30 and contractors from the HR department, and then
find the common tuples between these two relations.
The difference (−) operation returns tuples that are in the first relation but not in the second. By
applying selection first, you can filter the data before performing the difference.
Example: Let’s find employees who are older than 30 but not contractors in the IT department.
Relational Algebra: σ(Age > 30)(Employees) − σ(Department = 'IT')(Contractors)
The Division operation in relational algebra is used to handle queries that involve finding tuples
from one relation that are related to all tuples in another relation. It is a bit more specialized than
other operations like Selection or Union. Division is typically used to solve queries like "find all
employees who work in all departments" or "retrieve all products that have been supplied by every
supplier."
Concept of Division
In simple terms, division is used when you want to retrieve rows from one relation that are associated
with all tuples in another relation. It is applied between two relations: one representing the dividend
(the relation from which you want to select) and the other representing the divisor (the relation
whose tuples must be fully matched with those in the dividend).
The result of a division operation is a new relation that contains all tuples from the dividend relation
that are associated with every tuple in the divisor relation.
Syntax: Dividend ÷ Divisor
Where:
Dividend is the relation containing the tuples you are interested in.
Divisor is the relation whose tuples should be matched against all tuples in the dividend.
1. Projection: First, you project the attributes from the dividend that are not present in the
divisor. These are the attributes you are interested in finding.
2. Difference: Then, you find the tuples in the dividend that have all the corresponding values
in the divisor.
Example of Division
Let's take an example to better understand how division works.
Employee_ID Department_ID
1 D1
1 D2
2 D1
2 D2
3 D1
Relation 2 (Departments): This relation contains the departments we are interested in.
Department_ID
D1
D2
Problem: Find all employees who work in all departments (i.e., both D1 and D2).
Step 1: Project the attributes of the Employee_Department relation that are not in the
Departments relation. Here, the only attribute we care about is Employee_ID.
Step 2: Divide Employee_Department by Departments.
Employee_ID
1
2
Explanation:
X→Y
This means:
If two tuples (rows) have the same values for X, they must have the same values for Y.
X is called the determinant (left-hand side).
Y is called the dependent (right-hand side).
Example
Consider a relation STUDENT (Roll_No, Name, Course, Age) with the following data:
If two students have the same Roll_No, they must have the same Name, Course, and
Age.
A dependency is trivial if the right-hand side (Y) is a subset of the left-hand side (X).
Example:
o {A,B}→A is trivial because A is already in the left-hand side.
They are essential in database normalization, helping to minimize redundancy and ensure data
consistency.
Armstrong’s Axioms
1. Reflexivity
2. Augmentation
3. Transitivity
4. Union (Combination)
5. Decomposition (Projection)
6. Pseudo transitivity
Ms. Smita VK, Asst. Prof. , DSU
DATABASE MANAGEMENT SYSTEM (MMC103)
If X → Y and WY → Z, then WX → Z.
Example:
o If A → B and CB → D, then CA → D.
7. Composition
If X → Y and A→ B then XA→ YB
A Candidate Key is a minimal set of attributes that uniquely identify each tuple (row) in a
relation (table). It is the potential Primary Key, but only one candidate key is chosen as the
Primary Key.
Given Relation:
R (A, B, C, D, E)
1. A→B
2. A→C
3. CD→E
4. E→D
The closure of an attribute set X (denoted as X⁺) is the set of attributes functionally determined
by X.
Find A⁺ (Closure of A)
CD→E (given)
E→D (given, so E also determines D)
CD⁺ = {C, D, E}
CD⁺ does not include A or B, so CD is not a super key.
Prime and Non-Prime attributes are important in Normalization (2NF, 3NF, BCNF) to eliminate
redundancy and avoid anomalies.
1. A→B
2. B→C
3. A,D→C
Normalization is a process in database design that organizes data in a way that reduces
redundancy and ensures data integrity. The goal of normalization is to decompose a large, complex
table into smaller, more manageable tables while minimizing data anomalies such as update
anomalies, insert anomalies, and delete anomalies.
1. Reduce Data Redundancy: Avoid storing the same data multiple times.
2. Improve Data Integrity: Maintain consistency and accuracy by organizing data
logically.
3. Simplify Maintenance: Makes it easier to maintain, update, and delete data in a
structured way.
Without normalization, databases tend to have redundant data, leading to inconsistency, and
issues like:
Update Anomalies: Updating one piece of data in one place and forgetting to update it
elsewhere.
Insert Anomalies: Unable to insert certain data because the table structure does not
allow it.
Delete Anomalies: Deleting data can lead to loss of other important information.
Suppose we have the following Employee-Department table that contains employee details
along with department information:
1. Update Anomaly
An update anomaly occurs when updating a piece of data in one place, but forgetting to update it
elsewhere. This happens when redundant data is stored in multiple rows of the table, leading to
inconsistency.
Suppose the HR department changes its name to Human Resources. In the original table, there
are multiple employees in the HR department. If we forget to update the department name in one
of the rows, the data becomes inconsistent.
UPDATE Employee_Department
SET Dname = 'Human Resources'
WHERE Dept = 'D1';
However, imagine we forget to update the row for Bob, and his department name remains HR
while others are updated to Human Resources.
Inconsistent data: We have HR and Human Resources in the Dname column for the
same department, D1.
This is an update anomaly because updating the department name in some rows but not others
leads to inconsistent data.
2. Insert Anomaly
An insert anomaly occurs when certain data cannot be inserted into the table due to the way the
table is structured.
Let’s say we want to add a new department, Sales (with a department code D4), but no employee
currently belongs to that department.
In our original table, we are required to insert a dummy employee to insert the new department,
which is an undesirable behavior.
Insert fails because the table has a non-null Eno and Ename (employee-related
columns), and we cannot insert a row with null values for these columns.
To Work Around:
Unnecessary data insertion: A dummy employee (ID 999) was inserted to allow the
Sales department to be recorded.
This is an insert anomaly because we had to insert redundant data (a dummy employee) just to
add a new department. The table structure forces us to insert unnecessary data to maintain the
integrity of the table.
3. Delete Anomaly
A delete anomaly occurs when deleting data results in the unintentional loss of other important
data. This happens when related data is stored together in the same table.
Let’s say we want to delete the record for John (employee Eno = 101) who works in the HR
department.
If we delete John's row, we lose not only his information but also the department information
(since it's in the same table).
Lost Department Information: After deleting John, the HR department information (i.e.,
Dept = D1, Dname = HR) is still present for other employees (Bob, David), but we lose
the knowledge that HR exists because we didn’t store it separately.
If Bob and David were also deleted, the HR department would be lost completely, causing a
delete anomaly.
This happens because HR is redundantly stored for every employee in the department. Deleting
one employee (or a few) might unintentionally remove important department information.
To avoid these anomalies, the table should be normalized. Here’s how normalization can solve
these issues:
1. Update Anomaly: By splitting the department information into a separate table, we avoid
having to repeat it for each employee. The department name only needs to be updated in
one place.
2. Insert Anomaly: By separating the department information, we can insert a new
department without needing to create a dummy employee.
3. Delete Anomaly: By separating the department information into its own table, we can
delete employee data without accidentally losing department data.
Employee Table:
Eno Ename Dept
101 John D1
102 Alice D2
103 Bob D1
104 Carol D3
105 David D1
Department Table:
Dept Dname
D1 HR
D2 Finance
D3 IT
Example:
Consider the following Unnormalized Table for students and their courses:
In this table, the Courses column contains multiple values (a list of courses), which violates the
1NF rule.
To convert this into 1NF, we split the multiple values into separate rows:
Table in 1NF:
Student_ID Student_Name Course
101 John Math
101 John Science
102 Alice History
102 Alice English
103 Bob Math
103 Bob Chemistry
Example:
Consider a table storing student course registrations, where Student_ID and Course_Code
together form the primary key:
In this case, Instructor and Instructor_Phone depend only on the Course_Code, not on the
entire primary key. This violates 2NF.
The Instructor and Instructor_Phone are now dependent only on Course_Code, and not on
Student_ID.
Example:
Here, Dept_Location and Dept_Manager depend on the Department, not directly on the
Employee_ID. Therefore, Dept_Location and Dept_Manager are transitively dependent on
Employee_ID through Department.
To convert this into 3NF, we separate the department information into its own table:
Table 1: Employee Information
Now, there are no transitive dependencies, and the tables are in 3rd Normal Form (3NF).
Ms. Smita VK, Asst. Prof. , DSU
DATABASE MANAGEMENT SYSTEM (MMC103)
Boyce-Codd Normal Form (BCNF): Eliminate All Functional Dependencies
Example:
In this case, Course_ID determines Instructor and Room, but Course_ID is not a candidate
key (because the primary key is a composite of Student_ID and Course_ID).
Now, the Course_ID in Table 2 is a candidate key, and the tables are in BCNF
The Fourth Normal Form (4NF) is an advanced level of database normalization that deals with
Multi-Valued Dependencies (MVDs). Even if a table is in BCNF, it may still have redundancy
due to MVDs, which can lead to data anomalies.
Conditions for 4NF
MVD Notation:
If A →→ B, then for each A value, there exist multiple independent values of B, regardless of
other attributes.
Example: USN, Name, and Course (Before 4NF)
Consider a university database where students (USN, Name) can enroll in multiple courses. If
stored improperly, this can lead to redundancy.
Converting to 4NF
USN Name
U101 Sneha
U102 Swati
USN Course
U101 DBMS
U101 AI
U102 DBMS
U102 ML
The Fifth Normal Form (5NF), also known as Project-Join Normal Form (PJNF), is used to
eliminate redundancy caused by join dependencies (JDs). Even if a table is in 4NF, it may still
have redundancy due to complex relationships between attributes.
1. It is in 4NF
2. It has no join dependencies (JDs) that cause redundancy
A Join Dependency (JD) occurs when a relation (table) can be decomposed into two or more
smaller relations and reconstructed by joining them without losing any data.
Let's say we have the following table for students, courses, and instructors:
Observations:
Join Dependency (JD) exists, meaning we can decompose this table into smaller tables
without losing information.
U102 ML ML Prof. D
This incorrectly assigns students to instructors they never had! For example, U102 was
never taught DBMS by Prof. C, but this tuple appears in the incorrect result.
To correctly normalize into 5NF and avoid spurious tuples, we need three tables:
USN Course
U101 DBMS
U101 AI
U102 DBMS
U102 ML
Course Instructor
DBMS Prof. A
DBMS Prof. C
AI Prof. B
ML Prof. D
Since USN and Course match in both tables, the join correctly preserves original records.
Since Course and Instructor match in both tables, no extra tuples appear.
This exactly matches the original table, confirming the decomposition is correct!
3. 5NF ensures that there are no extra or missing records while eliminating redundancy.
SQL: Queries, sub queries, correlated sub query, views, updating of a database through views,
Update, Delete.
OVERVIEW OF SQL QUERY LANGUAGE
SQL (Structured Query Language) is the standard language for managing and manipulating
relational databases. It is a declarative language, meaning users specify what data they want, rather
than how to get it. SQL allows users to interact with databases by performing operations such as
data retrieval, insertion, updating, deletion, and database management.
Querying databases
Inserting, updating, and deleting records
Creating and modifying database structures (tables, schemas)
Creating views, triggers, and stored procedures
KEY COMPONENTS:
SQL supports various data types that define what kind of data a column in a table can store.
Different data types help in maintaining the integrity of data by ensuring that only appropriate
types of values are stored in each column. These data types are typically categorized into numeric
types, character types, date and time types, boolean types, and binary types.
These types are used to store numbers, both integers and floating-point numbers.
INT (Integer): Used to store whole numbers (positive, negative, or zero) without decimals.
o Example: INT is typically used for storing employee IDs or age.
o Range: Typically from -2,147,483,648 to 2,147,483,647 (depends on the database
system).
FLOAT: Used to store approximate numeric values with floating decimal points.
o Example: Used to store scientific data or measurements with decimals, like 3.14 or
-0.006.
o Range: Typically stores up to 7 decimal digits.
DECIMAL or NUMERIC: Stores exact numeric values with a specified precision and
scale. Unlike FLOAT, it ensures precise calculations.
o Example: Used to store financial data, like monetary values (DECIMAL(10, 2)
means 10 digits in total, with 2 digits after the decimal point).
o Syntax: DECIMAL(precision, scale) where precision is the total number of digits,
and scale is the number of digits to the right of the decimal.
DOUBLE (Double Precision): Used to store approximate numeric values, with a larger
precision than FLOAT.
o Example: Used for precise scientific calculations that require more decimal places
than FLOAT.
These types are used to store text data, such as names, descriptions, and other strings.
CHAR (Fixed Length): Used to store fixed-length strings. If the string is shorter than the
defined length, it will be padded with spaces.
o Example: CHAR(10) will always store 10 characters. If the string is shorter, the
remaining space will be filled with spaces.
o Use Case: Ideal for storing fixed-length values, like state abbreviations (e.g., NY,
CA).
VARCHAR (Variable Length): Used to store variable-length strings. It stores only the
characters that are actually used, saving space.
o Example: VARCHAR(100) can store a string of any length up to 100 characters.
o Use Case: Used for text fields like names, addresses, and email addresses.
TEXT: Used to store large amounts of text. Unlike CHAR or VARCHAR, the TEXT type
is used for storing long strings, such as large descriptions or articles.
o Example: Used for storing comments or large textual content.
o Use Case: Ideal for storing long notes, articles, or descriptions that exceed the
limits of VARCHAR.
These types are used to store binary data, such as images, files, or multimedia.
BLOB (Binary Large Object): Used to store large binary objects like images, audio files,
or any kind of file data.
o Example: Storing an image file or a PDF document.
o Use Case: Ideal for storing files that need to be retrieved and used within the
application.
VARBINARY: Similar to BLOB, but with a variable length. Used to store binary data
where the length of the data can vary.
o Example: Used to store encrypted data or file paths in binary format.
SQL CATEGORIES:
DDL commands are used to define and manage the structure of the database objects (such as
tables, schemas, and indexes). They do not manipulate the data itself, but rather the schema and its
components.
A. CREATING A DATABASE
The CREATE DATABASE statement is a foundational SQL command used to create new
databases in SQL-based Database Management Systems (DBMS), including MySQL, PostgreSQL,
SQL Server, and others. Understanding how to use this command effectively is crucial for
developers, database administrators, and anyone working with relational databases.
Syntax:
CREATE DATABASE database_name;
Example:
CREATE DATABASE College;
1. Naming Conventions: Use unique, descriptive names without spaces (use underscores).
Example: my_database.
2. Max Length: Database names are typically limited to 128 characters.
3. Permissions: You need administrative privileges to create a database.
4. Case Sensitivity: Most SQL systems are case-insensitive, but it’s best to use lowercase
consistently.
5. Cross-DB Compatibility: While the syntax is similar across systems (e.g., MySQL,
PostgreSQL, SQL Server), some systems have specific options (like character set or
storage engine).
In MongoDB, you don’t need to explicitly create a database; just use the use command to switch
to a database, and MongoDB will create it when you insert data.
Syntax:
use database_name;
Example:
use College;
We use the SHOW DATABASES command and it will return a list of databases that exist in our
system. Now we will look for the name of the database (GeeksForGeeks) that we have just created.
SQL Query:
SHOW DATABASES;
CREATE TABLE
SQL Syntax to Create a Table:
Steps:
1. Define the table's structure: List the columns with their data types.
2. Specify constraints: Indicate primary keys, unique fields, or not-null constraints.
B. ALTER TABLE
The most common use of ALTER is to modify a table. Here's how you can use it in different
scenarios
ADD A COLUMN
You can add one or more columns to an existing table using the ALTER TABLE statement with
the ADD keyword.
This adds a new column named email with a VARCHAR(100) data type to the students
table.
DROP A COLUMN
You can remove a column from a table using the ALTER TABLE statement with the DROP COLUMN
keyword.
You can change the data type or size of an existing column using the ALTER TABLE statement with
the MODIFY or CHANGE keyword (depending on the DBMS).
This changes the age column data type to INT (if it was something else previously).
ALTER TABLE table_name ALTER COLUMN column_name SET DATA TYPE new_datatype;
Example (POSTGRESQL):
RENAME A COLUMN
You can rename an existing column using the ALTER TABLE statement with the RENAME COLUMN
keyword.
Syntax (PostgreSQL):
Example:
RENAME A TABLE
You can rename a table using the ALTER TABLE statement with the RENAME TO keyword.
Important Considerations:
Irreversible: Dropping a database will remove all data and structures associated with it, so
ensure that you have a backup if necessary.
Permissions: You need administrative privileges to drop a database.
Make sure that no active connections are using the database, as some systems may prevent
dropping a database if it is in use.
Drop table
To drop a table in SQL, you use the DROP TABLE statement. This command permanently
deletes the table along with all the data and structure it contains.
Important Considerations:
Irreversible: Dropping a table will permanently remove the table and all its data. Make
sure to back up the data if needed.
Permissions: You need appropriate privileges to drop a table.
Dependencies: If there are foreign keys or other dependencies (e.g., views, triggers), you
may need to drop them first or adjust them before dropping the table.
D. TRUNCATE IN SQL
The TRUNCATE command in SQL is used to remove all rows from a table quickly, while keeping
the table structure intact. This means that the table itself remains available for use, but all the data
within it is deleted.
Example:If we have a table students and we want to remove all records from it, we would use:
2. DML COMMANDS
DML (Data Manipulation Language) commands in SQL are used to manipulate and manage data
within existing database tables. These commands are focused on inserting, updating, deleting, and
querying the data. DML operations are typically non-structural and affect only the data in the table,
not the schema or the table structure itself.
1. INSERT
2. SELECT
3. UPDATE
4. DELETE
1. INSERT
The INSERT command is used to add new rows of data into a table.
Syntax:
Example:
This adds a new row to the students table with the provided values.
2. SELECT
The SELECT command is used to retrieve data from one or more tables. You can use different
clauses like WHERE, ORDER BY, GROUP BY, etc., to filter, sort, or group the results.
Syntax:
Example:
SELECT first_name, last_name, age FROM students WHERE age > 21;
This retrieves the first_name, last_name, and age of students whose age is greater than
21.
3. UPDATE
The UPDATE command is used to modify the existing data in a table. It allows you to change values
in one or more columns for specific rows.
Syntax:
UPDATE table_name
SET column1 = value1, column2 = value2, ...
WHERE condition;
Example:
UPDATE students
SET age = 23
WHERE student_id = 1;
Note: Always use the WHERE clause to specify which records should be updated. Without it, all
rows in the table will be updated.
4. DELETE
The DELETE command is used to remove rows from a table. You can use the WHERE clause to
specify which rows to delete.
Syntax:
Example:
2. SELECT: To get the names and ages of students who are 22 or older:
SELECT first_name, last_name, age FROM students WHERE age >= 22;
UPDATE students
SET age = 22
WHERE student_id = 3;
CLAUSES IN SQL
In SQL, clauses are the components of a query that define its structure and functionality. They
specify the conditions, filters, sorting, grouping, and relationships between data. Here are the main
SQL clauses
1. SELECT Clause
2. FROM Clause
3. WHERE Clause
The WHERE clause in SQL filters rows based on conditions. It supports comparison operators (=, >,
<, etc.), logical operators (AND, OR, NOT), and special operators like IN, BETWEEN, LIKE, and IS
NULL for advanced filtering.
Example:
SELECT *
FROM employees
WHERE salary > 50000;
The GROUP BY clause in SQL is used to group rows that have the same values in specified columns.
It is often used with aggregate functions like COUNT(), SUM(), AVG(), MAX(), and MIN() to
summarize data within each group.
The GROUP BY clause must be used after the WHERE clause and before the ORDER BY clause. It
can be combined with the HAVING clause to filter grouped data based on conditions. Only columns
listed in the GROUP BY clause or used in aggregate functions can appear in the SELECT statement.
Syntax
Output: Groups employees by department and counts the number of employees in each.
Output: Groups rows by department and calculates the average salary for each.
5. HAVING Clause
The HAVING clause in SQL is used to filter grouped data after applying the GROUP BY clause. Unlike
the WHERE clause, which filters rows before grouping, the HAVING clause filters groups after
aggregation. It is often used with aggregate functions like COUNT(), SUM(), and AVG() to apply
conditions to grouped results.
Syntax
6. ORDER BY Clause
The ORDER BY clause in SQL is used to sort the result set in either ascending (ASC) or
descending (DESC) order. By default, the sorting is in ascending order. It can be applied to one
or more columns to arrange the data in a specific sequence.
Syntax
SELECT column1, column2
FROM table_name
ORDER BY column1 [ASC|DESC], column2 [ASC|DESC];
SELECT *
FROM employees
ORDER BY salary DESC;
SELECT *
FROM employees
ORDER BY department ASC, salary DESC;
SET OPERATIONS
Set operations in SQL are used to combine the results of two or more SELECT queries. They allow
you to perform operations like union, intersection, and difference on result sets. The most common
set operations in SQL are:
1. UNION
2. UNION ALL
3. INTERSECT
4. EXCEPT (or MINUS in some databases)
1. UNION
The UNION operator combines the results of two SELECT queries and removes duplicates. The
columns must have the same names and data types in both queries.
Alice, Charlie, and Eve appear in both tables, but only one instance of each is included in
the result.
2. UNION ALL
The UNION ALL operator combines the result sets of two queries but does not remove duplicates.
SELECT EmployeeName
FROM Employees
UNION ALL
3. UNION
The UNION operator combines the results of two SELECT queries and removes duplicates. The
columns must have the same names and data types in both queries.
Alice, Charlie, and Eve appear in both tables, but only one instance of each is included in
the result.
4. UNION ALL
The UNION ALL operator combines the result sets of two queries but does not remove duplicates.
Notice that Alice, Charlie, and Eve appear twice, as UNION ALL doesn't eliminate
duplicates.
5. INTERSECT
The INTERSECT operator returns only the rows that are present in both result sets.
Only Alice, Charlie, and Eve appear in both the Employees and Customers tables, so
they are returned.
The EXCEPT operator returns the rows from the first query that do not exist in the second query. It
removes any common rows.
Only Bob and David appear in the Employees table but not in the Customers table, so they
are returned.
AGGREGATE FUNCTIONS
Aggregate functions in SQL are used to perform calculations on a set of values and return a single
value. These functions are commonly used with the GROUP BY clause to summarize or group data,
but they can also be used without grouping.
1. COUNT()
2. SUM()
3. AVG()
4. MIN()
5. MAX()
1. COUNT(): The COUNT() function returns the number of rows that match a specified condition
or the total number of rows in a column.
Syntax:
COUNT(column_name)
Example:
SaleID Product Amount
1 A 100
2 B 150
3 A 200
4 C 120
5 A 180
If you want to count how many sales of product "A" there were:
2. SUM(): The SUM() function returns the total sum of a numeric column.
Syntax:
SUM(column_name)
Example:
To find the total amount of sales in the Sales table:
TotalAmount
SELECT SUM(Amount) AS TotalAmount
FROM Sales; 750
3. AVG(): The AVG() function returns the average value of a numeric column.
Syntax:
AVG(column_name)
Example:
To find the average sales amount:
Syntax:
MIN(column_name)
Example:
To find the minimum sales amount:
MIN() works on numeric, date, or text columns and returns the smallest value.
Syntax:
MAX(column_name)
Example:
To find the maximum sales amount:
MaxAmount
SELECT MAX(Amount) AS MaxAmount 200
FROM Sales;
TRIGGERS
A trigger in SQL is a special kind of stored procedure that automatically executes (or fires) in
response to certain events on a table or view. Triggers can be set to run before or after an INSERT,
UPDATE, or DELETE operation is performed on a table.
Trigger Types:
EXAMPLE
DELIMITER //
Create trigger T
after insert on voter
for each row
begin
update constituency set no_of_voters=no_of_voters+1 where
cons_id=new.cons_id;
end
//
insert into voter values(300,'suresh',25,'chikodi',111,121);
//
select *from constituency;
//
VIEWS
A view in SQL is a virtual table that provides a way to simplify complex queries, encapsulate logic,
and re-use SQL code. It doesn't store data itself but displays data from one or more tables or other
views. Views are commonly used to simplify complex queries, aggregate data, or hide specific data
from the end user.
Virtual Table: A view behaves like a table but doesn't physically store data. Instead, it
Ms. Smita VK, Asst. Prof. , DSU
DATABASE MANAGEMENT SYSTEM (MMC103)
stores a query that retrieves data when accessed.
Simplifying Queries: Views can simplify complex queries, so users don't need to write the
same SQL repeatedly.
Security: Views can be used to restrict access to specific columns or rows of a table,
providing an abstraction layer.
Creating a View
Syntax:
CREATE VIEW view_name AS
SELECT column1, column2, ...
FROM table_name
WHERE condition;
Example 1: Simple View
Suppose we have the following Employees table:
To create a view that only shows the names and salaries of employees:
Name Salary
Alice 50000
Bob 60000
Charlie 45000
David 65000
Eve 55000
You can create views that include filters. For example, let's create a view for employees with a
salary greater than 55,000.
Now, querying the HighEarners view will only return employees with a salary above 55,000:
Dropping a View
If you no longer need a view, you can remove it using the DROP VIEW statement:
Limitations of Views
1. Read-Only Views: Some views are read-only if they involve certain operations (such as
joins or aggregations). You cannot update or insert data into these views directly.
2. Performance: Since views are virtual and executed at runtime, they may impact
performance when dealing with complex queries.
3. No Indexing: Views do not have indexes, although indexes on underlying tables can still
affect performance.
4. Not Persistent Data: Views do not store data themselves; they are always generated on the
fly when queried.
Important Rule:
A subquery can be placed in a number of SQL clauses like WHERE clause, FROM clause,
HAVING clause.
You can use Subquery with SELECT, UPDATE, INSERT, DELETE statements along with
the operators like =, <, >, >=, <=, IN, BETWEEN, etc.
A subquery is a query within another query. The outer query is known as the main query,
and the inner query is known as a subquery.
Subqueries are on the right side of the comparison operator.
A subquery is enclosed in parentheses.
In the Subquery, ORDER BY command cannot be used. But GROUP BY command can be
used to perform the same function as ORDER BY command.
SQL subqueries are most frequently used with the Select statement.
Syntax
SELECT column_name
FROM table_name
WHERE column_name expression operator
( SELECT column_name from table_name WHERE ... );
Example
Consider the EMPLOYEE table have the following records:
1 John 20 US 2000.00
4 Alina 29 UK 6500.00
SELECT *
FROM EMPLOYEE
WHERE ID IN (SELECT ID
FROM EMPLOYEE
WHERE SALARY > 4500);
This would produce the following result:
4 Alina 29 UK 6500.00
SQL subquery can also be used with the Insert statement. In the insert statement, data returned
from the subquery is used to insert into another table.
In the subquery, the selected data can be modified with any of the character, date functions.
Syntax:
Example
Consider a table EMPLOYEE_BKP with similar as EMPLOYEE.
Now use the following syntax to copy the complete EMPLOYEE table into the EMPLOYEE_BKP
table.
The subquery of SQL can be used in conjunction with the Update statement. When a subquery is used
with the Update statement, then either single or multiple columns in a table can be updated.
Syntax
UPDATE table
SET column_name = new_value
WHERE VALUE OPERATOR
(SELECT COLUMN_NAME
FROM TABLE_NAME
WHERE condition);
Example
Let's assume we have an EMPLOYEE_BKP table available which is backup of EMPLOYEE table.
The given example updates the SALARY by .25 times in the EMPLOYEE table for all employee
whose AGE is greater than or equal to 29.
UPDATE EMPLOYEE
SET SALARY = SALARY * 0.25
WHERE AGE IN (SELECT AGE FROM CUSTOMERS_BKP
WHERE AGE >= 29);
This would impact three rows, and finally, the EMPLOYEE table would have the following records.
1 John 20 US 2000.00
4 Alina 29 UK 1625.00
The subquery of SQL can be used in conjunction with the Delete statement just like any other
statements mentioned above.
Syntax
1 John 20 US 2000.00