DBMS 2nd Unit Notes
DBMS 2nd Unit Notes
Fundamentals of RDBMS:
A Relational database management system (RDBMS) is a database
management system (DBMS) that is based on the relational model.
In RDBMS data is stored in different tables, theses tables are interlinked
with each other with the help of common fields (columns) in between them.
In such database the data and relations between them are organized into
tables.
What is a table?
Table is a collection of data which is organized in terms of rows and
columns.
This table is basically a collection of related data entries and data is stored in
database objects.
Example of a CUSTOMERS table which stores customer's ID, Name, Age,
Salary, City and Country
What is a field?
Every table is broken up into smaller segments called fields.
A field in a table that is designed to maintain specific information about
every record in the table.
For example, CUSTOMERS table consists of different fields like ID, Name,
Age, Salary, City and Country.
Ease of Use: Data stored in a tabular format in rows and columns makes
accessing, retrieving, and manipulating the data in simple manner.
Flexibility: Information can be retrieved easily by using tables and changes
can be done easily by using operations
Security: Security management sets rules that allow access in the
database.This function also restricts on what specific data any user can see
or write
Maintenance: It is simple to use. It allows database admins to easily
maintain, control, and update data in the database. Backing up data is easy
with RDMS.
Data Structure: This feature stores data in a user-friendly table format.
Data are organized in a structured manner
Dr Edgar F Codd’s Twelve Commandments
The database must be in relational form. So that the system can handle the
database through its relational capabilities.
Every single data (atomic value) may be accessed logically from a relational
database using the combination of primary key value, table name, and
column name.
This rule defines the systematic treatment of Null values in database records.
The null value has various meanings in the database, like missing the data,
no value in a cell
Data sub language rule states that database needs to support minimum one
clearly defined language.
All views table can be theoretically updated and must be practically updated
by the database systems.
Rule 7: Relational Level Operation (High-Level Insert, Update and delete)
Rule
A database system should follow high-level relational operations such as
insert, update, and delete in each level or a single row.
It also supports union, intersection and minus operation in the database
system.
The end users can access the database, and these access data should be
independent for every user to perform the SQL queries.
Rule 12: Non Subversion Rule
There should not exist a way to modify or change for database structure
except database language
Example: SQL
Types of Keys:
1. Super Key
2. Candidate Key
3. Primary Key
4. Alternate Key
5. Foreign key
6. Composite Key
A super key may have additional attribute that are not needed for
unique identity.
Super Keys:
1.{Emp_id} 2.{Emp_Adhar}
3.{Emp_id,Emp_name}
4.{Emp_id,Emp_Adharno}
5.{Emp_adharno,Emp_name}
6.{Emp_id,Emp_adharno,Emp_name}
7{Emp_adharno,Emp_name,Emp_phno}
It is called a minimal super key because we select a candidate key from a set
of super key.
Candidate keys are defined as distinct set of attributes from which primary
key can be selected
Candidate Key
Candidate Keys:
1. {Emp_id}
2. {Emp_adharno}
3. Primary Key: A primary key is one of the candidate key chosen by the
database designer to uniquely identify the tuple in the relation.
Primary key
4. Alternate Keys: Out of all candidate keys, only one gets selected as
primary key, remaining keys are known as alternate keys.
An attribute (or set of attributes) in one table that refers to the primary
key in another table.
Dept_Id Dept_Name
41 Sales
42 Marketing
43 HR
Foreign Key:
Foreign Key references the primary key of the table.
Foreign key can take only those values which are present in the primary key
of the referenced relation.
Referenced relation may also be called as the master table or primary table.
Referencing relation may also be called as the foreign table.
6. Composite Key: A Key that has more than one attribute is known as
composite key.
Normalization:
Normalization is the process of organizing the data in the database.
The normal form is used to reduce redundancy from the database table.
Data redundancy in dbms means having the same data at multiple places.
Normalization is used to minimize the redundancy from a relation or set of
relations. It is also used to eliminate Insertion, Update, and Deletion
Anomalies.
A database anomaly is a error in the database that occurs because of data
redundancy
Normalization divides the larger table into smaller and links them using
relationships.
1. Insert Anomalies: This occurs when we are not able to insert data
into a database because some columns may be missing at the time of
insertion.
EX: Insert into student(“Raj”,24,”B”,19);
2. Updation Anomalies: This occurs when the same data items are
repeated with same values.
Ex: two similar values
Name Rollno Section
Sai 21 A
Sai 21 A
1NF A relation is in 1NF if it contains an atomic value and no multi value attribute.
2NF A relation will be in 2NF if it is in 1NF and all non-key attributes are full
functional dependent on the primary key.
Advantages of Normalization
EMPLOYEE table:
14 John 7272826385, UP
9064738238
The decomposition of the EMPLOYEE table into 1NF has been shown below:
14 John 7272826385 UP
14 John 9064738238 UP
Stud_id Stud_name
1 Ram
2 Sham
3 Seeta
4 Geeta
Proj_Id Proj_name
P1 Banking
P2 Library
P3 college
P4 Library
EMPLOYEE_DETAIL table:
Non-prime attributes: In the given table, all attributes except EMP_ID are
non-prime.
That's why we need to move the EMP_CITY and EMP_STATE to the new
<EMPLOYEE_ZIP> table, with EMP_ZIP as a Primary key.
EMPLOYEE table:
201010 UP Noida
02228 US Boston
60007 US Chicago
06389 UK Norwich
462007 MP Bhopal
SQL Datatype
o SQL Datatype is used to define the values that a column can contain.
o Every column is required to have a name and data type in the database table.
Datatype of SQL:
Character data These are used to store character strings. Examples include CHAR,
types VARCHAR, and TEXT.
These are used to store date and time values. Examples include
Date and time data
DATE, TIME, and TIMESTAMP
types
.
This data type is used to store logical values. The only possible
Boolean data type
values are TRUE and FALSE.
SQL Commands
o SQL commands are instructions.
o It is used to communicate with the database. It is also used to perform
specific tasks, functions, and queries of data.
o SQL can perform various tasks like create a table, add data to tables, drop
the table, modify the table, set permission for users.
There are four types of SQL commands: DDL, DML, DCL and DQL.
o CREATE
o ALTER
o DROP
o TRUNCATE
b. ALTER: It is used to alter the structure of the database. This change could be
either to modify the characteristics of an existing attribute or probably to add a new
attribute.
EXAMPLE:
It removes an existing table along with its structure from the database.
D. Truncate: It is used to delete all the records from the table,delete all the rows
from the table but structure of table remains same.
E. Rename: The RENAME syntax helps in changing the name of the table.
o INSERT
o UPDATE
o DELETE
a. INSERT: The INSERT statement is a SQL query. It is used to insert data into
the row of a table.
For example: INSERT INTO javatpoint (Author, Subject) VALUES ("Singh", "DBMS");
For example:
1. UPDATE students
SET User_Name = 'Siya'
WHERE Student_Id = '3'
c. DELETE: It is used to remove one or more row from a table.
These are the types of DCL commands that are used to assign permission to the
users to perform a different task.
DCL commands are used to grant and take back authority from any database user.
o Grant
o Revoke
Parameters Used:
privileges_name: These are the access rights or privileges granted to the user.
object: In the case of granting privileges on a table, this would be the table
name.
user:It is the name of the user to whom the privileges would be granted.
Privileges: The privileges that can be granted to the users are listed below along
with_description:
1. REVOKE privilege_name(s)
ON object
FROM user_account_name;
1. Rollback means the database DML commands saves Savepoint helps to save
is restored to the last modification and it the transaction
committed state permanently saves the temporarily.
transaction.
o COMMIT
o ROLLBACK
o SAVEPOINT
a. Commit: Commit command is used to save all the transactions to the database.
Syntax: COMMIT;
COMMIT;
b. Rollback: Rollback command is used to undo transactions that have not already
been saved to the database.
Syntax: ROLLBACK;
Example:
o SELECT
Syntax:
1. SELECT column_name
FROM TABLES
WHERE conditions;
For example:
1. SELECT emp_name
FROM employee
WHERE age > 20;
Type of Data constraints
I/O Constraints:
a) Primary Key: The PRIMARY KEY constraint uniquely identifies each
record in a table.
Primary keys must contain UNIQUE values, and cannot contain NULL
values. A table can have only ONE primary key; in the table.
In simple words you can say that, a foreign key in one table used to point
primary key in another table.
Here are two tables first one is students table and second is orders table.
Here orders are given by students.
First table:
Second table:
1 99586465 2
2 78466588 2
3 22354846 3
4 57698656 1
Here you see that "S_Id" column in the "Orders" table points to the "S_Id" column
in"Students" table.
The "S_Id" column in the "Students" table is the PRIMARY KEY in the "Students" table.
The "S_Id" column in the "Orders" table is a FOREIGN KEY in the "Orders" table.
Roll number attribute is already assigned with the primary key and Citizen_ID
can have unique constraints where each entry in a Citizen_ID column should be
unique because each citizen of a country must have his or her Unique
identification number like an Aadhaar Number. But if the student is migrated to
another country in that case, he or she would not have any Citizen_ID and the
entry could have a NULL value as only one NULL is allowed in the unique
constraint.
We cannot change or
We can change unique
Modification delete values stored in
key values.
primary keys.
(
(
House_Number INT
Student_Id INT
UNIQUE,
PRIMARY KEY,
House_Name
Student_name
VARCHAR(150),
VARCHAR(150),
House_Address
roll_number INT(10)
VARCHAR(250)
)
)
b) Not Null: The NOT NULL constraint is used to ensure that a given column
of a table is never assigned the null value.
Once a NOT NULL constraint has been defined for a particular column, any insert
or update operation that attempts to place a null value in that column will fail.
Example: CREATE TABLE students (
S_Id int NOT NULL,
FirstName varchar (255),
Address varchar (255));
c) Check Constraint in SQL
o Check constraint is validation or a rule which is applied on the column of a
table.
o When we apply check constraint to any column, it checks for specific values
while inserting any record.
o Check constraint can be created on a table level as well as column level.
o Check constraints can be applied only to a single column,
o Example: CREATE TABLE student(
Arithmetic Operators
* Multiplication Numeric
(Multiply) value
/ (Divide) Division Numeric
value
2. Logical Operator:
Types of Logical Operators in SQL
Given below is the list of logical operators available in SQL.
Operator Meaning
a. AND Operator
The AND operator is used to combines two or more conditions but if it is true
when all the conditions are satisfied.
Query
SELECT * FROM employee WHERE emp_city = 'Allahabad' AND emp_country
= 'India';
Output
b. IN Operator
It is used to remove the multiple OR conditions in SELECT, INSERT, UPDATE,
or DELETE. and We can also use NOT IN to minimize the rows in your list and
any kind of duplicate entry will be retained.
Query
SELECT * FROM employee WHERE emp_city IN ('Allahabad', 'Patna');
Output
c. NOT Operator
Query
SELECT * FROM employee WHERE emp_city NOT LIKE 'A%';
Output
d. OR Operator
The OR operator is used to combines two or more conditions but if it is true when
one of the conditions are satisfied.
Query
SELECT * FROM employee WHERE emp_city = 'Varanasi' OR emp_country =
'India';
Output
e. LIKE Operator
In SQL, the LIKE operator is used in the WHERE clause to search for a specified
pattern in a column.
% – It is used for zero or more than one character.
_ – It is used for only one character means fixed length.
Query
SELECT * FROM employee WHERE emp_city LIKE 'P%';
Output
f. BETWEEN Operator
The SQL BETWEEN condition allows you to easily test if an expression is
within a range of values (inclusive).
Query
SELECT * FROM employee WHERE emp_id BETWEEN 101 AND 104;
Output
g. ALL Operator
The ALL operator returns TRUE if all of the subqueries values matches the
condition.
All operator is used with SELECT, WHERE, HAVING statement.
Query
SELECT * FROM employee WHERE emp_id = ALL
(SELECT emp_id FROM employee WHERE emp_city = 'Varanasi');
Output
h. ANY Operator
The ANY operator:
It returns a boolean value as a result
It returns TRUE if ANY of the subquery values match the condition
Query
SELECT * FROM employee WHERE emp_id = ANY
(SELECT emp_id FROM employee WHERE emp_city = 'Varanasi');
Output
i. EXISTS Operator
In SQL,Exists operator is used to check whether the result of a correlated nested
query is empty or not.
Exists operator is used with SELECT, UPDATE, INSERT or DELETE
statement.
Query
SELECT emp_name FROM employee WHERE EXISTS
(SELECT emp_id FROM employee WHERE emp_city = 'Patna');
Output
j. SOME Operator
In SQL, SOME operators are issued with comparison operators (<,>,=,<=, etc) to
compare the value with the result of a subquery.
Query
SELECT * FROM employee WHERE emp_id < SOME
(SELECT emp_id FROM employee WHERE emp_city = 'Patna');
Output
1. UNION
2. UNION ALL
3. INTERSECT
4. MINUS
There are certain rules which must be followed to perform operations using SET
operators in SQL. Rules are as follows:
Let us see each of the SET operators in more detail with the help of
examples.
Table 1: t_employees
ID Name Department Salary Year_of_Experience
Table 2: t2_employees
Table 3: t_students
Table 4: t2_students
1. UNION:
o UNION will be used to combine the result of two select statements.
o Duplicate rows will be eliminated from the results obtained after
performing the UNION operation.
Example 1:
Write a query to perform union between the table t_employees and the table
t2_employees.
1. Query:
mysql> SELECT *FROM t_employees UNION SELECT *FROM t2_employees
;
2. Here, in a single query, we have written two SELECT queries. The first
SELECT query will fetch the records from the t_employees table and
perform a UNION operation with the records fetched by the second
SELECT query from the t2_employees table.
3. You will get the following output:
2. UNION ALL
o This operator combines all the records from both the queries.
o Duplicate rows will be not be eliminated from the results obtained after
performing the UNION ALL operation.
Example 1:
Write a query to perform union all operation between the table t_employees
and the table t2_employees.
1. Query:
mysql> SELECT *FROM t_employees UNION ALL SELECT *FROM t2_emplo
yees;
2. Here, in a single query, we have written two SELECT queries. The first
SELECT query will fetch the records from the t_employees table and
perform UNION ALL operation with the records fetched by the second
SELECT query from the t2_employees table.
Since we have performed union all operation between both the tables, so all
the records from the first and second table are displayed, including the
duplicate records.
3. INTERSECT:
o It is used to combine two SELECT statements, but it only returns the
records which are common from both SELECT statements.
Example 1:
1. Query:
mysql> SELECT *FROM t_employees INTERSECT SELECT *FROM t2_empl
oyees;
2. Here, in a single query, we have written two SELECT queries. The first
SELECT query will fetch the records from the t_employees table and
perform INTERSECT operation with the records fetched by the second
SELECT query from the t2_employees table.
3. You will get the following output:
4. MINUS
o It displays the rows which are present in the first query but absent in
the second query with no duplicates.
Example 1:
Query:
1. mysql> SELECT *FROM t_employees MINUS SELECT *FROM t2_employees;
Here, in a single query, we have written two SELECT queries. The first
SELECT query will fetch the records from the t_employees table and perform
MINUS operation with the records fetched by the second SELECT query from
the t2_employees table.
Since we have performed Minus operation between both the tables, so only
the unmatched records from both the tables are displayed.