Unit 5 - Database Management System - WWW - Rgpvnotes.in
Unit 5 - Database Management System - WWW - Rgpvnotes.in
UNIT-V
DBMS Vs RDBMS
DBMS RDBMS
1) DBMS applications store data as file. RDBMS applications store data in a tabular form.
In DBMS, data is generally stored in either a In RDBMS, the tables have an identifier called primary key and
2)
hierarchical form or a navigational form. the data values are stored in the form of tables.
3) Normalization is not present in DBMS. Normalization is present in RDBMS.
DBMS does not apply any security with RDBMS defines the integrity constraint for the purpose of ACID
4)
regards to data manipulation. (Atomicity, Consistency, Isolation and Durability) property.
in RDBMS, data values are stored in the form of tables, so a
DBMS uses file system to store data, so there
5) relationship between these data values will be stored in the form
will be no relation between the tables.
of a table as well.
DBMS has to provide some uniform methods RDBMS system supports a tabular structure of the data and a
6)
to access the stored information. relationship between them to access the stored information.
DBMS does not support distributed
7) RDBMS supports distributed database.
database.
DBMS is meant to be for small organization
RDBMS is designed to handle large amount of data. it supports
8) and deal with small data. it supports single
multiple users.
user.
9) Examples of DBMS are file systems, xml etc. Example of RDBMS are MySQL, postage, SQL server, oracle etc.
The top ost layer o tai s the se i es that a e ’t u i ue to M “QL. The ’ e se i es ost et o k- ased
lie t/se e tools o se e s eed: o e tio ha dli g, authe ti atio , se u it , a d so fo th.
The se o d layer is he e thi gs get i te esti g. Mu h of M “QL’s ai s a e he e, i ludi g the ode fo ue
pa si g, a al sis, opti izatio , a hi g, a d all the uilt-i fu tio s e.g., dates, ti es, ath, a d e ptio .
A fu tio alit p o ided a oss sto age e gi es li es at this le el: sto ed p o edu es, t igge s, a d ie s, fo
e a ple.
The third layer o tai s the sto age e gi es. The a e espo si le fo sto i g a d et ie i g all data sto ed i
M “QL. Like the a ious files ste s a aila le fo GNU/Li u , ea h sto age e gi e has its o e efits a d
d a a ks. The se e o u i ates ith the th ough the storage e gi e API. This i te fa e hides diffe e es
et ee sto age e gi es a d akes the la gel t a spa e t at the ue la e . The API o tai s a ouple of
doze lo -le el fu tio s that pe fo ope atio s su h as egi a t a sa tio o fet h the o that has this
p i a ke . The sto age e gi es do ’t pa se “QL o o u i ate ith ea h othe ; the si pl espo d to
e uests f o the se e .
Primary Storage
The category includes storage media that can be operated on directly by the computer Central Processing
Unit(CPU), such as the computer main memory and smaller but faster cache memories. Primary storage usually
provides fast access to data but is of limited storage capacity.
Secondary Storage
This category includes magnetic disks, optical disks, and tapes. These devices usually have a larger capacity,
cost less, and provide slower access to data than do primary storage devices. Data in secondary storage cannot
be processed directly by the CPU: it must first be copied into
Primary storage.
The storage media are classified by the speed with which data can be accessed, by the cost per unit of data to
u the ediu , a d the ediu ’s elia ilit . Let’s look i to the edia that a e t pi all a ailable.
Cache: The cache is the fastest and most costly form of storage. Cache memory is small. The computer hardware
manages its use
Main Memory: The general-purpose machine instructions operate on main memory. If a power failure or system
crash occurs, the contents of main memory are usually lost. (i.e.) volatile memory type.
Flash Memory: Also, known as Electrically Erasable programmable read only memory (EEPROM). Data Survive
power failure in flash memory (Non-volatile Type). Reading data from flash memory takes less than 100
nanoseconds, which is as fast as reading data from main memory. However, writing data to flash memory is
more complicated. Data can be written once, but cannot be overwritten directly. To overwrite memory that has
been written already, we have to erase an entire bank of memory once.
It is used for storing small volumes of data (5-10 MB) in hand-held computers, digital cameras.
Table Spaces
A data file that can hold data for one or more InnoDB tables and associated indexes.
There are many types of tablespaces based on the configuration w.r.t the information clubbing per table. These
are:
a. System tablespace b. File per tablespace c. General tablespace
System tablespace contains,
1. InnoDB data dictionary.
2. DoubleWrite Buffer.
3. Change buffer
4. Undo Logs.
Apart from this it also contains,
1. Tables &
2. Index data
Associated file is. idbdata1
The innodb_file_per_table option, which is enabled by default in MySQL 5.6 and higher, allows tables to be
created in file-per-table tablespaces, with a separate data file for each table. Enabling the innodb_file_per_table
option makes available other MySQL features such as table compression and transportable tablespaces.
My sql allocates logical database space for all data in a database. The units of database space allocation are data
blocks, extents, and segments. Figure shows the relationships among these data structures:
Figure 5.1 The Relationships Among Segments, Extents, and Data Blocks"
At the finest level of granularity, Mysql stores data in data blocks (also called logical blocks, My sql blocks,
or pages). One data block corresponds to a specific number of bytes of physical database space on disk.
The next level of logical database space is an extent. An extent is a specific number of contiguous data blocks
allocated for storing a specific type of information.
The level of logical database storage greater than an extent is called a segment. A segment is a set of extents,
each of which has been allocated for a specific data structure and all of which are stored in the same tablespace.
For example, each table's data is stored in its own data segment, while each index's data is stored in its
own index segment. If the table or index is partitioned, each partition is stored in its own segment.
My sql allocates space for segments in units of one extent. When the existing extents of a segment are full, My
sql allocates another extent for that segment. Because extents are allocated as needed, the extents of a segment
may or may not be contiguous on disk.
A segment and all its extents are stored in one tablespace. Within a tablespace, a segment can include extents
from more than one file; that is, the segment can span datafiles. However, each extent can contain data from
only one datafile.
Although you can allocate additional extents, the blocks themselves are allocated separately. If you allocate an
extent to a specific instance, the blocks are immediately allocated to the free list. However, if the extent is not
allocated to a specific instance, then the blocks themselves are allocated only when the high-water mark moves.
The high-water mark is the boundary between used and unused space in a segment.
Profiles
MySQL query profiling is a useful technique when trying to analyze the overall performance of a database driven
application. When developing a mid to large size application, there tends to be hundreds of queries distributed
throughout a large code base and potentially numerous queries ran against the database per second. Without
some sort of query profiling techniques, it becomes very difficult to determine locations and causes of
bottlenecks and applications slow down. This article will demonstrate some useful query profiling techniques
using tools that are built into MySQL server.
Dedicated Server
A dedicated server is a single computer in a network reserved for serving the needs of the network. For example,
some networks require that one computer be set aside to manage communications between all the other
computers. A dedicated server could also be a computer that manages printer resources. Note, however, that
not all servers are dedicated. In some networks, it is possible for a computer to act as a server and perform
other functions as well.
In the Web hosting business, a dedicated server is typically a rented service. The user rents the server, software
and an Internet connection from the Web host.
Multithreaded server
Multithreaded server, also known as a shared server, allows many user processes to share a few shared server
processes to connect to the database. Without MTS, each user process spawns its own dedicated server process,
consuming OS memory. A dedicated server process remains associated to the user process for the remainder of
the connection.
Multithreading will allow a program or an OS to handle multiple users and requests simultaneously by utilizing
multiple threads, as opposed to running the program multiple times. It uses the program resources more
effectively by handling multiple queries or commands and tracking them with multiple threads until completed.
Advantages of Multithreading
Increase performance
Concurrency
Reduce number of servers needed reducing cost and maintenance
Compatibility with applications that create client threads such as API
Parallel tasks
More efficient use of system resources
Potential deadlocks
Increased complexity in programming as well as troubleshooting problems
A dist i uted data ase DDB is a olle tio of ultiple, logically i terrelated data ases dist i uted o e a
co puter etwork.
A dist i uted data ase a age e t s ste D-DBM“ is the soft a e that a ages the DDB a d p o ides a
a ess e ha is that akes this dist i utio t a spa e t to the use s.
Dist i uted data ase s ste DDB“ = DDB + D– DBM“
What is ot a DDB“?
A ti esha i g o pute s ste .
A loosel o tightl oupled ultip o esso s ste .
A data ase s ste hi h esides at o e of the odes of a et o k of o pute s - this is a e t alized data ase
o a et o k od.
S apshot
A snapshot is a replica of a target master table from a single point-in-time. Whereas in multimaster replication
tables are continuously being updated by other master sites, snapshots are updated by one or more master
tables via individual batch updates, known as a refresh, from a single master site.
Snapshots also have the option of containing a WHERE clause so that snapshot sites can contain custom data
sets, which can be very helpful for regional offices or sales forces that don't require the complete corporate
data set.
Why use Snapshots?
Oracle offers a variety of snapshots to meet the needs of many different replication (and non-replication)
situations; each of these snapshots will be discussed in detail in following sections. You might use a snapshot to
achieve one or more of the following:
Ease Network Loads
Mass Deployment
Data Subsetting
Disconnected Computing
Data dictionary
A metadata (also called the data dictionary) is the data about the data. It is the self describing nature of the
database that provides program-data independence. It is also called as the System Catalog. It holds the
following information about each data element in the databases, it normally includes:
Name
Type
Range of values
Source
Access authorization
Indicates which application programs use the data so that, when a change in a data structure is
contemplated, a list of the affected programs can be generated.
physical. Database security is a specialist topic within the broader realms of computer security, information
security and risk management.
Privileges
Authorization includes primarily two processes:
Permitting only certain users to access, process, or alter data.
Applying varying limitations on user access or actions. The limitations placed on (or removed from)
users can apply to objects such as schemas, tables, or rows or to resources such as time (CPU, connect, or idle
times).
A user privilege is the right to run a particular type of SQL statement, or the right to access an object that
belongs to another user, run a PL/SQL package, and so on. The types of privileges are defined by Oracle
Database.
Managing User Role
This section contains:
About User Roles
Predefined Roles in an Oracle Database Installation
Creating a Role
Specifying the Type of Role Authorization
Dropping Roles
Restricting SQL*Plus Users from Using Database Roles
Uses of Roles
In general, you create a role to serve one of two purposes:
To manage the privileges for a database application (see "Common Uses of Application Roles")
To manage the privileges for a user group (see "Common Uses of User Roles")
Introduction to SQL
The history of SQL begins in an IBM laboratory in San Jose, California, where SQL was developed in the late
1970s. The initials stand for Structured Query Language, and the language itself is often referred to as "sequel."
It was originally developed for IBM's DB2 product (a relational database management system, or RDBMS, that
can still be bought today for various platforms and environments). In fact, SQL makes an RDBMS possible. SQL
is a nonprocedural language, in contrast to the procedural or third-generation languages (3GLs) such as COBOL
and C that had been created up to that time.
1. Information rule-- All information in a relational database (including table and column names) is represented
explicitly as values in tables.
2. Guaranteed access--Every value in a relational database is guaranteed to be accessible by using a combination
of the table name, primary key value, and column name.
3. Systematic null value support--The DBMS provides systematic support for the treatment of null values
(unknown or inapplicable data), distinct from default values, and independent of any domain.
4. Active, online relational catalog--The description of the database and its contents is represented at the logical
level as tables and can therefore be queried using the database language.
5. Comprehensive data sublanguage--At least one supported language must have a well-defined syntax and be
comprehensive. It must support data definition, manipulation, integrity rules, authorization, and transactions.
6. View updating rule--All views that are theoretically updatable can be updated through the system.
7. Set-level insertion, update, and deletion--The DBMS supports not only set-level retrievals but also set-level
inserts, updates, and deletes.
8. Physical data independence--Application programs and ad hoc programs are logically unaffected when
physical access methods or storage structures are altered.
9. Logical data independence--Application programs and ad hoc programs are logically unaffected, to the extent
possible, when changes are made to the table structures.
10. Integrity independence--The database language must be capable of defining integrity rules. They must be
stored in the online catalog, and they cannot be bypassed.
11. Distribution independence--Application programs and ad hoc requests are logically unaffected when data is
first distributed or when it is redistributed.
12. No subversion--It must not be possible to bypass the integrity rules defined through the database language
by using lower-level
Module Language-Uses procedures within programs. These procedures can be called by the application program
and can return values to the program via parameter passing.
Embedded SQL-Uses SQL statements embedded with actual program code. This method often requires the use
of a precompile to process the SQL statements. The standard defines statements for Pascal, FORTRAN, COBOL,
and PL/1.
Direct Invocation-Left up to the implementer. Before the concept of dynamic SQL evolved, embedded SQL was
the most popular way to use SQL within a program. Embedded SQL, which is still used, uses static SQL--meaning
that the SQL statement is compiled into the application and cannot be changed at runtime. The principle is much
the same as a compiler versus an interpreter. The performance for this type of SQL is good; however, it is not
flexible--and cannot always meet the needs of today's changing business environments. Dynamic SQL is
discussed shortly.
The ANSI 1992 standard (SQL-92) extended the language and became an international standard. It defines three
levels of SQL compliance: entry, intermediate, and full. The new features introduced include the following:
Connections to databases
Scrollable cursors
Dynamic SQL
Outer joins
SQL queries
Structure Query Language(SQL) is a programming language used for storing and managing data in RDBMS. SQL
was the first commercial language introduced for E.F Codd's Relational model. Today almost all RDBMS (MySql,
Oracle, Infomix, Sybase, MS Access) uses SQL as the standard database language. SQL is used to perform all type
of data operations in RDBMS.
SQL Joins
SQL Joins are used to relate information in different tables. A Join condition is a part of the sql query that
retrieves rows from two or more tables. A SQL Join condition is used in the SQL WHERE Clause of select, update,
delete statements.
Joins in SQL
The SQL Syntax for joining two tables is:
Syntax:
SELECT column_list
FROM table1, table2....
WHERE table1.column_name =
table2.column_name;
or
SELECT *
FROM table1
JOIN table2
[ON (join_condition)]
If a sql join condition is omitted or if it is invalid the join operation will result in a Cartesian product. The Cartesian
product returns a number of rows equal to the product of all rows in all the tables being joined. For example, if
the first table has 20 rows and the second table has 10 rows, the result will be 20 * 10, or 200 rows. This query
takes a long time to execute.
SQL Joins Example
Lets use the below two tables to explain the sql join conditions.
The columns must be referenced by the table name in the join condition, because product_id is a column in
both the tables and needs a way to be identified. This avoids ambiguity in using the columns in the SQL SELECT
statement.
The number of join conditions is (n-1), if there are more than two tables joined in a query where 'n' is the number
of tables involved. The rule must be true to avoid Cartesian product.
We can also use aliases to reference the column name, then the above query would be like,
Result
ANY
The ANY operator returns true if any of the subquery values meet the condition.
SELECT column_name(s)
FROM table_name
WHERE column_name operator ANY
(SELECT column_name FROM table_name WHERE condition);
ALL
The ALL operator returns true if all of the subquery values meet the condition.
SELECT column_name(s)
FROM table_name
WHERE column_name operator ALL
(SELECT column_name FROM table_name WHERE condition);
LIKE Operator
The LIKE operator is used in a WHERE clause to search for a specified pattern in a column.
There are two wildcards used in conjunction with the LIKE operator:
% The percent sign represents zero, one, or multiple characters
_ The underscore represents a single character
SELECT column1, column2, ...
FROM table_name
WHERE columnN LIKE pattern;
Hierarchical Queries
A hierarchy is built upon a parent-child relationship within the same table or view.
CONNECT BY specifies the relationship between parent rows and child rows of the hierarchy.
The NOCYCLE parameter instructs Oracle Database to return rows from a query even if a CONNECT BY LOOP
exists in the data. Use this parameter along with the CONNECT_BY_ISCYCLE pseudocolumn to see which rows
contain the loop. Please refer to CONNECT_BY_ISCYCLE Pseudocolumn for more information.
In a hierarchical query, one expression in condition must be qualified with the PRIOR operator to refer to the
parent row.
Inline Query
When you use SQL Subquery in From clause of the select statement it is called inline view.
A common use for inline views in Oracle SQL is to simplify complex queries by removing join operations and
condensing several separate queries into a single query. A subquery which is enclosed in parenthesis in the
FROM clause may be given an alias name. The columns selected in the subquery can be referenced in the
parent query, just as you would select from any normal table or view.
Example
Display the top five earner names and salaries from the EMPLOYEES table:
Flashback Query
It is useful to recover from accidental statement failures. For example, suppose a user accidently deletes rows
from a table and commits it also then, using flash back query he can get back the rows.
Flashback feature depends upon on how much undo retention time you have specified. If you have set the
UNDO_RETENTION parameter to 2 hours then, Oracle will not overwrite the data in undo tablespace even
after committing until 2 Hours have passed. Users can recover from their mistakes made since last 2 hours
only.
For example, suppose John gives a delete statement at 10 AM and commits it. After 1 hour he realizes that
delete statement is mistakenly performed. Now he can give a flashback AS.. OF query to get back the deleted
rows like this.
Flashback Query
SQL>select * from emp as of timestamp sysdate-1/24;
Or
SQL> SELECT * FROM emp AS OF TIMESTAMP
TO_TIMESTAMP('2007-06-07 10:00:00', 'YYYY-MM-DD HH:MI:SS')
To insert the accidently deleted rows again in the table he can type
SQL> insert into emp (select * from emp as of timestamp sysdate-1/24)
Description
DECLARE
An optional keyword that starts the DECLARE statement, which can be used to declare data types, variables, or
cursors. The use of this keyword depends upon the context in which the block appears.
declaration
Specifies a variable, cursor, or type declaration whose scope is local to the block. Each declaration must be
terminated by a semicolon.
BEGIN
A mandatory keyword that introduces the executable section, which can include one or more SQL or PL/SQL
statements. A BEGIN-END block can contain nested BEGIN-END blocks.
statement
Specifies a PL/SQL or SQL statement. Each statement must be terminated by a semicolon.
EXCEPTION
An optional keyword that introduces the exception section.
WHEN exception-condition
Specifies a conditional expression that tests for one or more types of exceptions.
THEN handler-statement
Specifies a PL/SQL or SQL statement that is executed if a thrown exception matches an exception in exception-
condition. Each statement must be terminated by a semicolon.
END
A mandatory keyword that ends the block.
Examples
The following example shows the simplest possible anonymous block statement that the DB2 data server can
compile:
BEGIN
NULL;
END;
Example:
DECLARE
current_date DATE := SYSDATE;
BEGIN
dbms_output.put_line( current_date );
END;
Cursor
Database cursor is a control structure that enables traversal over the records in a database. Cursors facilitate
subsequent processing in conjunction with the traversal, such as retrieval, addition and removal of database
records.
The e a e t o t pes of u so s −
1) Implicit Cursors
Implicit cursors are automatically created by Oracle whenever an SQL statement is executed, when there is no
explicit cursor for the statement. Programmers cannot control the implicit cursors and the information in it.
2) Explicit Cursors
Explicit cursors are programmer-defined cursors for gaining more control over the context area. An explicit
cursor should be defined in the declaration section of the PL/SQL Block. It is created on a SELECT Statement
which returns more than one row.
Creating an explicit cursor is –
Stored procedure
A procedure (often called a stored procedure) is a subroutine like a subprogram in a regular computing language,
stored in database. A procedure has a name, a parameter list, and SQL statement(s).
Why Stored Procedures?
Stored procedures are fast. MySQL server takes some advantage of caching, just as prepared statements do.
The main speed gain comes from reduction of network traffic. If you have a repetitive task that requires
checking, looping, multiple statements, and no user interaction, do it with a single call to a procedure that's
stored on the server.
Stored procedures are portable. When you write your stored procedure in SQL, you know that it will run on
every platform that MySQL runs on, without obliging you to install an additional runtime-environment
package, or set permissions for program execution in the operating system, or deploy different packages if you
have different computer types. That's the advantage of writing in SQL rather than in an external language like
Java or C or PHP.
Stored procedures are always available as 'source code' in the database itself. And it makes sense to link the
data with the processes that operate on the data.
Stored procedures are migratory! MySQL adheres fairly closely to the SQL:2003 standard. Others (DB2,
Mimer) also adhere.
Create Procedure
Following statements create a stored procedure. By default, a procedure is associated with the default
database (currently used database). To associate the procedure with a given database, specify the name as
database_name. stored_procedure_name when you create it. Here is the complete syntax:
Syntax:
CREATE [DEFINER = { user | CURRENT_USER }]
PROCEDURE sp_name ([proc_parameter[,...]])
[characteristic ...] routine_body
proc_parameter: [ IN | OUT | INOUT ] param_name type
type:
Any valid MySQL data type
characteristic:
COMMENT 'string'
| LANGUAGE SQL
| [NOT] DETERMINISTIC
| {CONTAINS SQL | NO SQL | READS SQL DATA
| MODIFIES SQL DATA}
| SQL SECURITY {DEFINER | INVOKER}
routine_body:
Function
A stored function is a special kind stored program that returns a single value. You use stored functions to
encapsulate common formulas or business rules that are reusable among SQL statements or stored programs.
Different from a stored procedure, you can use a stored function in SQL statements wherever an expression is
used. This helps improve the readability and maintainability of the procedural code.
MySQL stored function syntax
DELIMITER $$
RETURN (lvl);
END
Trigger
In a DBMS, a trigger is a SQL procedure that initiates an action (i.e., fires an action) when an event (INSERT,
DELETE or UPDATE) occurs. Since triggers are event-driven specialized procedures, they are stored in and
managed by the DBMS. A trigger cannot be called or executed; the DBMS automatically fires the trigger as a
result of a data modification to the associated table. Triggers are used to maintain the referential integrity of
data by changing the data in a systematic fashion.
A DISTINCT operator
An aggregate or analytic function
A GROUP BY, ORDER BY, MODEL, CONNECT BY, or START WITH clause
A collection expression in a SELECT list
A subquery in a SELECT list
A subquery designated WITH READ ONLY
Joins, with some exceptions, as documented in Oracle Database Administrator's Guide
If a view contains pseudocolumns or expressions, then you can only update the view with
an UPDATE statement that does not refer to any of the pseudocolumns or expressions.
INSTEAD OF triggers provide the means to modify object view instances on the client-side through OCI calls.
To modify an object materialized by an object view in the client-side object cache and flush it back to the
persistent store, you must specify INSTEAD OFtriggers, unless the object view is modifiable. If the object is
read only, then it is not necessary to define triggers to pin it.
Mutating Errors
Mutating table errors occur when a trigger attempts to access a "mutating" table, like the table from which the
trigger has been called. In MySQL, triggers are not initiated by cascading foreign keys, and they cannot modify
the table from which they are called, so neither of those issues can cause a mutating table error. However,
foreign keys appear to operate before 'AFTER' triggers, so it is possible to create a trigger that will cause a
statement to fail.
Instead of Triggers
INSTEAD OF triggers can be defined on either tables or views; however, INSTEAD OF triggers are most useful for
extending the types of updates a view can support. For example, INSTEAD OF triggers can provide the logic to
modify multiple base tables through a view or to modify base tables.
END TRG_TEST_INST_OF;
DDL
Data Definition Language (DDL) statements are used to define the database structure or schema. Some
examples:
CREATE - to create objects in the database
ALTER - alters the structure of the database
DROP - delete objects from the database
TRUNCATE - remove all records from a table, including all spaces allocated for the records are removed
COMMENT - add comments to the data dictionary
RENAME - rename an object
DML
Data Manipulation Language (DML) statements are used for managing data within schema objects. Some
examples:
SELECT - retrieve data from the database
INSERT - insert data into a table
UPDATE - updates existing data within a table
DELETE - deletes all records from a table, the space for the records remain
MERGE - UPSERT operation (insert or update)
CALL - call a PL/SQL or Java subprogram
EXPLAIN PLAN - explain access path to data
LOCK TABLE - control concurrency
DCL
Data Control Language (DCL) statements. 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
CS-5003
DBMS