COC DBS For L3 and 4
COC DBS For L3 and 4
Lecture Notes
Types of Database
DBA Responsibilities
Database Security
Database Security means protection of the: -
Data from malicious attempts to steal (view) or modify data.
Database from unauthorized users.
Data security covers access and use of database objects (such as tables and views) and the
actions that users can have on the objects.
What is a DBMS?
The various functions provided by a DBMS are listed below (parentheses indicate what we will
cover in this course that will address this issue):
Integrity Constraint
is a constraint which used to ensure accuracy and consistency of data in a relational database by
prevents the user from entering the duplicate rows into tables.
Entity integrity
The entity integrity constraint states that no primary key value can be null, and there is no any
duplicate entries in the primary key column.
Referential Integrity
Referential integrity is a feature provided by relational DBMS that prevents users from entering
inconsistent data.
The referential integrity constraint is specified between two tables and is used to maintain the
consistency among tuples in the two tables.
Referential integrity means the foreign key in any referencing table must always refer to a valid
row in the referenced table.
Domain Integrity
The domain integrity states that every element from a table should respect the type and
restrictions of its corresponding attribute. A type can have a variable length which needs to be
respected. Restrictions could be the range of values that the element can have, the default value if
none is provided, and if the element can be NULL.
Business rules may dictate/state that when a specific action occurs further actions should be
triggered. For example, deletion of a record automatically writes that record to an audit table.
Data independence – the manipulation of the data is independent of where the data is
physically stored – in other words, data manipulation works with logical view of the data
and the process that is manipulating the data does not need to know where or how the
data is stored (will be learning how to use SQL)
Utility services – provides ways to import & export data, query the data etc (will be
using tools such as Query Analyzer). Manipulate data.
An Overview of Transactions
Database Administration Lecture notes for level – 3 & 4 Trainees 2015
Consistency
Data is in a consistent state when a transaction starts and when it ends.
For example, in an application that transfers funds from one account to another, the
consistency property ensures that the total value of funds in both the accounts is the same
at the start and end of each transaction.
Isolation
The intermediate state of a transaction is invisible to other transactions. As a result,
transactions that run concurrently appear to be serialized.
For example, in an application that transfers funds from one account to another, the
isolation property ensures that another transaction sees the transferred funds in one
account or the other, but not in both, nor in neither.
Durability
After a transaction successfully completes, changes to data persist and are not undone,
even in the event of a system failure.
For example, in an application that transfers funds from one account to another, the
durability property ensures that the changes made to each account will not be reversed.
Database Models
A Database model is a type of data model that determines the logical structure of a database and
fundamentally determines in which manner data can be stored, organized, and manipulated. The
Database Administration Lecture notes for level – 3 & 4 Trainees 2015
most popular example of a database model is the relational model, which uses a table-based
format.
Examples of data models include:
hierarchical
network
relational
In the network model, the data are represented by collections of records and Relationships
between data are represented by links. This structure holds for the hierarchical model as well.
The only difference is that, in the hierarchical model, records are organized as collections of
trees, rather than as arbitrary graphs.
Hierarchical Model
In a hierarchical model, data is organized into a tree-like structure, implying a single parent for
each record. This model allows one to one and one to many relationships.
Network Model
The network model expands upon the hierarchical structure, allowing many-to-many,one to
one and one to many relationships in a tree-like structure that allows multiple parents. It was
most popular before being replaced by the relational model.
The network model is able to represent redundancy in data more efficiently than in the
hierarchical model, and there can be more than one path from an ancestor node to a descendant
Database Administration Lecture notes for level – 3 & 4 Trainees 2015
Relational data model is the primary data model, which is used widely around the world for data
storage and processing. This model is simple and it has all the properties and capabilities
required to process data with storage efficiency. In Relational Database data represented as a set
of related tables (or relations).
Concepts
Tables − In relational data model, relations are saved in the format of Tables. This format stores
the relation among entities. A table has rows and columns, where rows represent records and
columns represent the attributes.
Tuple − A single row of a table, which contains a single record for that relation is called a tuple.
Relation instance − A finite set of tuples in the relational database system represents relation
instance. Relation instances do not have duplicate tuples.
Relation schema − A relation schema describes the relation name (table name), attributes, and
their names.
Relation key − Each row has one or more attributes, known as relation key, which can identify
the row in the relation (table) uniquely.
Attribute domain − every attribute has some pre-defined value scope, known as attribute
domain.
Constraints
Every relation has some conditions that must hold for it to be a valid relation. These conditions
are called Relational Integrity Constraints. There are three main integrity constraints −
Key constraints
Domain constraints
Referential integrity constraints
Database Administration Lecture notes for level – 3 & 4 Trainees 2015
Key Constraints
There must be at least one minimal subset of attributes in the relation, which can identify a tuple
uniquely. This minimal subset of attributes is called key for that relation. If there are more than
one such minimal subsets, these are called candidate keys.
Key constraints force that −
in a relation with a key attribute, no two tuples can have identical values for key attributes.
a key attribute can not have NULL values.
Key constraints are also referred to as Entity Constraints.
Domain Constraints
Attributes have specific values in real-world scenario. For example, age can only be a positive
integer. The same constraints have been tried to employ on the attributes of a relation. Every
attribute is bound to have a specific range of values. For example, age cannot be less than zero
and telephone numbers cannot contain a digit outside 0-9.
Referential integrity Constraints
Referential integrity constraints work on the concept of Foreign Keys. A foreign key is a key
attribute of a relation that can be referred in other relation.
Referential integrity constraint states that if a relation refers to a key attribute of a different or
same relation, then that key element must exist.
Using Entity relationship Modeling
Entity types, entity sets, attributes, and sets
1.1 What is entity in a database?
An Entity is a thing in real-world (an object with a physical existence, e.g. A particular person,
car, house, or employee or an object with a conceptual existence.
It can be roles, events, locations, tangible things or concepts.
Example: A person/an employee, department, payment, book, project, campus,
organization, or business.
Each entity must have its own identity that distinguishes it from every other entity, called unique
identifier.
Example: Each Student has a unique ID that distinguishes one from others.
1.2 In database terms, an entity is a table which is responsible for storing data in the database.
Entity type is the set of all possible values for an entity is called entity type.
Example: - student is an entity type with common attributes to all students such as student_ID,
Name, etc.
An entity set is a set of entities of the same type (e.g., all persons having an account at a bank).
An entity set is the collection of instances represented by (e1, e2, e3, .... en). It is also called an
extension of the entity type.
Example: - a company has many employees, and these employees are defined as entities (e1,
e2, e3 ...) and all these entities having the same attributes which are defined under ENTITY
TYPE employee, and set{e1, e2, .....} is called entity set.
In E-R Diagram, an entity type is represented by a rectangle, and the name is indicated in
capital letters.
Database Administration Lecture notes for level – 3 & 4 Trainees 2015
Example:
DEPARTMENT EMPLOYEE
Index on emp_id
Attributes …
… Index on
An attribute is a property that describes an entity. AMD15433F emp_id
… data type.
Each attributes has a particular value based on the defined
… is called attribute domain.
The set of all possible (allowable) values of an attribute
KFJ64308F
In E-R Diagram, attributes are named with an initial capital letter followed by Index on letters.
lowercase
Emp_id emp_id
PMA42628M …
…
An attribute can be represented by an ellipse (oval) shape with a line connected …it to the
associated entity. AMD15433F AMD15433F
Example: … …
KFJ64308F …
… KFJ64308F
…
It can also be represented by listing them within the entity rectangle, under the… entity name
(entity type). …
Example: Employee tableEMPLOYEE…
Emp_Id PMA42628M
D15433F Fname
Types of attributes … lname
KFJ64308F
An attribute can be: - simple or composite, single-value … or multi-value, stored or derived
or Null Values. …
Simple (Atomic) vs. Composite attribute
Simple attributes cannot be further divisible. … Index on
Example: SSN, Sex … emp_id
In E-R Diagram, simple attributes can be represented …by an ellipse shape. Example:
PMA42628M …
Composite attributes can be divided into smaller subparts. …
Example: - address (street address, city, state, zip code) AMD15433F
- Name (first name, last name, initial name) …
In E-R Diagram, composite attributes can be represented by: …
KFJ64308F
The value of composite attribute is the concatenation of the values of its constituent simple
attributes. …
Single-valued vs. Multi-valued attributes …
- Most attributes have a single value for a particular entity. …
PMA42628M
- In some cases, an attribute can have a set of value for the same entity, called multi-
valued.
Example: color attribute for a car, college degree for a person.
In E-R Diagram, Multi-valued attributes can be represented by double ellipse (oval) shape.
Stored vs. derived attributes.
In some cases, some attributes have a reference (derived from other attributes’ value).
Example: - The Age attribute is derived from the Birth-Date attribute.
- So, the Age attribute is derived attribute, and the Birth-Date attribute is stored
attribute.
Database Administration Lecture notes for level – 3 & 4 Trainees 2015
Key attributes
Identifiers (more commonly called keys or key attributes) uniquely identify each instance of an
entity, called candidate key. If such an attribute doesn't exist naturally, a new attribute is defined
for that purpose.
Example: an ID number of STUDENT OR EMPLOYEE Entity type.
In some cases, more than one attribute is required to identify a unique entity, called composite
candidate key.
In E-R Diagram, key attributes (identifiers) can be represented by ellipse shape with underline.
Relationship Types
A Relationship Type defines a relationship set among entities of certain entity types.
Relationship sets
A Relationship Set is a collection of relationships all belonging to one relationship type.
Example: EMPLOYEE works_for DEPARTEMENT
In most cases, the role names do not have to be specified, but in cases where the same entity
participates more than once in a relationship type in different roles.
For example, each employee has a supervisor, we need to include role name “Supervise” and
“supervisor”, both of them are employees. Since the employee entity type participates twice in
the relationship, once as an employee and once as a supervisor, we can specify two roles,
employee and supervisor
Employee
Supervisee
Supervisor Supervi
sion
I.e.: - It describes the storage structures and access methods used to achieve efficient access to
the data.
Example: The relationship b/n Supplier, Project and Part. Each part is supplied by a unique
supplier, and is used for a given project within a company; the relationship “Supplies” is a
Supplie
Supplier Project
s
Part
ternary (degree of three) between Suppliers, Project
And Part
Constraints on Relationship Types
Relationship types have certain constraints that limit the possible combination of entities that
may participate in relationship.
An example of a constraint on relationship is that if we have the entities Doctor and Patient, the
organization may have a rule that a patient cannot be seen by more than one doctor. This
constraint needs to be described in the schema.
There are two main types of relationship constraints: cardinality ratio, and participation.
Participation Constraints
The participation constraint specifies whether the existence of an entity depends on its being
related to another entity via the relationship type.
Database Administration Lecture notes for level – 3 & 4 Trainees 2015
The constraint specifies the minimum number of relationship instances that each entity can
participate in.
There are two types of participation constraints:
Total :
If an entity can exist, only if it participates in at least one relationship instance, then that is called
total participation.
Example: in the relationship between Employee and Department, if company policy states that
every employee must work for a department, then an employee can exist only if he/she
Participates in at lest one relationship instance (i.e. an employee can’t exist without a
department).
It is also sometimes called an existence dependency.
Total participation is represented by a double line.
Partial:
If only a part of the set of entities participate in a relationship, then it is called partial
participation.
Example: in the relationship between Employee and Department, if company policy states that
every employee will not be a manager of a department, then the participation of an employee in
the “Manages” relationship is partial.
Partial participation is represented by a single line.
Attributes of Relationship Types
Relationships can have attributes similar to entity types.
Example: - in the relationship Works_On, between the Employee entity and the Department
entity, we would like to keep track of the number of hours an employee works on a project.
Therefore we can include Number of Hours as an attribute of the relationship.
- In the “manages” relationship between employee and department, we can add Start Date
as an attribute of the Manages relationship.
For some relationships (1:1, or 1: N), the attribute can be placed on one of the participating entity
types.
Example: the “Manages” relationship is 1:1, so StartDate can either be migrated to
Employee or Department.
For some relationships (1:1, or 1: N), the attribute can be placed on one of the participating entity
types.
Example: the “works_for” relationship is N: 1, so since can be migrated to Employee entity
type.
Weak Entity Types
Entity types that do not have key attributes (strong key) are called weak entity types.
The relationship that relates the identifying entity type (strong entity type) with the weak entity
type is called an identifying relationship.
A weak entity type always has a total participation constraint with respect to the identifying
relationship, because a weak entity cannot exist without its owner.
A weak entity type usually has a partial key, which is the set of attributes that can uniquely
identify weak entities.
Example: let’s assume in a library database, we have an entity type Book. For each book, we
keep track of the author, and title. The library may own several copies of the same book, and for
each copy, it keeps track of the copy number (a different copy number for each copy of a given
book) and price of each copy.
Has
Database Administration Lecture notes for level – 3 & 4 Trainees 2015
Book Copy
The copy number is only unique for each book, meaning Book 123 may have copy 1, copy 2,
copy 3, and book 456 may also have copy 1, copy 2 and copy 3. The copy number cannot be
considered unique for each copy.
Therefore, the Copy entity does not have a key attribute; it is considered a weak entity type
where as the book entity is the identifying entity.
A copy cannot exist without the identifying entity (Book)
The Copy entity type has a total participation constraint with respect to the identifying
relationship.
The partial key of the Copy entity is Copy Number; for each owner entity Book, the Copy
Number uniquely identifies the copy for each book.
Normalization
Normalization is a process that involves dividing large tables (complex data structures) into
smaller (simple and stable data structures) that do not accept redundant values and defining
relationships between them.
Database normalization is the process of organizing the fields and tables of a relational
database to minimize redundancy and to avoid update anomalies (insert, modify, and delete
anomalies).
If an attribute of a table is multi-valued, it must be normalized.
Normalization is a process used to convert complex data structures into simple, stable data
structures that do not have data redundancies
A relation can be normalized to a specific form to prevent possible occurrence of update
anomalies.
Four most commonly used normal forms are: - First (1NF)
- Second (2NF)
- Third (3NF) normal forms, and
- Boyce–Codd normal form (BCNF).
1st Normal Form (1NF):
A table is in 1NF if: - There are no duplicated rows in the table.
- Each cell is single-valued.
- Entries in a column (attribute, field) are of the same kind.
2nd Normal Form (2NF):
A table is in 2NF if: - it is in 1NF
- All non-key attributes are dependent on the entire key (has no partial
dependencies).
3rd Normal Form (3NF):
A table is in 3NF if: - It is in 2NF
- If it has no transitive dependencies.
- There are no functional dependencies between two or more non-primary-
key attributes.
Functional dependency
One goal of schema design is to minimize the storage space that the base relations (files) occupy.
Database Administration Lecture notes for level – 3 & 4 Trainees 2015
Another serious problem with redundancy is the problem of update anomalies. These can be
classified into:
Insertion Anomalies
Deletion Anomalies
Modification/update Anomalies
Update anomalies:
INSERT: To INSERT new records, first you must check if there is referential constraint.
DELETE: Before try to DELETE a record from a table, you must check whether it is referred by
another table field(s) or not.
UPDATE: If you want to change the value of an attribute, you must check whether it is referred
by another table field(s) or not.
A functional dependency (FD) is a constraint between two sets of attributes in a table from a
table in the database.
A functional dependency occurs when one attribute in a table uniquely determines
another attribute. This can be written as: A B ("B is functionally dependent upon A").
If a particular value of one attribute (A) in a table uniquely determines the value of another
attribute (B) in the same table, then there is a functional dependency between attributes A and B.
Examples: In a table employee’s characteristic including employee ID and name, it can be said
that name is functionally dependent upon ID because an employee's name can be uniquely
determined from their ID. However, the reverse statement is not true because more than one
employee can have the same name but different ID.
Full functional dependency
A full functional dependency occurs when you already meet the requirements for a functional
dependency and the set of attributes on the left side of the functional dependency statement
cannot be reduced any farther.
Example: {SSN, age} name is a functional dependency, but it is not a full functional
dependency because you can remove age from the left side of the statement without impacting
the dependency relationship.
Transitive dependency.
Transitivity dependency is a functional dependency between non-primary-key.
I.e.: It occurs when there is an indirect relationship that causes a functional dependency.
Example: AC is a transitive dependency when “A B” and “B C” are true.
Introduction to SQL
Database Administration Lecture notes for level – 3 & 4 Trainees 2015
SQL stands for Structured Query Language. SQL is used to communicate with a
database. According to ANSI (American National Standards Institute), it is the standard
language for relational database management systems.
Data Dictionary
A Data dictionary is a collection of descriptions of the data or items in a data model for the
benefit of programmers and others who need to refer to them. I.e.: It is a file that defines the
basic organization of a database.
The definitions of all schema objects in the database (tables, views, indexes, clusters,
procedures, functions, triggers, and so on).
Name of the tables, names of attributes of each table, Data type of each field, Length of
each field.
How much space has been allocated for, and is currently used by the schema objects.
Default values for columns
Logical structure of database
Integrity constraint information
Privileges and roles each user has been granted
Auditing information, such as who has accessed or updated various schema objects
DML
Data Manipulation Language (DML) statements are used for managing data within schema
objects. Some examples:
SELECT - retrieve data from the a database
INSERT - insert data into a table
Database Administration Lecture notes for level – 3 & 4 Trainees 2015
DCL
Data Control Language (DCL) statements. Data Control Language (DCL) component of the SQL
language is used to create privileges to allow users access to, and manipulation of, the database.
There are two main commands:
GRANT : to grant a privilege to a user
REVOKE:-to revoke (remove) a privilege from a user. Some examples:
GRANT - gives user's access privileges to database.
REVOKE - withdraw access privileges given with the GRANT command.
TCL
Transaction Control (TCL) statements are used to manage the changes made by DML
statements. It allows statements to be grouped together into logical transactions.
COMMIT - save work done
SAVEPOINT - identify a point in a transaction to which you can later roll back
ROLLBACK - restore database to original since the last COMMIT
SET TRANSACTION - Change transaction options like isolation level and what rollback
segment to use.
SQL Constraints
SQL constraints are used to specify rules for the data in a table.
If there is any violation between the constraint and the data action, the action is aborted by the
constraint.
Constraints can be specified when the table is created (inside the CREATE TABLE statement) or
after the table is created (inside the ALTER TABLE statement).
Flow chart
A flowchart is a picture of the separate steps of a process in sequential order.
A flowchart is a visual representation of the sequence of steps and decisions needed to perform a
process. Each step in the sequence is noted within a diagram shape. Steps are linked by
connecting lines and directional arrows. This allows anyone to view the flowchart and logically
follow the process from beginning to end.
Symbols Name/Description/
Terminator
(Terminal Point, start or Stop Oval)
Data
(I/O)
Decision
Flow Line
(Arrow, Connector)
Database Administration Lecture notes for level – 3 & 4 Trainees 2015
Process
Deny Vs Revoke
GRANT, REVOKE, and DENY commands are T-SQL commands in SQL Server for managing
permission. Although we know the correct usage of them, REVOKE and DENY have confused
most of us, what exactly SQL Server does for these two commands. Here is a brief explanation
on them;
Database Administration Lecture notes for level – 3 & 4 Trainees 2015
SQL Functions
Function is a database object in SQL Server. Basically it is a set of SQL statements that accepts
only input parameters, perform actions and return the result. Function can return only single
value or a table. We can’t use function to Insert, Update, and Delete records in the database
table.
There are 2 basic Types of SQL functions:
1. Aggregate Function
2. Scalar functions
Aggregate functions operate on a SQL scalar functions return a single value,
collection of values and return a single based on the input value.
value. SQL aggregate functions return a Useful scalar functions:
single value, calculated from values in a UCASE() - Converts a field to upper case
column. LCASE() - Converts a field to lower case
MID() - Extract characters from a text field
Useful aggregate functions: LEN() - Returns the length of a text field
AVG() - Returns the average value ROUND() - Rounds a numeric field to the
COUNT() - Returns the number of rows number of decimals specified
FIRST() - Returns the first value NOW() - Returns the current system date
LAST() - Returns the last value and time
MAX() - Returns the largest value FORMAT() - Formats how a field is to be
MIN() - Returns the smallest value displayed.
SUM() - Returns the sum
Database Back up
Backup is the activity of copying files or databases so that they will be preserved in case of
equipment failure or other catastrophe. You should always have proper backup plan in place to
protect your database from failures.
Logical backup
Logical backup means backing up the individual database objects such as tables, views,
indexes. Using the utility called EXPORT, provided by Oracle. The objects exported in this way
can be imported into either same database or into any other database. The backed-up copy of
Database Administration Lecture notes for level – 3 & 4 Trainees 2015
information is stored in a dump file, and this file can be read using another utility called
IMPORT. There is no other way you can use this file.
The logical backup technique uses the IMPORT/EXPORT utilities to create the backup copy of
the database. A logical backup will backup the contents of the database.
It extracts every record in the database and stores it in a different format. Thus, it is not an exact
replica of the database file. Logical backups reclaim space occupied by deleted records, thereby
reducing database size. Logical backup will take more time compared to physical backup.
Hence, the restore process will need more time.
Logical backup will require less space for backup storage, while the physical backup will need
almost similar to production database size. In logical backup, you dont take the copies of any
physical things, you only extract the data from the datafiles into dump files. (Ex: using export).
Physical backup
This is an operating system backup. It saves the database data files onto tape or some other
storage media. This type of backup is often used to recover the database to the point of failure.
Physical backups rely on the Operating System to make a copy of the physical files like data
files, log files, control files that comprise the database. a physical backup creates an exact replica
of the database during backup process. Physical backup is to copy for backing up all the physical
files that belongs to database. (Like datafiles, control files, log files, executables etc).
Business Rule
A business rule is statements that put some form of constraint on a specific aspect of the
database, such as the elements within a field specification for a particular field or the
Database Administration Lecture notes for level – 3 & 4 Trainees 2015
Business Rules give the proper classification of entities, attributes, relationships, and
constraints. Sources of business rules are managers, policy makers, department
managers, written documentation, procedures, standards, operation manuals, and
interviews with end users.
Example
Departments ------offers---------Course
Course----------generates---------Class
Professor --------teaches----------Class
Inter-entity
Data Mining?
It is a process used by companies to turn raw data into useful information.
Data mining (sometimes called data or knowledge discovery) is the process of
analyzing data from different perspectives and summarizing it into useful
information - information that can be used to increase revenue, cuts costs, or
both. Data mining is the process of finding correlations or patterns among
dozens of fields in large relational databases.
Dead Lock?
Deadlock means Locking put on data when one transaction is being under operation on a
record. A deadlock occurs when two or more processes are waiting on the same resource
Database Administration Lecture notes for level – 3 & 4 Trainees 2015
and each process is waiting on the other process to complete before moving forward.
Deadlock occurs when one connection is blocked and waiting for a second to complete
its work, and this situation is again with another process as it waiting for first connection
to release the lock. Hence deadlock occurs.