SlideShare a Scribd company logo
1 of 50
Download to read offline
Database Management System
Prof. K. Adisesha 1
Introduction to DBMS
DBMS stands for Database Management System. We can break it like this DBMS = Database +
Management System. Database is a collection of data and Management System is a set of programs to
store and retrieve those data. Based on this we can define DBMS like this: DBMS is a collection of
inter-related data and set of programs to store & access those data in an easy and effective manner.
What is the need of DBMS?
Database systems are developed for large amount of data. When dealing with huge amount of data, there
are two things that require optimization: Storage of data and retrieval of data.
Storage: According to the principles of database systems, the data is stored in such a way that it
acquires lot less space as the redundant data (duplicate data) has been removed before storage. Let us
take a layman example to understand this:
In a banking system, suppose a customer is having two accounts, one is saving account and another is
salary account. Let’s say bank stores saving account data at one place (these places are called tables we
will learn them later) and salary account data at another place, in that case if the customer information
such as customer name, address etc. are stored at both places then this is just a wastage of storage
(redundancy/ duplication of data), to organize the data in a better way the information should be stored
at one place and both the accounts should be linked to that information somehow. The same thing we
achieve in DBMS.
Fast Retrieval of data: Along with storing the data in an optimized and systematic manner, it is also
important that we retrieve the data quickly when needed. Database systems ensure that the data is
retrieved as quickly as possible.
Building blocks/Components
DBMS Engine accepts logical requests from various other DBMS subsystems, converts them into
physical equivalents, and actually accesses the database and data dictionary as they exist on a storage
device.
Data Definition Subsystem helps the user create and maintain the data dictionary and define the
structure of the files in a database.
Data Manipulation Subsystem helps the user to add, change, and delete information in a database and
query it for valuable information. Software tools within the data manipulation subsystem are most often
the primary interface between user and the information contained in a database. It allows the user to
specify its logical information requirements.
Application Generation Subsystem contains facilities to help users develop transaction-intensive
applications. It usually requires that the user perform a detailed series of tasks to process a transaction. It
facilitates easy-to-use data entry screens, programming languages, and interfaces.
Database Management System
Prof. K. Adisesha 2
Data Administration Subsystem helps users manage the overall database environment by providing
facilities for backup and recovery, security management, query optimization, concurrency control, and
change management.
Database Applications – DBMS
Applications where we use Database Management Systems are:
 Telecom: There is a database to keeps track of the information regarding calls made, network
usage, customer details etc. Without the database systems it is hard to maintain that huge amount
of data that keeps updating every millisecond.
 Industry: Where it is a manufacturing unit, warehouse or distribution centre, each one needs a
database to keep the records of ins and outs. For example distribution centre should keep a track
of the product units that supplied into the centre as well as the products that got delivered out
from the distribution centre on each day; this is where DBMS comes into picture.
 Banking System: For storing customer info, tracking day to day credit and debit transactions,
generating bank statements etc. All this work has been done with the help of Database
management systems.
 Education sector: Database systems are frequently used in schools and colleges to store and
retrieve the data regarding student details, staff details, course details, exam details, payroll data,
attendance details, fees details etc. There is a hell lot amount of inter-related data that needs to be
stored and retrieved in an efficient manner.
 Online shopping: You must be aware of the online shopping websites such as Amazon, Flipkart
etc. These sites store the product information, your addresses and preferences, credit details and
provide you the relevant list of products based on your query. All this involves a Database
management system.
Data Model
A model is a representation of reality, 'real world' objects and events, associations. It is an abstraction
that concentrates on the essential, inherent aspects an organization and ignores the accidental properties.
A data model represents the organization itself. It should provide the basic concepts and notations that
will allow database designers and end users unambiguously and accurately to communicate their
understanding of the organizational data.
Data Model can be defined as an integrated collection of concepts for describing and manipulating data,
relationships between data, and constraints on the data in an organization.
A data model comprises of three components:
• A structural part, consisting of a set of rules according to which databases can be constructed.
• A manipulative part, defining the types of operation that are allowed on the data (this includes the
operations that are used for updating or retrieving data from the database and for changing the structure
of the database).
Database Management System
Prof. K. Adisesha 3
• Possibly a set of integrity rules, which ensures that the data is accurate.
The purpose of a data model is to represent data and to make the data understandable. There have been
many data models proposed in the literature. They fall into three broad categories:
• Object Based Data Models
• Physical Data Models
• Record Based Data Models
The object based and record based data models are used to describe data at the conceptual and external
levels, the physical data model is used to· describe data at the internal level.
Object Based Data Models
Object based data models use concepts such as entities, attributes, and relationships. An entity is a
distinct object (a person, place, concept, and event) in the organization that is to be represented in the
database. An attribute is a property that describes some aspect of the object that we wish to record, and a
relationship is an association between entities.
Some of the more common types of object based data model are:
• Entity-Relationship
• Object Oriented
• Semantic
• Functional
The Entity-Relationship model has emerged as one of the main techniques for modeling database design
and forms the basis for the database design methodology. The object oriented data model extends the
definition of an entity to include, not only the attributes that describe the state of the object but also the
actions that are associated with the object, that is, its behavior. The object is said to encapsulate both
state and behavior. Entities in semantic systems represent the equivalent of a record in a relational
system or an object in an OO system but they do not include behaviour (methods). They are abstractions
'used to represent real world (e.g. customer) or conceptual (e.g. bank account) objects. The functional
data model is now almost twenty years old. The original idea was to' view the database as a collection of
extensionally defined functions and to use a functional language for querying the database.
Physical Data Models
Physical data models describe how data is stored in the computer, representing information such as
record structures, record ordering, and access paths. There are not as many physical data models as
logical data models, the most common one being the Unifying Model.
Record Based Logical Models
Record based logical models are used in describing data at the logical and view levels. In contrast to
object based data models, they are used to specify the overall logical structure of the database and to
provide a higher-level description of the implementation. Record based models are so named because
Database Management System
Prof. K. Adisesha 4
the database is structured in fixed format records of several types. Each record type defines a fixed
number of fields, or attributes, and each field is usually of a fixed length.
Modeling language
A modeling language is a data modeling language to define the schema of each database hosted in the
DBMS, according to the DBMS database model. Database management systems (DBMS) are designed
to use one of five database structures to provide simplistic access to information stored in databases. The
five database structures are:
 Hierarchical model,
 Network model,
 Relational model,
 Object model.
Inverted lists and other methods are also used. A given database management system may provide one
or more of the five models. The optimal structure depends on the natural organization of the
application's data, and on the application's requirements, which include transaction rate (speed),
reliability, maintainability, scalability, and cost.
Hierarchical structure
The hierarchical structure was used in early mainframe DBMS. Records’ relationships form a treelike
model. This structure is simple but nonflexible because the relationship is confined to a one-to-many
relationship. IBM’s IMS system and the RDM Mobile are examples of a hierarchical database system
with multiple hierarchies over the same data. RDM Mobile is a newly designed embedded database for a
mobile computer system. The hierarchical structure is used primarily today for storing geographic
information and file systems.
Network Model
Database Management System
Prof. K. Adisesha 5
The network structure consists of more complex relationships. Unlike the hierarchical structure, it can
relate to many records and accesses them by following one of several paths. In other words, this
structure allows for many-to-many relationships.
The relational structure is the most commonly used today. It is used by mainframe, midrange and
microcomputer systems. It uses two-dimensional rows and columns to store data. The tables of records
can be connected by common key values. While working for IBM, E.F. Codd designed this structure in
1970. The model is not easy for the end user to run queries with because it may require a complex
combination of many tables.
The object oriented structure has the ability to handle graphics, pictures, voice and text, types of data,
without difficultly unlike the other database structures. This structure is popular for multimedia Web-
based applications. It was designed to work with object-oriented programming languages such as Java.
The dominant model in use today is the ad hoc one embedded in SQL, despite the objections of purists
who believe this model is a corruption of the relational model since it violates several fundamental
principles for the sake of practicality and performance. Many DBMSs also support the Open Database
Connectivity API that supports a standard way for programmers to access the DBMS.
Before the database management approach, organizations relied on file processing systems to organize,
store, and process data files. End users criticized file processing because the data is stored in many
different files and each organized in a different way. Each file was specialized to be used with a specific
application. File processing was bulky, costly and nonflexible when it came to supplying needed data
accurately and promptly. Data redundancy is an issue with the file processing system because the
independent data files produce duplicate data so when updates were needed each separate file would
need to be updated. Another issue is the lack of data integration. The data is dependent on other data to
organize and store it. Lastly, there was not any consistency or standardization of the data in a file
processing system, which makes maintenance difficult. For these reasons, the database management
approach was produced.
Characteristics of a DBMS
Database Management System
Prof. K. Adisesha 6
 Real-world entity − A modern DBMS is 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
students as an entity and their age as an attribute.
 Relation-based tables − DBMS allows entities and relations among them to form tables. A user
can understand the architecture of a database just by looking at the table names.
 Isolation of data and application − A database system is entirely different than its data. A
database is an active entity, whereas data is said to be passive, on which the database works and
organizes. DBMS also stores metadata, which is data about data, to ease its own process.
 Less redundancy − DBMS follows the rules of normalization, which splits a relation when any
of its attributes is having redundancy in values. Normalization is a mathematically rich and
scientific process that reduces data redundancy.
 Consistency − Consistency is a state where every relation in a database remains consistent.
There exist methods and techniques, which can detect attempt of leaving database in
inconsistent state. A DBMS can provide greater consistency as compared to earlier forms of data
storing applications like file-processing systems.
 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 as different filtering options as
required to retrieve a set of data. Traditionally it was not possible where file-processing system
was used.
 ACID Properties − DBMS follows the concepts of Atomicity, Consistency, Isolation,
and Durability (normally shortened as ACID). These concepts are applied on transactions, which
manipulate data in a database. ACID properties help the database stay healthy in multi-
transactional environments and in case of failure.
 Multiuser and Concurrent Access − DBMS supports multi-user environment and allows them
to access and manipulate data in parallel. Though there are restrictions on transactions when
users attempt to handle the same data item, but users are always unaware of them.
 Multiple views − DBMS offers multiple views for different users. A user who is in the Sales
department will have a different view of database than a person working in the Production
department. This feature enables the users to have a concentrate view of the database according
to their requirements.
 Security − Features like multiple views offer security to some extent where users are unable to
access data of other users and departments. DBMS offers methods to impose constraints while
entering data into the database and retrieving the same at a later stage. DBMS offers many
different levels of security features, which enables multiple users to have different views with
different features. For example, a user in the Sales department cannot see the data that belongs
to the Purchase department. Additionally, it can also be managed how much data of the Sales
department should be displayed to the user. Since a DBMS is not saved on the disk as traditional
file systems, it is very hard for miscreants to break the code.
Various Objectives of Database Management System
Mass Storage
DBMS can store huge data in it. So for all the big firms, DBMS is really ideal technology to use. It can
store thousands of records in it and one can fetch all that data whenever it is needed.
Removes Duplicity
Database Management System
Prof. K. Adisesha 7
If you have huge data then data duplicity will occur for sure at any instance. DBMS guarantee it that
there will be no data duplicity among all the records. While storing new records, DBMS makes sure that
same data was not inserted before.
Multiple Users Access
No one handles the whole database alone. Many users are able to access database. Therefore, this
situation may happen that two or more users are accessing database. They can change whatever they
want, at that time DBMS makes it sure that they can work concurrently.
Data Protection
Information such as bank details, employee’s salary details and sale purchase details should always be
kept secured. Also all the companies need their data secured from unauthorized use. DBMS gives a
master level security to their data. No one can alter or modify the information without the privilege of
using that data.
Data Back up and recovery
Sometimes database failure occurs so there is no option like one can say that all the data has been lost.
There should be a backup of database so that on database failure it can be recovered. DBMS has the
ability to backup and recover all the data in database.
Everyone can work on DBMS
There is no need to be a master of programming language if you want to work on DBMS. Any
accountant who is having less technical knowledge can work on DBMS. All the definitions and
descriptions are given in it so that even a non-technical background w=person can work on it.
Integrity
Integrity means your data is authentic and consistent. DBMS has various validity checks that make your
data completely accurate and consistence.
Platform Independent
One can run DBMS at any platform. No particular platform is required to work on database management
system.
Database Schema –
Database Schema is the overall Design of the Database. It is the skeleton structure that represents the
logical view of the entire database. It tells how the data is organized and how the relations among them
are associated.
It is sometimes also referred to as an overall model of the data, a conceptual model or a conceptual
schema. These terms mean essentially the same thing. The Database Schema also formulates all the
constraints that are to be applied on the data.
A schema is a chart of the types of data that are used. It gives the names of the entities and attributes,
and specifies the relation between them. It is just like a framework into which the values of data items
can be fitted/stored. All the constraints that are to be applied on the data is also formulated on the
schema.
Database Management System
Prof. K. Adisesha 8
A database Schema can be divided into two categories –
Physical Database Schema :The Schema which is related to actual storage of data or the Schema that
describes database design at physical level is called Physical Database Schema. It defines how the data
will be stored in the secondary storage.
Logical Database Schema :The schema that defines all the logical constraints that need to be applied on
the data stored or the schema that describes the database design at the logical level is called Logical
Database Schema. It defines tables, views, and integrity constraints.
Three Schema Approach
We know that the same thing, if viewed from different angles produces difference sights. Likewise, the
database that we have created already can have different aspects to reveal if seen from different levels of
abstraction. The term Abstraction is very important here. Generally it means the amount of detail you
want to hide. Any entity can be seen from different perspectives and levels of complexity to make it a
reveal its current amount of abstraction. Let us illustrate by a simple example.
A computer reveals the minimum of its internal details, when seen from outside. We do not know what
parts it is built with. This is the highest level of abstraction, meaning very few details are visible. If we
open the computer case and look inside at the hard disc, motherboard, CD drive, CPU and RAM, we are
in middle level of abstraction. If we move on to open the hard disc and examine its tracks, sectors and
read-write heads, we are at the lowest level of abstraction, where no details are invisible.
In the same manner, the database can also be viewed from different levels of abstraction to reveal
different levels of details. From a bottom-up manner, we may find that there are three levels of
abstraction or views in the database. We discuss them here.
The word schema means arrangement – how we want to arrange things that we have to store. The
diagram above shows the three different schemas used in DBMS, seen from different levels of
abstraction.
The lowest level, called the Internal or Physical schema, deals with the description of how raw data
items (like 1, ABC, KOL, H2 etc.) are stored in the physical storage (Hard Disc, CD, Tape Drive etc.). It
also describes the data type of these data items, the size of the items in the storage media, the location
(physical address) of the items in the storage device and so on. This schema is useful for database
application developers and database administrator.
The middle level is known as the Conceptual or Logical Schema, and deals with the structure of the
entire database. Please note that at this level we are not interested with the raw data items anymore, we
Database Management System
Prof. K. Adisesha 9
are interested with the structure of the database. This means we want to know the information about the
attributes of each table, the common attributes in different tables that help them to be combined, what
kind of data can be input into these attributes, and so on. Conceptual or Logical schema is very useful
for database administrators whose responsibility is to maintain the entire database.
The highest level of abstraction is the External or View Schema. This is targeted for the end users.
Now, an end user does not need to know everything about the structure of the entire database, rather
than the amount of details he/she needs to work with. We may not want the end user to become confused
with astounding amount of details by allowing him/her to have a look at the entire database, or we may
also not allow this for the purpose of security, where sensitive information must remain hidden from
unwanted persons. The database administrator may want to create custom made tables, keeping in mind
the specific kind of need for each user. These tables are also known as virtual tables, because they have
no separate physical existence. They are crated dynamically for the users at runtime. Say for example, in
our sample database we have created earlier, we have a special officer whose responsibility is to keep in
touch with the parents of any under aged student living in the hostels. That officer does not need to
know every detail except the Roll, Name, Addresss and Age. The database administrator may create a
virtual table with only these four attributes, only for the use of this officer.
Database Users and Administrators
A primary goal of a database system is to retrieve information from and store new information in the
database. People who work with a database can be categorized as database users or database
administrators.
DBA Responsibilities
 Installation, configuration and upgrading of Microsoft SQL Server/MySQL/Oracle server
software and related products.
 Evaluate MSSQL/MySQL/Oracle features and MSSQL/MySQL/Oracle related products.
 Establish and maintain sound backup and recovery policies and procedures.
 Take care of the Database design and implementation.
 Implement and maintain database security (create and maintain users and roles, assign
privileges).
 Database tuning and performance monitoring.
 Application tuning and performance monitoring.
 Setup and maintain documentation and standards.
 Plan growth and changes (capacity planning).
 Work as part of a team and provide 7×24 supports when required.
 Do general technical trouble shooting and give consultation to development teams.
 Interface with MSSQL/MySQL/Oracle for technical support.
 ITIL Skill set requirement (Problem Management/Incident Management/Chain Management etc)
Types of DBA
Database Management System
Prof. K. Adisesha 10
1. Administrative DBA – Work on maintaining the server and keeping it running. Concerned with
backups, security, patches, replication, etc. Things that concern the actual server software.
2. Development DBA – works on building queries, stored procedures, etc. that meet business
needs. This is the equivalent of the programmer. You primarily write T-SQL.
3. Architect – Design schemas. Build tables, FKs, PKs, etc. Work to build a structure that meets
the business needs in general. The design is then used by developers and development DBAs to
implement the actual application.
4. Data Warehouse DBA – Newer role, but responsible for merging data from multiple sources
into a data warehouse. May have to design warehouse, but cleans, standardizes, and scrubs data
before loading. In SQL Server, this DBA would use DTS heavily.
5. OLAP DBA – Builds multi-dimensional cubes for decision support or OLAP systems. The
primary language in SQL Server is MDX, not SQL here
Application DBA- Application DBAs straddle the fence between the DBMS and the application
software and are responsible for ensuring that the application is fully optimized for the database and vice
versa. They usually manage all the application components that interact with the database and carry out
activities such as application installation and patching, application upgrades, database cloning, building
and running data cleanup routines, data load process management, etc.
Database System Applications
Databases are widely used. Here are some representative applications:
•Banking: For customer information, accounts, and loans, and banking transactions.
•Airlines: For reservations and schedule information. Airlines were among the first to use databases in a
geographically distributed manner —terminals situated around the world accessed the central database
system through phone lines and other data networks.
•Universities: For student information, course registrations, and grades. Credit card transactions: For
purchases on credit cards and generation of monthly statements.
•Telecommunication: For keeping records of calls made, generating monthly bills, maintaining
balances on prepaid calling cards, and storing information about the communication networks.
•Finance: For storing information about holdings, sales, and purchases of financial instruments such as
stocks and bonds.
•Sales: For customer, product,and purchase information.
•Manufacturing: For management of supply chain and for tracking production of items in factories,
inventories of items in warehouses/stores, and orders for items.
•Human resources: For information about employees, salaries, payroll taxes and benefits, and for
generation of paychecks.
Data structure
Database Management System
Prof. K. Adisesha 11
Data structures (fields, records, files and objects) optimized to deal with very large amounts of data
stored on a permanent data storage device (which implies relatively slow access compared to volatile
main memory).
ER MODEL
Before define ER Model, there are some terms which is require to understand ER model properly. In
short, ER Model is a diagrammatic representation of entire Database tables. It represents a High Level
Database Design.
For Designing the Softwares : This phase is done in softwares by UML (Unified Modelling Language).
For Designing the Database : ER Model is used to design database.
Definitions :
Entity : an object (Record in the table). It a a real world object, such as person, place, thing, event, or
concept about which information is recorded. For example, In a banking environment, entities are
CUSTOMERS, BANK SUPPLIERS etc. Entities may be –
 Physical – Can be touched(sensed) Example Fan.
 Conceptual – Cannot be touched. Example course offered, brand name etc.
Storing conceptual entities is a big problem.
Entity Types : is defined as a collection of entities that have the same attributes. For example, employee
in a company database. As same attributes are shared by the employee entities, but such entity has its
own value for each attributes.
Entity Set : Collection of similar entities of same type that share the same attribute at any point in time.
For example, a Faculty set may contain all the teachers of a college, Set of all persons, Set of Companies
etc. The Entity Set is also called extension of the Entity Type.
Attribute : It is the name of the column. An attribute gives the characteristics of the entity. It is also
called as data element, data field, a field, a data item, or an elementary item.
Data Value : It is the actual data or information contained in each data element(attribute). The data
element “Supplier Name” can take value as “Anil Mittal”
Difference between Entity, Attributes, and Data Values –
ENTITY
ATTRIBUTES(OR DATA
ELEMENTS)
VALUES
CUSTOMER
Customer ID Number
Customer Name
Customer Address
Customer Phone Number
985678
Ankit Mittal
B-176,MC Colony, Bhiwani
57845-69821
ACCOUNT
Account Type
Account Number
Savings
1800221133665587
Database Management System
Prof. K. Adisesha 12
Account Balance
Account Statement
125000
5000_Credit
SUPPLIER
Supplier Name
Supplier Address
Supplier Phone Number
Supplier Bill Number
Vishal Rastogi
H-456, Shiv Nagar, Bhiwani
789546256
B-896541
Difference Between Entity, Entity Type and Entity Sets –
Entity Entity Type Entity Set
Entity is a person, place,
thing, event, or even a
concept. It may be tangible or
intangible.
An entity type defines the
collection of entities that have
the same attributes.
An entity set is a set of entities
of the same type that share the
same properties at any point in
time (or) It is the collection of
entities of an entity type at a
point of time.
For Example, Entity is e1 or
e2 or e3 or an apple
where e1 e2 e3 are employees
and an apple is a fruit.
For example, entity type is
EMPLOYEE or Fruit
Entity set is a bucket of apple,
banana, mango etc. or
{e1,e2……}
Entity –
An entity is an object that are represented in the database. For example Mohit, Vasu, CSE306 etc.
An entity is represented or defined by set of attributes. Attributes are the properties used to describe an
entity. For example, a STUDENT entity may have a Name, Roll number, Class, Marks etc. where
STUDENT is the entity and name roll number class marks are the attributes.
Types of Entity –
 Strong Entity Types
 Recursive Entity Types
 Weak Entity Types
 Composite Entity Types or Associative Entity Types
 Super Type and Sub Type Entities
Notations Of different Entity Type in ER Diagram
Entity
Strong Entity Type
Database Management System
Prof. K. Adisesha 13
Weak Entity Type
Recursive Entity Type
Composite Entity Type
(or)
Associative Entity
Subtypes and Supertypes
Strong Entity Type – are the entities which has a key attribute in its attribute list or a set that has a
primary key. The strong entity type is also called regular entity type. For Example,
The Student’s unique RollNo will identify the students. So, RollNo is set to be the Primary Key of the
STUDENT entity, & Hence STUDENT is a strong entity type because of its key attribute.
Recursive Entity Type – It is also called Self Referential Relationship Entity Type. It is an entity type
with foreign key referencing to same table or itself. Recursive Entity Type occurs in a unary
relationship.
Weak Entity Type –
Entity Type with no key or Primary Key are called weak entity Type.
The Tuples of weak entity type may not be possible to differentiate using one attribute of weak entity.
For every weak entity, there should be unique OWNER entity type.
Composite Entities–
If a Many to Many relationship exist then we must eliminate it by using composite entities. Composite
Entities are the entities which exists as a relationship as well as an entity. The many to many relationship
will be converted to 1 to many relationship.
Composite Entities are also called Bridge Entities, because they acts like a bridge between the two
entities which have many to many relationship.
Bridge or Composite entity composed of the primary keys of each of the entities to be connected.
A composite entity is represented by a diamond shape with in a rectangle in an ER Diagram.
Supertypes and Subtypes Entities –
A subtype is a subgrouping of the entities in an entity type that is meaningful to the organisation. For
example, In a University, a STUDENT is an entity type. Two subtypes of STUDENT entity are
GRADUATE STUDENT
UNDERGRADUATE STUDENT
A supertype is a generic entity type that has a relationship with one or more sub-types. In the above
example, STUDENT entity is a supertype.
Database Management System
Prof. K. Adisesha 14
Attributes and Its types
Attribute: It is the name of the column. An attribute gives the characteristics of the entity. For example,
A customer of bank may be described by : name, address, customer ID number. It is also called as data
element, data field, a field, a data item, or an elementary item.
Type of Attributes in DBMS –
Single valued Attributes: An attribute, that has a single value for a particular entity is known as single
valued attributes. For example, age of a employee entity.
Multi valued Attributes: An attributes that may have multiple values for the same entity is known as
multi valued attributes. For example colors of a car entity.
Compound Attribute/Composite Attribute: Attribute can be subdivided into two or more other
Attribute. For Example, Name can be divided into First name, Middle name and Last name.
Simple Attributes/Atomic Attributes: The attributes which cannot be divided into smaller subparts are
called simple or atomic attributes. For example, age of employee entity
Stored Attribute: An attribute, which cannot be derived from other attribute, is known as stored
attribute. For example, BirthDate of employee.
Derived Attribute: Attributes derived from other stored attribute. For example, age from Date of Birth
and Today’s date.
 An attribute can be derived from a single attribute. Example age from DOB and current date.
 An attribute can be derived from multiple attribute.
 An entity can be derived from a separate table. Example,
RNO Name DeptNO
1 A 1
2 B 1
3 C 2
4 D 3
5 E 1
⇒
DNO Dname
1 CSE
2 IT
3 ECE
Complex Attributes : If an attribute fr an entity, is built using composite and multivalued attributes,
then these attributes are called complex attributes. For example, a person can have more than one
residence and each residence can have multiple phones, an addressphone for a person entity can be
specified as –
{Addressphone (phone {(Area Code, Phone Number)}, Address(Sector Address (Sector
Number,House Number), City, State, Pin))}
Here {} are used to enclose multivalued attributes and () are used to enclose composite attributes with
comma separating individual attributes./
Key Attribute : represents primary key. (main characteristics of an entity). It is an attribute, that has
distinct value for each entity/element in an entity set. For example, Roll number in a Student Entity
Type.
Database Management System
Prof. K. Adisesha 15
Non Key Attributes : These are attributes other than candidate key attributes in a table. For example
Firstname is a non key attribute as it does not represent the main characteristics of the entity.
Required Attribute : A required attribute is an attribute that must have a data value.These attributes are
required because they describe what is important in the entity. For example, In a STUDENT entity,
firstname and lastname is a required attribute.
Optional Attribute/Null Value Attribute – An optional attribute may not have a value in it and can be
left blank. For example, In a STUDENT entity, Middlename or email address is an optional attribute. as
some students may not have middlename or email address.
Notations Of Attributes in ER Diagram
Attribute
Key Attribute
Multivalued Attribute
Compound/Composite
Attribute
Derived Attribute
Database Keys
For the purposes of clarity we will refer to keys in terms of RDBMS tables but the same definition,
principle and naming applies equally to Entity Modelling and Normalization.
Keys are, as their name suggests, a key part of a relational database and a vital part of the structure of a
table. They ensure each record within a table can be uniquely identified by one or a combination of
fields within the table. They help enforce integrity and help identify the relationship between tables.
There are three main types of keys, candidate keys, primary keys and foreign keys. There is also an
alternative key or secondary key that can be used, as the name suggests, as a secondary or alternative
key to the primary key
Super Key
A Super key is any combination of fields within a table that uniquely identifies each record within that
table.
Candidate Key
A candidate is a subset of a super key. A candidate key is a single field or the least combination of fields
that uniquely identifies each record in the table. The least combination of fields distinguishes a candidate
key from a super key. Every table must have at least one candidate key but at the same time can have
several.
Database Management System
Prof. K. Adisesha 16
As an example we might have a student_id that uniquely identifies the students in a student table. This
would be a candidate key. But in the same table we might have the student’s first name and last name
that also, when combined, uniquely identify the student in a student table. These would both be
candidate keys.
In order to be eligible for a candidate key it must pass certain criteria.
 It must contain unique values
 It must not contain null values
 It contains the minimum number of fields to ensure uniqueness
 It must uniquely identify each record in the table
Once your candidate keys have been identified, you can now select one to be your primary key
Primary Key
A primary key is a candidate key that is most appropriate to be the main reference key for the table. As
its name suggests, it is the primary key of reference for the table and is used throughout the database to
help establish relationships with other tables. As with any candidate key the primary key must contain
unique values, must never be null and uniquely identify each record in the table.
As an example, a student id might be a primary key in a student table, a department code in a table of all
departments in an organisation. This module has the code DH3D 35 that is no doubt used in a database
somewhere to identify RDBMS as a unit in a table of modules. In the table below we have selected the
candidate key student_id to be our most appropriate primary key
Primary keys are mandatory for every table each record must have a value for its primary key. When
choosing a primary key from the pool of candidate keys always choose a single simple key over a
composite key.
Database Management System
Prof. K. Adisesha 17
Foreign Key
A foreign key is generally a primary key from one table that appears as a field in another where the first
table has a relationship to the second. In other words, if we had a table A with a primary key X that
linked to a table B where X was a field in B, then X would be a foreign key in B.
An example might be a student table that contains the course_id the student is attending. Another table
lists the courses on offer with course_id being the primary key. The 2 tables are linked through
course_id and as such course_id would be a foreign key in the student table.
Secondary Key or Alternative Key
A table may have one or more choices for the primary key. Collectively these are known as candidate
keys as discuss earlier. One is selected as the primary key. Those not selected are known as secondary
keys or alternative keys.
For example in the table showing candidate keys above we identified two candidate keys, studentId and
firstName + lastName. The studentId would be the most appropriate for a primary key leaving the other
candidate key as secondary or alternative key. It should be noted for the other key to be candidate keys,
we are assuming you will never have a person with the same first and last name combination. As this is
unlikely we might consider fistName+lastName to be a suspect candidate key as it would be restrictive
of the data you might enter. It would seem a shame to not allow John Smith onto a course just because
there was already another John Smith.
Simple Key
Any of the keys described before (ie primary, secondary or foreign) may comprise one or more fields,
for example if firstName and lastName was our key this would be a key of two fields where as studentId
is only one. A simple key consists of a single field to uniquely identify a record. In addition the field in
itself cannot be broken down into other fields, for example, studentId, which uniquely identifies a
particular student, is a single field and therefore is a simple key. No two students would have the same
student number.
Compound Key
A compound key consists of more than one field to uniquely identify a record. A compound key is
distinguished from a composite key because each field, which makes up the primary key, is also a
simple key in its own right. An example might be a table that represents the modules a student is
attending. This table has a studentId and a moduleCode as its primary key. Each of the fields that make
up the primary key are simple keys because each represents a unique reference when identifying a
student in one instance and a module in the other.
Database Management System
Prof. K. Adisesha 18
Composite Key
A composite key consists of more than one field to uniquely identify a record. This differs from a
compound key in that one or more of the attributes, which make up the key, are not simple keys in their
own right. Taking the example from compound key, imagine we identified a student by their firstName
+ lastName. In our table representing students on modules our primary key would now be firstName +
lastName + moduleCode. Because firstName + lastName represent a unique reference to a student, they
are not each simple keys, they have to be combined in order to uniquely identify the student. Therefore
the key for this table is a composite key.
Types of Relationships in ER Diagram
Different types of relationships in er diagram are – 1) Relationships based on degree, 2) Recursive
Relationship. Let us discuss these one by one.
Relationship and Relationship Set :
Relationships connect the entities and represent meaningful dependencies between them.It represents an
association among several entities.
Relationships sets is a set of relationships of the same type. It is a mathematical relation on entity sets
(n>=2). Relationship set R is a subset of –
{(r1,r2,r3,....rn)| r1∈E1, r2∈E2, rn∈En}
where r1,r2,….rn are called relationships and E1,E2,….En are entity sets.
The way in which two or more entity types are related is called relation type.For example, consider a
relationship type WORKS_FOR between the two entity types EMPLOYEE and DEPARTMENT,
which associates or links each employee with the department the employee works for.
The WORKS_FOR relation type is shown as –
Database Management System
Prof. K. Adisesha 19
In the above figure, each instance of relation type WORKS_FOR i.e.(r1, r2,…,r5) is connected to
instances of employee and department entities. Employee e1, e2 and e5 work for department d2 and
employee e3 and e4 work for department d1.
Notation to Represent Relation Type in ER Diagram-
Relation types are represented as diamond shaped boxes.
Degree of a Relationship Type-
The number of participating entity types is known as the degree of relationship type.
Types of Relationship Type Based on Degree –
 Binary Relationship – A relationship type of degree two is called binary relationship.
The WORKS_FOR in above figure is a binary relationship as there are two participating
entities-employee and department.
.Ternary Relationship- A relationship type of degree three is a ternary relationship for example,
in the below figure supply relationship connects three entities SUPPLIER, PART AND
PROJECT.
Database Management System
Prof. K. Adisesha 20
The above diagram can be read as – a supplier supplies the parts to projects
N-ary Relationship Set – A relationship type of degree n is called n ary relationship . For
example
Cardinality/Mapping Cardinality-
Cardinality expresses the number of entities to which another entity can be associated via a relationship
set (or) It specifies the number of relationship instances that an entity can participate in a relation
set.There are 4 types of Cardinality Ratios :
 One-to-One Cardinality (1:1)
 One-to-Many Cardinality (1:m)
 Many-to-One Cardinality (m:1)
 Many-to-Many Cardinality (m:n)
Notations of Different Types of Cardinality In ER Diagram –
One-to-One Cardinality (1:1) –
Database Management System
Prof. K. Adisesha 21
An entity in set A is associated with atmost one entity in B, and an entity in B is associated with atmost
one entity in A. This type of cardinality is referred as one to one Cardinality.
For example, an Employee as a Manager manage only one Department and the vice versa is also true
as a department have only one Manager
One to Many Cardinality (1:M) –
An entity in A is associated with any number (0 or more) with an entity B, but a entity in B, however
can be associated with atmost one entity in A. For example, An employee as a Manager can manage
more than one Department.
Many to One Cardinality (M:1) –
An entity in A is associated with atmost one entity in B. An entity in B, however, can be associated with
any number (0 or more) of entities in A. For example, An Employee can work only for one Department,
But each Department can have 0 or more employees.
Database Management System
Prof. K. Adisesha 22
Many to Many Cardinality (M:N) –
An entity in A is associated with any number (0 or more) of entities in B, and an entity in B is associated
with any number( 0 or more) of entities in A. For example, An Employee can works on several Projects
and a Project may have several Employees.
ER diagram for a STUDENT database as below.
Now it is a complete ER diagram for simple Student database.
Database Management System
Prof. K. Adisesha 23
Database Normalization
Normalization is the process of removing redundant data from your tables in order to improve storage
efficiency, data integrity and scalability. This improvement is balanced against an increase in
complexity and potential performance losses from the joining of the normalized tables at query-time.
There are two goals of the normalization process: eliminating redundant data (for example, storing the
same data in more than one table) and ensuring data dependencies make sense (only storing related data
in a table). Both of these are worthy goals as they reduce the amount of space a database consumes and
ensure that data is logically stored.
Use of Normalization
Normalization is the aim of well design Relational Database Management System (RDBMS). It is step
by step set of rules by which data is put in its simplest forms. We normalize the relational database
management system because of the following reasons:
• Minimize data redundancy i.e. no unnecessarily duplication of data.
• To make database structure flexible i.e. it should be possible to add new data values and rows without
reorganizing the database structure.
• Data should be consistent throughout the database i.e. it should not suffer from following anomalies.
• Insert Anomaly - Due to lack of data i.e., all the data available for insertion such that null values in
keys should be avoided. This kind of anomaly can seriously damage a database
• Update Anomaly - It is due to data redundancy i.e. multiple occurrences of same values in a column.
This can lead to inefficiency.
• Deletion Anomaly - It leads to loss of data for rows that are not stored else where. It could result in
loss of vital data.
• Complex queries required by the user should be easy to handle.
On decomposition of a relation into smaller relations with fewer attributes on normalization the resulting
relations whenever joined must result in the same relation without any extra rows. The join operations
can be performed in any order. This is known as Lossless Join decomposition.
The resulting relations (tables) obtained on normalization should possess the properties such as each row
must be identified by a unique key, no repeating groups, homogenous columns, each column is assigned
a unique name etc.
Advantages of Normalization
The following are the advantages of the normalization.
• More efficient data structure.
• Avoid redundant fields or columns.
• More flexible data structure i.e. we should be able to add new rows and data values easily
• Better understanding of data.
• Ensures that distinct tables exist when necessary.
• Easier to maintain data structure i.e. it is easy to perform operations and complex queries can be easily
handled.
• Minimizes data duplication.
• Close modeling of real world entities, processes and their relationships.
Database Management System
Prof. K. Adisesha 24
Disadvantages of Normalization
The following are disadvantages of normalization.
• You cannot start building the database before you know what the user needs.
• On Normalizing the relations to higher normal forms i.e. 4NF, 5NF the performance degrades.
• It is very time consuming and difficult process in normalizing relations of higher degree.
• Careless decomposition may leads to bad design of database which may leads to serious problems.
Types of Normalization
Here are the most commonly used normal forms:
 First normal form(1NF)
 Second normal form(2NF)
 Third normal form(3NF)
 Boyce & Codd normal form (BCNF)
First normal form (1NF)
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:
emp_id emp_name emp_address emp_mobile
101 Herschel New Delhi 8912312390
102 Jon Kanpur
8812121212
9900012222
103 Ron Chennai 7778881212
104 Lester Bangalore
9990000123
8123450987
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:
emp_id emp_name emp_address emp_mobile
101 Herschel New Delhi 8912312390
Database Management System
Prof. K. Adisesha 25
102 Jon Kanpur 8812121212
102 Jon Kanpur 9900012222
103 Ron Chennai 7778881212
104 Lester Bangalore 9990000123
104 Lester Bangalore 8123450987
Second normal form (2NF)
A table is said to be in 2NF if both the following conditions hold:
 Table is in 1NF (First normal form)
 No non-prime attribute is dependent on the proper subset of any candidate key of table.
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.
teacher_id subject teacher_age
111 Maths 38
111 Physics 38
222 Biology 38
333 Physics 40
333 Chemistry 40
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:
teacher_details table:
teacher_id teacher_age
111 38
222 38
333 40
teacher_subject table:
Database Management System
Prof. K. Adisesha 26
teacher_id subject
111 Maths
111 Physics
222 Biology
333 Physics
333 Chemistry
Now the tables comply with Second normal form (2NF).
Third Normal form (3NF)
A table design is said to be in 3NF if both the following conditions hold:
 Table must be in 2NF
 Transitive functional dependency of non-prime attribute on any super key should be removed.
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:
emp_id emp_name emp_zip emp_state emp_city emp_district
1001 John 282005 UP Agra Dayal Bagh
1002 Ajeet 222008 TN Chennai M-City
1006 Lora 282007 TN Chennai Urrapakkam
1101 Lilly 292008 UK Pauri Bhagwan
1201 Steve 222999 MP Gwalior Ratan
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:
employee table:
emp_id emp_name emp_zip
Database Management System
Prof. K. Adisesha 27
1001 John 282005
1002 Ajeet 222008
1006 Lora 282007
1101 Lilly 292008
1201 Steve 222999
employee_zip table:
emp_zip emp_state emp_city emp_district
282005 UP Agra Dayal Bagh
222008 TN Chennai M-City
282007 TN Chennai Urrapakkam
292008 UK Pauri Bhagwan
222999 MP Gwalior Ratan
Boyce Codd normal form (BCNF)
It is an advance version of 3NF that’s why it is also referred as 3.5NF. BCNF is stricter than 3NF. A
table complies with BCNF if it is in 3NF and for every functional dependency X->Y, X should be the
super key of the table.
Example: Suppose there is a company wherein employees work in more than one department. They
store the data like this:
emp_id emp_nationality emp_dept dept_type dept_no_of_emp
1001 Austrian Production and planning D001 200
1001 Austrian stores D001 250
1002 American design and technical support D134 100
1002 American Purchasing department D134 600
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:
emp_nationality table:
Database Management System
Prof. K. Adisesha 28
emp_id emp_nationality
1001 Austrian
1002 American
emp_dept table:
emp_dept dept_type dept_no_of_emp
Production and planning D001 200
stores D001 250
design and technical support D134 100
Purchasing department D134 600
emp_dept_mapping table:
emp_id emp_dept
1001 Production and planning
1001 stores
1002 design and technical support
1002 Purchasing department
Functional dependencies:
emp_id -> emp_nationality
emp_dept -> {dept_type, dept_no_of_emp}
Candidate keys:
For first table: emp_id
For second table: emp_dept
For third table: {emp_id, emp_dept}
This is now in BCNF as in both the functional dependencies left side part is a key.
Database query language
A database query language and report writer allows users to interactively interrogate the database,
analyze its data and update it according to the users privileges on data. It also controls the security of the
database. Data security prevents unauthorized users from viewing or updating the database. Using
passwords, users are allowed access to the entire database or subsets of it called subschemas. For
example, an employee database can contain all the data about an individual employee, but one group of
users may be authorized to view only payroll data, while others are allowed access to only work history
and medical data.
If the DBMS provides a way to interactively enter and update the database, as well as interrogate it, this
capability allows for managing personal databases. However, it may not leave an audit trail of actions or
Database Management System
Prof. K. Adisesha 29
provide the kinds of controls necessary in a multi-user organization. These controls are only available
when a set of application programs are customized for each data entry and updating function.
DBMS languages
Database languages are used for read, update and store data in a database. There are several such
languages that can be used for this purpose; one of them is SQL (Structured Query Language).
Types of DBMS languages:
 Data Definition Language (DDL)
 Data Manipulation Language (DML)
 Data Control language (DCL)
Data Definition Language (DDL): DDL is used for specifying the database schema. Let’s take SQL for
instance to categorize the statements that comes under DDL.
To create the database instance – CREATE
To alter the structure of database – ALTER
To drop database instances – DROP
To delete tables in a database instance – TRUNCATE
To rename database instances – RENAME
All these commands specify or update the database schema that is why they come under Data Definition
language.
Data Manipulation Language (DML): DML is used for accessing and manipulating data in a database.
To read records from table(s) – SELECT
To insert record(s) into the table(s) – INSERT
Update the data in table(s) – UPDATE
Delete all the records from the table – DELETE
Data Control language (DCL): DCL is used for granting and revoking user access on a database –
 To grant access to user – GRANT
 To revoke access from user – REVOKE
In practical data definition language, data manipulation language and data control languages are not
separate language; rather they are the parts of a single database language such as SQL.
SQL is short for Structured Query Language and is a widely used database language, providing means
of data manipulation (store, retrieve, update, delete) and database creation.
Almost all modern Relational Database Management Systems like MS SQL Server, Microsoft Access,
MSDE, Oracle, DB2, Sybase, MySQL, Postgres and Informix use SQL as standard database language.
Now a word of warning here, although all those RDBMS use SQL, they use different SQL dialects. For
Database Management System
Prof. K. Adisesha 30
example MS SQL Server specific version of the SQL is called T-SQL, Oracle version of SQL is called
PL/SQL, MS Access version of SQL is called JET SQL, etc.
SQL commands
SQL> SELECT
Learn how to use the SELECT SQL statement to retrieve data from a SQL database table.
SQL> SELECT INTO
Learn how to use the SQL SELECT INTO statement to copy data between database tables.
SQL> DISTINCT
Learn how to use the SQL DISTINCT clause together with the SQL SELECT keyword, to return a
dataset with unique entries for certain database table column.
SQL> WHERE
The SQL WHERE command is used to specify selection criteria, thus restricting the result of a SQL
query.
SQL> LIKE
The SQL LIKE clause is used along with the SQL WHERE clause and specifies criteria based on a
string pattern.
SQL> INSERT INTO
Learn how to use the SQL INSERT INTO clause to insert data into a SQL database table.
SQL> UPDATE
Learn how to use the SQL UPDATE statement to update data in a SQL database table.
SQL> DELETE
Learn how to use the SQL DELETE statement to delete data from a SQL database table.
SQL> ORDER BY
Learn how to use the SQL ORDER BY statement to sort the data retrieved in your SQL query.
SQL> OR & AND
Learn how to use the SQL OR & AND keywords together with the SQL WHERE clause to add several
conditions to your SQL statement.
SQL> IN
The SQL IN clause allows you to specify discrete values in your SQL WHERE search criteria.
SQL> BETWEEN
The SQL BETWEEN & AND keywords define a range of data between 2 values.
SQL> Aliases
SQL aliases can be used with database tables and/or with database table columns, depending on task you
are performing.
SQL> COUNT
The SQL COUNT aggregate function is used to count the number of rows in a database table.
Database Management System
Prof. K. Adisesha 31
SQL> MAX
The SQL MAX aggregate function allows us to select the highest (maximum) value for a certain
column.
SQL> MIN
The SQL MIN aggregate function allows us to select the lowest (minimum) value for a certain column.
SQL> AVG
The SQL AVG aggregate function selects the average value for a certain table column.
SQL> SUM
The SQL SUM aggregate function allows selecting the total for a numeric column.
SQL> GROUP BY
The SQL GROUP BY statement is used along with the SQL aggregate functions like SUM to provide
means of grouping the result dataset by certain database table column(s).
SQL> HAVING
The SQL HAVING clause is used to restrict conditionally the output of a SQL statement, by a SQL
aggregate function used in your SELECT list of columns.
SQL> JOIN
The SQL JOIN clause is used whenever we have to select data from 2 or more tables.
The SQL SELECT statement is used to select data from a SQL database table. This is usually the very
first SQL command every SQL newbie learns and this is because the SELECT SQL statement is one of
the most used SQL commands.
Please have a look at the general SQL SELECT syntax:
SELECT Column1, Column2, Column3,
FROM Table1
The list of column names after the SQL SELECT command determines which columns you want to be
returned in your result set. If you want to select all columns from a database table, you can use the
following SQL statement:
SELECT *
FROM Table1
When the list of columns following the SELECT SQL command is replaced with asterix (*) all table
columns are returned. Word of caution here, it’s always better to explicitly specify the columns in the
SELECT list, as this will improve your query performance significantly.
The table name following the SQL FROM keyword (in our case Table1) tells the SQL interpreter which
table to use to retrieve the data.
The SQL SELECT INTO statement is used to select data from a SQL database table and to insert it to a
different table at the same time.
Database Management System
Prof. K. Adisesha 32
The general SQL SELECT INTO syntax looks like this:
SELECT Column1, Column2, Column3,
INTO Table2 FROM Table1
The list of column names after the SQL SELECT command determines which columns will be copied,
and the table name after the SQL INTO keyword specifies to which table to copy those rows.
If we want to make an exact copy of the data in our Customers table, we need the following SQL
SELECT INTO statement:
SELECT * INTO Customers_copy
FROM Customers
The SQL DISTINCT clause is used together with the SQL SELECT keyword, to return a dataset with
unique entries for certain database table column.
We will use our Customers database table to illustrate the usage of SQL DISTINCT.
FirstName LastName Email DOB Phone
Sunny Kumar sunny.kumar@yahoo.com 2/4/1968 626 222-2222
Prajwal Goldfish goldfish@fishhere.net 4/4/1974 323 455-4545
Paula Brown pb@herowndomain.org 5/24/1978 416 323-3232
James Kumar jim@supergig.co.in 20/10/1980 416 323-8888
For example if we want to select all distinct surnames from our Customers table, we will use the
following SQL DISTINCT statement:
SELECT DISTINCT LastName
FROM Customers
The result of the SQL DISTINCT expression above will look like this:
LastName
Kumar
Goldfish
Brown
The SQL WHERE clause is used to select data conditionally, by adding it to already existing SQL
SELECT query. We are going to use the Customers table from the previous chapter, to illustrate the use
of the SQL WHERE command.
Table: Customers
FirstName LastName Email DOB Phone
Database Management System
Prof. K. Adisesha 33
Sunny Kumar Sunny.Kumar@yahoo.com 2/4/1968 626 222-2222
Prajwal Goldfish goldfish@fishhere.net 4/4/1974 323 455-4545
Paula Brown pb@herowndomain.org 5/24/1978 416 323-3232
If we want to select all customers from our database table, having last name 'Kumar' we need to use the
following SQL syntax:
SELECT * FROM Customers
WHERE LastName = 'Kumar'
The result of the SQL expression above will be the following:
FirstName LastName Email DOB Phone
Sunny Kumar sunny.kumar@yahoo.com 2/4/1968 626 222-2222
James Kumar jim@supergig.co.in 20/10/1980 416 323-8888
In this simple SQL query we used the "=" (Equal) operator in our WHERE criteria:
LastName = 'Kumar'
But we can use any of the following comparison operators in conjunction with the SQL WHERE
clause:
<> (Not Equal)
SELECT * FROM Customers
WHERE LastName <> 'Kumar'
> (Greater than)
SELECT * FROM Customers
WHERE DOB > '1/1/1970'
>= (Greater or Equal)
SELECT * FROM Customers
WHERE DOB >= '1/1/1970'
< (Less than)
SELECT * FROM Customers
WHERE DOB < '1/1/1970'
<= (Less or Equal)
SELECT * FROM Customers
WHERE DOB =< '1/1/1970'
Database Management System
Prof. K. Adisesha 34
LIKE (similar to)
SELECT * FROM Customers
WHERE Phone LIKE '626%'
Note the LIKE syntax is different with the different RDBMS (SQL Server syntax used above). Check
the SQL LIKE article for more details.
Between (Defines a range)
SELECT * FROM Customers
WHERE DOB BETWEEN '1/1/1970' AND '1/1/1975'
We will use the Customers table to illustrate the SQL LIKE clause usage:
FirstName LastName Email DOB Phone
Sunny Kumar sunny.kumar@yahoo.com 2/4/1968 626 222-2222
Prajwal Goldfish goldfish@fishhere.net 4/4/1974 323 455-4545
Paula Brown pb@herowndomain.org 5/24/1978 416 323-3232
James Kumar jim@supergig.co.in 20/10/1980 416 323-8888
The SQL LIKE clause is very useful when you want to specify a search condition within your SQL
WHERE clause, based on a part of a column contents. For example if you want to select all customers
having FirstName starting with 'J' you need to use the following SQL statement:
SELECT * FROM Customers
WHERE FirstName LIKE 'J%'
Here is the result of the SQL statement above:
FirstName LastName Email DOB Phone
Sunny Kumar Sunny.kumar@yahoo.com 2/4/1968 626 222-2222
James Kumar jim@supergig.co.in 20/10/1980 416 323-8888
If you want to select all Customers with phone numbers starting with '416' you will use this SQL
expression:
SELECT * FROM Customers
WHERE Phone LIKE '416%'
The '%' is a so called wildcard character and represents any string in our pattern.
You can put the wildcard anywhere in the string following the SQL LIKE clause and you can put as
many wildcards as you like too.
Database Management System
Prof. K. Adisesha 35
Note that different databases use different characters as wildcard characters, for example '%' is a
wildcard character for MS SQL Server representing any string, and '*' is the corresponding wildcard
character used in MS Access.
Another wildcard character is '_' representing any single character.
The '[]' specifies a range of characters. Have a look at the following SQL statement:
SELECT * FROM Customers
WHERE Phone LIKE '[4-6]_6%'
This SQL expression will return all customers satisfying the following conditions:
The Phone column starts with a digit between 4 and 6 ([4-6])
Second character in the Phone column can be anything (_)
The third character in the Phone column is 6 (6)
The remainder of the Phone column can be any character string (%)
Here is the result of this SQL expression:
FirstName LastName Email DOB Phone
Sunny Kumar sunny.kumar@yahoo.com 2/4/1968 626 222-2222
Paula Brown pb@herowndomain.org 5/24/1978 416 323-3232
James Kumar jim@supergig.co.in 20/10/1980 416 323-8888
The SQL INSERT INTO syntax has 2 main forms and the result of either of them is adding a new row
into the database table.
The first syntax form of the INSERT INTO SQL clause doesn't specify the column names where the
data will be inserted, but just their values:
INSERT INTO Table1
VALUES (value1, value2, value3…)
The second form of the SQL INSERT INTO command, specifies both the columns and the values to be
inserted in them:
INSERT INTO Table1 (Column1, Column2, Column3…)
VALUES (Value1, Value2, Value3…)
As you might already have guessed, the number of the columns in the second INSERT INTO syntax
form must match the number of values into the SQL statement, otherwise you will get an error.
If we want to insert a new row into our Customers table, we are going to use one of the following 2 SQL
statements:
Database Management System
Prof. K. Adisesha 36
INSERT INTO Customers VALUES ('Adisesha', 'HOD',
'adisesha.HOD@tgmail.net', '1/1/1974', '626 888-8888')
INSERT INTO Customers (FirstName, LastName, Email, DOB, Phone)
VALUES ('Adisesha', 'HOD', 'adisesha.HOD@tgmail.net', '1/1/1974', '626 888-
8888')
The result of the execution of either of the 2 INSERT INTO SQL statements will be a new row added to
our Customers database table:
FirstName LastName Email DOB Phone
Sunny Kumar Sunny.Kumar@yahoo.com 2/4/1968 626 222-2222
Prajwal Goldfish goldfish@fishhere.net 4/4/1974 323 455-4545
Paula Brown pb@herowndomain.org 5/24/1978 416 323-3232
James Kumar jim@supergig.co.in 20/10/1980 416 323-8888
Adisesha HOD adisesha.hod@gmail.net 1/1/1974 626 888-8888
If you want to enter data for just a few of the table columns, you’ll have to use the second syntax form
of the SQL INSERT INTO clause, because the first form will produce an error if you haven’t supplied
values for all columns.
To insert only the FirstName and LastName columns, execute the following SQL statement:
INSERT INTO Customers (FirstName, LastName)
VALUES ('Adisesha', 'HOD')
The SQL UPDATE general syntax looks like this:
UPDATE Table1
SET Column1 = Value1, Column2 = Value2
WHERE Some_Column = Some_Value
The SQL UPDATE clause changes the data in already existing database row(s) and usually we need to
add a conditional SQL WHERE clause to our SQL UPDATE statement in order to specify which row(s)
we intend to update.
If we want to update the Mr. Prajwal Goldfish's date of birth to '5/10/1974' in our Customers database
table
FirstName LastName Email DOB Phone
Sunny Kumar Sunny.Kumar@yahoo.com 2/4/1968 626 222-2222
Prajwal Goldfish goldfish@fishhere.net 4/4/1974 323 455-4545
Paula Brown pb@herowndomain.org 5/24/1978 416 323-3232
Database Management System
Prof. K. Adisesha 37
James Kumar jim@supergig.co.in 20/10/1980 416 323-8888
we need the following SQL UPDATE statement:
UPDATE Customers SET DOB = '5/10/1974'
WHERE LastName = 'Goldfish' AND FirstName = 'Prajwal'
If we don’t specify a WHERE clause in the SQL expression above, all customers' DOB will be updated
to '5/10/1974', so be careful with the SQL UPDATE command usage.
We can update several database table rows at once, by using the SQL WHERE clause in our UPDATE
statement. For example if we want to change the phone number for all customers with last name Kumar
(we have 2 in our example Customers table), we need to use the following SQL UPDATE statement:
UPDATE Customers
SET Phone = '626 555-5555'
WHERE LastName = 'Kumar'
After the execution of the UPDATE SQL expression above, the Customers table will look as follows:
FirstName LastName Email DOB Phone
Sunny Kumar Sunny.Kumar@yahoo.com 2/4/1968 626 555-5555
Prajwal Goldfish goldfish@fishhere.net 4/4/1974 323 455-4545
Paula Brown pb@herowndomain.org 5/24/1978 416 323-3232
James Kumar jim@supergig.co.in 20/10/1980 626 555-5555
So far we’ve learnt how to select data from a database table and how to insert and update data into a
database table. Now it’s time to learn how to remove data from a database. Here comes the SQL
DELETE statement!
The SQL DELETE command has the following generic SQL syntax:
DELETE FROM Table1
WHERE Some_Column = Some_Value
If you skip the SQL WHERE clause when executing SQL DELETE expression, then all the data in the
specified table will be deleted. The following SQL statement will delete all the data from our Customers
table and we’ll end up with completely empty table:
DELETE FROM Table1
If you specify a WHERE clause in your SQL DELETE statement, only the table rows satisfying the
WHERE criteria will be deleted:
Database Management System
Prof. K. Adisesha 38
DELETE FROM Customers
WHERE LastName = 'Kumar'
The SQL query above will delete all database rows having LastName 'Kumar' and will leave the
Customers table in the following state:
FirstName LastName Email DOB Phone
Prajwal Goldfish goldfish@fishhere.net 4/4/1974 323 455-4545
Paula Brown pb@herowndomain.org 5/24/1978 416 323-3232
The SQL ORDER BY clause comes in handy when you want to sort your SQL result sets by some
column(s). For example if you want to select all the persons from the already familiar Customers table
and order the result by date of birth, you will use the following statement:
SELECT * FROM Customers
ORDER BY DOB
The result of the above SQL expression will be the following:
FirstName LastName Email DOB Phone
Sunny Kumar Sunny.Kumar@yahoo.com 2/4/1968 626 222-2222
Prajwal Goldfish goldfish@fishhere.net 4/4/1974 323 455-4545
Paula Brown pb@herowndomain.org 5/24/1978 416 323-3232
James Kumar jim@supergig.co.in 20/10/1980 416 323-8888
As you can see the rows are sorted in ascending order by the DOB column, but what if you want to sort
them in descending order? To do that you will have to add the DESC SQL keyword after your SQL
ORDER BY clause:
SELECT * FROM Customers
ORDER BY DOB DESC
The result of the SQL query above will look like this:
FirstName LastName Email DOB Phone
James Kumar jim@supergig.co.in 20/10/1980
416 323-
8888
Paula Brown pb@herowndomain.org 5/24/1978
416 323-
3232
Prajwal Goldfish goldfish@fishhere.net 4/4/1974
323 455-
4545
Database Management System
Prof. K. Adisesha 39
Sunny Kumar Sunny.Kumar@yahoo.com 2/4/1968
626 222-
2222
If you don't specify how to order your rows, alphabetically or reverse, than the result set is ordered
alphabetically, hence the following to SQL expressions produce the same result:
SELECT * FROM Customers
ORDER BY DOB
SELECT * FROM Customers
ORDER BY DOB ASC
You can sort your result set by more than one column by specifying those columns in the SQL ORDER
BY list. The following SQL expression will order by DOB and LastName:
SELECT * FROM Customers
ORDER BY DOB, LastName
The SQL AND clause is used when you want to specify more than one condition in your SQL WHERE
clause, and at the same time you want all conditions to be true.
For example if you want to select all customers with FirstName "Sunny" and LastName "Kumar", you
will use the following SQL expression:
SELECT * FROM Customers
WHERE FirstName = 'Sunny' AND LastName = 'Kumar'
The result of the SQL query above is:
FirstName LastName Email DOB Phone
Sunny Kumar Sunny.Kumar@yahoo.com 2/4/1968 626 222-2222
The following row in our Customer table, satisfies the second of the conditions (LastName = 'Kumar'),
but not the first one (FirstName = 'Sunny'), and that's why it's not returned by our SQL query:
FirstName LastName Email DOB Phone
James Kumar jim@supergig.co.in 20/10/1980 416 323-8888
The SQL OR statement is used in similar fashion and the major difference compared to the SQL AND
is that OR clause will return all rows satisfying any of the conditions listed in the WHERE clause.
If we want to select all customers having FirstName 'James' or FirstName 'Paula' we need to use the
following SQL statement:
SELECT * FROM Customers
WHERE FirstName = 'James' OR FirstName = 'Paula'
The result of this query will be the following:
FirstName LastName Email DOB Phone
Database Management System
Prof. K. Adisesha 40
Paula Brown pb@herowndomain.org 5/24/1978 416 323-3232
James Kumar jim@supergig.co.in 20/10/1980 416 323-8888
You can combine AND and OR clauses anyway you want and you can use parentheses to define your
logical expressions.
Here is an example of such a SQL query, selecting all customers with LastName 'Brown' and FirstName
either 'James' or 'Paula':
SELECT * FROM Customers
WHERE (FirstName = 'James' OR FirstName = 'Paula') AND
LastName = 'Brown'
The result of the SQL expression above will be:
FirstName LastName Email DOB Phone
Paula Brown pb@herowndomain.org 5/24/1978 416 323-3232
The SQL IN clause allows you to specify discrete values in your SQL WHERE search criteria.
THE SQL IN syntax looks like this:
SELECT Column1, Column2, Column3, …
FROM Table1
WHERE Column1 IN (Valu1, Value2, …)
Lets use the EmployeeHours table to illustrate how SQL IN works:
Employee Date Hours
Sunny Kumar 5/6/2004 8
Allan Babel 5/6/2004 8
Tina Crown 5/6/2004 8
Sunny Kumar 5/7/2004 9
Allan Babel 5/7/2004 8
Tina Crown 5/7/2004 10
Sunny Kumar 5/8/2004 8
Allan Babel 5/8/2004 8
Tina Crown 5/8/2004 9
Consider the following SQL query using the SQL IN clause:
Database Management System
Prof. K. Adisesha 41
SELECT * FROM EmployeeHours
WHERE Date IN ('5/6/2004', '5/7/2004')
This SQL expression will select only the entries where the column Date has value of '5/6/2004' or
'5/7/2004', and you can see the result below:
Employee Date Hours
Sunny Kumar 5/6/2004 8
Allan Babel 5/6/2004 8
Tina Crown 5/6/2004 8
Sunny Kumar 5/7/2004 9
Allan Babel 5/7/2004 8
Tina Crown 5/7/2004 10
We can use the SQL IN statement with another column in our EmployeeHours table:
SELECT * FROM EmployeeHours
WHERE Hours IN (9, 10)
The result of the SQL query above will be:
Employee Date Hours
Sunny Kumar 5/7/2004 9
Tina Crown 5/7/2004 10
Tina Crown 5/8/2004 9
The SQL BETWEEN & AND keywords define a range of data between 2 values.
The SQL BETWEEN syntax looks like this:
SELECT Column1, Column2, Column3, …
FROM Table1
WHERE Column1 BETWEEN Value1 AND Value2
The 2 values defining the range for SQL BETWEEN clause can be dates, numbers or just text.
In contrast with the SQL IN keyword, which allows you to specify discrete values in your SQL WHERE
criteria, the SQL BETWEEN gives you the ability to specify a range in your search criteria.
We are going to use the familiar Customers table to show how SQL BETWEEN works:
Database Management System
Prof. K. Adisesha 42
FirstName LastName Email DOB Phone
Sunny Kumar Sunny.Kumar@yahoo.com 2/4/1968 626 222-2222
Prajwal Goldfish goldfish@fishhere.net 4/4/1974 323 455-4545
Paula Brown pb@herowndomain.org 5/24/1978 416 323-3232
James Kumar jim@supergig.co.in 20/10/1980 416 323-8888
Consider the following SQL BETWEEN statement:
SELECT * FROM Customers
WHERE DOB BETWEEN '1/1/1975' AND '1/1/2004'
The SQL BETWEEN statement above will select all Customers having DOB column between
'1/1/1975' and '1/1/2004' dates. Here is the result of this SQL expression:
FirstName LastName Email DOB Phone
Paula Brown pb@herowndomain.org 5/24/1978 416 323-3232
James Kumar jim@supergig.co.in 20/10/1980 416 323-8888
SQL aliases can be used with database tables and with database table columns, depending on task you
are performing.
SQL column aliases are used to make the output of your SQL queries easy to read and more meaningful:
SELECT Employee, SUM(Hours) As
SumHoursPerEmployee
FROM EmployeeHours
GROUP BY Employee
In the example above we created SQL alias SumHoursPerEmployee and the result of this SQL query
will be the following:
Employee SumHoursPerEmployee
Sunny Kumar 25
Allan Babel 24
Tina Crown 27
Consider the following SQL statement, showing how to use SQL table aliases:
SELECT Emp.Employee
FROM EmployeeHours AS Emp
Database Management System
Prof. K. Adisesha 43
Here is the result of the SQL expression above:
Employee
Sunny Kumar
Allan Babel
Tina Crown
The SQL table aliases are very useful when you select data from multiple tables.
The SQL COUNT aggregate function is used to count the number of rows in a database table.
The SQL COUNT syntax is simple and looks like this:
SELECT COUNT(Column1) FROM Table1
If we want to count the number of customers in our Customers table, we will use the following SQL
COUNT statement:
SELECT COUNT(LastName) AS
NumberOfCustomers
FROM Customers
The result of this SQL COUNT query will be:
NumberOfCustomers
4
The SQL MAX aggregate function allows us to select the highest (maximum) value for a certain
column.
The SQL MAX function syntax is very simple and it looks like this:
SELECT MAX(Column1)
FROM Table1
If we use the Customers table from our previous chapters, we can select the highest date of birth with the
following SQL MAX expression:
SELECT MAX(DOB) AS MaxDOB
FROM Customers
The SQL MIN aggregate function allows us to select the lowest (minimum) value for a certain column.
The SQL MIN function syntax is very simple and it looks like this:
SELECT MIN(Column1)
FROM Table1
If we use the Customers table from our previous chapters, we can select the lowest date of birth with the
following SQL MIN expression:
Database Management System
Prof. K. Adisesha 44
SELECT MIN(DOB) AS MinDOB
FROM Customers
The SQL AVG aggregate function selects the average value for certain table column.
Have a look at the SQL AVG syntax:
SELECT AVG(Column1)
FROM Table1
If we want to find out what is the average SaleAmount in the Sales table, we will use the following SQL
AVG statement:
SELECT AVG(SaleAmount) AS
AvgSaleAmount FROM Sales
which will result in the following dataset:
AvgSaleAmount
$195.73
The SQL SUM aggregate function allows selecting the total for a numeric column.
The SQL SUM syntax is displayed below:
SELECT SUM(Column1)
FROM Table1
We are going to use the Sales table to illustrate the use of SQL SUM clause:
Sales:
CustomerID Date SaleAmount
2 5/6/2004 $100.22
1 5/7/2004 $99.95
3 5/7/2004 $122.95
3 5/13/2004 $100.00
4 5/22/2004 $555.55
Consider the following SQL SUM statement:
SELECT SUM(SaleAmount) FROM Sales
This SQL statement will return the sum of all SaleAmount fields and the result of it will be:
SaleAmount
$978.67
Database Management System
Prof. K. Adisesha 45
Of course you can specify search criteria using the SQL WHERE clause in your SQL SUM statement. If
you want to select the total sales for customer with CustomerID = 3, you will use the following SQL
SUM statement:
SELECT SUM(SaleAmount) FROM Sales WHERE CustomerID = 3
The result will be:
SaleAmount
$222.95
The SQL GROUP BY statement is used along with the SQL aggregate functions like SUM to provide
means of grouping the result dataset by certain database table column(s).
The best way to explain how and when to use the SQL GROUP BY statement is by example, and that’s
what we are going to do.
Consider the following database table called EmployeeHours storing the daily hours for each employee
of a factious company:
Employee Date Hours
Sunny Kumar 5/6/2004 8
Allan Babel 5/6/2004 8
Tina Crown 5/6/2004 8
Sunny Kumar 5/7/2004 9
Allan Babel 5/7/2004 8
Tina Crown 5/7/2004 10
Sunny Kumar 5/8/2004 8
Allan Babel 5/8/2004 8
Tina Crown 5/8/2004 9
If the manager of the company wants to get the simple sum of all hours worked by all employees, he
needs to execute the following SQL statement:
SELECT SUM (Hours)
FROM EmployeeHours
But what if the manager wants to get the sum of all hours for each of his employees?
To do that he need to modify his SQL query and use the SQL GROUP BY statement:
SELECT Employee, SUM (Hours)
FROM EmployeeHours
GROUP BY Employee
The result of the SQL expression above will be the following:
Employee Hours
Database Management System
Prof. K. Adisesha 46
Sunny Kumar 25
Allan Babel 24
Tina Crown 27
As you can see we have only one entry for each employee, because we are grouping by the Employee
column.
The SQL GROUP BY clause can be used with other SQL aggregate functions, for example SQL AVG:
SELECT Employee, AVG(Hours)
FROM EmployeeHours
GROUP BY Employee
The result of the SQL statement above will be:
Employee Hours
Sunny Kumar 8.33
Allan Babel 8
Tina Crown 9
In our Employee table we can group by the date column too, to find out what is the total number of
hours worked on each of the dates into the table:
SELECT Date, SUM(Hours)
FROM EmployeeHours
GROUP BY Date
Here is the result of the above SQL expression:
Date Hours
5/6/2004 24
5/7/2004 27
5/8/2004 25
The SQL HAVING clause is used to restrict conditionally the output of a SQL statement, by a SQL
aggregate function used in your SELECT list of columns.
You can't specify criteria in a SQL WHERE clause against a column in the SELECT list for which SQL
aggregate function is used. For example the following SQL statement will generate an error:
SELECT Employee, SUM (Hours)
FROM EmployeeHours
WHERE SUM (Hours) > 24
GROUP BY Employee
Database Management System
Prof. K. Adisesha 47
The SQL HAVING clause is used to do exactly this, to specify a condition for an aggregate function
which is used in your query:
SELECT Employee, SUM (Hours)
FROM EmployeeHours
GROUP BY Employee
HAVING SUM (Hours) > 24
The above SQL statement will select all employees and the sum of their respective hours, as long as this
sum is greater than 24. The result of the SQL HAVING clause can be seen below:
Employee Hours
Sunny Kumar 25
Tina Crown 27
The SQL JOIN clause is used whenever we have to select data from 2 or more tables.
To be able to use SQL JOIN clause to extract data from 2 (or more) tables, we need a relationship
between certain columns in these tables.
We are going to illustrate our SQL JOIN example with the following 2 tables:
Customers:
CustomerID FirstName LastName Email DOB Phone
1 Sunny Kumar Sunny.Kumar@yahoo.com 2/4/1968 626 222-2222
2 Prajwal Goldfish goldfish@fishhere.net 4/4/1974 323 455-4545
3 Paula Brown pb@herowndomain.org 5/24/1978 416 323-3232
4 James Kumar jim@supergig.co.in 20/10/1980 416 323-8888
Sales:
CustomerID Date SaleAmount
2 5/6/2004 $100.22
1 5/7/2004 $99.95
3 5/7/2004 $122.95
3 5/13/2004 $100.00
4 5/22/2004 $555.55
As you can see those 2 tables have common field called CustomerID and thanks to that we can extract
information from both tables by matching their CustomerID columns.
Consider the following SQL statement:
Database Management System
Prof. K. Adisesha 48
SELECT Customers.FirstName, Customers.LastName,
SUM(Sales.SaleAmount) AS SalesPerCustomer
FROM Customers, Sales
WHERE Customers.CustomerID = Sales.CustomerID
GROUP BY Customers.FirstName, Customers.LastName
The SQL expression above will select all distinct customers (their first and last names) and the total
respective amount of dollars they have spent. The SQL JOIN condition has been specified after the
SQL WHERE clause and says that the 2 tables have to be matched by their respective CustomerID
columns.
Here is the result of this SQL statement:
FirstName LastName SalesPerCustomers
Sunny Kumar $99.95
Prajwal Goldfish $100.22
Paula Brown $222.95
James Kumar $555.55
The SQL statement above can be re-written using the SQL JOIN clause like this:
SELECT Customers.FirstName, Customers.LastName,
SUM(Sales.SaleAmount) AS SalesPerCustomer
FROM Customers JOIN Sales
ON Customers.CustomerID = Sales.CustomerID
GROUP BY Customers.FirstName, Customers.LastName
There are 2 types of SQL JOINS – INNER JOINS and OUTER JOINS. If you don't put INNER or
OUTER keywords in front of the SQL JOIN keyword, then INNER JOIN is used. In short "INNER
JOIN" = "JOIN" (note that different databases have different syntax for their JOIN clauses).
The INNER JOIN will select all rows from both tables as long as there is a match between the columns
we are matching on. In case we have a customer in the Customers table, which still hasn't made any
orders (there are no entries for this customer in the Sales table), this customer will not be listed in the
result of our SQL query above.
If the Sales table has the following rows:
CustomerID Date SaleAmount
2 5/6/2004 $100.22
1 5/6/2004 $99.95
And we use the same SQL JOIN statement from above:
Database Management System
Prof. K. Adisesha 49
SELECT Customers.FirstName, Customers.LastName, SUM(Sales.SaleAmount) AS
SalesPerCustomer
FROM Customers JOIN Sales
ON Customers.CustomerID = Sales.CustomerID
GROUP BY Customers.FirstName, Customers.LastName
We'll get the following result:
FirstName LastName SalesPerCustomers
Sunny Kumar $99.95
Prajwal Goldfish $100.22
Even though Paula and James are listed as customers in the Customers table they won't be displayed
because they haven't purchased anything yet.
But what if you want to display all the customers and their sales, no matter if they have ordered
something or not? We’ll do that with the help of SQL OUTER JOIN clause.
The second type of SQL JOIN is called SQL OUTER JOIN and it has 2 sub-types called LEFT
OUTER JOIN and RIGHT OUTER JOIN.
The LEFT OUTER JOIN or simply LEFT JOIN (you can omit the OUTER keyword in most
databases), selects all the rows from the first table listed after the FROM clause, no matter if they have
matches in the second table.
If we slightly modify our last SQL statement to:
SELECT Customers.FirstName, Customers.LastName,
SUM(Sales.SaleAmount) AS SalesPerCustomer
FROM Customers LEFT JOIN Sales
ON Customers.CustomerID = Sales.CustomerID
GROUP BY Customers.FirstName,
Customers.LastName
and the Sales table still has the following rows:
CustomerID Date SaleAmount
2 5/6/2004 $100.22
1 5/6/2004 $99.95
The result will be the following:
FirstName LastName SalesPerCustomers
Sunny Kumar $99.95
Prajwal Goldfish $100.22
Database Management System
Prof. K. Adisesha 50
Paula Brown NULL
James Kumar NULL
As you can see we have selected everything from the Customers (first table). For all rows from
Customers, which don’t have a match in the Sales (second table), the SalesPerCustomer column has
amount NULL (NULL means a column contains nothing).
The RIGHT OUTER JOIN or just RIGHT JOIN behaves exactly as SQL LEFT JOIN, except that it
returns all rows from the second table (the right table in our SQL JOIN statement).
SQL Hosting is a web hosting plan featuring one or more SQL databases. Having SQL database
backend is essential for most of the modern websites, that’s why almost all web hosting providers offer
SQL hosting plans nowadays.
The most commonly used SQL hosting databases are MySQL, MS SQL Server 2000/ MS SQL Server
2005, MS Access and Oracle.
Which SQL database to choose for your hosting?
You can have SQL hosting on both Windows and Linux/UNIX operating systems, but some of the
database engines run only on one of them. For example if you want SQL hosting with MS SQL Server
2000/2005 backend, then you need to look for Windows hosting plan, because SQL Server runs only on
Windows. If you are looking for MySQL hosting then you are in luck, because MySQL runs on both
Windows and Linux. Oracle also runs on both operating systems, but MS Access hosting requires
Windows OS.
What are the advantages of SQL hosting?
Having SQL Hosting backend allows for developing robust and scalable web applications, which can
handle thousands of visitors daily. When the website content is in a SQL database, it’s very easy to
search the content for particular keyword or phrase. Having your data in a RDBMS allows for easy
management of the content. Also when a website utilizes SQL hosting, the content of the website is
separated by its presentation, which makes it really easy to apply design and layout changes.

More Related Content

What's hot

All data models in dbms
All data models in dbmsAll data models in dbms
All data models in dbms
Naresh Kumar
 
Codd's rules
Codd's rulesCodd's rules
Codd's rules
Mohd Arif
 
2. Entity Relationship Model in DBMS
2. Entity Relationship Model in DBMS2. Entity Relationship Model in DBMS
2. Entity Relationship Model in DBMS
koolkampus
 

What's hot (20)

3 Level Architecture
3 Level Architecture3 Level Architecture
3 Level Architecture
 
Dbms classification according to data models
Dbms classification according to data modelsDbms classification according to data models
Dbms classification according to data models
 
All data models in dbms
All data models in dbmsAll data models in dbms
All data models in dbms
 
Data model and entity relationship
Data model and entity relationshipData model and entity relationship
Data model and entity relationship
 
data modeling and models
data modeling and modelsdata modeling and models
data modeling and models
 
Entity (types, attibute types)
Entity (types, attibute types)Entity (types, attibute types)
Entity (types, attibute types)
 
Dbms architecture
Dbms architectureDbms architecture
Dbms architecture
 
DML, DDL, DCL ,DRL/DQL and TCL Statements in SQL with Examples
DML, DDL, DCL ,DRL/DQL and TCL Statements in SQL with ExamplesDML, DDL, DCL ,DRL/DQL and TCL Statements in SQL with Examples
DML, DDL, DCL ,DRL/DQL and TCL Statements in SQL with Examples
 
Data Models
Data ModelsData Models
Data Models
 
Data independence
Data independenceData independence
Data independence
 
Er model ppt
Er model pptEr model ppt
Er model ppt
 
Codd's rules
Codd's rulesCodd's rules
Codd's rules
 
DBMS Keys
DBMS KeysDBMS Keys
DBMS Keys
 
Database language
Database languageDatabase language
Database language
 
Relational Data Model Introduction
Relational Data Model IntroductionRelational Data Model Introduction
Relational Data Model Introduction
 
Chapter1
Chapter1Chapter1
Chapter1
 
Databases: Normalisation
Databases: NormalisationDatabases: Normalisation
Databases: Normalisation
 
2. Entity Relationship Model in DBMS
2. Entity Relationship Model in DBMS2. Entity Relationship Model in DBMS
2. Entity Relationship Model in DBMS
 
Elmasri Navathe DBMS Unit-1 ppt
Elmasri Navathe DBMS Unit-1 pptElmasri Navathe DBMS Unit-1 ppt
Elmasri Navathe DBMS Unit-1 ppt
 
Types of Database Models
Types of Database ModelsTypes of Database Models
Types of Database Models
 

Similar to Dbms notes

Data base management system
Data base management systemData base management system
Data base management system
Navneet Jingar
 
Database Management Systems ( Dbms )
Database Management Systems ( Dbms )Database Management Systems ( Dbms )
Database Management Systems ( Dbms )
Patty Buckley
 
DATABASE MANAGEMENT SYSTEMS university course materials useful for students ...
DATABASE MANAGEMENT SYSTEMS  university course materials useful for students ...DATABASE MANAGEMENT SYSTEMS  university course materials useful for students ...
DATABASE MANAGEMENT SYSTEMS university course materials useful for students ...
SakkaravarthiS1
 
A database is generally used for storing related, structured data, w.pdf
A database is generally used for storing related, structured data, w.pdfA database is generally used for storing related, structured data, w.pdf
A database is generally used for storing related, structured data, w.pdf
angelfashions02
 

Similar to Dbms notes (20)

DBMS Part 1.pptx
DBMS Part 1.pptxDBMS Part 1.pptx
DBMS Part 1.pptx
 
Data base management system
Data base management systemData base management system
Data base management system
 
Dbms
DbmsDbms
Dbms
 
Relational database management systems
Relational database management systemsRelational database management systems
Relational database management systems
 
Ch01
Ch01Ch01
Ch01
 
Unit 1.pptx
Unit 1.pptxUnit 1.pptx
Unit 1.pptx
 
Database, Lecture-1.ppt
Database, Lecture-1.pptDatabase, Lecture-1.ppt
Database, Lecture-1.ppt
 
Introduction to Databases and Transactions
Introduction to Databases and TransactionsIntroduction to Databases and Transactions
Introduction to Databases and Transactions
 
Database Management Systems ( Dbms )
Database Management Systems ( Dbms )Database Management Systems ( Dbms )
Database Management Systems ( Dbms )
 
Database Management System ( Dbms )
Database Management System ( Dbms )Database Management System ( Dbms )
Database Management System ( Dbms )
 
Dbms unit i
Dbms unit iDbms unit i
Dbms unit i
 
Introduction-to-Databases.pptx
Introduction-to-Databases.pptxIntroduction-to-Databases.pptx
Introduction-to-Databases.pptx
 
Database Management System
Database Management SystemDatabase Management System
Database Management System
 
DATABASE MANAGEMENT SYSTEMS university course materials useful for students ...
DATABASE MANAGEMENT SYSTEMS  university course materials useful for students ...DATABASE MANAGEMENT SYSTEMS  university course materials useful for students ...
DATABASE MANAGEMENT SYSTEMS university course materials useful for students ...
 
DATABASE MANAGEMENT SYSTEMS.pdf
DATABASE MANAGEMENT SYSTEMS.pdfDATABASE MANAGEMENT SYSTEMS.pdf
DATABASE MANAGEMENT SYSTEMS.pdf
 
Database management systems
Database management systemsDatabase management systems
Database management systems
 
Database Management system
Database Management systemDatabase Management system
Database Management system
 
DBMS-Unit-1.pptx
DBMS-Unit-1.pptxDBMS-Unit-1.pptx
DBMS-Unit-1.pptx
 
MS-CIT Unit 9.pptx
MS-CIT Unit 9.pptxMS-CIT Unit 9.pptx
MS-CIT Unit 9.pptx
 
A database is generally used for storing related, structured data, w.pdf
A database is generally used for storing related, structured data, w.pdfA database is generally used for storing related, structured data, w.pdf
A database is generally used for storing related, structured data, w.pdf
 

More from Prof. Dr. K. Adisesha

More from Prof. Dr. K. Adisesha (20)

Software Engineering notes by K. Adisesha.pdf
Software Engineering notes by K. Adisesha.pdfSoftware Engineering notes by K. Adisesha.pdf
Software Engineering notes by K. Adisesha.pdf
 
Software Engineering-Unit 1 by Adisesha.pdf
Software Engineering-Unit 1 by Adisesha.pdfSoftware Engineering-Unit 1 by Adisesha.pdf
Software Engineering-Unit 1 by Adisesha.pdf
 
Software Engineering-Unit 2 "Requirement Engineering" by Adi.pdf
Software Engineering-Unit 2 "Requirement Engineering" by Adi.pdfSoftware Engineering-Unit 2 "Requirement Engineering" by Adi.pdf
Software Engineering-Unit 2 "Requirement Engineering" by Adi.pdf
 
Software Engineering-Unit 3 "System Modelling" by Adi.pdf
Software Engineering-Unit 3 "System Modelling" by Adi.pdfSoftware Engineering-Unit 3 "System Modelling" by Adi.pdf
Software Engineering-Unit 3 "System Modelling" by Adi.pdf
 
Software Engineering-Unit 4 "Architectural Design" by Adi.pdf
Software Engineering-Unit 4 "Architectural Design" by Adi.pdfSoftware Engineering-Unit 4 "Architectural Design" by Adi.pdf
Software Engineering-Unit 4 "Architectural Design" by Adi.pdf
 
Software Engineering-Unit 5 "Software Testing"by Adi.pdf
Software Engineering-Unit 5 "Software Testing"by Adi.pdfSoftware Engineering-Unit 5 "Software Testing"by Adi.pdf
Software Engineering-Unit 5 "Software Testing"by Adi.pdf
 
Computer Networks Notes by -Dr. K. Adisesha
Computer Networks Notes by -Dr. K. AdiseshaComputer Networks Notes by -Dr. K. Adisesha
Computer Networks Notes by -Dr. K. Adisesha
 
CCN Unit-1&2 Data Communication &Networking by K. Adiaesha
CCN Unit-1&2 Data Communication &Networking by K. AdiaeshaCCN Unit-1&2 Data Communication &Networking by K. Adiaesha
CCN Unit-1&2 Data Communication &Networking by K. Adiaesha
 
CCN Unit-3 Data Link Layer by Dr. K. Adisesha
CCN Unit-3 Data Link Layer by Dr. K. AdiseshaCCN Unit-3 Data Link Layer by Dr. K. Adisesha
CCN Unit-3 Data Link Layer by Dr. K. Adisesha
 
CCN Unit-4 Network Layer by Dr. K. Adisesha
CCN Unit-4 Network Layer by Dr. K. AdiseshaCCN Unit-4 Network Layer by Dr. K. Adisesha
CCN Unit-4 Network Layer by Dr. K. Adisesha
 
CCN Unit-5 Transport & Application Layer by Adi.pdf
CCN Unit-5 Transport & Application Layer by Adi.pdfCCN Unit-5 Transport & Application Layer by Adi.pdf
CCN Unit-5 Transport & Application Layer by Adi.pdf
 
Introduction to Computers.pdf
Introduction to Computers.pdfIntroduction to Computers.pdf
Introduction to Computers.pdf
 
R_Programming.pdf
R_Programming.pdfR_Programming.pdf
R_Programming.pdf
 
Scholarship.pdf
Scholarship.pdfScholarship.pdf
Scholarship.pdf
 
Operating System-2 by Adi.pdf
Operating System-2 by Adi.pdfOperating System-2 by Adi.pdf
Operating System-2 by Adi.pdf
 
Operating System-1 by Adi.pdf
Operating System-1 by Adi.pdfOperating System-1 by Adi.pdf
Operating System-1 by Adi.pdf
 
Operating System-adi.pdf
Operating System-adi.pdfOperating System-adi.pdf
Operating System-adi.pdf
 
Data_structure using C-Adi.pdf
Data_structure using C-Adi.pdfData_structure using C-Adi.pdf
Data_structure using C-Adi.pdf
 
JAVA PPT -2 BY ADI.pdf
JAVA PPT -2 BY ADI.pdfJAVA PPT -2 BY ADI.pdf
JAVA PPT -2 BY ADI.pdf
 
JAVA PPT -5 BY ADI.pdf
JAVA PPT -5 BY ADI.pdfJAVA PPT -5 BY ADI.pdf
JAVA PPT -5 BY ADI.pdf
 

Recently uploaded

Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global Impact
PECB
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
heathfieldcps1
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
QucHHunhnh
 

Recently uploaded (20)

TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot Graph
 
Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptx
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global Impact
 
Micro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfMicro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdf
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdf
 
Asian American Pacific Islander Month DDSD 2024.pptx
Asian American Pacific Islander Month DDSD 2024.pptxAsian American Pacific Islander Month DDSD 2024.pptx
Asian American Pacific Islander Month DDSD 2024.pptx
 
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdf
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The Basics
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
 
Unit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptxUnit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptx
 
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
 
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17  How to Extend Models Using Mixin ClassesMixin Classes in Odoo 17  How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17
 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan Fellows
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
 
Class 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdfClass 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdf
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and Mode
 

Dbms notes

  • 1. Database Management System Prof. K. Adisesha 1 Introduction to DBMS DBMS stands for Database Management System. We can break it like this DBMS = Database + Management System. Database is a collection of data and Management System is a set of programs to store and retrieve those data. Based on this we can define DBMS like this: DBMS is a collection of inter-related data and set of programs to store & access those data in an easy and effective manner. What is the need of DBMS? Database systems are developed for large amount of data. When dealing with huge amount of data, there are two things that require optimization: Storage of data and retrieval of data. Storage: According to the principles of database systems, the data is stored in such a way that it acquires lot less space as the redundant data (duplicate data) has been removed before storage. Let us take a layman example to understand this: In a banking system, suppose a customer is having two accounts, one is saving account and another is salary account. Let’s say bank stores saving account data at one place (these places are called tables we will learn them later) and salary account data at another place, in that case if the customer information such as customer name, address etc. are stored at both places then this is just a wastage of storage (redundancy/ duplication of data), to organize the data in a better way the information should be stored at one place and both the accounts should be linked to that information somehow. The same thing we achieve in DBMS. Fast Retrieval of data: Along with storing the data in an optimized and systematic manner, it is also important that we retrieve the data quickly when needed. Database systems ensure that the data is retrieved as quickly as possible. Building blocks/Components DBMS Engine accepts logical requests from various other DBMS subsystems, converts them into physical equivalents, and actually accesses the database and data dictionary as they exist on a storage device. Data Definition Subsystem helps the user create and maintain the data dictionary and define the structure of the files in a database. Data Manipulation Subsystem helps the user to add, change, and delete information in a database and query it for valuable information. Software tools within the data manipulation subsystem are most often the primary interface between user and the information contained in a database. It allows the user to specify its logical information requirements. Application Generation Subsystem contains facilities to help users develop transaction-intensive applications. It usually requires that the user perform a detailed series of tasks to process a transaction. It facilitates easy-to-use data entry screens, programming languages, and interfaces.
  • 2. Database Management System Prof. K. Adisesha 2 Data Administration Subsystem helps users manage the overall database environment by providing facilities for backup and recovery, security management, query optimization, concurrency control, and change management. Database Applications – DBMS Applications where we use Database Management Systems are:  Telecom: There is a database to keeps track of the information regarding calls made, network usage, customer details etc. Without the database systems it is hard to maintain that huge amount of data that keeps updating every millisecond.  Industry: Where it is a manufacturing unit, warehouse or distribution centre, each one needs a database to keep the records of ins and outs. For example distribution centre should keep a track of the product units that supplied into the centre as well as the products that got delivered out from the distribution centre on each day; this is where DBMS comes into picture.  Banking System: For storing customer info, tracking day to day credit and debit transactions, generating bank statements etc. All this work has been done with the help of Database management systems.  Education sector: Database systems are frequently used in schools and colleges to store and retrieve the data regarding student details, staff details, course details, exam details, payroll data, attendance details, fees details etc. There is a hell lot amount of inter-related data that needs to be stored and retrieved in an efficient manner.  Online shopping: You must be aware of the online shopping websites such as Amazon, Flipkart etc. These sites store the product information, your addresses and preferences, credit details and provide you the relevant list of products based on your query. All this involves a Database management system. Data Model A model is a representation of reality, 'real world' objects and events, associations. It is an abstraction that concentrates on the essential, inherent aspects an organization and ignores the accidental properties. A data model represents the organization itself. It should provide the basic concepts and notations that will allow database designers and end users unambiguously and accurately to communicate their understanding of the organizational data. Data Model can be defined as an integrated collection of concepts for describing and manipulating data, relationships between data, and constraints on the data in an organization. A data model comprises of three components: • A structural part, consisting of a set of rules according to which databases can be constructed. • A manipulative part, defining the types of operation that are allowed on the data (this includes the operations that are used for updating or retrieving data from the database and for changing the structure of the database).
  • 3. Database Management System Prof. K. Adisesha 3 • Possibly a set of integrity rules, which ensures that the data is accurate. The purpose of a data model is to represent data and to make the data understandable. There have been many data models proposed in the literature. They fall into three broad categories: • Object Based Data Models • Physical Data Models • Record Based Data Models The object based and record based data models are used to describe data at the conceptual and external levels, the physical data model is used to· describe data at the internal level. Object Based Data Models Object based data models use concepts such as entities, attributes, and relationships. An entity is a distinct object (a person, place, concept, and event) in the organization that is to be represented in the database. An attribute is a property that describes some aspect of the object that we wish to record, and a relationship is an association between entities. Some of the more common types of object based data model are: • Entity-Relationship • Object Oriented • Semantic • Functional The Entity-Relationship model has emerged as one of the main techniques for modeling database design and forms the basis for the database design methodology. The object oriented data model extends the definition of an entity to include, not only the attributes that describe the state of the object but also the actions that are associated with the object, that is, its behavior. The object is said to encapsulate both state and behavior. Entities in semantic systems represent the equivalent of a record in a relational system or an object in an OO system but they do not include behaviour (methods). They are abstractions 'used to represent real world (e.g. customer) or conceptual (e.g. bank account) objects. The functional data model is now almost twenty years old. The original idea was to' view the database as a collection of extensionally defined functions and to use a functional language for querying the database. Physical Data Models Physical data models describe how data is stored in the computer, representing information such as record structures, record ordering, and access paths. There are not as many physical data models as logical data models, the most common one being the Unifying Model. Record Based Logical Models Record based logical models are used in describing data at the logical and view levels. In contrast to object based data models, they are used to specify the overall logical structure of the database and to provide a higher-level description of the implementation. Record based models are so named because
  • 4. Database Management System Prof. K. Adisesha 4 the database is structured in fixed format records of several types. Each record type defines a fixed number of fields, or attributes, and each field is usually of a fixed length. Modeling language A modeling language is a data modeling language to define the schema of each database hosted in the DBMS, according to the DBMS database model. Database management systems (DBMS) are designed to use one of five database structures to provide simplistic access to information stored in databases. The five database structures are:  Hierarchical model,  Network model,  Relational model,  Object model. Inverted lists and other methods are also used. A given database management system may provide one or more of the five models. The optimal structure depends on the natural organization of the application's data, and on the application's requirements, which include transaction rate (speed), reliability, maintainability, scalability, and cost. Hierarchical structure The hierarchical structure was used in early mainframe DBMS. Records’ relationships form a treelike model. This structure is simple but nonflexible because the relationship is confined to a one-to-many relationship. IBM’s IMS system and the RDM Mobile are examples of a hierarchical database system with multiple hierarchies over the same data. RDM Mobile is a newly designed embedded database for a mobile computer system. The hierarchical structure is used primarily today for storing geographic information and file systems. Network Model
  • 5. Database Management System Prof. K. Adisesha 5 The network structure consists of more complex relationships. Unlike the hierarchical structure, it can relate to many records and accesses them by following one of several paths. In other words, this structure allows for many-to-many relationships. The relational structure is the most commonly used today. It is used by mainframe, midrange and microcomputer systems. It uses two-dimensional rows and columns to store data. The tables of records can be connected by common key values. While working for IBM, E.F. Codd designed this structure in 1970. The model is not easy for the end user to run queries with because it may require a complex combination of many tables. The object oriented structure has the ability to handle graphics, pictures, voice and text, types of data, without difficultly unlike the other database structures. This structure is popular for multimedia Web- based applications. It was designed to work with object-oriented programming languages such as Java. The dominant model in use today is the ad hoc one embedded in SQL, despite the objections of purists who believe this model is a corruption of the relational model since it violates several fundamental principles for the sake of practicality and performance. Many DBMSs also support the Open Database Connectivity API that supports a standard way for programmers to access the DBMS. Before the database management approach, organizations relied on file processing systems to organize, store, and process data files. End users criticized file processing because the data is stored in many different files and each organized in a different way. Each file was specialized to be used with a specific application. File processing was bulky, costly and nonflexible when it came to supplying needed data accurately and promptly. Data redundancy is an issue with the file processing system because the independent data files produce duplicate data so when updates were needed each separate file would need to be updated. Another issue is the lack of data integration. The data is dependent on other data to organize and store it. Lastly, there was not any consistency or standardization of the data in a file processing system, which makes maintenance difficult. For these reasons, the database management approach was produced. Characteristics of a DBMS
  • 6. Database Management System Prof. K. Adisesha 6  Real-world entity − A modern DBMS is 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 students as an entity and their age as an attribute.  Relation-based tables − DBMS allows entities and relations among them to form tables. A user can understand the architecture of a database just by looking at the table names.  Isolation of data and application − A database system is entirely different than its data. A database is an active entity, whereas data is said to be passive, on which the database works and organizes. DBMS also stores metadata, which is data about data, to ease its own process.  Less redundancy − DBMS follows the rules of normalization, which splits a relation when any of its attributes is having redundancy in values. Normalization is a mathematically rich and scientific process that reduces data redundancy.  Consistency − Consistency is a state where every relation in a database remains consistent. There exist methods and techniques, which can detect attempt of leaving database in inconsistent state. A DBMS can provide greater consistency as compared to earlier forms of data storing applications like file-processing systems.  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 as different filtering options as required to retrieve a set of data. Traditionally it was not possible where file-processing system was used.  ACID Properties − DBMS follows the concepts of Atomicity, Consistency, Isolation, and Durability (normally shortened as ACID). These concepts are applied on transactions, which manipulate data in a database. ACID properties help the database stay healthy in multi- transactional environments and in case of failure.  Multiuser and Concurrent Access − DBMS supports multi-user environment and allows them to access and manipulate data in parallel. Though there are restrictions on transactions when users attempt to handle the same data item, but users are always unaware of them.  Multiple views − DBMS offers multiple views for different users. A user who is in the Sales department will have a different view of database than a person working in the Production department. This feature enables the users to have a concentrate view of the database according to their requirements.  Security − Features like multiple views offer security to some extent where users are unable to access data of other users and departments. DBMS offers methods to impose constraints while entering data into the database and retrieving the same at a later stage. DBMS offers many different levels of security features, which enables multiple users to have different views with different features. For example, a user in the Sales department cannot see the data that belongs to the Purchase department. Additionally, it can also be managed how much data of the Sales department should be displayed to the user. Since a DBMS is not saved on the disk as traditional file systems, it is very hard for miscreants to break the code. Various Objectives of Database Management System Mass Storage DBMS can store huge data in it. So for all the big firms, DBMS is really ideal technology to use. It can store thousands of records in it and one can fetch all that data whenever it is needed. Removes Duplicity
  • 7. Database Management System Prof. K. Adisesha 7 If you have huge data then data duplicity will occur for sure at any instance. DBMS guarantee it that there will be no data duplicity among all the records. While storing new records, DBMS makes sure that same data was not inserted before. Multiple Users Access No one handles the whole database alone. Many users are able to access database. Therefore, this situation may happen that two or more users are accessing database. They can change whatever they want, at that time DBMS makes it sure that they can work concurrently. Data Protection Information such as bank details, employee’s salary details and sale purchase details should always be kept secured. Also all the companies need their data secured from unauthorized use. DBMS gives a master level security to their data. No one can alter or modify the information without the privilege of using that data. Data Back up and recovery Sometimes database failure occurs so there is no option like one can say that all the data has been lost. There should be a backup of database so that on database failure it can be recovered. DBMS has the ability to backup and recover all the data in database. Everyone can work on DBMS There is no need to be a master of programming language if you want to work on DBMS. Any accountant who is having less technical knowledge can work on DBMS. All the definitions and descriptions are given in it so that even a non-technical background w=person can work on it. Integrity Integrity means your data is authentic and consistent. DBMS has various validity checks that make your data completely accurate and consistence. Platform Independent One can run DBMS at any platform. No particular platform is required to work on database management system. Database Schema – Database Schema is the overall Design of the Database. It is the skeleton structure that represents the logical view of the entire database. It tells how the data is organized and how the relations among them are associated. It is sometimes also referred to as an overall model of the data, a conceptual model or a conceptual schema. These terms mean essentially the same thing. The Database Schema also formulates all the constraints that are to be applied on the data. A schema is a chart of the types of data that are used. It gives the names of the entities and attributes, and specifies the relation between them. It is just like a framework into which the values of data items can be fitted/stored. All the constraints that are to be applied on the data is also formulated on the schema.
  • 8. Database Management System Prof. K. Adisesha 8 A database Schema can be divided into two categories – Physical Database Schema :The Schema which is related to actual storage of data or the Schema that describes database design at physical level is called Physical Database Schema. It defines how the data will be stored in the secondary storage. Logical Database Schema :The schema that defines all the logical constraints that need to be applied on the data stored or the schema that describes the database design at the logical level is called Logical Database Schema. It defines tables, views, and integrity constraints. Three Schema Approach We know that the same thing, if viewed from different angles produces difference sights. Likewise, the database that we have created already can have different aspects to reveal if seen from different levels of abstraction. The term Abstraction is very important here. Generally it means the amount of detail you want to hide. Any entity can be seen from different perspectives and levels of complexity to make it a reveal its current amount of abstraction. Let us illustrate by a simple example. A computer reveals the minimum of its internal details, when seen from outside. We do not know what parts it is built with. This is the highest level of abstraction, meaning very few details are visible. If we open the computer case and look inside at the hard disc, motherboard, CD drive, CPU and RAM, we are in middle level of abstraction. If we move on to open the hard disc and examine its tracks, sectors and read-write heads, we are at the lowest level of abstraction, where no details are invisible. In the same manner, the database can also be viewed from different levels of abstraction to reveal different levels of details. From a bottom-up manner, we may find that there are three levels of abstraction or views in the database. We discuss them here. The word schema means arrangement – how we want to arrange things that we have to store. The diagram above shows the three different schemas used in DBMS, seen from different levels of abstraction. The lowest level, called the Internal or Physical schema, deals with the description of how raw data items (like 1, ABC, KOL, H2 etc.) are stored in the physical storage (Hard Disc, CD, Tape Drive etc.). It also describes the data type of these data items, the size of the items in the storage media, the location (physical address) of the items in the storage device and so on. This schema is useful for database application developers and database administrator. The middle level is known as the Conceptual or Logical Schema, and deals with the structure of the entire database. Please note that at this level we are not interested with the raw data items anymore, we
  • 9. Database Management System Prof. K. Adisesha 9 are interested with the structure of the database. This means we want to know the information about the attributes of each table, the common attributes in different tables that help them to be combined, what kind of data can be input into these attributes, and so on. Conceptual or Logical schema is very useful for database administrators whose responsibility is to maintain the entire database. The highest level of abstraction is the External or View Schema. This is targeted for the end users. Now, an end user does not need to know everything about the structure of the entire database, rather than the amount of details he/she needs to work with. We may not want the end user to become confused with astounding amount of details by allowing him/her to have a look at the entire database, or we may also not allow this for the purpose of security, where sensitive information must remain hidden from unwanted persons. The database administrator may want to create custom made tables, keeping in mind the specific kind of need for each user. These tables are also known as virtual tables, because they have no separate physical existence. They are crated dynamically for the users at runtime. Say for example, in our sample database we have created earlier, we have a special officer whose responsibility is to keep in touch with the parents of any under aged student living in the hostels. That officer does not need to know every detail except the Roll, Name, Addresss and Age. The database administrator may create a virtual table with only these four attributes, only for the use of this officer. Database Users and Administrators A primary goal of a database system is to retrieve information from and store new information in the database. People who work with a database can be categorized as database users or database administrators. DBA Responsibilities  Installation, configuration and upgrading of Microsoft SQL Server/MySQL/Oracle server software and related products.  Evaluate MSSQL/MySQL/Oracle features and MSSQL/MySQL/Oracle related products.  Establish and maintain sound backup and recovery policies and procedures.  Take care of the Database design and implementation.  Implement and maintain database security (create and maintain users and roles, assign privileges).  Database tuning and performance monitoring.  Application tuning and performance monitoring.  Setup and maintain documentation and standards.  Plan growth and changes (capacity planning).  Work as part of a team and provide 7×24 supports when required.  Do general technical trouble shooting and give consultation to development teams.  Interface with MSSQL/MySQL/Oracle for technical support.  ITIL Skill set requirement (Problem Management/Incident Management/Chain Management etc) Types of DBA
  • 10. Database Management System Prof. K. Adisesha 10 1. Administrative DBA – Work on maintaining the server and keeping it running. Concerned with backups, security, patches, replication, etc. Things that concern the actual server software. 2. Development DBA – works on building queries, stored procedures, etc. that meet business needs. This is the equivalent of the programmer. You primarily write T-SQL. 3. Architect – Design schemas. Build tables, FKs, PKs, etc. Work to build a structure that meets the business needs in general. The design is then used by developers and development DBAs to implement the actual application. 4. Data Warehouse DBA – Newer role, but responsible for merging data from multiple sources into a data warehouse. May have to design warehouse, but cleans, standardizes, and scrubs data before loading. In SQL Server, this DBA would use DTS heavily. 5. OLAP DBA – Builds multi-dimensional cubes for decision support or OLAP systems. The primary language in SQL Server is MDX, not SQL here Application DBA- Application DBAs straddle the fence between the DBMS and the application software and are responsible for ensuring that the application is fully optimized for the database and vice versa. They usually manage all the application components that interact with the database and carry out activities such as application installation and patching, application upgrades, database cloning, building and running data cleanup routines, data load process management, etc. Database System Applications Databases are widely used. Here are some representative applications: •Banking: For customer information, accounts, and loans, and banking transactions. •Airlines: For reservations and schedule information. Airlines were among the first to use databases in a geographically distributed manner —terminals situated around the world accessed the central database system through phone lines and other data networks. •Universities: For student information, course registrations, and grades. Credit card transactions: For purchases on credit cards and generation of monthly statements. •Telecommunication: For keeping records of calls made, generating monthly bills, maintaining balances on prepaid calling cards, and storing information about the communication networks. •Finance: For storing information about holdings, sales, and purchases of financial instruments such as stocks and bonds. •Sales: For customer, product,and purchase information. •Manufacturing: For management of supply chain and for tracking production of items in factories, inventories of items in warehouses/stores, and orders for items. •Human resources: For information about employees, salaries, payroll taxes and benefits, and for generation of paychecks. Data structure
  • 11. Database Management System Prof. K. Adisesha 11 Data structures (fields, records, files and objects) optimized to deal with very large amounts of data stored on a permanent data storage device (which implies relatively slow access compared to volatile main memory). ER MODEL Before define ER Model, there are some terms which is require to understand ER model properly. In short, ER Model is a diagrammatic representation of entire Database tables. It represents a High Level Database Design. For Designing the Softwares : This phase is done in softwares by UML (Unified Modelling Language). For Designing the Database : ER Model is used to design database. Definitions : Entity : an object (Record in the table). It a a real world object, such as person, place, thing, event, or concept about which information is recorded. For example, In a banking environment, entities are CUSTOMERS, BANK SUPPLIERS etc. Entities may be –  Physical – Can be touched(sensed) Example Fan.  Conceptual – Cannot be touched. Example course offered, brand name etc. Storing conceptual entities is a big problem. Entity Types : is defined as a collection of entities that have the same attributes. For example, employee in a company database. As same attributes are shared by the employee entities, but such entity has its own value for each attributes. Entity Set : Collection of similar entities of same type that share the same attribute at any point in time. For example, a Faculty set may contain all the teachers of a college, Set of all persons, Set of Companies etc. The Entity Set is also called extension of the Entity Type. Attribute : It is the name of the column. An attribute gives the characteristics of the entity. It is also called as data element, data field, a field, a data item, or an elementary item. Data Value : It is the actual data or information contained in each data element(attribute). The data element “Supplier Name” can take value as “Anil Mittal” Difference between Entity, Attributes, and Data Values – ENTITY ATTRIBUTES(OR DATA ELEMENTS) VALUES CUSTOMER Customer ID Number Customer Name Customer Address Customer Phone Number 985678 Ankit Mittal B-176,MC Colony, Bhiwani 57845-69821 ACCOUNT Account Type Account Number Savings 1800221133665587
  • 12. Database Management System Prof. K. Adisesha 12 Account Balance Account Statement 125000 5000_Credit SUPPLIER Supplier Name Supplier Address Supplier Phone Number Supplier Bill Number Vishal Rastogi H-456, Shiv Nagar, Bhiwani 789546256 B-896541 Difference Between Entity, Entity Type and Entity Sets – Entity Entity Type Entity Set Entity is a person, place, thing, event, or even a concept. It may be tangible or intangible. An entity type defines the collection of entities that have the same attributes. An entity set is a set of entities of the same type that share the same properties at any point in time (or) It is the collection of entities of an entity type at a point of time. For Example, Entity is e1 or e2 or e3 or an apple where e1 e2 e3 are employees and an apple is a fruit. For example, entity type is EMPLOYEE or Fruit Entity set is a bucket of apple, banana, mango etc. or {e1,e2……} Entity – An entity is an object that are represented in the database. For example Mohit, Vasu, CSE306 etc. An entity is represented or defined by set of attributes. Attributes are the properties used to describe an entity. For example, a STUDENT entity may have a Name, Roll number, Class, Marks etc. where STUDENT is the entity and name roll number class marks are the attributes. Types of Entity –  Strong Entity Types  Recursive Entity Types  Weak Entity Types  Composite Entity Types or Associative Entity Types  Super Type and Sub Type Entities Notations Of different Entity Type in ER Diagram Entity Strong Entity Type
  • 13. Database Management System Prof. K. Adisesha 13 Weak Entity Type Recursive Entity Type Composite Entity Type (or) Associative Entity Subtypes and Supertypes Strong Entity Type – are the entities which has a key attribute in its attribute list or a set that has a primary key. The strong entity type is also called regular entity type. For Example, The Student’s unique RollNo will identify the students. So, RollNo is set to be the Primary Key of the STUDENT entity, & Hence STUDENT is a strong entity type because of its key attribute. Recursive Entity Type – It is also called Self Referential Relationship Entity Type. It is an entity type with foreign key referencing to same table or itself. Recursive Entity Type occurs in a unary relationship. Weak Entity Type – Entity Type with no key or Primary Key are called weak entity Type. The Tuples of weak entity type may not be possible to differentiate using one attribute of weak entity. For every weak entity, there should be unique OWNER entity type. Composite Entities– If a Many to Many relationship exist then we must eliminate it by using composite entities. Composite Entities are the entities which exists as a relationship as well as an entity. The many to many relationship will be converted to 1 to many relationship. Composite Entities are also called Bridge Entities, because they acts like a bridge between the two entities which have many to many relationship. Bridge or Composite entity composed of the primary keys of each of the entities to be connected. A composite entity is represented by a diamond shape with in a rectangle in an ER Diagram. Supertypes and Subtypes Entities – A subtype is a subgrouping of the entities in an entity type that is meaningful to the organisation. For example, In a University, a STUDENT is an entity type. Two subtypes of STUDENT entity are GRADUATE STUDENT UNDERGRADUATE STUDENT A supertype is a generic entity type that has a relationship with one or more sub-types. In the above example, STUDENT entity is a supertype.
  • 14. Database Management System Prof. K. Adisesha 14 Attributes and Its types Attribute: It is the name of the column. An attribute gives the characteristics of the entity. For example, A customer of bank may be described by : name, address, customer ID number. It is also called as data element, data field, a field, a data item, or an elementary item. Type of Attributes in DBMS – Single valued Attributes: An attribute, that has a single value for a particular entity is known as single valued attributes. For example, age of a employee entity. Multi valued Attributes: An attributes that may have multiple values for the same entity is known as multi valued attributes. For example colors of a car entity. Compound Attribute/Composite Attribute: Attribute can be subdivided into two or more other Attribute. For Example, Name can be divided into First name, Middle name and Last name. Simple Attributes/Atomic Attributes: The attributes which cannot be divided into smaller subparts are called simple or atomic attributes. For example, age of employee entity Stored Attribute: An attribute, which cannot be derived from other attribute, is known as stored attribute. For example, BirthDate of employee. Derived Attribute: Attributes derived from other stored attribute. For example, age from Date of Birth and Today’s date.  An attribute can be derived from a single attribute. Example age from DOB and current date.  An attribute can be derived from multiple attribute.  An entity can be derived from a separate table. Example, RNO Name DeptNO 1 A 1 2 B 1 3 C 2 4 D 3 5 E 1 ⇒ DNO Dname 1 CSE 2 IT 3 ECE Complex Attributes : If an attribute fr an entity, is built using composite and multivalued attributes, then these attributes are called complex attributes. For example, a person can have more than one residence and each residence can have multiple phones, an addressphone for a person entity can be specified as – {Addressphone (phone {(Area Code, Phone Number)}, Address(Sector Address (Sector Number,House Number), City, State, Pin))} Here {} are used to enclose multivalued attributes and () are used to enclose composite attributes with comma separating individual attributes./ Key Attribute : represents primary key. (main characteristics of an entity). It is an attribute, that has distinct value for each entity/element in an entity set. For example, Roll number in a Student Entity Type.
  • 15. Database Management System Prof. K. Adisesha 15 Non Key Attributes : These are attributes other than candidate key attributes in a table. For example Firstname is a non key attribute as it does not represent the main characteristics of the entity. Required Attribute : A required attribute is an attribute that must have a data value.These attributes are required because they describe what is important in the entity. For example, In a STUDENT entity, firstname and lastname is a required attribute. Optional Attribute/Null Value Attribute – An optional attribute may not have a value in it and can be left blank. For example, In a STUDENT entity, Middlename or email address is an optional attribute. as some students may not have middlename or email address. Notations Of Attributes in ER Diagram Attribute Key Attribute Multivalued Attribute Compound/Composite Attribute Derived Attribute Database Keys For the purposes of clarity we will refer to keys in terms of RDBMS tables but the same definition, principle and naming applies equally to Entity Modelling and Normalization. Keys are, as their name suggests, a key part of a relational database and a vital part of the structure of a table. They ensure each record within a table can be uniquely identified by one or a combination of fields within the table. They help enforce integrity and help identify the relationship between tables. There are three main types of keys, candidate keys, primary keys and foreign keys. There is also an alternative key or secondary key that can be used, as the name suggests, as a secondary or alternative key to the primary key Super Key A Super key is any combination of fields within a table that uniquely identifies each record within that table. Candidate Key A candidate is a subset of a super key. A candidate key is a single field or the least combination of fields that uniquely identifies each record in the table. The least combination of fields distinguishes a candidate key from a super key. Every table must have at least one candidate key but at the same time can have several.
  • 16. Database Management System Prof. K. Adisesha 16 As an example we might have a student_id that uniquely identifies the students in a student table. This would be a candidate key. But in the same table we might have the student’s first name and last name that also, when combined, uniquely identify the student in a student table. These would both be candidate keys. In order to be eligible for a candidate key it must pass certain criteria.  It must contain unique values  It must not contain null values  It contains the minimum number of fields to ensure uniqueness  It must uniquely identify each record in the table Once your candidate keys have been identified, you can now select one to be your primary key Primary Key A primary key is a candidate key that is most appropriate to be the main reference key for the table. As its name suggests, it is the primary key of reference for the table and is used throughout the database to help establish relationships with other tables. As with any candidate key the primary key must contain unique values, must never be null and uniquely identify each record in the table. As an example, a student id might be a primary key in a student table, a department code in a table of all departments in an organisation. This module has the code DH3D 35 that is no doubt used in a database somewhere to identify RDBMS as a unit in a table of modules. In the table below we have selected the candidate key student_id to be our most appropriate primary key Primary keys are mandatory for every table each record must have a value for its primary key. When choosing a primary key from the pool of candidate keys always choose a single simple key over a composite key.
  • 17. Database Management System Prof. K. Adisesha 17 Foreign Key A foreign key is generally a primary key from one table that appears as a field in another where the first table has a relationship to the second. In other words, if we had a table A with a primary key X that linked to a table B where X was a field in B, then X would be a foreign key in B. An example might be a student table that contains the course_id the student is attending. Another table lists the courses on offer with course_id being the primary key. The 2 tables are linked through course_id and as such course_id would be a foreign key in the student table. Secondary Key or Alternative Key A table may have one or more choices for the primary key. Collectively these are known as candidate keys as discuss earlier. One is selected as the primary key. Those not selected are known as secondary keys or alternative keys. For example in the table showing candidate keys above we identified two candidate keys, studentId and firstName + lastName. The studentId would be the most appropriate for a primary key leaving the other candidate key as secondary or alternative key. It should be noted for the other key to be candidate keys, we are assuming you will never have a person with the same first and last name combination. As this is unlikely we might consider fistName+lastName to be a suspect candidate key as it would be restrictive of the data you might enter. It would seem a shame to not allow John Smith onto a course just because there was already another John Smith. Simple Key Any of the keys described before (ie primary, secondary or foreign) may comprise one or more fields, for example if firstName and lastName was our key this would be a key of two fields where as studentId is only one. A simple key consists of a single field to uniquely identify a record. In addition the field in itself cannot be broken down into other fields, for example, studentId, which uniquely identifies a particular student, is a single field and therefore is a simple key. No two students would have the same student number. Compound Key A compound key consists of more than one field to uniquely identify a record. A compound key is distinguished from a composite key because each field, which makes up the primary key, is also a simple key in its own right. An example might be a table that represents the modules a student is attending. This table has a studentId and a moduleCode as its primary key. Each of the fields that make up the primary key are simple keys because each represents a unique reference when identifying a student in one instance and a module in the other.
  • 18. Database Management System Prof. K. Adisesha 18 Composite Key A composite key consists of more than one field to uniquely identify a record. This differs from a compound key in that one or more of the attributes, which make up the key, are not simple keys in their own right. Taking the example from compound key, imagine we identified a student by their firstName + lastName. In our table representing students on modules our primary key would now be firstName + lastName + moduleCode. Because firstName + lastName represent a unique reference to a student, they are not each simple keys, they have to be combined in order to uniquely identify the student. Therefore the key for this table is a composite key. Types of Relationships in ER Diagram Different types of relationships in er diagram are – 1) Relationships based on degree, 2) Recursive Relationship. Let us discuss these one by one. Relationship and Relationship Set : Relationships connect the entities and represent meaningful dependencies between them.It represents an association among several entities. Relationships sets is a set of relationships of the same type. It is a mathematical relation on entity sets (n>=2). Relationship set R is a subset of – {(r1,r2,r3,....rn)| r1∈E1, r2∈E2, rn∈En} where r1,r2,….rn are called relationships and E1,E2,….En are entity sets. The way in which two or more entity types are related is called relation type.For example, consider a relationship type WORKS_FOR between the two entity types EMPLOYEE and DEPARTMENT, which associates or links each employee with the department the employee works for. The WORKS_FOR relation type is shown as –
  • 19. Database Management System Prof. K. Adisesha 19 In the above figure, each instance of relation type WORKS_FOR i.e.(r1, r2,…,r5) is connected to instances of employee and department entities. Employee e1, e2 and e5 work for department d2 and employee e3 and e4 work for department d1. Notation to Represent Relation Type in ER Diagram- Relation types are represented as diamond shaped boxes. Degree of a Relationship Type- The number of participating entity types is known as the degree of relationship type. Types of Relationship Type Based on Degree –  Binary Relationship – A relationship type of degree two is called binary relationship. The WORKS_FOR in above figure is a binary relationship as there are two participating entities-employee and department. .Ternary Relationship- A relationship type of degree three is a ternary relationship for example, in the below figure supply relationship connects three entities SUPPLIER, PART AND PROJECT.
  • 20. Database Management System Prof. K. Adisesha 20 The above diagram can be read as – a supplier supplies the parts to projects N-ary Relationship Set – A relationship type of degree n is called n ary relationship . For example Cardinality/Mapping Cardinality- Cardinality expresses the number of entities to which another entity can be associated via a relationship set (or) It specifies the number of relationship instances that an entity can participate in a relation set.There are 4 types of Cardinality Ratios :  One-to-One Cardinality (1:1)  One-to-Many Cardinality (1:m)  Many-to-One Cardinality (m:1)  Many-to-Many Cardinality (m:n) Notations of Different Types of Cardinality In ER Diagram – One-to-One Cardinality (1:1) –
  • 21. Database Management System Prof. K. Adisesha 21 An entity in set A is associated with atmost one entity in B, and an entity in B is associated with atmost one entity in A. This type of cardinality is referred as one to one Cardinality. For example, an Employee as a Manager manage only one Department and the vice versa is also true as a department have only one Manager One to Many Cardinality (1:M) – An entity in A is associated with any number (0 or more) with an entity B, but a entity in B, however can be associated with atmost one entity in A. For example, An employee as a Manager can manage more than one Department. Many to One Cardinality (M:1) – An entity in A is associated with atmost one entity in B. An entity in B, however, can be associated with any number (0 or more) of entities in A. For example, An Employee can work only for one Department, But each Department can have 0 or more employees.
  • 22. Database Management System Prof. K. Adisesha 22 Many to Many Cardinality (M:N) – An entity in A is associated with any number (0 or more) of entities in B, and an entity in B is associated with any number( 0 or more) of entities in A. For example, An Employee can works on several Projects and a Project may have several Employees. ER diagram for a STUDENT database as below. Now it is a complete ER diagram for simple Student database.
  • 23. Database Management System Prof. K. Adisesha 23 Database Normalization Normalization is the process of removing redundant data from your tables in order to improve storage efficiency, data integrity and scalability. This improvement is balanced against an increase in complexity and potential performance losses from the joining of the normalized tables at query-time. There are two goals of the normalization process: eliminating redundant data (for example, storing the same data in more than one table) and ensuring data dependencies make sense (only storing related data in a table). Both of these are worthy goals as they reduce the amount of space a database consumes and ensure that data is logically stored. Use of Normalization Normalization is the aim of well design Relational Database Management System (RDBMS). It is step by step set of rules by which data is put in its simplest forms. We normalize the relational database management system because of the following reasons: • Minimize data redundancy i.e. no unnecessarily duplication of data. • To make database structure flexible i.e. it should be possible to add new data values and rows without reorganizing the database structure. • Data should be consistent throughout the database i.e. it should not suffer from following anomalies. • Insert Anomaly - Due to lack of data i.e., all the data available for insertion such that null values in keys should be avoided. This kind of anomaly can seriously damage a database • Update Anomaly - It is due to data redundancy i.e. multiple occurrences of same values in a column. This can lead to inefficiency. • Deletion Anomaly - It leads to loss of data for rows that are not stored else where. It could result in loss of vital data. • Complex queries required by the user should be easy to handle. On decomposition of a relation into smaller relations with fewer attributes on normalization the resulting relations whenever joined must result in the same relation without any extra rows. The join operations can be performed in any order. This is known as Lossless Join decomposition. The resulting relations (tables) obtained on normalization should possess the properties such as each row must be identified by a unique key, no repeating groups, homogenous columns, each column is assigned a unique name etc. Advantages of Normalization The following are the advantages of the normalization. • More efficient data structure. • Avoid redundant fields or columns. • More flexible data structure i.e. we should be able to add new rows and data values easily • Better understanding of data. • Ensures that distinct tables exist when necessary. • Easier to maintain data structure i.e. it is easy to perform operations and complex queries can be easily handled. • Minimizes data duplication. • Close modeling of real world entities, processes and their relationships.
  • 24. Database Management System Prof. K. Adisesha 24 Disadvantages of Normalization The following are disadvantages of normalization. • You cannot start building the database before you know what the user needs. • On Normalizing the relations to higher normal forms i.e. 4NF, 5NF the performance degrades. • It is very time consuming and difficult process in normalizing relations of higher degree. • Careless decomposition may leads to bad design of database which may leads to serious problems. Types of Normalization Here are the most commonly used normal forms:  First normal form(1NF)  Second normal form(2NF)  Third normal form(3NF)  Boyce & Codd normal form (BCNF) First normal form (1NF) 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: emp_id emp_name emp_address emp_mobile 101 Herschel New Delhi 8912312390 102 Jon Kanpur 8812121212 9900012222 103 Ron Chennai 7778881212 104 Lester Bangalore 9990000123 8123450987 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: emp_id emp_name emp_address emp_mobile 101 Herschel New Delhi 8912312390
  • 25. Database Management System Prof. K. Adisesha 25 102 Jon Kanpur 8812121212 102 Jon Kanpur 9900012222 103 Ron Chennai 7778881212 104 Lester Bangalore 9990000123 104 Lester Bangalore 8123450987 Second normal form (2NF) A table is said to be in 2NF if both the following conditions hold:  Table is in 1NF (First normal form)  No non-prime attribute is dependent on the proper subset of any candidate key of table. 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. teacher_id subject teacher_age 111 Maths 38 111 Physics 38 222 Biology 38 333 Physics 40 333 Chemistry 40 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: teacher_details table: teacher_id teacher_age 111 38 222 38 333 40 teacher_subject table:
  • 26. Database Management System Prof. K. Adisesha 26 teacher_id subject 111 Maths 111 Physics 222 Biology 333 Physics 333 Chemistry Now the tables comply with Second normal form (2NF). Third Normal form (3NF) A table design is said to be in 3NF if both the following conditions hold:  Table must be in 2NF  Transitive functional dependency of non-prime attribute on any super key should be removed. 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: emp_id emp_name emp_zip emp_state emp_city emp_district 1001 John 282005 UP Agra Dayal Bagh 1002 Ajeet 222008 TN Chennai M-City 1006 Lora 282007 TN Chennai Urrapakkam 1101 Lilly 292008 UK Pauri Bhagwan 1201 Steve 222999 MP Gwalior Ratan 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: employee table: emp_id emp_name emp_zip
  • 27. Database Management System Prof. K. Adisesha 27 1001 John 282005 1002 Ajeet 222008 1006 Lora 282007 1101 Lilly 292008 1201 Steve 222999 employee_zip table: emp_zip emp_state emp_city emp_district 282005 UP Agra Dayal Bagh 222008 TN Chennai M-City 282007 TN Chennai Urrapakkam 292008 UK Pauri Bhagwan 222999 MP Gwalior Ratan Boyce Codd normal form (BCNF) It is an advance version of 3NF that’s why it is also referred as 3.5NF. BCNF is stricter than 3NF. A table complies with BCNF if it is in 3NF and for every functional dependency X->Y, X should be the super key of the table. Example: Suppose there is a company wherein employees work in more than one department. They store the data like this: emp_id emp_nationality emp_dept dept_type dept_no_of_emp 1001 Austrian Production and planning D001 200 1001 Austrian stores D001 250 1002 American design and technical support D134 100 1002 American Purchasing department D134 600 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: emp_nationality table:
  • 28. Database Management System Prof. K. Adisesha 28 emp_id emp_nationality 1001 Austrian 1002 American emp_dept table: emp_dept dept_type dept_no_of_emp Production and planning D001 200 stores D001 250 design and technical support D134 100 Purchasing department D134 600 emp_dept_mapping table: emp_id emp_dept 1001 Production and planning 1001 stores 1002 design and technical support 1002 Purchasing department Functional dependencies: emp_id -> emp_nationality emp_dept -> {dept_type, dept_no_of_emp} Candidate keys: For first table: emp_id For second table: emp_dept For third table: {emp_id, emp_dept} This is now in BCNF as in both the functional dependencies left side part is a key. Database query language A database query language and report writer allows users to interactively interrogate the database, analyze its data and update it according to the users privileges on data. It also controls the security of the database. Data security prevents unauthorized users from viewing or updating the database. Using passwords, users are allowed access to the entire database or subsets of it called subschemas. For example, an employee database can contain all the data about an individual employee, but one group of users may be authorized to view only payroll data, while others are allowed access to only work history and medical data. If the DBMS provides a way to interactively enter and update the database, as well as interrogate it, this capability allows for managing personal databases. However, it may not leave an audit trail of actions or
  • 29. Database Management System Prof. K. Adisesha 29 provide the kinds of controls necessary in a multi-user organization. These controls are only available when a set of application programs are customized for each data entry and updating function. DBMS languages Database languages are used for read, update and store data in a database. There are several such languages that can be used for this purpose; one of them is SQL (Structured Query Language). Types of DBMS languages:  Data Definition Language (DDL)  Data Manipulation Language (DML)  Data Control language (DCL) Data Definition Language (DDL): DDL is used for specifying the database schema. Let’s take SQL for instance to categorize the statements that comes under DDL. To create the database instance – CREATE To alter the structure of database – ALTER To drop database instances – DROP To delete tables in a database instance – TRUNCATE To rename database instances – RENAME All these commands specify or update the database schema that is why they come under Data Definition language. Data Manipulation Language (DML): DML is used for accessing and manipulating data in a database. To read records from table(s) – SELECT To insert record(s) into the table(s) – INSERT Update the data in table(s) – UPDATE Delete all the records from the table – DELETE Data Control language (DCL): DCL is used for granting and revoking user access on a database –  To grant access to user – GRANT  To revoke access from user – REVOKE In practical data definition language, data manipulation language and data control languages are not separate language; rather they are the parts of a single database language such as SQL. SQL is short for Structured Query Language and is a widely used database language, providing means of data manipulation (store, retrieve, update, delete) and database creation. Almost all modern Relational Database Management Systems like MS SQL Server, Microsoft Access, MSDE, Oracle, DB2, Sybase, MySQL, Postgres and Informix use SQL as standard database language. Now a word of warning here, although all those RDBMS use SQL, they use different SQL dialects. For
  • 30. Database Management System Prof. K. Adisesha 30 example MS SQL Server specific version of the SQL is called T-SQL, Oracle version of SQL is called PL/SQL, MS Access version of SQL is called JET SQL, etc. SQL commands SQL> SELECT Learn how to use the SELECT SQL statement to retrieve data from a SQL database table. SQL> SELECT INTO Learn how to use the SQL SELECT INTO statement to copy data between database tables. SQL> DISTINCT Learn how to use the SQL DISTINCT clause together with the SQL SELECT keyword, to return a dataset with unique entries for certain database table column. SQL> WHERE The SQL WHERE command is used to specify selection criteria, thus restricting the result of a SQL query. SQL> LIKE The SQL LIKE clause is used along with the SQL WHERE clause and specifies criteria based on a string pattern. SQL> INSERT INTO Learn how to use the SQL INSERT INTO clause to insert data into a SQL database table. SQL> UPDATE Learn how to use the SQL UPDATE statement to update data in a SQL database table. SQL> DELETE Learn how to use the SQL DELETE statement to delete data from a SQL database table. SQL> ORDER BY Learn how to use the SQL ORDER BY statement to sort the data retrieved in your SQL query. SQL> OR & AND Learn how to use the SQL OR & AND keywords together with the SQL WHERE clause to add several conditions to your SQL statement. SQL> IN The SQL IN clause allows you to specify discrete values in your SQL WHERE search criteria. SQL> BETWEEN The SQL BETWEEN & AND keywords define a range of data between 2 values. SQL> Aliases SQL aliases can be used with database tables and/or with database table columns, depending on task you are performing. SQL> COUNT The SQL COUNT aggregate function is used to count the number of rows in a database table.
  • 31. Database Management System Prof. K. Adisesha 31 SQL> MAX The SQL MAX aggregate function allows us to select the highest (maximum) value for a certain column. SQL> MIN The SQL MIN aggregate function allows us to select the lowest (minimum) value for a certain column. SQL> AVG The SQL AVG aggregate function selects the average value for a certain table column. SQL> SUM The SQL SUM aggregate function allows selecting the total for a numeric column. SQL> GROUP BY The SQL GROUP BY statement is used along with the SQL aggregate functions like SUM to provide means of grouping the result dataset by certain database table column(s). SQL> HAVING The SQL HAVING clause is used to restrict conditionally the output of a SQL statement, by a SQL aggregate function used in your SELECT list of columns. SQL> JOIN The SQL JOIN clause is used whenever we have to select data from 2 or more tables. The SQL SELECT statement is used to select data from a SQL database table. This is usually the very first SQL command every SQL newbie learns and this is because the SELECT SQL statement is one of the most used SQL commands. Please have a look at the general SQL SELECT syntax: SELECT Column1, Column2, Column3, FROM Table1 The list of column names after the SQL SELECT command determines which columns you want to be returned in your result set. If you want to select all columns from a database table, you can use the following SQL statement: SELECT * FROM Table1 When the list of columns following the SELECT SQL command is replaced with asterix (*) all table columns are returned. Word of caution here, it’s always better to explicitly specify the columns in the SELECT list, as this will improve your query performance significantly. The table name following the SQL FROM keyword (in our case Table1) tells the SQL interpreter which table to use to retrieve the data. The SQL SELECT INTO statement is used to select data from a SQL database table and to insert it to a different table at the same time.
  • 32. Database Management System Prof. K. Adisesha 32 The general SQL SELECT INTO syntax looks like this: SELECT Column1, Column2, Column3, INTO Table2 FROM Table1 The list of column names after the SQL SELECT command determines which columns will be copied, and the table name after the SQL INTO keyword specifies to which table to copy those rows. If we want to make an exact copy of the data in our Customers table, we need the following SQL SELECT INTO statement: SELECT * INTO Customers_copy FROM Customers The SQL DISTINCT clause is used together with the SQL SELECT keyword, to return a dataset with unique entries for certain database table column. We will use our Customers database table to illustrate the usage of SQL DISTINCT. FirstName LastName Email DOB Phone Sunny Kumar sunny.kumar@yahoo.com 2/4/1968 626 222-2222 Prajwal Goldfish goldfish@fishhere.net 4/4/1974 323 455-4545 Paula Brown pb@herowndomain.org 5/24/1978 416 323-3232 James Kumar jim@supergig.co.in 20/10/1980 416 323-8888 For example if we want to select all distinct surnames from our Customers table, we will use the following SQL DISTINCT statement: SELECT DISTINCT LastName FROM Customers The result of the SQL DISTINCT expression above will look like this: LastName Kumar Goldfish Brown The SQL WHERE clause is used to select data conditionally, by adding it to already existing SQL SELECT query. We are going to use the Customers table from the previous chapter, to illustrate the use of the SQL WHERE command. Table: Customers FirstName LastName Email DOB Phone
  • 33. Database Management System Prof. K. Adisesha 33 Sunny Kumar Sunny.Kumar@yahoo.com 2/4/1968 626 222-2222 Prajwal Goldfish goldfish@fishhere.net 4/4/1974 323 455-4545 Paula Brown pb@herowndomain.org 5/24/1978 416 323-3232 If we want to select all customers from our database table, having last name 'Kumar' we need to use the following SQL syntax: SELECT * FROM Customers WHERE LastName = 'Kumar' The result of the SQL expression above will be the following: FirstName LastName Email DOB Phone Sunny Kumar sunny.kumar@yahoo.com 2/4/1968 626 222-2222 James Kumar jim@supergig.co.in 20/10/1980 416 323-8888 In this simple SQL query we used the "=" (Equal) operator in our WHERE criteria: LastName = 'Kumar' But we can use any of the following comparison operators in conjunction with the SQL WHERE clause: <> (Not Equal) SELECT * FROM Customers WHERE LastName <> 'Kumar' > (Greater than) SELECT * FROM Customers WHERE DOB > '1/1/1970' >= (Greater or Equal) SELECT * FROM Customers WHERE DOB >= '1/1/1970' < (Less than) SELECT * FROM Customers WHERE DOB < '1/1/1970' <= (Less or Equal) SELECT * FROM Customers WHERE DOB =< '1/1/1970'
  • 34. Database Management System Prof. K. Adisesha 34 LIKE (similar to) SELECT * FROM Customers WHERE Phone LIKE '626%' Note the LIKE syntax is different with the different RDBMS (SQL Server syntax used above). Check the SQL LIKE article for more details. Between (Defines a range) SELECT * FROM Customers WHERE DOB BETWEEN '1/1/1970' AND '1/1/1975' We will use the Customers table to illustrate the SQL LIKE clause usage: FirstName LastName Email DOB Phone Sunny Kumar sunny.kumar@yahoo.com 2/4/1968 626 222-2222 Prajwal Goldfish goldfish@fishhere.net 4/4/1974 323 455-4545 Paula Brown pb@herowndomain.org 5/24/1978 416 323-3232 James Kumar jim@supergig.co.in 20/10/1980 416 323-8888 The SQL LIKE clause is very useful when you want to specify a search condition within your SQL WHERE clause, based on a part of a column contents. For example if you want to select all customers having FirstName starting with 'J' you need to use the following SQL statement: SELECT * FROM Customers WHERE FirstName LIKE 'J%' Here is the result of the SQL statement above: FirstName LastName Email DOB Phone Sunny Kumar Sunny.kumar@yahoo.com 2/4/1968 626 222-2222 James Kumar jim@supergig.co.in 20/10/1980 416 323-8888 If you want to select all Customers with phone numbers starting with '416' you will use this SQL expression: SELECT * FROM Customers WHERE Phone LIKE '416%' The '%' is a so called wildcard character and represents any string in our pattern. You can put the wildcard anywhere in the string following the SQL LIKE clause and you can put as many wildcards as you like too.
  • 35. Database Management System Prof. K. Adisesha 35 Note that different databases use different characters as wildcard characters, for example '%' is a wildcard character for MS SQL Server representing any string, and '*' is the corresponding wildcard character used in MS Access. Another wildcard character is '_' representing any single character. The '[]' specifies a range of characters. Have a look at the following SQL statement: SELECT * FROM Customers WHERE Phone LIKE '[4-6]_6%' This SQL expression will return all customers satisfying the following conditions: The Phone column starts with a digit between 4 and 6 ([4-6]) Second character in the Phone column can be anything (_) The third character in the Phone column is 6 (6) The remainder of the Phone column can be any character string (%) Here is the result of this SQL expression: FirstName LastName Email DOB Phone Sunny Kumar sunny.kumar@yahoo.com 2/4/1968 626 222-2222 Paula Brown pb@herowndomain.org 5/24/1978 416 323-3232 James Kumar jim@supergig.co.in 20/10/1980 416 323-8888 The SQL INSERT INTO syntax has 2 main forms and the result of either of them is adding a new row into the database table. The first syntax form of the INSERT INTO SQL clause doesn't specify the column names where the data will be inserted, but just their values: INSERT INTO Table1 VALUES (value1, value2, value3…) The second form of the SQL INSERT INTO command, specifies both the columns and the values to be inserted in them: INSERT INTO Table1 (Column1, Column2, Column3…) VALUES (Value1, Value2, Value3…) As you might already have guessed, the number of the columns in the second INSERT INTO syntax form must match the number of values into the SQL statement, otherwise you will get an error. If we want to insert a new row into our Customers table, we are going to use one of the following 2 SQL statements:
  • 36. Database Management System Prof. K. Adisesha 36 INSERT INTO Customers VALUES ('Adisesha', 'HOD', 'adisesha.HOD@tgmail.net', '1/1/1974', '626 888-8888') INSERT INTO Customers (FirstName, LastName, Email, DOB, Phone) VALUES ('Adisesha', 'HOD', 'adisesha.HOD@tgmail.net', '1/1/1974', '626 888- 8888') The result of the execution of either of the 2 INSERT INTO SQL statements will be a new row added to our Customers database table: FirstName LastName Email DOB Phone Sunny Kumar Sunny.Kumar@yahoo.com 2/4/1968 626 222-2222 Prajwal Goldfish goldfish@fishhere.net 4/4/1974 323 455-4545 Paula Brown pb@herowndomain.org 5/24/1978 416 323-3232 James Kumar jim@supergig.co.in 20/10/1980 416 323-8888 Adisesha HOD adisesha.hod@gmail.net 1/1/1974 626 888-8888 If you want to enter data for just a few of the table columns, you’ll have to use the second syntax form of the SQL INSERT INTO clause, because the first form will produce an error if you haven’t supplied values for all columns. To insert only the FirstName and LastName columns, execute the following SQL statement: INSERT INTO Customers (FirstName, LastName) VALUES ('Adisesha', 'HOD') The SQL UPDATE general syntax looks like this: UPDATE Table1 SET Column1 = Value1, Column2 = Value2 WHERE Some_Column = Some_Value The SQL UPDATE clause changes the data in already existing database row(s) and usually we need to add a conditional SQL WHERE clause to our SQL UPDATE statement in order to specify which row(s) we intend to update. If we want to update the Mr. Prajwal Goldfish's date of birth to '5/10/1974' in our Customers database table FirstName LastName Email DOB Phone Sunny Kumar Sunny.Kumar@yahoo.com 2/4/1968 626 222-2222 Prajwal Goldfish goldfish@fishhere.net 4/4/1974 323 455-4545 Paula Brown pb@herowndomain.org 5/24/1978 416 323-3232
  • 37. Database Management System Prof. K. Adisesha 37 James Kumar jim@supergig.co.in 20/10/1980 416 323-8888 we need the following SQL UPDATE statement: UPDATE Customers SET DOB = '5/10/1974' WHERE LastName = 'Goldfish' AND FirstName = 'Prajwal' If we don’t specify a WHERE clause in the SQL expression above, all customers' DOB will be updated to '5/10/1974', so be careful with the SQL UPDATE command usage. We can update several database table rows at once, by using the SQL WHERE clause in our UPDATE statement. For example if we want to change the phone number for all customers with last name Kumar (we have 2 in our example Customers table), we need to use the following SQL UPDATE statement: UPDATE Customers SET Phone = '626 555-5555' WHERE LastName = 'Kumar' After the execution of the UPDATE SQL expression above, the Customers table will look as follows: FirstName LastName Email DOB Phone Sunny Kumar Sunny.Kumar@yahoo.com 2/4/1968 626 555-5555 Prajwal Goldfish goldfish@fishhere.net 4/4/1974 323 455-4545 Paula Brown pb@herowndomain.org 5/24/1978 416 323-3232 James Kumar jim@supergig.co.in 20/10/1980 626 555-5555 So far we’ve learnt how to select data from a database table and how to insert and update data into a database table. Now it’s time to learn how to remove data from a database. Here comes the SQL DELETE statement! The SQL DELETE command has the following generic SQL syntax: DELETE FROM Table1 WHERE Some_Column = Some_Value If you skip the SQL WHERE clause when executing SQL DELETE expression, then all the data in the specified table will be deleted. The following SQL statement will delete all the data from our Customers table and we’ll end up with completely empty table: DELETE FROM Table1 If you specify a WHERE clause in your SQL DELETE statement, only the table rows satisfying the WHERE criteria will be deleted:
  • 38. Database Management System Prof. K. Adisesha 38 DELETE FROM Customers WHERE LastName = 'Kumar' The SQL query above will delete all database rows having LastName 'Kumar' and will leave the Customers table in the following state: FirstName LastName Email DOB Phone Prajwal Goldfish goldfish@fishhere.net 4/4/1974 323 455-4545 Paula Brown pb@herowndomain.org 5/24/1978 416 323-3232 The SQL ORDER BY clause comes in handy when you want to sort your SQL result sets by some column(s). For example if you want to select all the persons from the already familiar Customers table and order the result by date of birth, you will use the following statement: SELECT * FROM Customers ORDER BY DOB The result of the above SQL expression will be the following: FirstName LastName Email DOB Phone Sunny Kumar Sunny.Kumar@yahoo.com 2/4/1968 626 222-2222 Prajwal Goldfish goldfish@fishhere.net 4/4/1974 323 455-4545 Paula Brown pb@herowndomain.org 5/24/1978 416 323-3232 James Kumar jim@supergig.co.in 20/10/1980 416 323-8888 As you can see the rows are sorted in ascending order by the DOB column, but what if you want to sort them in descending order? To do that you will have to add the DESC SQL keyword after your SQL ORDER BY clause: SELECT * FROM Customers ORDER BY DOB DESC The result of the SQL query above will look like this: FirstName LastName Email DOB Phone James Kumar jim@supergig.co.in 20/10/1980 416 323- 8888 Paula Brown pb@herowndomain.org 5/24/1978 416 323- 3232 Prajwal Goldfish goldfish@fishhere.net 4/4/1974 323 455- 4545
  • 39. Database Management System Prof. K. Adisesha 39 Sunny Kumar Sunny.Kumar@yahoo.com 2/4/1968 626 222- 2222 If you don't specify how to order your rows, alphabetically or reverse, than the result set is ordered alphabetically, hence the following to SQL expressions produce the same result: SELECT * FROM Customers ORDER BY DOB SELECT * FROM Customers ORDER BY DOB ASC You can sort your result set by more than one column by specifying those columns in the SQL ORDER BY list. The following SQL expression will order by DOB and LastName: SELECT * FROM Customers ORDER BY DOB, LastName The SQL AND clause is used when you want to specify more than one condition in your SQL WHERE clause, and at the same time you want all conditions to be true. For example if you want to select all customers with FirstName "Sunny" and LastName "Kumar", you will use the following SQL expression: SELECT * FROM Customers WHERE FirstName = 'Sunny' AND LastName = 'Kumar' The result of the SQL query above is: FirstName LastName Email DOB Phone Sunny Kumar Sunny.Kumar@yahoo.com 2/4/1968 626 222-2222 The following row in our Customer table, satisfies the second of the conditions (LastName = 'Kumar'), but not the first one (FirstName = 'Sunny'), and that's why it's not returned by our SQL query: FirstName LastName Email DOB Phone James Kumar jim@supergig.co.in 20/10/1980 416 323-8888 The SQL OR statement is used in similar fashion and the major difference compared to the SQL AND is that OR clause will return all rows satisfying any of the conditions listed in the WHERE clause. If we want to select all customers having FirstName 'James' or FirstName 'Paula' we need to use the following SQL statement: SELECT * FROM Customers WHERE FirstName = 'James' OR FirstName = 'Paula' The result of this query will be the following: FirstName LastName Email DOB Phone
  • 40. Database Management System Prof. K. Adisesha 40 Paula Brown pb@herowndomain.org 5/24/1978 416 323-3232 James Kumar jim@supergig.co.in 20/10/1980 416 323-8888 You can combine AND and OR clauses anyway you want and you can use parentheses to define your logical expressions. Here is an example of such a SQL query, selecting all customers with LastName 'Brown' and FirstName either 'James' or 'Paula': SELECT * FROM Customers WHERE (FirstName = 'James' OR FirstName = 'Paula') AND LastName = 'Brown' The result of the SQL expression above will be: FirstName LastName Email DOB Phone Paula Brown pb@herowndomain.org 5/24/1978 416 323-3232 The SQL IN clause allows you to specify discrete values in your SQL WHERE search criteria. THE SQL IN syntax looks like this: SELECT Column1, Column2, Column3, … FROM Table1 WHERE Column1 IN (Valu1, Value2, …) Lets use the EmployeeHours table to illustrate how SQL IN works: Employee Date Hours Sunny Kumar 5/6/2004 8 Allan Babel 5/6/2004 8 Tina Crown 5/6/2004 8 Sunny Kumar 5/7/2004 9 Allan Babel 5/7/2004 8 Tina Crown 5/7/2004 10 Sunny Kumar 5/8/2004 8 Allan Babel 5/8/2004 8 Tina Crown 5/8/2004 9 Consider the following SQL query using the SQL IN clause:
  • 41. Database Management System Prof. K. Adisesha 41 SELECT * FROM EmployeeHours WHERE Date IN ('5/6/2004', '5/7/2004') This SQL expression will select only the entries where the column Date has value of '5/6/2004' or '5/7/2004', and you can see the result below: Employee Date Hours Sunny Kumar 5/6/2004 8 Allan Babel 5/6/2004 8 Tina Crown 5/6/2004 8 Sunny Kumar 5/7/2004 9 Allan Babel 5/7/2004 8 Tina Crown 5/7/2004 10 We can use the SQL IN statement with another column in our EmployeeHours table: SELECT * FROM EmployeeHours WHERE Hours IN (9, 10) The result of the SQL query above will be: Employee Date Hours Sunny Kumar 5/7/2004 9 Tina Crown 5/7/2004 10 Tina Crown 5/8/2004 9 The SQL BETWEEN & AND keywords define a range of data between 2 values. The SQL BETWEEN syntax looks like this: SELECT Column1, Column2, Column3, … FROM Table1 WHERE Column1 BETWEEN Value1 AND Value2 The 2 values defining the range for SQL BETWEEN clause can be dates, numbers or just text. In contrast with the SQL IN keyword, which allows you to specify discrete values in your SQL WHERE criteria, the SQL BETWEEN gives you the ability to specify a range in your search criteria. We are going to use the familiar Customers table to show how SQL BETWEEN works:
  • 42. Database Management System Prof. K. Adisesha 42 FirstName LastName Email DOB Phone Sunny Kumar Sunny.Kumar@yahoo.com 2/4/1968 626 222-2222 Prajwal Goldfish goldfish@fishhere.net 4/4/1974 323 455-4545 Paula Brown pb@herowndomain.org 5/24/1978 416 323-3232 James Kumar jim@supergig.co.in 20/10/1980 416 323-8888 Consider the following SQL BETWEEN statement: SELECT * FROM Customers WHERE DOB BETWEEN '1/1/1975' AND '1/1/2004' The SQL BETWEEN statement above will select all Customers having DOB column between '1/1/1975' and '1/1/2004' dates. Here is the result of this SQL expression: FirstName LastName Email DOB Phone Paula Brown pb@herowndomain.org 5/24/1978 416 323-3232 James Kumar jim@supergig.co.in 20/10/1980 416 323-8888 SQL aliases can be used with database tables and with database table columns, depending on task you are performing. SQL column aliases are used to make the output of your SQL queries easy to read and more meaningful: SELECT Employee, SUM(Hours) As SumHoursPerEmployee FROM EmployeeHours GROUP BY Employee In the example above we created SQL alias SumHoursPerEmployee and the result of this SQL query will be the following: Employee SumHoursPerEmployee Sunny Kumar 25 Allan Babel 24 Tina Crown 27 Consider the following SQL statement, showing how to use SQL table aliases: SELECT Emp.Employee FROM EmployeeHours AS Emp
  • 43. Database Management System Prof. K. Adisesha 43 Here is the result of the SQL expression above: Employee Sunny Kumar Allan Babel Tina Crown The SQL table aliases are very useful when you select data from multiple tables. The SQL COUNT aggregate function is used to count the number of rows in a database table. The SQL COUNT syntax is simple and looks like this: SELECT COUNT(Column1) FROM Table1 If we want to count the number of customers in our Customers table, we will use the following SQL COUNT statement: SELECT COUNT(LastName) AS NumberOfCustomers FROM Customers The result of this SQL COUNT query will be: NumberOfCustomers 4 The SQL MAX aggregate function allows us to select the highest (maximum) value for a certain column. The SQL MAX function syntax is very simple and it looks like this: SELECT MAX(Column1) FROM Table1 If we use the Customers table from our previous chapters, we can select the highest date of birth with the following SQL MAX expression: SELECT MAX(DOB) AS MaxDOB FROM Customers The SQL MIN aggregate function allows us to select the lowest (minimum) value for a certain column. The SQL MIN function syntax is very simple and it looks like this: SELECT MIN(Column1) FROM Table1 If we use the Customers table from our previous chapters, we can select the lowest date of birth with the following SQL MIN expression:
  • 44. Database Management System Prof. K. Adisesha 44 SELECT MIN(DOB) AS MinDOB FROM Customers The SQL AVG aggregate function selects the average value for certain table column. Have a look at the SQL AVG syntax: SELECT AVG(Column1) FROM Table1 If we want to find out what is the average SaleAmount in the Sales table, we will use the following SQL AVG statement: SELECT AVG(SaleAmount) AS AvgSaleAmount FROM Sales which will result in the following dataset: AvgSaleAmount $195.73 The SQL SUM aggregate function allows selecting the total for a numeric column. The SQL SUM syntax is displayed below: SELECT SUM(Column1) FROM Table1 We are going to use the Sales table to illustrate the use of SQL SUM clause: Sales: CustomerID Date SaleAmount 2 5/6/2004 $100.22 1 5/7/2004 $99.95 3 5/7/2004 $122.95 3 5/13/2004 $100.00 4 5/22/2004 $555.55 Consider the following SQL SUM statement: SELECT SUM(SaleAmount) FROM Sales This SQL statement will return the sum of all SaleAmount fields and the result of it will be: SaleAmount $978.67
  • 45. Database Management System Prof. K. Adisesha 45 Of course you can specify search criteria using the SQL WHERE clause in your SQL SUM statement. If you want to select the total sales for customer with CustomerID = 3, you will use the following SQL SUM statement: SELECT SUM(SaleAmount) FROM Sales WHERE CustomerID = 3 The result will be: SaleAmount $222.95 The SQL GROUP BY statement is used along with the SQL aggregate functions like SUM to provide means of grouping the result dataset by certain database table column(s). The best way to explain how and when to use the SQL GROUP BY statement is by example, and that’s what we are going to do. Consider the following database table called EmployeeHours storing the daily hours for each employee of a factious company: Employee Date Hours Sunny Kumar 5/6/2004 8 Allan Babel 5/6/2004 8 Tina Crown 5/6/2004 8 Sunny Kumar 5/7/2004 9 Allan Babel 5/7/2004 8 Tina Crown 5/7/2004 10 Sunny Kumar 5/8/2004 8 Allan Babel 5/8/2004 8 Tina Crown 5/8/2004 9 If the manager of the company wants to get the simple sum of all hours worked by all employees, he needs to execute the following SQL statement: SELECT SUM (Hours) FROM EmployeeHours But what if the manager wants to get the sum of all hours for each of his employees? To do that he need to modify his SQL query and use the SQL GROUP BY statement: SELECT Employee, SUM (Hours) FROM EmployeeHours GROUP BY Employee The result of the SQL expression above will be the following: Employee Hours
  • 46. Database Management System Prof. K. Adisesha 46 Sunny Kumar 25 Allan Babel 24 Tina Crown 27 As you can see we have only one entry for each employee, because we are grouping by the Employee column. The SQL GROUP BY clause can be used with other SQL aggregate functions, for example SQL AVG: SELECT Employee, AVG(Hours) FROM EmployeeHours GROUP BY Employee The result of the SQL statement above will be: Employee Hours Sunny Kumar 8.33 Allan Babel 8 Tina Crown 9 In our Employee table we can group by the date column too, to find out what is the total number of hours worked on each of the dates into the table: SELECT Date, SUM(Hours) FROM EmployeeHours GROUP BY Date Here is the result of the above SQL expression: Date Hours 5/6/2004 24 5/7/2004 27 5/8/2004 25 The SQL HAVING clause is used to restrict conditionally the output of a SQL statement, by a SQL aggregate function used in your SELECT list of columns. You can't specify criteria in a SQL WHERE clause against a column in the SELECT list for which SQL aggregate function is used. For example the following SQL statement will generate an error: SELECT Employee, SUM (Hours) FROM EmployeeHours WHERE SUM (Hours) > 24 GROUP BY Employee
  • 47. Database Management System Prof. K. Adisesha 47 The SQL HAVING clause is used to do exactly this, to specify a condition for an aggregate function which is used in your query: SELECT Employee, SUM (Hours) FROM EmployeeHours GROUP BY Employee HAVING SUM (Hours) > 24 The above SQL statement will select all employees and the sum of their respective hours, as long as this sum is greater than 24. The result of the SQL HAVING clause can be seen below: Employee Hours Sunny Kumar 25 Tina Crown 27 The SQL JOIN clause is used whenever we have to select data from 2 or more tables. To be able to use SQL JOIN clause to extract data from 2 (or more) tables, we need a relationship between certain columns in these tables. We are going to illustrate our SQL JOIN example with the following 2 tables: Customers: CustomerID FirstName LastName Email DOB Phone 1 Sunny Kumar Sunny.Kumar@yahoo.com 2/4/1968 626 222-2222 2 Prajwal Goldfish goldfish@fishhere.net 4/4/1974 323 455-4545 3 Paula Brown pb@herowndomain.org 5/24/1978 416 323-3232 4 James Kumar jim@supergig.co.in 20/10/1980 416 323-8888 Sales: CustomerID Date SaleAmount 2 5/6/2004 $100.22 1 5/7/2004 $99.95 3 5/7/2004 $122.95 3 5/13/2004 $100.00 4 5/22/2004 $555.55 As you can see those 2 tables have common field called CustomerID and thanks to that we can extract information from both tables by matching their CustomerID columns. Consider the following SQL statement:
  • 48. Database Management System Prof. K. Adisesha 48 SELECT Customers.FirstName, Customers.LastName, SUM(Sales.SaleAmount) AS SalesPerCustomer FROM Customers, Sales WHERE Customers.CustomerID = Sales.CustomerID GROUP BY Customers.FirstName, Customers.LastName The SQL expression above will select all distinct customers (their first and last names) and the total respective amount of dollars they have spent. The SQL JOIN condition has been specified after the SQL WHERE clause and says that the 2 tables have to be matched by their respective CustomerID columns. Here is the result of this SQL statement: FirstName LastName SalesPerCustomers Sunny Kumar $99.95 Prajwal Goldfish $100.22 Paula Brown $222.95 James Kumar $555.55 The SQL statement above can be re-written using the SQL JOIN clause like this: SELECT Customers.FirstName, Customers.LastName, SUM(Sales.SaleAmount) AS SalesPerCustomer FROM Customers JOIN Sales ON Customers.CustomerID = Sales.CustomerID GROUP BY Customers.FirstName, Customers.LastName There are 2 types of SQL JOINS – INNER JOINS and OUTER JOINS. If you don't put INNER or OUTER keywords in front of the SQL JOIN keyword, then INNER JOIN is used. In short "INNER JOIN" = "JOIN" (note that different databases have different syntax for their JOIN clauses). The INNER JOIN will select all rows from both tables as long as there is a match between the columns we are matching on. In case we have a customer in the Customers table, which still hasn't made any orders (there are no entries for this customer in the Sales table), this customer will not be listed in the result of our SQL query above. If the Sales table has the following rows: CustomerID Date SaleAmount 2 5/6/2004 $100.22 1 5/6/2004 $99.95 And we use the same SQL JOIN statement from above:
  • 49. Database Management System Prof. K. Adisesha 49 SELECT Customers.FirstName, Customers.LastName, SUM(Sales.SaleAmount) AS SalesPerCustomer FROM Customers JOIN Sales ON Customers.CustomerID = Sales.CustomerID GROUP BY Customers.FirstName, Customers.LastName We'll get the following result: FirstName LastName SalesPerCustomers Sunny Kumar $99.95 Prajwal Goldfish $100.22 Even though Paula and James are listed as customers in the Customers table they won't be displayed because they haven't purchased anything yet. But what if you want to display all the customers and their sales, no matter if they have ordered something or not? We’ll do that with the help of SQL OUTER JOIN clause. The second type of SQL JOIN is called SQL OUTER JOIN and it has 2 sub-types called LEFT OUTER JOIN and RIGHT OUTER JOIN. The LEFT OUTER JOIN or simply LEFT JOIN (you can omit the OUTER keyword in most databases), selects all the rows from the first table listed after the FROM clause, no matter if they have matches in the second table. If we slightly modify our last SQL statement to: SELECT Customers.FirstName, Customers.LastName, SUM(Sales.SaleAmount) AS SalesPerCustomer FROM Customers LEFT JOIN Sales ON Customers.CustomerID = Sales.CustomerID GROUP BY Customers.FirstName, Customers.LastName and the Sales table still has the following rows: CustomerID Date SaleAmount 2 5/6/2004 $100.22 1 5/6/2004 $99.95 The result will be the following: FirstName LastName SalesPerCustomers Sunny Kumar $99.95 Prajwal Goldfish $100.22
  • 50. Database Management System Prof. K. Adisesha 50 Paula Brown NULL James Kumar NULL As you can see we have selected everything from the Customers (first table). For all rows from Customers, which don’t have a match in the Sales (second table), the SalesPerCustomer column has amount NULL (NULL means a column contains nothing). The RIGHT OUTER JOIN or just RIGHT JOIN behaves exactly as SQL LEFT JOIN, except that it returns all rows from the second table (the right table in our SQL JOIN statement). SQL Hosting is a web hosting plan featuring one or more SQL databases. Having SQL database backend is essential for most of the modern websites, that’s why almost all web hosting providers offer SQL hosting plans nowadays. The most commonly used SQL hosting databases are MySQL, MS SQL Server 2000/ MS SQL Server 2005, MS Access and Oracle. Which SQL database to choose for your hosting? You can have SQL hosting on both Windows and Linux/UNIX operating systems, but some of the database engines run only on one of them. For example if you want SQL hosting with MS SQL Server 2000/2005 backend, then you need to look for Windows hosting plan, because SQL Server runs only on Windows. If you are looking for MySQL hosting then you are in luck, because MySQL runs on both Windows and Linux. Oracle also runs on both operating systems, but MS Access hosting requires Windows OS. What are the advantages of SQL hosting? Having SQL Hosting backend allows for developing robust and scalable web applications, which can handle thousands of visitors daily. When the website content is in a SQL database, it’s very easy to search the content for particular keyword or phrase. Having your data in a RDBMS allows for easy management of the content. Also when a website utilizes SQL hosting, the content of the website is separated by its presentation, which makes it really easy to apply design and layout changes.