0% found this document useful (0 votes)
16 views

Data Base Management System

The document provides an overview of Database Management Systems (DBMS), detailing their functions, characteristics, and advantages over traditional file processing systems. It explains key concepts such as data, tables, records, and fields, along with the differences between DBMS and RDBMS. Additionally, it outlines the components of a DBMS, its applications, and the role of a Database Administrator.

Uploaded by

yagdomyonon
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views

Data Base Management System

The document provides an overview of Database Management Systems (DBMS), detailing their functions, characteristics, and advantages over traditional file processing systems. It explains key concepts such as data, tables, records, and fields, along with the differences between DBMS and RDBMS. Additionally, it outlines the components of a DBMS, its applications, and the role of a Database Administrator.

Uploaded by

yagdomyonon
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 61

Data Base Management System

UNIT-1

1 Introduction to DBMS
A database management system (DBMS) is system software for creating and managing
databases. The DBMS provides users and programmers with a systematic way to create,
retrieve, update and manage the database
What is Data?
In computing, data is information that has been translated into a form that is more convenient
to move or process. Relative to today's computers and transmission media, data is
information converted into binary digital form
Collection of Information is called Data

Collection of Data is called File

Collection of File is called Record

Collection of Record is called Table

Collection of Table is called database

Collection of database is called Data warehouse

Data Mining
Easily and quickly retrieve the data from data warehouse. Data Mining is defined as
the procedure of extracting information from huge sets of data
Definition of Data

Data is row, unorganized facts that need to be processed.

Example: Each student’s test score is one piece of Data.

What is Information?

The processed data which provides useful info or organized Facts.

Example: The class average score is the information that can be concluded from the given
data.

What is Table?

In Relational database, a table is a collection of data elements organised in terms of rows and
columns. A table is also considered as convenient representation of relations. But a table can
have duplicate tuples while a true relation cannot have duplicate tuples. Table is the simplest
form of data storage. Below is an example of Employee table.

ID NAME AGE SALARY

1 ADAM 34 13000

2 ALEX 28 15000

3 STUART 20 18000

4 ROSS 42 19020

Figure 1: Table EMP

What is Record?
A single entry in a table is called a Record or Row. A Record in a table represents set of
related data. For example, the above Employee table has 4 records. Following is an example
of single record.
1 Adam 34 13000

Figure 1.1:Table EMP

What is Field?
A table consists of several records (row), each record can be broken into several smaller
entities known as Fields. The above Employee table consist of four
fields, ID, Name, Age and Salary.
What is Column?
In Relational table, a column is a set of value of a particular type. The term Attribute is also
used to represent a column. For example, in Employee table, Name is a column that
represents names of employee

1.2 Difference between DBMS and RDBMS

DBMS and RDBMS both are used to store information in physical database but they have
some differences, which is given below

DBMS RDBMS

In DBMS data are store as a file. In RDBMS data are store in tabular form.

In DBMS relationship between two tables In RDBMS relationship between two tables
or files are maintained programmatically or files can be specified at the time of table
creation.

DBMS does not support client server RDBMS supports client server architecture.
architecture.

DBMS does not support distributed RDBMS support distributed architecture.


architecture.

In DBMS there are no security of data. In RDBMS there are multiple level of
security, at logging level, at command level,
at object level.
DBMS may satisfy less than 7 rules of RDBMS usually satisfy more than 7 to 8
Dr.E.F. codd rules of Dr. E.F. codd.

Normalization is not available in DBMS. Normalization is available in RDBMS.

Examples of DBMS are file systems, xml Example of RDBMS are mysql, postgre, sql
etc. server, oracle etc.

Difference between DBMS and RDBMS

2. Characteristics of database System

Traditionally data was organized in file formats. DBMS was all new concepts then and all the
research was done to make it to overcome all the deficiencies in traditional style of data
management. Modern DBMS has the following characteristics:

1. Real-world entity: Modern DBMS are more realistic and uses real world entities to
design its architecture. It uses the behavior and attributes too. For example, a school
database may use student as entity and their age as their attribute.
2. Relation-based tables: DBMS allows entities and relations among them to form as
tables. This eases the concept of data saving. A user can understand the architecture of
database just by looking at table names etc.
3. Isolation of data and application: A database system is entirely different than its data.
Where database is said to active entity, data is said to be passive one on which the
database works and organizes. DBMS also stores metadata which is data about data,
to ease its own process.
4. Less redundancy: DBMS follows rules of normalization, which splits a relation when
any of its attributes is having redundancy in values. Following normalization, which
itself is a mathematically rich and scientific process, make the entire database to
contain as less redundancy as possible.
5. Consistency: DBMS always enjoy the state on consistency where the previous form of
data storing applications like file processing does not guarantee this. Consistency is a
state where every relation in database remains consistent. There exist methods and
techniques, which can detect attempt of leaving database in inconsistent state.
6. Query Language: DBMS is equipped with query language, which makes it more
efficient to retrieve and manipulate data. A user can apply as many and different
filtering options, as he or she wants. Traditionally it was not possible where file-
processing system was used.
7. ACID Properties: DBMS follows the concepts for ACID properties, which stands for
Atomicity, Consistency, Isolation and Durability. These concepts are applied on
transactions, which manipulate data in database. ACID properties maintains database
in healthy state in multi-transactional environment and in case of failure.

3. Advantages of DBMS over File Processing System

1. Better use of data or information - We can easily and efficiently access well-
managed and synchronized forms of data with the help of DBMS. It makes data
handling simple, provides an integrated perspective of how a certain business is
operating and also aids in keeping track of how one element of the business affects
another portion.
2. Secured Data - The likelihood of security problems increases as a database becomes
more functional and accessible. The danger to data security rises as a result of the rate
at which data is shared or transferred growing along with the user base. It is
frequently utilized in the business world where organizations spend a lot of time,
money, and effort making sure data is protected and handled effectively. Data
management systems (DBMS) offer a stronger framework for data privacy and
security policies, assisting businesses in enhancing data security.
3. Reduces Data Inconsistency and Redundancy - The major issues faced during the
process of storing data are inconsistency and redundancy. Inconsistent data may lead
to a big loss to an individual or a business model and the storage capacity is not
utilized properly because of the data redundancy. When multiple copies with different
versions or values of the same data exist in various locations, then it causes
inconsistency. Data Redundancy and inconsistency can both be significantly
decreased by properly designing a database with the help of a database management
system.
4. Better Recovery and Backups - Backup and recovery are handled automatically by
the DBMS. Users don't need to regularly back up their data because the DBMS
handles this for them. Additionally, it returns the database to its prior state following a
crash or system failure.
5. Fast Data Sharing - Database administration makes it possible for consumers to
access more and better-managed data. DBMS enables end users to quickly scan their
environment and react to any alterations made there.
6. Helps in decision-making - Because of the well-managed data and improved data
access provided by DBMS, we are able to produce better-quality information and, as a
result, make better judgments. Accuracy, validity, and the time it takes to read data
are all improved by better data quality. Although DBMS does not ensure data quality,
it does offer a framework that makes it simple to enhance data quality.
7. Increases Privacy - The privacy rule in a database specifies the privacy restrictions
that can only be accessed by authorized users. A user can only view the data he is
permitted to view since there are different degrees of database access. For instance, on
social networking sites, different accounts that a user wishes to access have varying
access restrictions and a user can only see his/her account details, not others.
8. User Friendly - Data are presented in a straightforward and logical manner by
database management systems (DBMS). It is simple to carry out many activities, such
as the addition, deletion, or creation of files or data.
9. Data Abstraction - In order to give users an abstract overview of the data, database
systems are primarily used. Since numerous intricate algorithms are employed by
developers to boost the effectiveness of databases that are concealed from users by
several degrees of data abstraction, consumers can easily engage with the system.

4. Applications of DBMS

DBMS performs multiple functions. Some of the main functions of DBMS include data
definition, data manipulation, data alteration, security and access control, recovery and
system., concurrency control, storage and data retrieval, user access, data integrity, and
performance optimization.
5. Purpose of Database System

The Database Management System (DBMS) is defined as a software system that allows the
user to define, create and maintain the database and provide control access to the data.

It is a collection of programs used for managing data and simultaneously it supports different
types of users to create, manage, retrieve, update and store information.

Purpose

The purpose of DBMS is to transform the following −

 Data into information.


 Information into knowledge.
 Knowledge to the action.

The diagram given below explains the process as to how the transformation of data to
information to knowledge to action happens respectively in the DBMS −
7. Components of a DBMS

The DBMS accepts the SQL commands generated from a variety of user interfaces, produces
query evaluation plans, executes these plans against the database, and returns the answers. As
shown, the major software modules or components of DBMS are as follows:
(i) Query processor: The query processor transforms user queries into a series of low level
instructions. It is used to interpret the online user's query and convert it into an efficient series
of operations in a form capable of being sent to the run time data manager for execution. The
query processor uses the data dictionary to find the structure of the relevant portion of the
database and uses this information in modifying the query and preparing and optimal plan to
access the database.
(ii) Run time database manager: Run time database manager is the central software
component of the DBMS, which interfaces with user-submitted application programs and
queries. It handles database access at run time. It converts operations in user's queries
coming. Directly via the query processor or indirectly via an application program from the
user's logical view to a physical file system. It accepts queries and examines the external and
conceptual schemas to determine what conceptual records are required to satisfy the user’s
request. It enforces constraints to maintain the consistency and integrity of the data, as well as
its security. It also performs backing and recovery operations. Run time database manager is
sometimes referred to as the database control system and has the following components:
• Authorization control: The authorization control module checks the authorization of users
in terms of various privileges to users.
• Command processor: The command processor processes the queries passed by
authorization control module.

Figure 4: Architecture of DBMS

Integrity checker: It .checks the integrity constraints so that only valid data can be entered
into the database.
Query optimizer: The query optimizers determine an optimal strategy for the query
execution.
• Transaction manager: The transaction manager ensures that the transaction properties
should be maintained by the system.
• Scheduler: It provides an environment in which multiple users can work on same piece of
data at the same time in other words it supports concurrency.
(iii) Data Manager: The data manager is responsible for the actual handling of data in the
database. It provides recovery to the system which that system should be able to recover the
data after some failure. It includes Recovery manager and Buffer manager. The buffer
manager is responsible for the transfer of data between the main memory and secondary
storage (such as disk or tape). It is also referred as the cache manger.
8. Database Environment

One of the primary aims of a database is to supply users with an abstract view of data, hiding
a certain element of how data is stored and manipulated. Therefore, the starting point for the
design of a database should be an abstract and general description of the information needs of
the organization that is to be represented in the database. And hence you will require an
environment to store data and make it work as a database. In this chapter, you will learn
about the database environment and its architecture.

9. Functions of DBMS

DBMS provides several functionalities, which are discussed below:

Data Storage Management

One of the most important tasks for DBMS is to create a database for complex data and
manage the data. It gives relief to the user by creating a structure for the complex data sets so
that users can access it and manipulate them very easily. Modern database systems not only
provide storage for the data but they store and manage the metadata (data of data) like data
procedural rules, validation rules etc. DBMS also provides performance tuning, which makes
accessing data faster and easier.

Security Management

Security is another aspect which is handled by the Database Management Systems. Database
systems provide a high level of security measures using various security algorithms to keep
the data safe and ensure the data privacy. There are certain security rules that ensure what
data can be accessed from the database and which user can access it. It also makes sure what
operations (read, write, delete) can be performed on the specific data. It is very important for
the organizations where multi-user databases are required.
Backup and Recovery Management

To keep the data safe and ensure the integrity, the database system provides the features for
backup and recovery management. If the system fails due to some reason then it recovers the
data and keeps the data safe.

Database Access Language and Application Programming Interface

DBMS provides a database access language which is also called a query language. Query
languages are non-procedural languages used to access the database and manipulate the data.
SQL is an example of a query language. The majority of DBMS vendors provide the support
of various query languages to access the data.

Data Dictionary Management

Data dictionary management is also useful functionality provided by the Database


Management System. In the data dictionary, it stores the data and its related information
about its relationship. So, a data dictionary keeps the data structures and their relationships
with other data, so that a programmer is not responsible for storing the relationship in the
database through complex coding. DBMS provides the data abstraction and removes the
dependency of the data from the system.

Data Transformation and Presentation

DBMS provides the functionality of data transformation, which means programmers need not
worry about the logical and physical representation of the data. DBMS stores the data in the
determined data structure.

For example, if a user asks for the date from a database and he receives it as 14 December
2022, but in the database, it is stored in different columns of month, date and year.

Multi User Access Control

Multi User Access control is another feature which is provided by the modern Database
Systems. So, more than one user can access the database at the same time without any
problem. This feature makes sure the integrity of the data present in the database. It also
follows the ACID property, so the database will be consistent while multiple users are
accessing it concurrently. It is very useful for the database of organizations where multiple
database engineers are working concurrently.

Data Integrity Management

Database systems provide data integrity management by maximizing the data consistency and
minimizing the data redundancy. The data dictionary is the feature database system used to
store the relationships of the data to keep the data integrity. Data integrity is needed where a
transaction based database system is present.

Database Communication Interface

When a user requests data from the database, it uses some environments like browsers
(Chrome or Firefox etc.) to get the data.

10. Database Users

Database Administrator (DBA)


A Database Administrator holds an important position in any DBMS environment. DBAs are
usually charged with the responsibility of maintaining the health as well as high performance
of the database. They guarantee the proper functioning and protection of the database.

Key Responsibilities:

 Schema Definition: DBAs define the structure of the database, including tables,
indexes, and relationships.
 Security Management: They set up user accounts, manage access permissions, and
ensure that data is protected from unauthorised access.
 Performance Monitoring: DBAs continuously monitor the database’s performance
and optimise it for speed and efficiency.
 Backup and Recovery: Regular backups are crucial. DBAs ensure that data is
backed up and can be recovered in case of a failure.
 Data Integrity: They make sure that the data remains accurate and consistent over its
lifecycle.

Example: Imagine managing an international shipping company’s logistics system. Here, a


DBA would ensure that all shipping records, tracking information, and customer data are
correctly stored, easily accessible, and secure.

Naive / Parametric End Users


Naive or Parametric End Users are those who interact with the database without having any
deep knowledge of its inner workings. They rely on pre-developed applications to perform
their tasks. These database users in DBMS are typically focused on completing specific, often
repetitive tasks.
Characteristics:

 No DBMS Knowledge: They do not need to understand how the database works.
 Use Pre-Developed Applications: Interaction with the database is through
applications with user-friendly interfaces.
 Task-Oriented: Their primary focus is on performing specific tasks, like data entry
or retrieval.

Example: Consider front desk staff at a hotel using a reservation management system. They
input guest information, check room availability, and update bookings without needing to
understand how the database processes this data.

System Analysts
System Analysts come in the middle of end users and the technical support staff. They
understand the requirements of the users and translate them into technical needs for the
database designers and programmers.
Key Responsibilities:

 Requirements Analysis: Understanding and documenting what the users need from
the database.
 Feasibility Studies: Assessing whether the requirements can be met within the
constraints of time, budget, and technology.
 Design Oversight: Ensuring that the database design meets the specified
requirements.

Example: A system analyst working on an online learning platform needs to compile a list of
requirements from the teachers and students. This includes but is not limited to managing the
course content, student enrollment, and the track progress among the learners.

Sophisticated Users
Sophisticated Users are those who have a deeper understanding of the DBMS. They can
directly interact with the database using complex queries and tools. These database users in
DBMS often include data analysts, scientists, and engineers who need detailed and flexible
access to the data.
Characteristics:

 Advanced Knowledge: They understand SQL and other query languages.


 Direct Interaction: They use tools and interfaces to run queries, generate reports, and
perform complex data analysis.
 Flexible Data Access: They need the ability to manipulate and retrieve data in
various ways to suit their needs.

Example: Accountants could employ SQL to sort through the numerous records of a
particular company’s sales database and produce highly reliable and detailed financial
statements and projections. It enables them to pull out certain data and calculate and analyse
trends all from a database.

Database Designers
Database Designers are greatly involved in the creation of the basic structure of any DBMS.
They are responsible for the task of determining the structural framework of the database
compliance to various conditions and optimum execution. Their duties entail a lot of planning
and designing to ensure that the database meets the needs of the organisation.
Key Responsibilities:

 Creating Database Structures: This includes defining tables, indexes, views,


triggers, and constraints. These elements are the building blocks of any database.
 Ensuring Data Integrity: Database designers implement rules and constraints that
maintain the accuracy and consistency of the data.
 Optimising Data Storage: They design databases in a way that optimises storage and
retrieval times.
 Interfacing with Users: They gather requirements from end users and translate these
into a functional database design.

Example: Consider a database designer working on an e-commerce platform. They need to


create tables for products, customers, orders, and reviews. Each table must be linked in a way
that queries can be run efficiently, like retrieving a customer’s order history or managing
inventory levels.

Application Programmers
Application programmers, or Back-End Developers as they are often called, are the people
who sit down and write the actual code that interacts with the database. They develop
applications that enable the user to carry out different operations on the data and guarantee
that they are intuitive and effective.

Key Responsibilities:

 Developing Applications: Writing code to build applications that interact with the
database.
 Maintaining Code: Regularly updating and debugging applications to ensure they
run smoothly.
 Optimising Performance: Ensuring that the applications are optimised for speed and
efficiency.
 User Support: Providing support and updates to ensure that user needs are met
effectively.
Example: Suppose software developers have been asked to create a mobile banking
application. They have to provide a code through which the user can view his/her balance,
transfer money, or review his/her transactions. This means that the app should be fast, highly
secure, and convenient in its usage facets.

Casual Users / Temporary Users


Casual Users, also known as Temporary Users, interact with the database occasionally and
for specific purposes. They usually require the database for tasks that do not occur on a daily
basis but are crucial when needed.

Characteristics:

 Occasional Access: They access the database intermittently, often to gather specific
information.
 User-Friendly Interfaces: They rely on interfaces that are easy to use and understand
without needing in-depth knowledge of the DBMS.
 Specific Tasks: Their interaction is task-oriented, such as generating reports or
retrieving specific data.

Example: A marketing manager might be a casual user who accesses the database quarterly
to generate reports on sales performance and customer demographics.

Specialised Users
Specialised Users can be defined as the individual who needs databases to fulfil specific, and
frequently demanding purposes. These database users in DBMS are the ones that require
unconventional data processing and need specific applications to accomplish this task.
11. Data Independence

 Data independence can be explained using the three-schema architecture.

 Data independence refers characteristic of being able to modify the schema at one
level of the database system without altering the schema at the next higher level.

There are two types of data independence:


1. Logical Data Independence
 Logical data independence refers characteristic of being able to change the conceptual
schema without having to change the external schema.

 Logical data independence is used to separate the external level from the conceptual view.

 If we do any changes in the conceptual view of the data, then the user view of the data
would not be affected.

 Logical data independence occurs at the user interface level.

2. Physical Data Independence

 Physical data independence can be defined as the capacity to change the internal
schema without having to change the conceptual schema.

 If we do any changes in the storage size of the database system server, then the
Conceptual structure of the database will not be affected.

 Physical data independence is used to separate conceptual levels from the internal
levels.

 Physical data independence occurs at the logical interface level.


Fig: Data Independence

13. Database System Architecture

DBMS Architecture 1-level, 2-Level, 3-Level


Last Updated : 06 Nov, 2023

A Database stores a lot of critical information to access data quickly and securely. Hence it is
important to select the correct architecture for efficient data management. DBMS
Architecture helps users to get their requests done while connecting to the database. We
choose database architecture depending on several factors like the size of the database,
number of users, and relationships between the users. There are two types of database models
that we generally use, logical model and physical model. Several types of architecture are
there in the database which we will deal with in the next section.
Types of DBMS Architecture
There are several types of DBMS Architecture that we use according to the usage
requirements. Types of DBMS Architecture are discussed here.
 1-Tier Architecture
 2-Tier Architecture
 3-Tier Architecture
1-Tier Architecture
In 1-Tier Architecture the database is directly available to the user, the user can directly sit on
the DBMS and use it that is, the client, server, and Database are all present on the same
machine. For Example: to learn SQL we set up an SQL server and the database on the local
system. This enables us to directly interact with the relational database and execute
operations. The industry won’t use this architecture they logically go for 2-tier and 3-tier
Architecture.

DBMS 1-Tier Architecture

Advantages of 1-Tier Architecture


Below mentioned are the advantages of 1-Tier Architecture.
 Simple Architecture: 1-Tier Architecture is the most simple architecture to set up, as
only a single machine is required to maintain it.
 Cost-Effective: No additional hardware is required for implementing 1-Tier
Architecture, which makes it cost-effective.
 Easy to Implement: 1-Tier Architecture can be easily deployed, and hence it is
mostly used in small projects.
2-Tier Architecture
The 2-tier architecture is similar to a basic client-server model. The application at the client
end directly communicates with the database on the server side. APIs like ODBC and JDBC
are used for this interaction. The server side is responsible for providing query processing and
transaction management functionalities. On the client side, the user interfaces and application
programs are run. The application on the client side establishes a connection with the server
side to communicate with the DBMS.
An advantage of this type is that maintenance and understanding are easier, and compatible
with existing systems. However, this model gives poor performance when there are a large
number of users.

DBMS 2-Tier Architecture

Advantages of 2-Tier Architecture


 Easy to Access: 2-Tier Architecture makes easy access to the database, which makes
fast retrieval.
 Scalable: We can scale the database easily, by adding clients or upgrading hardware.
 Low Cost: 2-Tier Architecture is cheaper than 3-Tier Architecture and Multi-Tier
Architecture.
 Easy Deployment: 2-Tier Architecture is easier to deploy than 3-Tier Architecture.
 Simple: 2-Tier Architecture is easily understandable as well as simple because of only
two components.
3-Tier Architecture
In 3-Tier Architecture, there is another layer between the client and the server. The client
does not directly communicate with the server. Instead, it interacts with an application server
which further communicates with the database system and then the query processing and
transaction management takes place. This intermediate layer acts as a medium for the
exchange of partially processed data between the server and the client. This type of
architecture is used in the case of large web applications.

DBMS 3-Tier Architecture

Advantages of 3-Tier Architecture


 Enhanced scalability: Scalability is enhanced due to the distributed deployment
of application servers. Now, individual connections need not be made between the
client and server.
 Data Integrity: 3-Tier Architecture maintains Data Integrity. Since there is a
middle layer between the client and the server, data corruption can be
avoided/removed.
 Security: 3-Tier Architecture Improves Security. This type of model prevents
direct interaction of the client with the server thereby reducing access to
unauthorized data.
 Disadvantages of 3-Tier Architecture
 More Complex: 3-Tier Architecture is more complex in comparison to 2-Tier
Architecture. Communication Points are also doubled in 3-Tier Architecture.
 Difficult to Interact: It becomes difficult for this sort of interaction to take place
due to the presence of middle layers.
14. Role of Database Administrator

A database is a storehouse for a large amount of data, which is used in organizations and
firms. A database management system is a program that helps in updating, accessing,
retrieving, representing, and manipulating the data in a database system.

Database administration involves every activity that is performed by an individual to ensure


that a database is available when needed. Maintaining the integrity of a database is the
primary goal of database administration.

A database administrator carries out several activities and operations to ensure the integrity,
security, and availability of the data stored in the database.

Why do we need a database administrator?

1. A database administrator ensures the security of a database.


2. A database administrator ensures integrity in the database, that is, they make sure that
all the data stored in the database is not misplaced or mismatched.
3. A database administrator works to see if the database is always available and
accessible.
4. A database administrator works with fellow colleagues to plan the future actions of
the organization.

Roles of a database administrator

1. Database backup:
A database administrator has the responsibility to back up every data in the database,
recurrently. This is necessary, so that operations can be restored in times of disaster or
downtime.
2. Database availability:
A database administrator has the responsibility of ensuring database accessibility to
users from time to time.
3. Database restore:
A database administrator has the responsibility of restoring a file from a backup state,
when there is a need for it.
4. Database design:
A database administrator has the responsibility of designing a database that meets the
demands of users. Hence, having knowledge of database design is crucial for an
administrator.
5. Data move:
A database administrator has the responsibility of moving a database set, say from a
physical base to a cloud base, or from an existing application to a new application.
6. Database upgrade:
A database administrator has the responsibility of upgrading database software files
when there is a new update for them, as this protects software from security breaches.
7. Database patch:
In times of new upgrades for database software, the database administrator has the
responsibility of ensuring that the database system functions perfectly and works to
close up any gaps in the new update.
8. Database security:
Datasets are assets, and one major responsibility of database administrators is to
protect the data and ensure adequate security in an organization’s database.
9. Capacity planning:
A database administrator has the responsibility of planning for increased capacity, in
case of sudden growth in database need.
10. Database monitoring:
A database administrator has the responsibility of monitoring the database and the
movement of data in the database. Administrators provide access for users who
require access to the database.
11. Error log review:
A database administrator has the responsibility of interpreting the error messages sent
by a database when there is a fault or bridge.

Skills needed by a database administrator

1. Good communication skills


2. Good problem-solving skills
3. Good understanding of query language(s) for database operations
4. Sufficient knowledge of Windows and Linux command operations
5. Sufficient knowledge of database design
6. Sufficient knowledge of database management systems
7. Good understanding of database tools
16. Introduction to relational databases and Relational Model

The relational model for database management is an approach to logically represent and
manage the data stored in a database. In this model, the data is organized into a collection of
two-dimensional inter-related tables, also known as relations. Each relation is a collection
of columns and rows, where the column represents the attributes of an entity and the rows (or
tuples) represent the records.

The use of tables to store the data provided a straightforward, efficient, and flexible way to
store and access structured information. Because of this simplicity, this data model provides
easy data sorting and data access. Hence, it is used widely around the world for data storage
and processing.

Let's look at a scenario to understand the relational model:

Consider a case where you wish to store the name, the CGPA attained, and the roll number of
all the students of a particular class. This structured data can be easily stored in a table as
described below:
As we can notice from the above relation:

 Any given row of the relation indicates a student i.e., the row of the table describes a
real-world entity.
 The columns of the table indicate the attributes related to the entity. In this case, the
roll number, CGPA, and the name of the student.

Relational Model Concepts

As discussed earlier, a relational database is based on the relational model. This database
consists of various components based on the relational model. These include:

 Relation: Two-dimensional table used to store a collection of data elements.


 Tuple: Row of the relation, depicting a real-world entity.
 Attribute/Field: Column of the relation, depicting properties that define the relation.
 Attribute Domain: Set of pre-defined atomic values that an attribute can take i.e., it
describes the legal values that an attribute can take.
 Degree: It is the total number of attributes present in the relation.
 Cardinality: It specifies the number of entities involved in the relation i.e., it is the
total number of rows present in the relation. Read more about Cardinality in DBMS.
 Relational Schema: It is the logical blueprint of the relation i.e., it describes the
design and the structure of the relation. It contains the table name, its attributes, and
their types:

TABLE_NAME(ATTRIBUTE_1 TYPE_1, ATTRIBUTE_2 TYPE_2, ...)

For our Student relation example, the relational schema will be:
STUDENT(ROLL_NUMBER INTEGER, NAME VARCHAR(20), CGPA FLOAT)

 Relational Instance: It is the collection of records present in the relation at a given


time.
 Relation Key: It is an attribute or a group of attributes that can be used to uniquely
identify an entity in a table or to determine the relationship between two tables.
Relation keys can be of 6 different types:
1. Candidate Key
2. Super Key
3. Composite Key
4. Primary Key
5. Alternate Key
6. Foreign Key

17. Terminologies used in relational model

The terms entity, attribute, table, primary key, foreign key, relational database, query, index,
normalization, ACID properties, data warehousing, data mining, backup and recovery, data
migration, and replication were all covered in detail in this article.

INTRODUCTION TO SQL

DATABASE LANGUAGES:

Four Types of Languages:

1. Data Definition languages (DDL)

2. Data manipulation language (DML)

3. Transaction Control language (TCL)

4. Data Control Language (DCL)


Figure 18: Database Language

Data Definition Language

DDL is used to create on object (table) alter the structure of an object and also to drop the
object created, the concepts relating to Data Definition Language.

Table

A table is a unit of storage that holds data in the form of Rows and Columns.

Syntax

Create table <table name>(column definition 1,column definition 2,……..);

1. We should specify a unique column name


2. We should specify proper data type along with its width.

Create table Employee (Emp_no Number (5), Emp_name varchar2 (10),Emp_address


varchar2(10)Emp_phoneno number(10));

EMP_NO EMP_NAME EMP_ADDRESS EMP_PHONENO

101 AAA XXXX 12345678


102 BBB XXXX 12289989

103 CCC XXXX 33445556

104 DDD XXXX 76654433

105 EEE XXXX 34456433

Figure 19: EMPLOYEE TABLE

Alter Table

There may be a need to change the structure of a table

Two types

 Modify
 Add
Syntax:
Alter table <table name> Modify (column definition….);
Alter table <table name> Add (column definition….);
 When the user wants to add a new column.
 To change the width of a data type or the data type itself.
 To include or drop integrity constraints.

SQL> alter table employee Modify (EMP name varchar2 (25));

SQL>alter table employee Add (EMP_MobileNo number (10));

Truncate Table

 Truncate means only deleted the particular records in the table but table structure
cannot deleted
 We have Reuse the table structure.
 Truncate command deletes all rows from the specified table
 Truncate table employee reuse storage the below command deletes all rows from the
table employee and reuse any space that was occupied by the table.

Syntax

SQL> Truncate Table <Table name>


SQL> Truncate table Employee;

Command to View the Table Structure

This command used for show the overall table structure.

SQL> Desc<Table name>

SQL> Desc Employee;

DROP Command

 We have used for Drop command the table deleted permanently in the data base we
cannot Reuse the table

SQL> Drop table<table name>

SQL>Drop table Employee;

DML COMMANDS (Data Manipulation Language)

1. Insert
2. Select
3. Update
4. Delete

Insert command

The insert command is used to add one or more Rows to a table.

Insert command is used to insert data into a table

Syntax:

INSERT into table-name values (data1,data2,..)

INSERT into Employee values (102,'Alex', null);

INSERT ALL

into employee(emp_No, emp_name) values(1,’aaa’)

into employee(emp_No, emp_name) values(1,’aaa’)


into employee(emp_No, emp_name) values(1,’aaa’)

into employee(emp_No, emp_name) values(1,’aaa’)

into employee(emp_No, emp_name ) values(1,’aaa’)

select * from employee;

SELECT COMMAND

Select * from tablename;

Select column-name……… from table name…;

Select empNo, empname, emp telephone from employee;

SELECT DISTINCT ROWS

To represent the selection of duplicate Rows otherwise to avoid the duplicate values

Select distinct empno, empname from employee;

Select command using where clause

Select columns…from table where conditions(order by);

SQLl> select * from order_master where vencode=’v004’ order by del-date;

Update command

The update command we can update rows in the table. A single column may be updated
or more than one column could be updated specific rows could be updated based on a specific
condition.

The update command consists of a ‘set’ clause and an optional –‘where’ clause

Update table-name set field=value…….where condition;

SQL> update vendor_master set tel no=12345

Where vencode=’V002’ ;

SQL> update order-master set (order status)=’c’


Where orderno=’006’ ;

Delete command

Delete from <table name > where conditions ;

Delete from order_master where orderno =’006’ ;

TCL (Transaction control language)

1.commit

2. save point

3. Roll back

Commit

1. This command is used to end a transaction

2. Transaction change scan be made permanent to the database

Commit work;

Commit

Savepoint

Save points are like markers to divide a very lengthy transaction to smaller ones.

Savepoint savepoint name;

Roll back

A roll back is used to undo the work done in the current transaction.

Rollback work

Roll back;

DCL

 Data control language provides user within privilege commands.


 The owner can allow other database users access to the objects
Grand privilege command

The user they will provide grand privileges to the other user

Grand privileges on <object-name> to <user name>;

Grand select on vendor_master to accounts with grant option;

Revoke privilege command

Revoke privilaes on <objectname> from <username>

SQL > revoke select, update on order_master from warehouse;

Different select statements

Select * from tablename;

Select column-name<alics –name>from tablename;

SQL>select vencode id from vendor_master;

2.2 GROUP FUNCTIONS

1) Avg ( )
2) Max ( )
3) Min ( )
4) Sum ( )
5) Count ( )

A group functions returns a result based on a group of rows this function purely mathematical
function. Expressions that are not encapsulated within an aggregate function and must be
included in the GROUP BY Clause at the end of the SQL statement. This is an
aggregate function such as the SUM, COUNT, MIN, MAX, or AVG functions. This is the
column or expression that the aggregate function will be used on Optional.

1. AVG() Function
The Avg() function will return the average of values of the column specified in the
argument of the column.
Select Avg (salary) from Employee;
Using Where clause
SQL >Select Avg (salary) from employee where salary >6000;
2. Max() Function

To perform an operation which gives the maximum of a set of values the max function
can be made use of….

SQL >Select Max(Midlevel) from itemfile where P_categery =’Spares’;

3. Min() Function
This function will give the least of all values of the column present in the argument.
SQL >Select Min(Midlevel) from itemfile where P_categery =’Spares’;

4. Sum() Function

The sum function can be used to obtain the sum of a range of values of a record set

SQL> select sum(itemrate*maxlevel) from itemfile;

5. Count Function

In order to count the number of rows count function is used, count(*) it counts all
Records inclusive of duplicates and Nulls.

SQL> Select count(*) from itemfile;

SQL> select count(item rate)from itemfile;

5.1 Count Distinct col_name

It is similar to count(col_name) but eliminates duplicate values while counting

SQL> Select count(distinct qty_hand) from itemfile;

This query will eliminate the duplicate and null values in the column and provide the
result.

2.3 GROUP BY CLAUSE


GROUP BY clause specifies a summary query this is usually used with aggregate
functions Like(SUM,AVG,MAX,MIN) etc. Simultaneous usage of column name and
group function is totally absurd in a select statement.

SQL> create table itemfile(itemcode varchar2(5),itemdesc varchar2(20),p_category


varchar2(20),qty_hand number(5),re_level number(5),max_level number(5),Itemrate
number(9,2));

SQL> select P_category max(itemrate) from item file group by p_category;

P_category Max(itemrate)
Accessories 4000
Spares 2000

Figure 20: Itemfile Table

SQL> select P_category Max(itemrate)from itemfile groupby p_category;

SQL>Select itemcode,itemdesc,p_category,Qty_hand,re_level,max_level from itemfile


order by p_category itemcode desc;

HAVING CLAUSE

The having clause is used to specify certain conditions on rows retrieved by using group
by clause this clause should be preceded by group by clause

SQL> select p_category max(itemrate) from itemfile group by p_category having


p_category not in (‘accessories’);

P_category Max(item rate)


Spares 2000

Figure 20.1: Itemfile Table using Having Class

Group By

Arithmetic operations can also be performed in a group function some additional


functions are also available like stddev and variance.
Stddev

The stddev function gives the standard deviation of a norm of values

SQL> select p_category stddev(itemrate) from item file groupby p_category;

SQL> select p_category variance(itemrate) from item file groupby p_category;

ORDERBY [Asce][Desc]

Clause sorts or orders the results based on the data in one or more columns in the
ascending or descending Order.

2.4 SET OPERATORS

Set operators combine the results of two queries into a single one.

1. Union
2. Union all
3. Inter sect
4. Minus

Rules of set operators

1. The queries which one related by a set operators should have the same number of
columns and the corresponding columns must be of the same data type.
2. Such a query should not contain any column of type long.
3. The label under which the rows are displayed are those from the first select statement

Union

Union operator returns all distinct rows selected by both queries.

Query

Select order no from order_master UNION Select orderno from order_detail;

Result

001
002

003

004

006

015

Union All

The Union all operator returns all rows selected by either query including duplicates.

SQL>Select orderno from order_master UNION ALL select orderno from order_detail;

Result

001

002

003

006

001

001

001

002

Intersect

Intersect operator returns only rows that are common to both the queries

SQL> select orderno from order_master intersect select orderno from order_detail;

Minus

Minus operator returns all distinct rows selected only by the first query and not by the second
SQL> Select orderno from order_master minus select orderno from order_detail;

2.5 Sub Queries

Sub queries means nesting of queries

1. The sub query Returns Multiple rows where as it is a single row sub query

SQL>Select* from order_detail where orderno=(select orderno from order_master where


del_date=’25-may-98’ and ven_code=’V001’);

SQL>Select* from itemfile where itemdesc=(select itemdesc from itemfile where


p_category=’spares’);

This select statement is single row subqueries returns multiple rows where as it is a single
row subquery

Note

1. Always enclose sub query within parenthesis.


2. Sub query will be evaluated first followed by the main query.
3. Sub queries can also return more than one value.
4. We should include operators link.

Comparison Operator

Any

All

In

Not in

Between

Example

Select*from order_detail where qty_del<any(select qty_hand from itemfile where itemrate


between 50 and 120);
SQL Subquery

Subquery or Inner query or Nested query is a query in a query. SQL subquery is usually
added in the WHERE Clause of the SQL statement. Most of the time, a subquery is used
when you know how to search for a value using a SELECT statement, but do not know the
exact value in the database. Subqueries are an alternate way of returning data from multiple
tables. Subqueries can be used with the following SQL statements along with the comparision
operators like =, <, >, >=, <= etc.

1. SELECT
2. INSERT
3. UPDATE
4. DELETE

SQL Subquery Example

1) Usually, a subquery should return only one record, but sometimes it can also return
multiple records when used with operators LIKE IN, NOT IN in the where clause. The query
syntax would be like,

SELECT first_name, last_name, subject

FROM student_details

WHERE games NOT IN ('Cricket', 'Football');

Subquery output would be similar to

first_name last_name subject

------------- ------------- ----------

Shekar Gowda Badminton

Priya Chandra Chess


2) Lets consider the student_details table which we have used earlier. If you know the name
of the students who are studying science subject, you can get their id's by using this query
below,

SELECT id, first_name

FROM student_details

WHERE first_name IN ('Rahul', 'Stephen');

but, if you do not know their names, then to get their id's you need to write the query in this
manner,

SELECT id, first_name

FROM student_details

WHERE first_name IN (SELECT first_name

FROM student_details

WHERE subject= 'Science');

Subquery Output:

Id first_name

-------- -------------

100 Rahul

102 Stephen
In the above sql statement, first the inner query is processed first and then the outer query is
processed.

SQL Subquery; INSERT Statement

3) Subquery can be used with INSERT statement to add rows of data from one or more tables
to another table. Lets try to group all the students who study Maths in a table 'maths_group'.

INSERT INTO maths_group(id, name)

SELECT id, first_name || ' ' || last_name

FROM student_details WHERE subject= 'Maths'

SQL Subquery; SELECT Statement

4) A subquery can be used in the SELECT statement as follows. Lets use the product and
order_items table defined in the sql_joins section.

select p.product_name, p.supplier_name, (select order_id from order_items where

product_id = 101) as order_id from product p where p.product_id = 101

product_name supplier_name order_id

------------------ ------------------ ----------

Television Onida 5103

Correlated Subquery

A query is called correlated subquery when both the inner query and the outer query are
interdependent. For every row processed by the inner query, the outer query is processed as
well. The inner query depends on the outer query before it can be processed.
SELECT p.product_name FROM product p

WHERE p.product_id = (SELECT o.product_id FROM order_items o

WHERE o.product_id = p.product_id);

2.6 Nested Subquery

1) You can nest as many queries you want but it is recommended not to nest more than 16
subqueries in oracle.

Non-Corelated Subquery

2) If a subquery is not dependent on the outer query it is called a non-correlated subquery.

Subquery Errors

3) Minimize subquery errors: Use drag and drop, copy and paste to avoid running subqueries
with spelling and database types. Watch your multiple field SELECT comma use, extra or to
few getting SQL error message "Incorrect syntax".

SQL Subquery Comments

Adding SQL Sub query comments are good habit (/* your command comment */) which can
save you time, clarify your previous work results in less SQL headaches.

2.7 Like clause

Like clause is used as condition in SQL query. Like clause compares data with an expression
using wildcard operators. It is used to find similar data from the table.

2.8 Wildcard operators

There are two wildcard operators that are used in like clause.

Percent sign % : Represents zero, one or more than one character.

Underscore sign _ : Represents only one character.


Example of LIKE clause

Consider the following Student table.

s_id s_Name Age

101 Adam 15

102 Alex 18

103 Abhi 17

Figure 21.1: Student Table

SELECT * from Student where s_name like 'A%';

The above query will return all records where s_name starts with character 'A'.

s_id s_Name Age

101 Adam 15

102 Alex 18

103 Abhi 17

Figure 21.2: Student Table

Example

SELECT * from Student where s_name like '_d%';


The above query will return all records from Student table where s_name contain 'd' as
second character.

s_id s_Name Age

101 Adam 15

Figure 21.3: Student Table

Example

SELECT * from Student where s_name like '%x';

The above query will return all records from Student table where s_name contain 'x' as last
character.

s_id s_Name Age

102 Alex 18

Figure 21.4: Student Table

2.9 Order by Clause

Order by clause is used with Select statement for arranging retrieved data in sorted order.
The Order by clause by default sort data in ascending order. To sort data in descending
order DESC keyword is used with Order by clause.

Syntax of Order By

SELECT column-list|* from table-name order by asc|desc;

Example using Order by

Consider the following Emp table


Eid Name Age Salary

401 Anu 22 9000

402 Shane 29 8000

403 Rohan 34 6000

404 Scott 44 10000

405 Tiger 35 8000

Figure 22.1: EMP Table

SELECT * from Emp order by salary;

The above query will return result in ascending order of the salary.

Eid Name Age Salary

403 Rohan 34 6000

402 Shane 29 8000

405 Tiger 35 8000

401 Anu 22 9000

404 Scott 44 10000


Figure 22.2: EMP Table

Example of Order by DESC

Consider the Emp table described above,

SELECT * from Emp order by salary DESC;

The above query will return result in descending order of the salary.

Eid Name Age Salary

404 Scott 44 10000

401 Anu 22 9000

405 Tiger 35 8000

402 Shane 29 8000

403 Rohan 34 6000

Figure 22.3: EMP Table

2.10 Subtotals and Group By Clause

Group by clause is used to group the results of a SELECT query based on one or more
columns. It is also used with SQL functions to group the result from one or more tables.

Syntax for using Group by in a statement.

SELECT column_name, function(column_name)

FROM table_name

WHERE condition
GROUP BY column_name

Example of Group by in a Statement

Consider the following Emp table.

Eid Name Age Salary

401 Anu 22 9000

402 Shane 29 8000

403 Rohan 34 6000

404 Scott 44 9000

405 Tiger 35 8000

Figure 22.4: EMP Table

Here we want to find name and age of employees grouped by their salaries

SQL query for the above requirement will be,

SELECT name, age

from Emp group by salary

Result will be,

Name Age

Rohan 34
Shane 29

Anu 22

Figure 22.5: EMP Table

Example of Group by in a Statement with WHERE clause

Consider the following Emp table

Eid Name Age Salary

401 Anu 22 9000

402 Shane 29 8000

403 Rohan 34 6000

404 Scott 44 9000

405 Tiger 35 8000

Figure 22.6: EMP Table

SQL query will be,

select name, salary

from Emp

where age > 25


group by salary

Result will be

Name Salary

Rohan 6000

Shane 8000

Scott 9000

Figure 22.7: EMP Table

You must remember that Group By clause will always come at the end, just like the Order by
clause.

2.11 HAVING Clause

Having clause is used with SQL Queries to give more precise condition for a statement. It is
used to mention condition in Group based SQL functions, just like WHERE clause.

Syntax for having will be,

select column_name, function(column_name)

FROM table_name

WHERE column_name condition

GROUP BY column_name

HAVING function(column_name) condition

Example of HAVING Statement


Consider the following Sale table.

Oid order_name previous_balance customer

11 ord1 2000 Alex

12 ord2 1000 Adam

13 ord3 2000 Abhi

14 ord4 1000 Adam

15 ord5 2000 Alex

Figure 22.8: Sale Table

Suppose we want to find the customer whose previous_balance sum is more than 3000.

We will use the below SQL query,

SELECT *

from sale group customer

having sum(previous_balance) > 3000

Result will be,

Oid order_name previous_balance customer

11 ord1 2000 Alex

Figure 22.9: Sale Table


AND & OR operator

AND and OR operators are used with Where clause to make more precise conditions for
fetching data from database by combining more than one condition together.

AND operator

AND operator is used to set multiple conditions with Where clause.

Example of AND: Consider the following Emp table

Eid Name Age Salary

401 Anu 22 5000

402 Shane 29 8000

403 Rohan 34 12000

404 Scott 44 10000

405 Tiger 35 9000

Figure 23.1: EMP Table

SELECT * from Emp WHERE salary < 10000 AND age > 25

The above query will return records where salary is less than 10000 and age greater than 25.

Eid Name Age salary

402 Shane 29 8000


405 Tiger 35 9000

Figure 23.2: EMP Table

OR operator

OR operator is also used to combine multiple conditions with Where clause. The only
difference between AND and OR is their behaviour. When we use AND to combine two or
more than two conditions, records satisfying all the condition will be in the result. But in case
of OR, atleast one condition from the conditions specified must be satisfied by any record to
be in the result.

Example of OR

Consider the following Emp table

Eid Name Age Salary

401 Anu 22 5000

402 Shane 29 8000

403 Rohan 34 12000

404 Scott 44 10000

405 Tiger 35 9000

Figure 23.3: EMP Table

SELECT * from Emp WHERE salary > 10000 OR age > 25


The above query will return records where either salary is greater than 10000 or age greater
than 25.

402 Shane 29 8000

403 Rohan 34 12000

404 Scott 44 10000

405 Tiger 35 9000

Figure 23.4: EMP Table

2.12 SQL View

A view in SQL is a logical subset of data from one or more tables. View is used to restrict
data access.

Syntax for creating a View,

CREATE or REPLACE view view_name AS

SELECT column_name(s)

FROM table_name

WHERE condition

Example of Creating a View

Consider following Sale table,

Oid order_name previous_balance Customer

11 ord1 2000 Alex


12 ord2 1000 Adam

13 ord3 2000 Abhi

14 ord4 1000 Adam

15 ord5 2000 Alex

Figure 24: Sale Table

SQL Query to Create View

CREATE or REPLACE view sale_view as select * from Sale where customer = 'Alex';

The data fetched from select statement will be stored in another object called sale_view. We
can use create separately and replace too but using both together works better.

Example of Displaying a View

Syntax of displaying a view is similar to fetching data from table using Select statement.

SELECT * from sale_view;

Force View Creation

Force keyword is used while creating a view. This keyword force to create View even if the
table does not exist. After creating a force View if we create the base table and enter values in
it, the view will be automatically updated.

Syntax for forced View is,

CREATE or REPLACE force view view_name AS

SELECT column_name(s)

FROM table_name

WHERE condition
Update a View

Update command for view is same as for tables.

Syntax to Update a View is,

UPDATE view-name

set value

WHERE condition;

If we update a view it also updates base table data automatically.

Read-Only View

We can create a view with read-only option to restrict access to the view.

Syntax to create a view with Read-Only Access

CREATE or REPLACE force view view_name AS

SELECT column_name(s)

FROM table_name

WHERE condition with read-only

The above syntax will create view for read-only purpose, we cannot Update or Insert data
into read-only view. It will throw an error.

Types of View

There are two types of view,

1. Simple View
2. Complex View

Difference between Simple View and Complex View

Simple View Complex View


Created from one table Created from one or more table

Does not contain functions Contain functions

Does not contain groups of data Contains groups of data

22. Key constraints and it types

Keys

This ensures the accuracy and reliability of the data in the table. It is used for identifying
unique rows from table. Data should not Repeaters as in the table. A key is an attribute or a
set of attributes in a relation that identifies a tuple in a relation. The keys are defined in a
table to access or sequence the stored data quickly and smoothly. They are also used to create
relationship between different tables.
Types of Keys
1. Unique key

2. Primary key

3. Foreign key

4. Super key

5. Candidate key

6. Composite key / compound key

7. Alternate key

1.Unique key

The unique key uniquely identifies each record in a database table, the data should not
Repeaters.
2.Primary key

The primary key uniquely identifies each record in a database table; primary key must
contain unique values and cannot contain NULL values.

Primary Key Unique Key

Primary key can`t accept null values Unique key can accept only one null
values

We can have only one primary in the We can have more than one unique key in
table the table

Primary key can be made foreign key into In SQL server unique key can be made
another table. foreign key into another table.

Figure 5: Difference between Unique key and Primary key

3.Foreign keys
Foreign keys are the columns of a table that points to the primary key of another table. They
act as a cross-reference between tables.
4.Super key
A super key is a set or one of more columns (attributes) to uniquely identify rows in a table.
Often people get confused between super key and candidate key, so we will also discuss a
little about candidate key here.
5.Candidate key
A candidate key is a column, or set of columns, in a table that can uniquely identify any
database record without referring to any other data. Each table may have one or
more candidate keys, but one candidate key is special, and it is called the primary key.
6.Composite key
A composite key, in the context of relational databases, is a combination of two or more
columns in a table that can be used to uniquely identify each row in the table. Uniqueness is
only guaranteed when the columns are combined; when taken individually the columns do
not guarantee uniqueness.
7.Alternate key
An alternate key is a key associated with one or more columns whose values uniquely
identify every row in the table, but which is not the primary key. For example, where the
primary key for a table may be the employee id, the alternate key might combine the first,
middle, and last names of the employee.
23. Specifying Foreign Key Constraints in SQL

Foreign Key

 The foreign key a constraint is a column or list of columns that points to the primary
key column of another table
 The main purpose of the foreign key is only those values are allowed in the present
table that will match the primary key column of another table.

Example to create a foreign key

Reference Table

CREATE TABLE CUSTOMERS1(


ID INT ,
NAME VARCHAR (20) ,
COURSE VARCHAR(10) ,
PRIMARY KEY (ID)
);

Child Table

CREATE TABLE CUSTOMERS2(


ID INT ,
MARKS INT,
REFERENCES CUSTOMERS1(ID)
);
24. Integrity Constraints over Relations

Integrity constraints are the set of predefined rules that are used to maintain the quality of
information. Integrity constraints ensure that the data insertion, data updating, data deleting
and other processes have to be performed in such a way that the data integrity is not
affected. They act as guidelines ensuring that data in the database remain accurate and
consistent. So, integrity constraints are used to protect databases. The various types of
integrity constraints are
Types of Integrity Constraints:
 Domain Constraints
 Not-Null Constraints
 Entity integrity Constraints
 Key Constraints
 Primary Key Constrains
 Referential integrity constraints

Domain Constraints
These are defined as the definition of valid set of values for an attribute. The data type of
domain include string, char, time, integer, date, currency etc. The value of the attribute
must be available in comparable domains.
Example:

Student_Id Name Semester Age

21CSE100 Ramesh 5th 20

21CSE101 Kamlesh 5th 21


Student_Id Name Semester Age

21CSE102 Aakash 5th 22

21CSE103 Mukesh 5th 20

Not-Null Constraints
It specifies that within a tuple, attributes overs which not-null constraint is specified must
not contain any null value.
Example:
Let, the not-null constraint be specified on the “Semester” attribute in the relation/table
given below, then the data entry of 4th tuple will violate this integrity constraint, because
the “Semester” attribute in this tuple contains null value. To make this database instance a
legal instance, its entry must not be allowed by database management system.

Student_id Name Semester Age

21CSE100 Ramesh 5th 20

21CSE101 Kamlesh 5th 21

21CSE102 Akash 5th 22

21CSE103 Mukesh 20

Entity Integrity Constraints


Entity integrity constraints state that primary key can never contain null value because
primary key is used to determine individual rows in a relation uniquely, if primary key
contains null value then we cannot identify those rows. A table can contain null value in it
except primary key field.
Example:
It is not allowed because it is containing primary key as NULL value.

Student_id Name Semester Age

21CSE101 Ramesh 5th 20


Student_id Name Semester Age

21CSE102 Kamlesh 5th 21

21CSE103 Aakash 5th 22

Mukesh 5th 20

Key Constraints
Keys are the entity set that are used to identify an entity within its entity set uniquely. An
entity set can contain multiple keys, bit out of them one key will be primary key. A primary
key is always unique, it does not contain any null value in table.
Example:

Student_id Name Semester Age

21CSE101 Ramesh 5th 20

21CSE102 Kamlesh 5th 21

21CSE103 Aakash 5th 22

21CSE102 Mukesh 5th 20

It is now acceptable because all rows must be unique.


Primary Key Constraints
It states that the primary key attributes are required to be unique and not null. That is,
primary key attributes of a relation must not have null values and primary key attributes of
two tuples must never be same. This constraint is specified on database schema to the
primary key attributes to ensure that no two tuples are same.
Example
Here, in the below example the Student_id is the primary key attribute. The data entry of
4th tuple violates the primary key constraint that is specifies on the database schema and
therefore this instance of database is not a legal instance.

Student_id Name Semester Age


Student_id Name Semester Age

21CSE101 Ramesh 5th 20

21CSE102 Kamlesh 5th 21

21CSE103 Akash 5th 22

21CSE103 Mukesh 5th 20

Referential integrity constraints


It can be specified between two tables. In case of referential integrity constraints, if
a Foreign key in Table 1 refers to Primary key of Table 2 then every value of the Foreign
key in Table 1 must be null or available in Table 2.
Example:
Here, in below example Block_No 22 entry is not allowed because it is not present in 2nd
table.

Student_id Name Semester Block_No

22CSE101 Ramesh 5th 20

21CSE105 Kamlesh 6th 21

22CSE102 Aakash 5th 20

You might also like