UNIT 3 (4th Sem)
UNIT 3 (4th Sem)
Subject: DBMS
Sub Code: KMBA 406
UNIT-3
SQL:
Backbone of the data industry. SQL is considered the backbone of the data
industry. It's widely used by data-centric professionals including data
analysts, data scientists, business analysts and database developers.
Universal language. SQL is a universal language that is transferable to other
disciplines and languages. Learning SQL can help one understand the workings
of other languages such as Python and Java. It also makes collaboration easy, as
it has a large supportive community.
In-demand skill. SQL knowledge is one of the most in-demand skills in the
field of data science. It appears in a significant percentage of data science job
postings, making it a prized skill for professionals in this field.
Data manipulation. SQL is well-suited for data manipulation. It enables users
to easily test and manipulate data, making it efficient for tasks such as filtering,
sorting and aggregating data.
Rapid query processing. SQL enables rapid query processing, enabling users
to retrieve, manipulate or store data quickly and efficiently. However,
optimizing queries for rapid processing involves a combination of proper
indexing, query optimization and database design considerations.
Security features. SQL provides various security features such as
authentication, access control, audit trails and encryption, making it easy to
manage permissions and ensure the security of data.
Commonality and compatibility. SQL is widely used in various IT systems
and is compatible with multiple other languages. Its commonality benefits
beginners in the profession, as they are likely to use SQL throughout their
careers. It also contributes to ease of application and improves the production
and efficiency of businesses.
Scalability. SQL is suitable for organizations of any size. It can easily scale up
to accommodate future growth, making it a versatile choice for small and large
businesses alike.
Open source and community support. SQL is an open source language with a
vibrant community of developers that regularly provide updates and
troubleshooting assistance to SQL users.
Cost-effective. Due to its open source nature, SQL is more cost-effective than
proprietary solutions, making it ideal for organizations with budget constraints.
SQL commands and syntax
SQL is, fundamentally, a programming language designed for accessing,
modifying and extracting information from relational databases. As a programming
language, SQL has commands and a syntax for issuing those commands.
SQL commands are divided into several different types, including the following:
Data Definition Language (DDL) commands are also called data definition
commands because they are used to define data tables.
Data Manipulation Language (DML) commands are used to manipulate data
in existing tables by adding, changing or removing data. Unlike DDL
commands that define how data is stored, DML commands operate in the tables
defined with DDL commands.
Data Query Language consists of just one command, SELECT, used to get
specific data from tables. This command is sometimes grouped with the DML
commands.
Data Control Language commands are used to grant or revoke user access
privileges.
Transaction Control Language commands are used to change the state of
some data -- for example, to COMMIT transaction changes or to ROLLBACK
transaction changes.
What Is a Data Type?
SQL data types define the nature of data that can be stored in database objects, like
tables. Every table has columns, and each column has a name and data type
associated with it.
There are six primary categories of pre-defined SQL data types that you can
choose from when creating a table. These categories are as follows:
What Is an SQL operator?
SQL Operators
SQL queries are used by every database administrator and user to manipulate and
access the data of database tables and views. Reserved words and characters,
which are utilized to carry out arithmetic operations, logical operations,
comparison operations, compound operations, etc., are used to manipulate and
retrieve the data.
SQL operators are what make the SQL queries more useful in data retrieval in
terms of data filtration.
Generally, there are three types of operators that are used in SQL.
1. Arithmetic Operators
2. Comparison Operators
3. Logical Operators
Here is a table describing all the SQL Arithmetic Operators along with their
functionalities.
Comparison operators in SQL are used to check the equality of two expressions. It
checks whether one expression is identical to another. Comparison operators are
generally used in the WHERE clause of a SQL query. The result of a comparison
operation may be TRUE, FALSE or UNKNOWN. When one or both the
expression is NULL, then the operator returns UNKNOWN.
Look at the table below to know what all comparison operators are allowed in
SQL.
Logical operators are those operators that take two expressions as operands and
return TRUE or False as output. While working with complex SQL statements and
queries, comparison operators come in handy and these operators work in the same
way as logic gates do
OR: This logical OR returns TRUE if at least one of the given conditions
separated by OR is fulfilled, otherwise returns FALSE.
AND: This logical operator returns TRUE if all the mentioned conditions separated
by AND are met, otherwise returns FALSE
NOT: Logical NOT operator has been used for the negation of the output of any
logical operator. It checks for the reverse of a given logical expression.
…………………………………………………..
An aggregate function in SQL returns one value after calculating multiple column
values. We often use aggregate functions with the SELECT statement's GROUP BY
and HAVING clauses.
Sum()
Avg()
Min()
Max()
………………………………………………………………………………….
1.INNER JOIN
An INNER JOIN is one of the most commonly used joins in SQL. It
returns only the rows that have matching values in both tables. If there
is no match, the row is not included in the result set.
The INNER JOIN essentially combines the data from two or more
tables based on a related column, and it filters out rows that do not
satisfy the join condition.
inner join
SELECT columns
FROM table1
INNER JOIN table2
ON table1.common_column = table2.common_column;
2.LEFT JOIN
A LEFT JOIN, also referred to as a LEFT OUTER JOIN, is a type of
SQL join where all rows from the left table (table1) are returned
regardless of whether there is a match in the right table (table2). It
includes the matching rows from the right table based on a related
column specified in the ON clause of the LEFT JOIN statement.
This join condition ensures that for each row from the left table, the
LEFT JOIN includes the corresponding matching rows from the right
table in the result set. If there is no match found in the right table,
NULL values are returned for the columns from the right table.
left join
SELECT columns
FROM table1
LEFT JOIN table2
ON table1.common_column = table2.common_column;
SELECT columns specifies the columns you want to select from the
combined result set.
table1 and table2 are the names of the tables you want to join.
3.RIGHT JOIN
The RIGHT JOIN ensures that all rows from the right table are
retained in the result set, and for each row from the right table, it
includes the corresponding matching rows from the left table based on
the join condition. If there is no match found in the left table, NULL
values are returned for the columns from the left table.
right join
SELECT columns
FROM table1
RIGHT JOIN table2
ON table1.common_column = table2.common_column;
SELECT columns specifies the columns you want to select from the
combined result set.
table1 and table2 are the names of the tables you want to join.
4.FULL JOIN
A FULL JOIN, also known as a FULL OUTER JOIN, is a type of SQL
join that returns all rows from both the left table (table1) and the right
table (table2). When there is a match between the tables based on the
join condition, the result set includes the matched rows from both
tables. When there is no match, the result set includes NULL values for
columns from the table without a match.
full join
SELECT columns
FROM table1
FULL JOIN table2
ON table1.common_column = table2.common_column;
table1 and table2 are the names of the tables you want to join.
5.CROSS JOIN
A CROSS JOIN is a type of SQL join that returns the Cartesian product
of the two joined tables. This means that each row from the first table
is combined with each row from the second table. The result set
includes all possible combinations of rows from both tables.
cross join
customers table:
| id | name | email |
|----|---------|--------------------|
| 1 | Alice | alice@example.com |
| 2 | Bob | bob@example.com |
| 3 | Charlie | charlie@example.com|
| 4 | David | david@example.com |
products table:
Result:
The CROSS JOIN will combine each row from the customers table with
each row from the products table, resulting in the Cartesian product of
the two tables. Here is what the result will look like:
The SELF JOIN is a powerful SQL technique that enables you to join a
table with itself, allowing for comparisons and relationships between
rows within the same table. Understanding how to use SELF JOIN can
be beneficial in various scenarios, especially when dealing with
hierarchical data or comparing records within a dataset.
……………………………………….
PL/SQL:
Cursor:
A cursor in a PL/SQL context is treated as a WITH HOLD cursor. For more information
about WITH HOLD cursors, see “DECLARE CURSOR statement”.
Each cursor has a set of attributes that enables an application program to test the state
of the cursor.
%NOTFOUND
This attribute is the logical opposite of the %FOUND attribute.
%ROWCOUNT
This attribute is used to determine the number of rows that have been fetched since
a cursor was opened.
Example
Command> DECLARE
CURSOR c1 IS
SELECT last_name, salary, hire_date, job_id FROM employees
WHERE employee_id = 120;
--declare record variable that represents a row
--fetched from the employees table
employee_rec c1%ROWTYPE;
BEGIN
-- open the explicit cursor
-- and use it to fetch data into employee_rec
OPEN c1;
FETCH c1 INTO employee_rec;
DBMS_OUTPUT.PUT_LINE('Employee name: ' || employee_rec.last_name);
CLOSE c1;
END;
/
Employee name: Weiss
…………………..
TRIGGERS:
Triggers are procedures that are stored in the database and implicitly run, or fired,
when something happens.
Designing Triggers
Creating Triggers
Triggers are created using the CREATE TRIGGER statement. This statement can be used
with any interactive tool, such as SQL*Plus or Enterprise Manager. When using an
interactive tool, a single slash (/) on the last line is necessary to activate
the CREATE TRIGGER statement.
The trigger is fired when DML operations (INSERT, UPDATE, and DELETE statements) are
performed on the table. You can choose what combination of operations should fire
the trigger.
……………………….
Procedure in PL/SQL:
PL/SQL procedure syntax
BEGIN
[execution statements]
EXCEPTION
[exception handler]
A procedure begins with a header that specifies its name and an optional
parameter list.
Each parameter can be in either IN, OUT, or INOUT mode. The parameter mode
specifies whether a parameter can be read from or written to.
IN
An INOUT parameter is both readable and writable. The procedure can be read
and modified.
Note that OR REPLACE option allows you to overwrite the current procedure with
the new code.
Similar to an anonymous block, the procedure body has three parts. The
executable part is mandatory whereas the declarative and exception-handling
parts are optional. The executable part must contain at least one executable
statement.
1) Declarative part
In this part, you can declare variables, constants, cursors, etc. Unlike
an anonymous block, a declaration part of a procedure does not start with
the DECLARE keyword.
2) Executable part
This part contains one or more statements that implement specific business
logic. It might contain only a NULL statement.
3) Exception-handling part
The following procedure accepts a customer id and prints out the customer’s
contact information including first name, last name, and email:
EXCEPTION
WHEN OTHERS THEN
dbms_output.put_line( SQLERRM );
END;
Code language: SQL (Structured Query Language) (sql)
……………………………………………………………………..
What is Functional Dependency in
DBMS
Functional dependency in a database management system refers to the relationship
between attributes in a table, where one attribute uniquely determines another. It helps
maintain data accuracy and consistency by ensuring that the value of one attribute (the
determinant) defines the value of another attribute (the dependent).
Reflexivity:
If an attribute or set of attributes is a subset of another, it determines itself.
Example: If A is part of AB, then AB → A.
Augmentation:
Adding extra attributes to both sides of a dependency keeps it valid.
Example: If A → B, then AC → BC.
Transitivity:
If one attribute depends on another, and that one depends on a third, the first can
determine the third.
Example: If A → B and B → C, then A → C.
Types of Dependency in DBMS
In DBMS, functional dependencies assist in describing how attributes of a table are
related to one another. Different types of dependency in DBMS exist, and each is
extremely important in the nature of the data being arranged and the accuracy's
consistency. It is important for the database to be designed and normalised to these
types of specialisations.
Student_ID → Student_ID
This is a trivial functional dependency because Student_ID trivially determines itself. All
attributes will always functionally determine themselves; therefore, this is a trivial
dependency.
Name → Name
Similarly, Name trivially determines Name. Like before, an attribute always determines
itself.
Here, Student_ID uniquely determines both Name and Department. This is a non-trivial
functional dependency because the left-hand side (Student_ID) determines two other
attributes (Name and Department), which are not subsets of Student_ID. The value of
Student_ID is enough to uniquely determine the corresponding Name and Department,
making this a valid and meaningful dependency.
Here, Student_ID determines a set of values for both Name and Department. The
dependency indicates that for each unique Student_ID, there is a corresponding set of
Name and Department values. Importantly, changes in Name or Department do not
affect the other attributes. So each Student_ID maps to these values independently.
Student_ID → Name
Name → Department
From these two functional dependencies, we can infer the transitive dependency:
Student_ID → Department
Since Student_ID → Name and Name → Department, we can conclude that Student_ID
→ Department through a transitive relationship.
The attribute Name is fully dependent on Student_ID because knowing the Student_ID
alone is sufficient to uniquely determine the corresponding Name. Similarly, the
Department is also fully dependent on Student_ID, as each Student_ID uniquely
determines the associated Department.
In the table, since we are working with single attributes (like Student_ID) as primary
keys, no Partial Functional Dependency exists. However, if the table had a composite
key like a combination of Student_ID and Department, a partial dependency could
occur.
For example:
Suppose Student_ID and Department together form a composite primary key, and the
attribute Name depends only on Student_ID. In this case:
Student_ID → Name
Normalization in DBMS
Normalization in DBMS is a technique using which you can organize the data in the
database tables so that:
It is a multi-step process that puts data into tabular form, removes duplicate data, and
set up the relationship between tables.
For a table to be in the First Normal Form, it should follow the following 4 rules:
example.
If we have an Employee table in which we store the employee information along with
the employee skillset, the table will look like this:
Hence the above table fails to pass the First Normal form.
So how do you fix the above table? There are two ways to do this:
1. Remove the emp_skills column from the Employee table and keep it in some
other table.
2. Or add multiple rows for the employee and each row is linked with one skill.
emp_id emp_skill
1 Python
1 JavaScript
emp_id emp_skill
2 HTML
2 CSS
2 JavaScript
3 Java
3 Linux
3 C++
If you want to learn about the First Normal Form in detail, check out DBMS First
Normal Form tutorial.
Let's take an example to understand Partial dependency and the Second Normal Form.
If we have two tables Students and Subjects, to store student information and
information related to subjects.
Student table:
1 Akon CSE
2 Bkon Mechanical
Subject Table:
subject_id subject_name
1 C Language
subject_id subject_name
2 DSA
3 Operating System
And we have another table Score to store the marks scored by students in any subject
like this,
1 1 70 Miss. C
1 2 82 Mr. D
2 1 65 Mr. Op
Now in the above table, the primary key is student_id + subject_id, because both these
information are required to select any row of data.
But in the Score table, we have a column teacher_name, which depends on the subject
information or just the subject_id, so we should not keep that information in
the Score table.
The column teacher_name should be in the Subjects table. And then the entire system
will be Normalized as per the Second Normal Form.
1 C Language Miss. C
2 DSA Mr. D
1 1 70
1 2 82
2 1 65
To understand what is Partial Dependency and how you can normalize a table to 2nd
normal form, jump to the DBMS Second Normal Form tutorial.
Watch this YouTube video to understand the Third Normal Form (if you like videos) - DBMS Third
Normal Form 3NF with Example
Let's take an example. We had the Score table in the Second Normal Form above. If we
have to store some extra information in it, like,
1. exam_type
2. total_marks
To store the type of exam and the total marks in the exam so that we can later calculate
the percentage of marks scored by each student.
1 1 70 Theory 100
1 2 82 Theory 100
2 1 42 Practical 50
1 Practical 50 45
We have created a new table ExamType and we have added more related information
in it like duration(duration of exam in mins.), and now we can use the exam_type_id in
the Score table.
Here is the DBMS Third Normal Form tutorial. But we suggest you first study the
second normal form and then head over to the third normal form.