You are on page 1of 86

DBMS

Database:
A database is a collection of information that is organized. So that it can
easily be accessed, managed, and updated.

DBMS:
DBMS stands for Database Management System. It is a collection of
programs that enables user to create and maintain a database
Advantages of DBMS
• Redundancy is controlled.
• Providing multiple user interfaces.
• Providing backup and recovery
• Unauthorized access is restricted.
• Enforcing integrity constraints.

Manjunatha A S, Senior Asst. Prof. Dept. of Computer Science


& Engg
What is RDBMS

• RDBMS is the Relational Database Management System which


contains data in the form of the tables and data is accessed on the
basis of the common fields among the tables.
Types of Databases and Database
Applications
• Numeric and Textual Databases
• Multimedia Databases
• Geographic Information Systems (GIS)
• Data Warehouses and Online Analytical Processing
(OLAP):OLAP allows users to analyze database information
from multiple database systems at one time
• Real-time and Active Databases
Basic Definitions
• Database: A collection of related data.

• Data: Known facts that can be recorded and have an implicit meaning.

• Database Management System (DBMS): A collection of programs


that enables users to create and maintain database. DBMS is a general
purpose software system that facilitates the processes of defining,
constructing, manipulating and sharing databases among various users
and applications.

• Database System: The DBMS software together with the database.


Sometimes, the applications are also included.
2-Tier architecture
• The 2-Tier architecture is the same as basic client-server. In the two-
tier architecture, applications on the client end can directly
communicate with the database at the server side.
3-Tier architecture?

• The 3-Tier architecture contains another layer between the client and
server.
• Introduction of 3-tier architecture is for the ease of the users as it
provides the GUI, which, make the system secure and much more
accessible.
• In this architecture, the application on the client-end interacts with an
application on the server which further communicates with the
database system.
How do you communicate with an RDBMS?

• You have to use Structured Query Language (SQL) to communicate


with the RDBMS.
• Using queries of SQL, we can give the input to the database and then
after processing of the queries database will provide us the required
output.
Schema
• The description of a data base is called the database schema , which is
specified during database design and is not expected to change
frequently . A displayed schema is called schema diagram
• Schema is of three types:
1. Physical schema,
2. logical schema and
3. view schema
A database schema can be divided broadly into two categories −
• Physical Database Schema − This schema pertains to the actual
storage of data and its form of storage like files, indices, etc. It defines
how the data will be stored in a secondary storage.
• Logical Database Schema − This schema defines all the logical
constraints that need to be applied on the data stored. It defines tables,
views, and integrity constraints.
• A database schema is the skeleton structure that represents the logical
view of the entire database. It defines how the data is organized and
how the relations among them are associated. It formulates all the
constraints that are to be applied on the data.
Entity, Entity Type, and Entity Set in
DBMS?
• Entity is an object, place or thing which has its independent existence in
the real world and about which data can be stored in a database. 
For Example, any person, book, etc.
• Entity Type is a collection of entities that have the same attributes. 
For Example, the STUDENT table contains rows in which each row is an
entity holding the attributes like name, age, and id of the students, hence
STUDENT is an Entity Type which holds the entities having the same
attributes.
• Entity Set is a collection of entities of the same type. 
For Example, A collection of the employees of a firm.
Attribute

• An attribute refers to a database component. It is used to describe the


property of an entity. An attribute can be defined as the characteristics
of the entity. Entities can be uniquely identified using the attributes.
Attributes represent the instances in the row of the database.
• For example: If a student is an entity in the table then age will be the
attribute of that student.
Weak Entity set?

• An entity set that doesn't have sufficient attributes to form a primary


key is referred to as a weak entity set
• The member of a weak entity set is known as a subordinate entity.
Weak entity set does not have a primary key, but we need a mean to
differentiate among all those entries in the entity set that depend on
one particular strong entity set.
What are super, primary, candidate and
foreign keys?
• A super key is a set of attributes of a relation schema upon which all
attributes of the schema are functionally dependent. No two rows can
have the same value of super key attributes.
• A Candidate key is minimal superkey, i.e., no proper subset of
Candidate key attributes can be a superkey.
• A Primary Key is one of the candidate keys. One of the candidate keys
is selected as most important and becomes the primary key. There
cannot be more that one primary keys in a table.
• A Foreign key is a field (or collection of fields) in one table that
uniquely identifies a row of another table. See this for an example.
Difference between primary key and
unique constraints?
• Primary key cannot have NULL value, the unique constraints can have
NULL values. There is only one primary key in a table, but there can
be multiple unique constrains.
 Concepts of a Primary key and Foreign
Key.
• Primary Key is used to uniquely identify the records in a database
table while Foreign Key is mainly used to link two or more tables
together, as this is a particular field(s) in one of the database tables
which are the primary key of some other table

• Example: There are 2 tables – Employee and Department. Both have


one common field/column as ‘ID’ where ID is the primary key of
the Employee table while this is the foreign key for
the Department table.
Different types of keys in the database?
Database normalization?
It is a process of analyzing the given relation schemas based on their
functional dependencies and primary keys to achieve the following
desirable properties:
1. Minimizing Redundancy
2. Minimizing the Insertion, Deletion, And Update Anomalies

Relation schemas that do not meet the properties are decomposed into
smaller relation schemas that could meet desirable properties.
Different levels of abstraction in the
DBMS?
• Physical Level: This is the lowest level of the data abstraction which
states how the data is stored in the database.
• Logical Level: This is the next level of the data abstraction which
states the type of the data and the relationship among the data that is
stored in the database.
• View Level: This is the highest level in the data abstraction which
shows/states only a part of the database.
Integrity rules exist in the DBMS
• Entity Integrity: This states a very important rule that value of a
Primary key can never have a NULL value.
• Referential Integrity: This rule is related to the Foreign key which
states that either the value of a Foreign key is a NULL value or it
should be the primary key of any other relation.
Different type of relationships in the
DBMS?
• One-to-One: This basically states that there should be a one-to-one
relationship between the tables i.e. there should be one record in both the
tables. Example: Among a married couple, both wife and husband can
have only one spouse.
• One-to-Many: This states that there can be many relationships for one
i.e. a primary key table hold only one record which can have many, one
or none records in the related table. Example: A Mother can have many
children.
• Many-to-Many: This states that both the tables can be related to many
other tables. Example: One can have many siblings and so do they have.
E-R model in the DBMS
• E-R model is known as an Entity-Relationship model in the DBMS
which is based on the concept of the Entities and the relationship that
exists among these entities.
Functional dependency in the DBMS
• This is basically a constraint which is useful in describing the
relationship among the different attributes in a relation.
• Its helps in preventing data redundancy and get to know about bad
design.
• Given a relation R,a set of attributes X in R said to functionally
determine another set of attributes Y, also in R,(Written XY) if, and
only if, each X value is associated with precisely one Y value;R is then
said to satisfy the functional dependency XY
What is SQL?
• SQL is Structured Query Language designed for inserting and
modifying in a relational database system.
What is the concept of sub-query in terms
of SQL?
• Sub-query is basically the query which is included inside some other
query and can also be called as an inner query which is found inside
the outer query.
Differences between DROP, TRUNCATE
and DELETE commands?
• DROP and TRUNCATE commands are the DDL commands which
are used to delete tables from the database and once the table gets
deleted, all the privileges and indexes that are related to the table also
get deleted. These 2 operations cannot be rolled back and so should be
used only when necessary.
• DELETE command, on the other hand, is a DML Command which is
also used to delete rows from the table and this can be rolled back.
Difference between UNION and UNION
ALL?
• UNION and UNION ALL are used to join the data from 2 or more
tables but UNION removes duplicate rows and picks the rows which
are distinct after combining the data from the tables whereas UNION
ALL does not remove the duplicate rows, it just picks all the data from
the tables.
join in the SQL
• A Join is one of the SQL statements which is used to join the data or
the rows from 2 or more tables on the basis of a common field/column
among the.m
different types of joins in SQL?
• Inner Join: This type of join is used to fetch the data among the tables
which are common in both the tables.
• Left Join: This returns all the rows from the table which is on the left
side of the join but only the matching rows from the table which is on
the right side of the join.
• Right Join: This returns all the rows from the table which is on the
right side of the join but only the matching rows from the table which
is on the left side of the join.
• Full Join: This returns the rows from all the tables on which the join
condition has put and the rows which do not match hold null values.
Different types of languages that are
available in the DBMS?
• DDL: DDL is Data Definition Language which is used to define the
database and schema structure by using some set of SQL Queries
like CREATE, ALTER, TRUNCATE, DROP and RENAME.
• DCL: DCL is Data Control Language which is used to control the
access of the users inside the database by using some set of SQL
Queries like GRANT and REVOKE.
• DML: DML is Data Manipulation Language which is used to do
some manipulations in the database like Insertion, Deletion, etc. by
using some set of SQL Queries
like SELECT, INSERT, DELETE and UPDATE.
What is a view in SQL? How to create one
• A view is a virtual table based on the result-set of an SQL statement.
We can create using create view syntax.
What is the purpose of SQL?
• SQL stands for Structured Query Language whose main purpose is
to interact with the relational databases in the form of inserting and
updating/modifying the data in the database.
 uses of view?
1. Views can represent a subset of the data contained in a table;
consequently, a view can limit the degree of exposure of the
underlying tables to the outer world: a given user may have
permission to query the view, while denied access to the rest of the
base table.
2. Views can join and simplify multiple tables into a single virtual table
3. Views can act as aggregated tables, where the database engine
aggregates data (sum, average etc.) and presents the calculated
results as part of the data.
4. Views can hide the complexity of data; for example a view could
appear as Sales2000 or Sales2001, transparently partitioning the actual
underlying table.

5. Views take very little space to store; the database contains only the
definition of a view, not a copy of all the data which it presents.
6. Depending on the SQL engine used, views can provide extra security
DROP, TRUNCATE and DELETE
commands?
Pattern matching in SQL?

You can perform pattern matching in SQL by using the LIKE operator. 
With the LIKE operator, you can use the following symbols:
• %(Percentage sign) – To match zero or more characters.
• _ (Underscore) –To match exactly one character.
• Example:
• SELECT * FROM Customers WHERE CustomerName LIKE ‘s%’
• SELECT * FROM Customers WHERE CustomerName like ‘xyz_’
Trigger
• A Trigger is a code that associated with insert, update or delete
operations. The code is executed automatically whenever the
associated query is executed on a table. Triggers can be useful to
maintain integrity in database.
Stored procedure?
• A stored procedure is like a function that contains a set of operations
compiled together. It contains a set of operations that are commonly
used in an application to do some common database tasks.
Difference between Trigger and Stored
Procedure?
• Unlike Stored Procedures, Triggers cannot be called directly. They can
only be associated with queries.
Four applications for triggers.
(1)Providing default values,
(2) Enforcing data constraints,
(3) Updating views and
(4) Enforcing referential integrity
Advantages of using stored procedures?
The advantages of stored procedures are
(1) greater security,
(2) decreased network traffic,
(3) the fact that SQL can be optimized and
(4) code sharing which leads to less work, standardized processing, and
specialization among developers.
What is a transaction?
• A Database Transaction is a set of database operations that must be
treated as whole, means either all operations are executed or none of
them.

• An example can be bank transaction from one account to another


account. Either both debit and credit operations must be executed or
none of them.
ACID properties?
• ACID (Atomicity, Consistency, Isolation, Durability) is a set of
properties that guarantee that database transactions are processed
reliably.
• ACID properties is the combination of Atomicity, Consistency,
Isolation, and Durability properties. These properties are very helpful
in allowing a safe and secure way of sharing the data among multiple
users.
• Atomicity: This is based on the concept of “either all or nothing”
which basically means that if any update occurs inside the database
then that update should either be available to all the others beyond
user and application program or it should not be available to anyone
beyond the user and application program.
• Consistency: This ensures that the consistency is maintained in the
database before or after any transaction that takes place inside the
database.
• Isolation: As the name itself suggests, this property states that each
transaction that occurs is in isolation with others i.e. a transaction
which has started but not yet completed should be in isolation with
others so that the other transaction does not get impacted with this
transaction.
• Durability: This property states that the data should always be in a
durable state i.e. any data which is in the committed state should be
available in the same state even if any failure or restart occurs in the
system.
Difference between a shared lock and exclusive lock?

• Shared lock: Shared lock is required for reading a data item. In the
shared lock, many transactions may hold a lock on the same data item.
When more than one transaction is allowed to read the data items then
that is known as the shared lock.
• Exclusive lock: When any transaction is about to perform the write
operation, then the lock on the data item is an exclusive lock. Because,
if we allow more than one transaction then that will lead to the
inconsistency in the database.
Purpose of normalization in DBMS?
Normalization is the process of analyzing the relational schemas which
are based on their respective functional dependencies and the primary
keys in order to fulfill certain properties.
The properties include:
• To minimize the redundancy of the data.
• To minimize the Insert, Delete and Update Anomalies.
Anomalies in DBMS

• There are three types of anomalies that occur when the database is not
normalized. These are – Insertion, update and deletion anomaly.
• Example: Suppose a manufacturing company stores the employee
details in a table named employee that has four attributes: emp_id for
storing employee’s id, emp_name for storing employee’s name,
emp_address for storing employee’s address and emp_dept for storing
the department details in which the employee works. At some point of
time the table looks like this:
The Below table is not normalized. We will see the
problems that we face when a table is not normalized.
• Update anomaly: In the above table we have two rows for employee
Rick as he belongs to two departments of the company. If we want to
update the address of Rick then we have to update the same in two
rows or the data will become inconsistent. If somehow, the correct
address gets updated in one department but not in other then as per the
database, Rick would be having two different addresses, which is not
correct and would lead to inconsistent data.
• Insert anomaly: Suppose a new employee joins the company, who is
under training and currently not assigned to any department then we
would not be able to insert the data into the table if emp_dept field
doesn’t allo
• Delete anomaly: Suppose, if at a point of time the company closes the
department D890 then deleting the rows that are having emp_dept as
D890 would also delete the information of employee Maggie since she
is assigned only to this department.
To overcome these anomalies we need to normalize the data
 Commonly used normal forms are:
• First Normal Form(1NF)
• Second Normal Form(2NF)
• Third Normal Form(3NF)
• Boyce & Codd Normal Form(BCNF)
1NF?

1NF is the First Normal Form. It is the simplest type of normalization


that you can implement in a database. The primary objectives of 1NF
are to:
• Every column must have atomic (single value)
• To Remove duplicate columns from the same table
• Create separate tables for each group of related data and identify each
row with a unique column
• As per the rule of first normal form, an attribute (column) of a table
cannot hold multiple values. It should hold only atomic values.
• Example: Suppose a company wants to store the names and contact
details of its employees. It creates a table that looks like this:
• Two employees (Jon & Lester) are having two mobile numbers so the
company stored them in the same field as you can see in the table
above.
• This table is not in 1NF as the rule says “each attribute of a table must
have atomic (single) values”, the emp_mobile values for employees
Jon & Lester violates that rule.
To make the table complies with 1NF we should have the data like this:
2NF?

2NF is the Second Normal Form. A table is said to be 2NF if it follows


the following conditions:
• The table is in 1NF, i.e., firstly it is necessary that the table should
follow the rules of 1NF.
• Every non-prime attribute is fully functionally dependent on the
primary key, i.e., every non-key attribute should be dependent on the
primary key in such a way that if any key element is deleted, then even
the non_key element will still be saved in the database.
• An attribute that is not part of any candidate key is known as non-
prime attribute.
• Example: Suppose a school wants to store the data of teachers and the
subjects they teach. They create a table that looks like this: Since a
teacher can teach more than one subjects, the table can have multiple
rows for a same teacher.
• Candidate Keys: {teacher_id, subject}
Non prime attribute: teacher_age
• The table is in 1 NF because each attribute has atomic values.
However, it is not in 2NF because non prime attribute teacher_age is
dependent on teacher_id alone which is a proper subset of candidate
key. This violates the rule for 2NF as the rule says “no non-prime
attribute is dependent on the proper subset of any candidate key of the
table”.
• To make the table complies with 2NF we can break it in two tables like
this:
Now the tables comply with Second normal form (2NF).
3NF
3NF stands for Third Normal Form. A database is called in 3NF if it
satisfies the following conditions:
• It is in second normal form.
• There is no transitive functional dependency.
• For example: X->Z
• Where:
X->Y
Y does not -> X
Y->Z so, X->Z
• An attribute that is not part of any candidate key is known as non-
prime attribute.
• In other words 3NF can be explained like this: A table is in 3NF if it is
in 2NF and for each functional dependency X-> Y at least one of the
following conditions hold:
• X is a super key of table
• Y is a prime attribute of table
An attribute that is a part of one of the candidate keys is known as prime
attribute.
• Example: Suppose a company wants to store the complete address of
each employee, they create a table named employee_details that looks
like this:
• Super keys: {emp_id}, {emp_id, emp_name}, {emp_id, emp_name,
emp_zip}…so on
• Candidate Keys: {emp_id}
• Non-prime attributes: all attributes except emp_id are non-prime as
they are not part of any candidate keys.
• Here, emp_state, emp_city & emp_district dependent on emp_zip.
And, emp_zip is dependent on emp_id that makes non-prime attributes
(emp_state, emp_city & emp_district) transitively dependent on super
key (emp_id). This violates the rule of 3NF.
To make this table complies with 3NF we have to break the
table into two tables to remove the transitive dependency:
BCNF

BCMF stands for Boyce-Codd Normal Form. It is an advanced


version of 3NF, so it is also referred to as 3.5NF. BCNF is stricter than
3NF.
A table complies with BCNF if it satisfies the following conditions:
• It is in 3NF.
• For every functional dependency X->Y, X should be the super key of
the table. It merely means that X cannot be a non-prime attribute if Y
is a prime attribute.
• Example: Suppose there is a company wherein employees work
in more than one department. They store the data like this:
• Functional dependencies in the table above:
emp_id -> emp_nationality
emp_dept -> {dept_type, dept_no_of_emp}
• Candidate key: {emp_id, emp_dept}
• The table is not in BCNF as neither emp_id nor emp_dept alone are
keys
• To make the table comply with BCNF we can break the table in three
tables like this:
• This is now in BCNF as in both the functional dependencies left side
part is a key.
Denormalization

• Denormalization is the process of boosting up database performance


and adding of redundant data which helps to get rid of complex data.
Denormalization is a part of database optimization technique. This
process is used to avoid the use of complex and costly joins.
What is Hashing technique?
• This is a primary file organization technique that provides very fast
access to records on certain search conditions.
Differences between structured data and
unstructured data.
• Structured data are facts concerning objects and events. The most
important structured data are numeric, character, and dates.
• Structured data are stored in tabular form. Unstructured data are
multimedia data such as documents, photographs, maps, images,
sound, and video clips. Unstructured data are most commonly found
on Web servers and Web-enabled databases.
Major functions of the database
administrator?
• Managing database structure, controlling concurrent processing,
managing processing rights and responsibilities, developing database
security, providing for database recovery, managing the DBMS and
maintaining the data repository.
Primitive operations common to all record
management System?
• Addition, deletion and modification.
Disadvantages of using a dbms?
1) High initial investments in h/w, s/w, and training.
2) Generality that a DBMS provides for defining and processing data.
3) Overhead for providing security, concurrency control, recovery, and
integrity functions.
• COMMIT takes more time than ROLLBACK?F
• Which is better ? 
Procedure or SQL

You might also like