Dbms Notes
Dbms Notes
Dbms Notes
System
By,
Mr. Rahul Shyam
(BCA, B.Ed, MCA, M.Tech(CSE), Ph.D(Pursuing))
Assistant Professor
Dept. of Computer Science & Engineering
Basic Definitions
Data:
Data is a collection of facts, figures and statistics that
can be recorded and have an implicit meaning.
e.g., names, telephone numbers, addresses.
Information: information is a processed data with implicit
meaning.
Banking system
Hospital management system
University database
Library database
Airline reservations system etc.
Some of the commercial databases are
Amazon ,Flipcart, facebook, WhatsApp etc.
Note: Earlier data was maintained using
traditional file processing system .
i.e., flies of records.
An example of large commercial database is Amazon.com ,
which contains a data of over 200 million books, CDs ,
Videos, DVDs, games, electronics, apparel & other items.
In this database , data is stored on more than 200 servers.
About 100 million visitors access Amazon.com each day &
use the database to make purchases. And the database is
continually updated.
About 500 people are responsible for keeping
the Amazon.com database up to date.
Drawback of File processing system
1. Data Redundancy
2. Data dependency
3. Lack of flexibility
4. Data security
5. Data isolation
Database System vs. File System
DBMS ??
Database System
Database system is a combination of both database & DBMS software
DBMS Functionality
The DBMS is a general purpose software system ,basic functions of DBMS
are
1. Defining,
2. Constructing and
3. Manipulating databases for various applications.
Other features or functionalities are
Protection or Security measures to prevent unauthorized access
Presentation and Visualization of data
Maintaining the database and associated programs over the lifetime of the
database applications.
1.Defining a database involves specifying the data types, structures and
constraints for the data to be stored in the database.
Ex: Let us construct the process of creating the EMPLOYEE database for
a typical organization. The first step is to , defining a database
involves the following steps:
Specifying the data types and structures of each field in the database.
Identifying the constraints on different elements.
Entity Set Attribute Data type Constraints
Emp_address Char(50) -
EMPLOYEE
Emp_designation Char(10) -
Emp_department Char(10) -
Emp_salary Number(6,2) -
2.Constructing the database is a process of storing the data itself on some storage
medium that is controlled by DBMS.
Ex: EMPLOYEE
SL No DBMS COMPANY
1 IMS IBM
7 SUPRA Cincom
Different RDBMS’
MS SQL Server
3. Data abstraction.
4.Supports multiple views of data
5.Sharing of data & multi-user transaction processing
Characteristics of the Database Approach
system:
The database system not only contains the data , but also the description of
the data.
2.Insulation between programs and data:
Called program-data independence.
Allows changing data structures and storage
organization without having to change the
DBMS access programs.
PDI [ Program Data Independence ]
In database system environment , if both data &
program is stored independent , it is known as
Program Data Independence.
POI [ Program Operation Independence ]
In database system environment , if both data &
program is operated independent , it is known as
Program Operation Independence.
Characteristics of the Database Approach
(continued)
3.Data Abstraction:
The characteristic that allows program-data
independence and program-operation
independence called data abstraction.
A data model is used to hide storage details
and present the users with a conceptual view
of the database.
4.Support of multiple views of the data:
Each user may see a different view of the
database, which describes only the data of
interest to that user.
Characteristics of the Database Approach (continued)
iii) Sophisticated:
This group includes Engineers , Scientists , Business analysts etc ,
They are thoroughly familiar them selves with the facilities provided by the DBMS , in
order to implement their applications to meet their complex requirements.
iv)Stand-alone end users
Stand-alone end-users Mostly maintain personal databases using ready-to-use packaged
applications.
II “Workers Behind the Scene”.
Those who design and develop the DBMS software and
related tools, and the computer systems operators
called “Workers Behind the Scene”
They are.
1. DBMS System Designers and Implementers
2. Tool Developers
3. Operators and Maintenance Personnel
1.DBMS System Designers and Implementers
Database designers are responsible for identifying the data to be stored in the database by
choosing appropriate structure to represent & store this data .
2.Tool Developers
Tools are the software packages that facilitate database modeling & design and also
improves the performance of the database systems
In many cases , independent software vendors develop & market these tools.
3.Operators & maintenance personnel
They are responsible for actual running & maintenance of the hardware & software
environment for the database system.
****Implications(significance) of Database Approach
1. Controlling Redundancy
2. Restricting unauthorized access of database
3. Providing persistent storage of program objects
4. Provides storage structure for efficient query processing
5. Provides Back up & recovery
6. Provides multiple user interfaces
7.Representing complex relationship among data
8.Enforcing integrity constraints
1.Controlling Redundancy
Redundancy leads to several problems such as “Duplication of efforts ” & inconsistency
Storage space is wasted when same data is stored repeatedly
2.Restricting unauthorized access of database
Depending on the type of data only limited users are allowed to access the database
3. Providing persistent ( constant ) storage of program objects
DBMS provides an environment to provide persistent storage of program objects.
4.Provides storage structure for efficient query processing
DBMS provides facilities for storage structure & efficient execution of queries &
updates.
5.Provides Back up & recovery
DBMS provides facilities to recover from hardware and software failures . i.e., back up and
recovery mechanism
6.Provides multiple user interfaces
Query language for Casual users
Programming language for Application programmers
Form & command codes for parametric users
Menu based ( graphics based) interfaces for stand-alone users
7.Representing complex relationship among data
DBMS is capable of representing a variety of relationships among the data
8. Enforcing integrity constraints
DBMS provides facilities for defining & enforcing integrity constraints .
Simplest integrity constraint is specifying data type for each data item
Ex: Primary key , Foreign Key , Referential integrity
Modern/Advanced Databases
1.Distributed Databases
A distributed DB system consists of several sites
Sites are connected by a network
Each site can hold data and process it
It shouldn’t matter where the data is - the system is a single entity
Distributed Databases
Client(s)
Server Client(s)
Server
Network
Client(s)
Server
Client(s)
Client(s)
Server
Server
'Modern' Databases
Client/Server Architecture
Typical operation
Database access over the
internet Client sends a request for a
page to the web server
Web-based clients
Web server sends SQL to
Web server
database
Database server(s) The web server uses results
Web server serves pages to to create page
browsers (clients) and can The page is returned to the
access database(s) client
Web-based Databases
Database
Server SQL result
Web-based Databases
Advantages Disadvantages
World-wide access Security can be a problem if you
Internet protocols (HTTP, SSL, are not careful
etc) give uniform access and Interface is less flexible using
security standard browsers
Database structure is hidden Limited interactivity over slow
from clients connections
Uses a familiar interface
3.Object Oriented Databases
An object oriented database An object oriented DBMS
(OODB) is a collection of Manages a collection of
objects
persistent objects
Allows objects to be made
Objects - instances of a defined
persistent
class
Permits queries to be made
Persistent - object exist
of the objects
independently of any program
Does all the normal DBMS
things as well
Object Oriented Databases
Advantages Disadvantages
Good integration with Java, C+ There is no underlying theory to
+, etc match the relational model
Can store complex information Can be more complex and less
Fast to recover whole objects efficient
OODB queries tend to be
Has the advantages of the
procedural, unlike SQL
(familiar) object paradigm
4.Object Relational Databases
Entity-Relationship [ ER ] model
6
1
Entity-Relationship[ER]model
ER-Model is used to represent objects in the real world and of relationship among these
objects, which represents the overall logical structure of a database.
ER-model is a high level conceptual data model developed by chen in 1976 to facilitate
database design.
High level Conceptual data models for database design.
S
li
d
e
1
2.Conceptual design
The next step is to create a conceptual schema for the database , using a high level
conceptual data model. Hence, this step is known as “ Conceptual Design”
This conceptual schema explains description of the entity types , relationship &
constraints
S
li
d
e
1
3. Logical design ( Data model mapping )
Logical design is the step , which involves the actual implementation of database, using
a commercial database
In this step, there exists transformation between high-level data model to implement data
model. Hence, called “ Data model mapping”
S
li
d
e
1
4.Physical design:
During this step, the internal storage structures, indexes, access paths and file
organization for the database files are specified.
S
li
d
e
1
. 68
Define the following
1. Entity
2. Attributes
3. Entity sets.
4. Entity types,
5. Key attributes of an entity type.
6. Value sets (Domain) of attributes.
S
li
d
e
1
Examples of entities and attributes:
Make Volkswagen
Model Bora
S Year 2014
li
d
e
1
An entity type defines a collection of entities that have the same attributes.
S
li
d
e
1
Key attributes of an entity type:A key attribute is a minimal set of attributes of an entity set,
which uniquely identifies an entity in an entity set. Example:
For the company entity, name can be the key attribute because no two companies can have the
same name.
For the student entity, regno can be the key attribute.
Different Types of attributes.
S
li
d
e
1
1.Simple or atomic attributes:
These attributes can’t be subdivided into further.
Example: Regno, ID, age, Deptno.
2.Composite attributes: Composite attributes can be divided into smaller subparts.
Address
Street No. Area City State Pin code
S
li
d
e
1
3.Single valued attributes: An attribute that can take only one value at a time is called single
valued attributes.
Example: The age attribute will have a single value.
S
li
d
e
1
4.Multi valued attributes: Some attribute have more than one value for the same attribute
and are called multi valued attributes.
Example: A college degree attribute can have multiple values.
Degree [BCA,B,Sc, MCA, PhD]
Admin [Bangalore, Mumbai]
Carcolor [Red, Black].
S
li
d
e
1
5.Derived attributes: If the value of an attribute can be derived from some other attributes,
then such attributes are called derived attributes.
Example, gross pay of an employee this can be derived by knowing basic pay, allowances, and
deduction for employee.
S
li
d
e
1
6.Stored attributes: The value of certain attributes
cannot be obtained or derived from some other
attributes, that is, they are not derived from any
other attributes.
Example: Birth date, Book_ID
S
li
d
e
1
7.Null attributes: A null attributes used when an
attributes does not have any value.
Email: All employees in an employee database
may not have e-mail address.
S
li
d
e
1
8. Key attributes: An entity type usually has an
individual whose values are distinct for each
individual entity. Such an attribute is called a key
attribute.
S
li
d
e
1
What is Relationship?
Definition:
A relationship is an association among two or more entities. A relationship captures how two
or more entities are related to one another.
S
li
d
e
1
1.Relationship Set: A collection of relationships
of same type is called the relationship set.
S
li
d
e
1
Degree of Relationship:
The degree of relationship is the number of entities
participating in a relation.
Relationship types
1.Binary relationship : A relationship of degree 2 is
known
as Binary relationship
2.Ternary relationship : A relationship of degree 3 is
known as Ternary relationship
8
1.Binary relationship: A relationship of degree 2 is called binary relationship. Example, the
figure shows an example of binary relationships.
Publishers Publishes
Book
S
li
d
e
1
2.Ternary relationship: Relations of degree three are called ternary relationship.
Teacher
Book
Publishers Publishes
S
li
d
e
1
Different Types of relationship or Explain cardinality in Dbms
S
li
d
e
1
1.One to One relationship (1:1) relationships: An entity in A is associated with at most one
entity in B and vice versa.
1
Manager Mange’s
Department
S
li
d
e
1
2.One to Many relationship (1:M) relationships: An entity in A is associated with any
number in B, an entity in B however can be associated with at most one entity in A.
S
li
d
e
1
3. Many to One relationship (M:1) relationships: An entity in A is associated with at most
one entity in B. An entity in B however it can be associated with any number of entities in A.
many Depositors deposit to single account.
M
Depositor Deposit Account
S
li
d
e
1
4. Many to Many relationship (M:N) relationships: An entity in A is associated with any
number of entities in B and, an entity in B is associated with any number of entities in A.
Employee Works_on
Projects
S
li
d
e
1
Relationship type vs. relationship set (1)
Relationship Type:
Is the schema description of a relationship
Identifies the relationship name and the participating entity types
Also identifies certain relationship constraints
Relationship Set:
The current set of relationship instances represented in the database
The current state of a relationship type
Relationships of Higher Degree
RAM is the memory available for the operating system, programs and processes to use
when the computer is running.
ROM is the memory that comes with your computer that is pre-written to hold the
instructions for booting-up the computer.
RAM requires a flow of electricity to retain data (e.g. the computer powered on).
ROM will retain data without the flow of electricity (e.g. when computer is powered off).
RAM is a type of volatile memory. Data in RAM is not permanently written. When you
power off your computer the data stored in RAM is deleted.
ROM is a type of non- volatile memory. Data in ROM is permanently written and is not
erased when you power off your computer.
There are different types of RAM, including DRAM (Dynamic Random Access Memory)
and SRAM (Static Random Access Memory).
There are different types of ROM, including PROM (programmable read-only memory)
that is manufactured as blank memory (e.g. a CD-ROM) and EPROM (erasable
programmable read-only memory).
Cache memory which is static RAM.
Cache memory is used to speed up execution of programs.
Flash memory is an electronic (i.e. no moving parts) non-volatile computer storage device
that can be electrically erased and reprogrammed.
Flash memory was developed from EEPROM (electrically erasable programmable read-only
memory).
Secondary memory
Disk Storage Devices[HDD]
Preferred secondary storage device for high storage capacity and low cost.
Data stored as magnetized areas on magnetic disk surfaces.
A disk pack contains several magnetic disks connected to a rotating spindle.
Disks are divided into concentric circular tracks on each disk surface.
Track capacities vary typically from 4 to 50 Kbytes or more
Disk Storage Devices (contd.)
Whole blocks are transferred between disk and main memory for
processing.
Disk Storage Devices (contd.)
Disks consist of platters, each with two surfaces
Each track consists of sectors separated by gaps
tracks
surface
track k gaps
spindle
sectors
Disk structure
(Muliple-Platter View)
cylinder k
Aligned tracks form a cylinder
surface 0
platter 0
surface 1
surface 2
platter 1
surface 3
surface 4
platter 2
surface 5
spindle
Disk Operation (Single-Platter View)
The disk surface
Read/write head
spins at a fixed
rate is attached to end
rotational
of the arm and flies over
disk surface on
thin cushion of air
spindle
Seek time :
Time to position heads over cylinder containing target sector
Rotational latency :
Time waiting for first bit of target sector to pass under r/w head
Transfer time :
Time to read the bits in the target sector.
Buffering of blocks: Before processing any data, data in terms of blocks are copied into
main memory buffer. When several blocks needed to be transferred from disks to main
memory and all the block address are known. Several buffers can be reserved in main
memory to speed up the transfer, while one buffer is being read or written, the CPU can
process data in the other buffer.
Double buffering:
Once the transfer a block of memory is done from secondary memory to primary memory,
the CPU can start processing this block. Simultaneously the disk I/O processor can be
reading and transferring the next block into a different buffer. This technique is called
double buffering. This can be used to write a continuous stream of blocks from memory to
the disk.
Blocking factor
The blocking factor bfr for a file is the (average) number of file records stored in a disk
block.
Records: Data is usually stored in the form of records. Each record consist of a collection
of related data values or items, where each value corresponds to a particular field of the
record.
Record types: A collection of fields names and their corresponding data types constitutes
a record type or record format definition.
Data type: A data type associated with each field specifies the types of values a field can
take.
Files: A file is a sequence of records, all records in a file are of the same
record type.
Fixed length record: If every record in the file has exactly the same
size in bytes, the file is said to be made up of fixed length records.
Variable length record: If different records in the file have different
sizes, the file is said to be made up of variable length records.
A file descriptor (or file header) includes information that describes
the file, such as the field names and their data types, and the addresses
of the file blocks on disk.
Allocating file blocks on disks:
Allocating file blocks on disks: There are various methods for allocating the blocks of a
file on a disk.
1. Contiguous allocation
2. Linked allocation:
3. Indexed allocations
There are various methods for allocating the blocks of a file on a disk.
1. Contiguous allocation: Here, file blocks are allocated to consecutive disks blocks. This
makes reading the whole file very fast using double buffering, but it makes expanding
the file difficult.
2. Linked allocation: In linked allocation each file block contains a pointer to next file
block. A combination of the two allocates clusters of consecutive disk blocks and the
cluster are linked together clusters are sometime called segments or extents.
3.Indexed Allocation. Provides solutions to problems of contiguous and linked allocation.
A index block is created having all pointers to files. Each file has its own index block which
stores the addresses of disk space occupied by the file. Directory contains the addresses of
index blocks of files.
Operations on file:
Operation on files are usually grouped into retrieval operations and update operations such as
insertion or deletion of records or by modification of field values.
1. Find (or Locate): Searches for the first record satisfying a search condition.
2. Read (or Get): Copies the current record from the buffer to a program variable.
3. Find next: Searches for the next record.
4. Delete: Deletes the current record.
5. Modify: Modifies some fixed values for the current record.
6. Insert: Inserts a new record into the file.
Files of Records
What is Hashing?
Hashing is an effective technique to calculate the direct location of a data record on the disk
without using index structure.
The basic terms associated with the hashing
technique are:
Bucket − A hash file stores data in bucket format. Bucket is considered a unit of
storage. A bucket typically stores one complete disk block, which in turn can
store one or more records.
Hash Function − A hash function, h, is a mapping function that maps all the
set of search-keys K to the address where actual records are placed. It is a
function from search keys to bucket addresses.
H(K) = key MOD M
let us consider a hash function H(K) such that H(K) = key MOD M which produce a
remainder between 0 and M-1 depending on the value of key, this value then used for the
record address.
H(K) = K Mod 10
H(K) = Produces bucket address
********Hashing Types
Basically Hashing are of two types,
1.Static Hashing
2.Dynamic Hashing
1.Static Hashing
1
4
The relational model for database management is a database model based on
first-order predicate logic, first formulated and proposed in 1969 by Edgar F. Codd. In the
relational model of a database, all data is represented in terms of tuples, grouped into
relations. A database organized in terms of the relational model is a relational database
S
li
d
e
1
Relation structure
attributes
(or columns)
customer_name customer_street customer_city
S
li
d
e
1
1.Key constraint:
All tuples in a relation must also be distinct, this means that no 2 tuples can have same
value
2.Constraints on NULL:
S
li
d
e
1
Integrity constraint:
a) Domain constraint:
It specify that, the value of each attribute (A )must be an atomic value from the domain (A) for
that attribute.
Eg: integer type hold only integer but not float value
b)Entity integrity constraints:
It states that no Primary Key value can be NULL,
S
li
d
e
1
c)Referential Integrity Constraints:
The value in the foreign key matches a value in the
primary key [unique or primary key of the same or
different table referenced key].
S
li
d
e
1
*****Codd’s 12 Rules
The information rule simply requires all information in the database to be represented in
one and only one way.
Rule 2: Guaranteed Access
S
li
d
e
1
Rule 3: Systematic Treatment of Null Values
RDBMS must allow each attribute to remain null,specially it must support a representation
of missing information and inapplicable information
Eg:primary keys –not null
S
li
d
e
1
Rule 4: Dynamic Online Catalog
Data dictionary should be stored as relational tables and accessible through the regular data
access language.
Data dictionary (catalog) to have description of the database
The same query language to be used on catalog as on the application database
Eg:SQL is used for both the purpose
S
li
d
e
1
Rule 5: Data Sub language
S
li
d
e
1
Rule 6: View Updating
S
li
d
e
1
Rule 7: High-level Insert, Update, Delete
The system must support set at a time insert, update and delete operations.
Set operations like union, intersection and minus should be supported
S
li
d
e
1
Rule 8: Physical Data Independence
S
li
d
e
1
Rule 9: Logical Data Independence
If there is change in the logical strucutre (table structure)of the database the user view of
the data should not change implemented through views.
It says if a table is spilt into two tables a new view should give result as the join of the two
tables.
S
li
d
e
1
Rule 10: Integrity Independence
The database should be able to enforce its own integrity rather than using other programs
A minimum of the following two integrity constraints must be
supported:
Referential Integrity: For each distinct nonnull foreign key value in
a relational database, there must exist a matching primary key value
from the same domain.
Integrity rules=filters to allow correct data, should be stored in data dictionary.
S
li
d
e .
1
Rule 11: Distribution Independence
The distribution of portion of the database to various location should be invisible to the
user of the database
A database should work properly regardless of its distribution across a network.
S
li
d
e .
1
Rule 12: Non subversion Rule
If low level access is allowed it must not bypass security nor
integrity rules
If low level access is allowed to a system it should not be able to
subvert or bypass integrity rules to change data
S
li
d
e .
1
*******Relational algebra[10M]
The basic set of operations for the relational model is the relational algebra.
It enable the specification of basic retrievals
The result of a retrieval is a new relation, which may have been formed from one or more
relations.
Algebra operations thus produce new relations, which can be further manipulated the same
algebra.
S
li
d
e .
1
Applications
S
li
d
e .
1
*****Different operations of Relational Algebra:
S
li
d
e .
1
2.Additional Operations:
Developed specifically for the relational database.
1. SELECT
2. PROJECT and
3. Join operations
S
li
d
e .
1
UNION [U]:
S
li
d
e
1 .
Relation R and S
R S
1 Arun 3 Smith
2 John
4 David
3 Smith
5 Anand
4 David
6 Nikil
S
li
d
e
1 .
Q =RUS
ENO NAME
1 Arun
2 John
3 Smith
4 David
5 Anand
6 Nikil
S
li
d
e
1 .
INTERSECTION [ ]:
The intersection operation selects the common tuple from the two
relations.
Q=R S
Q =R S
ENO NAME
3 Smith
S
li 4 David
d
e
1 .
DIFFERENCE (-):
Q =R-S
ENO NAME
1 Arun
2 John
S
li
d
e
1 .
CARTESIAN PRODUCTS (X):
The Cartesian product or cross product is binary operation that is used to combine two
relations. Assuming R and S as relations of n and m attributes respectively, the Cartesian
products R X S can be written as
R(A1,A2,A3,…An) X S (B1,B2,B3,….Bm)
The result of the above set operation is,
Q= R X S(A1,A2,A3,…An, B1,B2,B3,….Bm).
S
li
d
e
1 .
R S
1 COMPUTER
10 NETWORKING
2 MANAGEMENT
11 PAYROLL
3 SCIENCE
R S
d 3 SCIENCE 10 NETWORKING
3 SCIENCE 11 PAYROLL
e
1 .
UNARY RELATIONAL OPERATIONS:
Basic operations:
1. Selection ( ) Selects a subset of rows from
relation.
2. Projection ( ) Projection operation is used to
select only few columns from a relation.
3. Renaming:
This operation is used to rename the relations or
S attributes.
li
d The syntax is as fallows:
e . Rename <old table> to <new table>
1
Select operation ( ):
It selects required rows from the table. This operation is used to select the subset of the tuples
from a relation that satisfy a selection condition or search criteria.
Syntax:
S
li
d
e
1 .
Projection(PI)
Projection operation is used to select only few columns from a relation. The
mathematical symbol (PI) is used to denote the project operation.
The general syntax for projection operation is shown below.
1
7 .
Selection and projection
Selects rows that satisfy
selection condition. sid sname rating age
28 John 9 35.0
58 Smith 10 35.0
sname rating
John 9
Smith 10
sname,rating( rating 8(S2))
1
7 .
Projection
sname,age(S 2)
sname age
Arun 25
Anand 28
Smith 30
1
8 .
Binary Relational Operations
.
Pearson Education © 2009 181
What is join?
Join is a combination of a Cartesian product followed by a selection process. A Join operation
pairs two tuples from different relations, if and only if a given join condition is satisfied.
Natural Join ( ) operation:
1
8 .
Example for natural join
Project
Department
PNO PNAME DNUM
DNUM DNAME
10 Library MGT 2
1 Admin
20 ERP 1
2 Research
30 Hospital MGT 3
3 Accounts
40 Wireless n/w 2
PROJ_DEPT
20 ERP 1 Admin
S
li
d
e .
1
WORKER SKILLS
WORKER
NAME SKILL
NAME AGE ADDRESS
Ankith Work
Ankith 23 -
Bhrath 21 - Ganesh Smithy
Chaya 20 - Lokesh Driver
Deepa 21 - Mahesh Fitter
Ganesh 24 - Pandu Smithy
Lokesh 25 - Madhu Fitting
Nataraju 23 -
Madhu 22 -
RESULT
NAME AGE SKILL
Ankith 23 Work
Bhrath 21
Chaya 20
Deepa 21
S Ganesh 24 Smithy
Lokesh 25 Driver
li Nataraju 23
d Madhu 22 Fitting
e .
1
The outer join can be used when we want to keep all the tuples in R of in S are those in both
relations, whether or not they have matching tuples in the other relation.
S
li
d
e .
1
LEFT OUTER JOIN( ): It is denoted by , the left outer join operation keeps
every tuple in the first or left relations R in the result of relation R and S, if no
matching tuple is found in S in the JOIN result are failed with null values.
RIGHT OUTER JOIN ( ): It is denoted by , keeps every tuple in the
second or right relation S in the result of R.
S
li
d
e .
1
FULL OUTER JOIN ( ): It is denoted by , keeps all tuples in both the
left and right relations when no matching tuples are found, filled with NULL values are
needed.
S
li
d
e .
1
Left outer join
BRANCH_LOAN CUSTOMER_LOAN
BRANCH_LOAN CUSTOMER_LOAN
e .
1
Full outer join
BRANCH_LOAN CUSTOMER_LOAN
S
li
d
e .
1
COUNT(*) Counts the number of rows of the query result
S
li
d
e .
1
Structured Query Language
(SQL)
The ANSI standard language for the
definition and manipulation of relational
database.
Includes data definition language (DDL),
statements that specify and modify database
schemas.
Includes a data manipulation language (DML),
statements that manipulate database
S content.
li
d
e .
1
Some Facts on SQL
SQL data is case-sensitive, SQL commands
are not.
1
Attribute names
Tables in Table
SQL name
Product
Tuples or rows
Different SQL Languages and their
commands
1. Data Definition Language [DDL]
2. Data Manipulation Language [DML]
3. Data Control Language [DCL]
4. Transaction Control Language [TCL]
5. Data Query Language [DQL]
S
li
d
e .
1
Different data types of SQL.
1. CHAR(size) -Fixed length character
2. VARCHAR(size)-Varying length characters.
This is also specified as VARCHAR2(Size)
3. NUMBER(size)-Integer number without
decimal point.
4. INTEGER OR INT-Integer number. Size can’t
be specified.
5. DATE-For representing date.
S
li Eg: YYYY-MM-DD.
d
e . 6.TIME-For representing time, HH: MM:SS.
1
SQL: DDL Commands
CREATE TABLE: used to create a table.
S
li
d
e .
1
SQL: CREATE TABLE Statement
Things to consider before you create your
table are:
The type of data
the table name
what column(s) will make up the primary
key
the names of the columns
Syntax:
CREATE TABLE statement syntax:
CREATE TABLE <table name>
S ( columnname1 datatype ( NOT NULL ),
li columnname2 datatype);
d
e .
1
SQL: ALTER TABLE Statement
To add or drop columns on existing tables.
1
SQL-DML COMMANDS
S
li
d
e .
1
Transaction Control Language [TCL]:
It is used to control transactions.
COMMIT: - To permanently save the changes made to transaction.
ROLLBACK: - To undo or cancel (discards) the changes up to the previous commit point.
S
li
d
e .
1
Data Query Language [DQL]: It is used retrieve data from the database .
S
li
d
e .
1
SQL: INSERT Statement
To insert a row into a table, it is necessary to have
a value for each attribute, and order matters.
INSERT statement syntax:
INSERT into <table name>
VALUES ('value1', 'value2', NULL);
Example: INSERT into FoodCart
VALUES (’02/26/08', ‘pizza', 70 );
FoodCart
1
SQL: UPDATE Statement
To update the content of the table:
UPDATE statement syntax:
UPDATE <table name> SET <attr> = <value>
WHERE <selection condition>;
Example: UPDATE FoodCart SET sold = 349
WHERE date = ’02/25/08’ AND food =
date ‘pizza’; food sold
FoodCart date food sold
02/25/08 pizza 350
02/25/08 pizza 349
S 02/26/08 hotdog 500
li 02/26/08 hotdog 500
02/26/08 pizza 70
d 02/26/08 pizza 70
e .
1
SQL: DELETE Statement
To delete rows from the table:
DELETE statement syntax:
DELETE FROM <table name>
WHERE <condition>;
Example: DELETE FROM FoodCart
WHERE food = ‘hotdog’;
FoodCart
1
SQL Statements, Operations, Clauses
SQL Statements:
Select
SQL Operations:
Join
Left Join
Right Join
Like
SQL Clauses:
Order By
S
li Group By
d Having
e .
1
SQL: SELECT Statement
A basic SELECT statement includes 3 clauses
1
SQL: SELECT Statement (cont.)
Using a “*” in a select statement
indicates that every attribute of the input
table is to be selected.
Example: SELECT * FROM … WHERE …;
1
Example: 1) SELECT *
Person FROM person
Name Age Weight WHERE age > 30;
Harry 34 80
Name Age Weight
Sally 28 64
Harry 34 80
George 29 70
Helena 54 54
Helena 54 54
Peter 34 80
Peter 34 80
S
li
d
e .
1
CASCADE Option: Deletes or Drops a schema along with its elements for example, To
remove the company database Schema and all its tables, domains and other elements the
cascade option is used as fallows.
DROP SCHEMA COMPANY CASCADE;
S
li
d
e .
1
Restrict Option Drops: a Schema only if it has no elements init .
For example, To remove the company schema if it has no elements in it, the RESTRICT
option is used as fallows.
DROP SCHEMA COMPANY RESTRICT;
Other wise the DROP commands will not be executed.
S
li
d
e .
1
The DROP Table command: If a base relation within a schema is not needed any longer,
the relation and its definition can be deleted by using the drop Table command.
For example: If we want to delete a employee table from company schema:
S
li
d
e .
1
DROP TABLE EMPLOYEE CASCADE;
This command drops employee table with all constraints and views that reference the table
automatically from the schema along with the table itself
DROP TABLE EMPLOYEE RESTRICT;
This command drops a table only it is not referenced in any constraints
S
li
d
e .
1
Views in SQL
VIEWS (Virtual Tables) in SQL: A View in SQL is a single table that is derived from
other tables. These other tables could be base tables or previously defined Views. A view
does not exist in physical form. It is called as virtual table in contrast to base tables whose
tuples are actually stored in the databases.
S
li
d
e .
1
The general syntax for creating a VIEW is as follows:
CREATE VIEW <View Name> AS
SELECT <Table name1>, <Table name2> …………..
WHERE [Condition];
S
li
d
e .
1
Limitations of SQL:
SQL does not have any procedural capabilities. It does not provide the programming
techniques of condition checking, looping and branching that is vital for data testing before
its permanent storage.
SQL statements are passed to the oracle engine one at a time, each time on SQL statement
is executed, a call is made to engines resources.
While processing on SQL sentence if an error occurs, the oracle engine displays its own
error messages, SQL has no facilities for program handling of errors that arise during the
manipulation of data.
It is not a fully structured programming language.
Pl/SQL
Advantages of PL/SQL:
PL/SQL is a development tool that not only supports SQL data manipulation but also
provides facilities of conditional checking, branching and looping.
PL/SQL sends on entire block of SQL statements to the oracle engine all together
communication between the program block and the oracle engine reduces considerably
reducing network traffic. The code is processed much faster.
PL/SQL allows declaration and use of variables in blocks of code.
These variables can be used to store intermediate results of query
for later processing or calculate values and insert them into an
oracle table later.
Using PL/SQL all sorts of calculations can be done quickly and
efficiently without the use of the oracle engine. This improves
transaction performance.
Applications written in PL/SQL are portable to any computer
hardware and operating system, where oracle is operational.
Pl/SQL Block: A PL/SQL block has a definite structure, which can be divided into 4 sections.
1. The declaration section
2. The begin section
3. The exception section
4. The end section.
PL/SQL Character set:
Upper case letters: A…Z
Lower case letters: a…z
Numerals: 0…9
Special symbols: (, ), +, -, *, /, <, >, =, !, :, :, ., ‘, ”, @, #, $, %, &, _, \, {, }, [, ], ?, |.
Literals: A sequence of valid PL/SQL characters is called literal.
PL/SQL Data types:
1. Number
2. Char
3. Date
4. Boolean
PL/SQL Execution:
SQL>ed
Wrote file afiedt.buf
1 declare
2 d1 date;
3 begin
4 d1 :=sysdate;
5 dbms_output.put_Line(d1);
6* end;
SQL> /
31-MAR-10
PL/SQL procedure successfully completed.
Difference between SQL and PLSQL
2.) PL/SQL can be the application language just like Java or PHP
can. PL/SQL might be the language we use to build, format and
display those screens, web pages and reports.
SQL may be the source of data for our screens, web pages and reports.
Embedded SQL
Embedded SQL is the one which combines the high level
language with the DB language like SQL. It allows the
application languages to communicate with DB and get
requested result. The high level languages which supports
embedding SQLs within it are also known as host language.
There are different host languages which support
embedding SQL within it like C, C++, ADA, Pascal,
FORTRAN, Java etc. When SQL is embedded within C or
C++, then it is known as Pro*C/C++ or simply Pro*C
language. Pro*C is the most commonly used embedded
SQL.
Example
.Slide 2- 238
Primary key: It is a candidate key chosen by the database designers as the
principal means of identifying entities within an entity set.
.Slide 2- 239
Weak entity set: An entity set not having sufficient attributes to form a
primary key is called weak entity set.
Strong entity set: An entity set that has a primary key is termed a strong
entity set.
.Slide 2- 240
Foreign key: A set of attributes in a relation is a foreign key if it satisfies
the following conditions.
It should have the some domain as the primary key attributes of another relation
schema and is said to refer to this relation.
A value of foreign key in tuple t1 either occurs as a value of primary key for
some tuple t2 in another relation.
Composite key: If a key has more than one attribute, it is called composite
key.
.Slide 2- 241
Relation key: Given a relation, if the value of an attribute X uniquely
determinates the value of all other attribute in a row, then X is said to be
the key of that relation.
.Slide 2- 242
The
Evils of Redundancy
Redundancy is at the root of several problems associated with relational
schemas:
redundant storage, insert/delete/update anomalies
Integrity constraints, in particular functional dependencies, can be used to
identify schemas with such problems and to suggest refinements.
Main refinement technique: decomposition (replacing ABCD with, say, AB
and BCD, or ACD and ABD).
Decomposition should be used judiciously:
Is there reason to decompose a relation?
What problems (if any) does the decomposition cause?
243
Redundancy
245
Anomalies
246
Decomposition
247
Normalization Theory
Anomalies:
update anomaly occurs if changing the value of an attribute leads to an inconsistent
database state.
insertion anomaly occurs if we cannot insert a tuple due to some design flaw.
deletion anomaly occurs if deleting a tuple results in unexpected loss of
information.
Normalization is the systematic process for removing all such anomalies in
database design, based on functional dependencies
248
**What are the Building blocks of Normalization?
(1)Functional dependency;
(2)Determinants
(3)Key attributes
(4)Non-key attributes
.Slide 2- 249
Different Types of Functional Dependencies
Fully Functional dependency
Partially Functional dependency
Transitive Functional dependency
.Slide 2- 250
Functional Dependencies
A functional dependency is a constraint between two sets of
attributes in a relational database.
If X and Y are two sets of attributes in the same relation T, then X
Y means that X functionally determines Y so that
the values of the attributes in X uniquely determine the values of the
attributes in Y
for any two tuples t1 and t2 in T, t1[X] = t2[X] implies that t1[Y] =
t2[Y]
if two tuples in T agree in their X column(s), then their Y column(s)
should also be the same.
251
Another Example: FDs & Redundancy
Consider relation obtained from Hourly_Emps:
Hourly_Emps (ssn, name, lot, rating, hrly_wages, hrs_worked)
Notation: We will denote this relation schema by listing the attributes:
SNLRWH
This is really the set of attributes {S,N,L,R,W,H}.
Sometimes, we will refer to all attributes of a relation by using the relation name.
(e.g., Hourly_Emps for SNLRWH)
Some FDs on Hourly_Emps:
ssn is the key: S SNLRWH
rating determines hrly_wages: R W
252
R W
Wages
Example (Contd.) 8 10
5 7
Hourly_Emps2
Problems due to R W : S N L R H
Update anomaly: Can 123-22-3666 Attishoo 48 8 40
we change W in just 231-31-5368 Smiley 22 8 30
the 1st tuple of SNLRWH?
131-24-3650 Smethurst 35 5 30
Insertion anomaly: What if
we want to insert an employee 434-26-3751 Guldu 35 5 32
and don’t know the hourly 612-67-4134 Madayan 35 8 40
wage for his rating? S N L R W H
Deletion anomaly: If we 123-22-3666 Attishoo 48 8 10 40
delete all employees with
231-31-5368 Smiley 22 8 10 30
rating 5, we lose the
information about the wage 131-24-3650 Smethurst 35 5 7 30
for rating 5! 434-26-3751 Guldu 35 5 7 32
612-67-4134 Madayan 35 8 10 40
Will 2 smaller tables be better? 253
Decompositions
Do we need to decompose a relation?
Several normal forms for relations. If schema in these normal forms certain
problems don’t arise
What problems does decomposition cause?
Lossless-join property: get original relation by joining the resulting relations
Dependency-preservation property: enforce constraints on original relation by
enforcing some constraints on resulting relations
Queries may require a join of decomposed relations!
254
Functional Dependencies
Dependencies for this
R A B C D E F relation:
a1 b1 c1 d1 e1 f1 AB
a1 b1 c2 d1 e2 f3 AD
a2 b1 c2 d3 e2 f3 B,C E,F
a3 b2 c3 d4 e3 f2 Do they all hold in this
a2 b1 c3 d3 e4 f4 instance of the relation
a4 b1 c1 d5 e1 f1 R?
company_name company_address
company_name date_founded
company_name, owner_id owner_title
company_name, owner_id #shares
company_name, owner_title owner_id
owner_id owner_name
256
Types of functional dependencies
Full dependency
Partial dependency
Transitive dependency
257
Full dependencies
This implies that for a given pair of (S#,C#) values occurring in the
relation Report there is exactly one value of Marks. ie Marks are
dependent on S# & C# as a composite pair, but not on either
individually
258
Partial dependencies
The attributes Title, LName are said to be partially dependent on the key (S#, C#) since
they are dependent only on C# and not on S#.
259
Transitive dependencies
Report( S#,C#,Title,Lname,Room#,Marks)
C# LName LName Room#
260
Keys in the relational model
Superkey
A set of one or more attributes, which, taken collectively, allow us to identify
uniquely a tuple in a relation.
Let R be a relation scheme. A subset K of R is a superkey of R if, in any legal
relation [instance] r of R, for all pairs t1 and t2 of tuples in r such that t1[K] = t2[K]
t1 = t2.
Candidate key
A superkey for which no proper subset is a superkey.
Primary key
The candidate key that is chosen by the database designer as the principle key.
261
FD and Keys
For a key, no two rows share the same values, thus by default, when ever a tuple agrees on LHS it
agrees on the RHS.
262
Armstrong’s Axioms of FDs
263
Other derived rules
264
Normalization
265
What is a Normalization?
Normalization of data can be looked upon as a process of analyzing the given relation
schemas based on their FDs and primary keys to achieve the desirable properties of
Minimizing the redundancy
Minimizing the insertion, deletion and update anomalies.
266
Some Definitions
Prime Attributes:
An attribute of relation schema R is called a prime attribute of R if it is a
member of some candidate key of R.
o Non Prime Attributes:
An attribute is called a nonprime if it is not a prime attribute.
That is it is not a member of candidate key.
1NF implies:
• Composite attributes are represented only by their component attributes
• Attributes cannot have multiple values
268
Example of 1NF
DEPARTMENT
DEPARTMENT
Is it a 1NF?
269
There are 3 main techniques to achieve
first normal
Remove the attributesform
DLocationfor such
that voilates a place
1NF.and relation.
it in separate
relation Dept_location.
Expand the key so that there will be a separate tuple in the original
Department relation for each location of a department.
If it is known that at most three locations can exist for department
example Dloc1,Dloc2,Dloc3
270
Then, what is first 1NF?
DEPARTMENT
Is it a 1NF?
271
Another Example
272
Example Cont.
273
Second normal form: 2NF
In this,
E#, Project# -> Role
E# -> Number_Of_shares
274
Consider the relational schema:
Empdetails( E#, Project#, Role,Number_Of_shares, Share_worth)
In this,
E#, Project# -> Role
E# -> Number_Of_shares
275
A typical snapshot may look like…
276
2NF Example cont.
To make the previous table in 2NF, the table must be split into two separate
tables:
Professor Subject
Jones Math42
Professor Office
Jones MH 410 Jones CS49C
Smith DH 211 Smith Chem1A
Lee MH 320
Smith Chem100W
Lee Math161A
277
Third normal form:3 NF
A relation schema R is in 3NF.if it is in 2NFand if, whenever a nontrivial
functional dependency X A holds in R, either
(a) X is super key of R
(b) A is a prime attribute of R.
278
Example of 3NF
Ename ssn Bdate addres Dnumber Dname Dmgssn
s
Functional dependencies
SSN Ename,Bdate,Address,Dnumber
Dnumber Dname,DmgSSN.
279
What is a solution?
280
3NF Normaliztion.
Enam SSN Bdate Address Dnuumber
ED1 e
Functional dependencies
SSN Ename,Bdate,Address,Dnumber
Functional dependencies
Dnumber Dname,Dmgssn
281
Boyce Codd Normal Form
The Formal definition of BCNF differs slightly from the definition of 3NF. The only
difference between the 3NF and BCNF is the condition (b) of 3NF. Which allows A to be a
prime.
282
An example
Courses (Dept#, Course#, Lecturer#, Num_Students)
Consider the relation:
Assumptions
– Each Department offers may courses
– Course# is unique within a Department only
– Each Lecturer belongs to one Dept only
– Each Lecturer may handle several courses within the dept.
– A particular course offered by a department may be handled by a single lecturer
283
The functional dependencies
• {Dept#,Course#}->Lecturer#
• {Dept#,Course#}-> Num-of_students
• {Lecturer#,Course#}->Num-of_students
• Lecturer# -> Course#
284
A sample table
D1 C1 L1 20
D1 C2 L1 15
D1 C3 L2 42
… … … …
D2 C5 L3 12
D2 C6 L4 19
285
Observations
286
The solution
287
Difference between BCNF and 3NF
288
Conclusion
289
Properties of Relational Decompositions.
290
Properties of Relational Decompositions
(2)
Another goal of decomposition is to have each individual relation Ri in the decomposition
D be in BCNF or 3NF.
Additional properties of decomposition are needed to prevent from generating spurious
tuples.
291
Properties of Relational Decompositions (3)
292
(Example) Lossless (Non-additive) Join
Property
S P D S P P D
S1 P1 D1 S1 P1 P1 D1
S2 P2 D2 S2 P2 P2 D2
S3 P1 D3 S3 P1 P1 D3
S P D
S1 P1 D1
S2 P2 D2
S3 P1 D3
S1 P1 D3
S3 P1 D1
2
9
Dependency Preservation property of a
Decomposition.
If each FD XY Specified in F either appeared directly in one of the relation schemas Ri
in the decomposition D or could be inferred from the dependencies that appear in some
Ri. Informally this is the dependency preservation.
294
Formal Definition for Dependency
Preservation.
Given a set of dependencies F on R, the projection of F on Ri denoted by Ri(F) where Ri
is a subset of R,
295
UNIT 05
Transaction Processing Concept
What is Transaction?
A transaction is an atomic unit comprised of one or more SQL statements.
A transaction begins with the first executable statements and ends when it is
committed or rollback.
S
li
d
e
1
Transaction and system concepts: A transaction is a logical unit of
database processing that includes one or more database access operation.
298
****Desirable Properties of transactions or
ACID properties of transactions[6M]
299
Desirable Properties of transactions (continued)
3.Isolation :
The execution of the transaction should be isolated
from other transactions (Locking).
No inference with other transactions.
4.Durability :
Once a transation completes, the changes made to database permanent and are available to all the
transactions that follow it.
300
*****Transaction state diagram and additional operations[6M]
301
Transaction states and additional operations
(continued)
/READ
WRITE
BEGIN END
TRANSACTION TRANSACTION COMMIT
ACTIVE PARTIALLY COMMITTED
COMMITTED
ABORT
ABORT
FAILD TERMINATED
303
Read_item(x) include the following steps:
S
li
d
e
1
Concurrency control protocols can be broadly divided into two categories −
i. Lock based protocols
ii. Time stamp based protocols
Advantages of concurrent execution:
1.Increased performance
2.Resource utilization.
3.Decreased waiting time
Why concurrency control is needed?
In a multiuser database, transaction submitted by the various user may execute concurrently
and many update the same data concurrently executing transactions must be guaranteed to
produce the same effect as serial execution of transaction (one by one).
S
li
d
e
1
****Problems with concurrent execution
S
li
d
e
1
***The Lost update problem:
S
li
d
e
1
Example
***2,Dirty read Problem:
S
li
d
e
1
Example for dirty read problem
***3.Incorrect summary problem:
Sum:=0
read_item(a)
sum := sum+a;
read_item(x);
x:=x-N;
write_item(x);
read_iterm(x);
sum:=sum+x; T3 reads X after N is
read_item(y); subtracted and reads y
sum:= sum+y; before N is added.
read_item(y);
y:=y+N;
write_item(y); -a wrong summary is the result.
fig(b) Transaction[T2].
fig(a): Transaction [t1]
Why Recovery Is Needed
319
Why Recovery Is Needed (continued)
4.Concurrency control enforcement :
The concurrency control method may decide to abort the transaction.
5.Disk failure : all disk or some disk blocks may lose their data
The system must keep sufficient information to recover from the failure.
320
****Concurrency control techniques
S
li
d
e
1
3.Exclusive LOCK: Exclusive locks are used for data modification operations such as
update, delete and insert.
Once a transaction puts the X lock on a particular resource, no other transaction can put any
kind of lock on this resource.
This resource is exclusively reserved for the first transaction and no other transaction can use
it for read or write operation.
Hence X lock allows least concurrency
S
li
d
e
1
Locking Technique
The effect of a lock is to lock other transaction out of the object.
B X S
A
X N N Y
Compatibility matrix
S N Y Y
Y Y Y
: no lock
N
Y
: request not compatible
S : request compatible
li
d
e
1
OBJECTIVE
• Diagrammatic study of
VIEW,
• Concept of VIEW,
• Creation of VIEW,
• Concept of ASSETION,
• Examples of
ASSERTION.
DIAGRAMMATIC REPRESENTATION OF VIEW
A B C D E F
ACTUAL TABLE
WHAT IS VIEW
• A view is a virtual table. It does not physically exist. Rather, it is created by a query
joining one or more tables.
• The fields in a view are fields from one or more real tables in the database.
Syntax:
CREATE VIEW view_name AS
SELECT column_name(s)
FROM table_name
WHERE conditions;
VIEW CREATION-EXAMPLE
• View Creation-Example
CREATE VIEW sup_orders
• The view(create statement) would create a virtual table based on the result set of the
select statement. You can now query the view as follows
SELECT*FROM sup_orders;
Updating View Modify View-Example Dropping View
DBMS checks the assertion after any change that may violate the
expression
• Concept of Indexing
• Types of Indexing
INDEXING
• Indexes are used by queries to find data from tables quickly. Indexes are created on tables
and views. Index on a table or a view, is very similar to an index that we find in a book.
• If you don’t have an index, and I ask you to locate a specific chapter in the book, you will
have to look at every pages starting from the first page of the book.
• On the other hand, if you have the index, you look up the page number of the chapter in
the index, and then directly go to that page number to locate the chapter.
• Obviously, the book index is helping drastically reduce the time it takes to find the
chapters.
• In a similar way, Table and View indexes, can help the query to find data quickly.
• In fact, the existence of the right indexes, can directically improve the performance of the
query. If there is no index to help the query , then the query engine, checks every row in
the table from the beginning to the end. This is called Table Scan. Table scan is bad for
performance.
INDEX EXAMPLE
At the moment, the employees table, does not have an index on SALARY column.
Id Name Salary Gender
To find all the employees, who has salary greater than 5000 and less than 7000, the query engine has to
check each and every row in the table, resulting in a table scan, which can adversely affect the
performance, especially if the table is large. Sine there is no index, to help the query, the query engine
performs an entire table scan.
CREATING AN INDEX
CREATE INDEX IX_tblEmployee_Salary
ON tblEmployee (SALARY ASC)
The index stores salary of employees , in the ascending order as shown below. The actual index may
look slightly different.
I Salary Row Address
Name Salary Gender
d
2500 Row address
1 Sam 2500 Male
3100 Row address
2 Pam 6500 Female
4500 Row address
3 John 4500 Male
5500 Row address
4 Sara 5500 Female
5 Todd 3100 Male 6500 Row address
Now , when the SQL server has to execute the same query, it has an index on the salary column to help
this query. Salaries between the range of 5000 and 7000 are usually present at the bottom, since the
salaries are arranged in an ascending order. SQL server picks up the row address from the index and
directly fetch the records from the table, rather than scanning each row in the table. This is called
Index Seek.
As we studied, Indexes are used to retrieve data from the database very fast. The users
cannot see the indexes, they are just used to speed up searches/queries.
M.s.Access :
CREATE INDEX index_name
DROP INDEX index_name
ON table_name(column1,
ON table_name;
column2,….);
SQL Server :
DROP
INDEX table_name.index_name;
CREATE UNIQUE INDEX
DB2/Oracle :
Index_name
DROP INDEX index_name;
ON table_name(column1,
My SQL :
Column2,….);
ALTER TABLE table_name
DROP INDEX index_name;
CLUSTERED INDEX
A clustered index determines the physical order of data in a table. For this reason, a table can have
only one clustered index.
CREATE TABLE (tblEmployee)
{
[Id] int Primary key,
[Name] nvarchar (50),
[Salary] int,
[Gender] nvarchar (10),
[City] nvarchar (50)
}
Note that id column is marked as primary key. Primary key, constraint create clustered indexes
Automatically if no clustered index already exists on the table.
Insert into tblEmployee Value(3,’John’ , 4500,’male’,’New York’) 2 Pam 6500 Female Sydney
Insert into tblEmployee Value(1,’Sam’,2500,’female’,’London’)
3 John 4500 Male New York
Insert into tblEmployee Value(4,’Sara’,5500,’female’,’Tokyo’
Insert into tblEmployee Value (5, ’Todd’,3100,’male’,’Toronto’) 4 Sara 5500 Female Tokyo
Insert into tblEmployee Value (2, ‘Pam’,6500, ’female’, Sydney’)
5 Todd 3100 Male Toronto
CLUSTERED INDEX
A clustered index is analogous to a telephone directory, where the data is arranged by
the last name.
A nonclustered index is analogous to an index in a textbook. The data is stored in one place, the index
in another place. The index will have pointers to the storage location of the data.
In the index itself, the data is stored in an ascending or descending order of the index key, which
doesn’t in any way influence the storage of data in the table.
DBMS
SYLLABUS
COMPLETED.
346
ANY QUESTIONS FROM
UNIT 1,2,3,4 & 5.
347
Thank you