DBMS File
DBMS File
1. INTRODUCTION TO DBMS:
A Database Management System (DBMS) is a software system that is designed to manage and organize
data in a structured manner. A database management system (or DBMS) is essentially nothing more than
a computerized data-keeping system.
Users of the system are given facilities to perform several kinds of operations on such a system for
either manipulation of the data in the database or the management of the database structure itself.
It can be used for:
Creation of a database.
Retrieval of information from the database.
Updating the database.
Managing a database.
Multiple User Interface
Data scalability, expandability and flexibility
Security
2. COMPONENTS OF DBMS:
Storage Engine: The storage engine is the core component of the DBMS that interacts with the file
system at an OS level to store data. All SQL queries which interact with the underlying data go through
the storage engine.
Query language: A database access language is required for interacting with a database, from creating
databases to simply inserting or retrieving data. A proper DBMS must support one or multiple query
languages and language dialects. Structured query language (SQL) and MongoDB Query Language
(MQL) are two query languages that are used to interact with the databases.
In many query languages, the query language functionality can be further categorized according to
specific tasks:
Data Definition Language (DDL). This consists of commands that can be used to define database
schemas or modify the structure of database objects.
Data Manipulation Language (DML). Commands that directly deal with the data in the database.
All CRUD operations come under DML.
Data Control Language (DCL). This deals with the permissions and other access controls of the
database.
Transaction Control Language (TCL). Command which deals with internal database
transactions.
Query Processor: This is the intermediary between the user queries and the database. The query
processor interprets the queries of users and makes them actionable commands that can be understood by
the database to perform the appropriate functionality.
Optimization Engine: The optimization Engine allows the DBMS to provide insights into the
performance of the database in terms of optimizing the database itself and queries. When coupled
with database monitoring tools, it can provide a powerful toolset to gain the best performance out of the
database.
Metadata Catalog: This is the centralized catalog of all the objects within the database. When an object
is created, the DBMS keeps a record of that object with some metadata about it using the metadata
catalog. Then, this record can be used to:
Log Manager: This component will keep all the logs of the DBMS. These logs will consist of user logins
and activity, database functions, backups and restore functions, etc. The log manager ensures all these logs
are properly recorded and easily accessible.
Reporting and Monitoring tools: Reporting and monitoring tools are another standard component that
comes with a DBMS. Reporting tools will enable users to generate reports while monitoring tools enable
monitoring the databases for resource consumption, user activity, etc.
Data Utilities: In addition to all the above, most DBMS software comes with additional inbuilt utilities to
provide functionality such as:
Etc.
3. ORACLE
Oracle database is a relational database management system. It is also called OracleDB, or simply Oracle.
It is produced and marketed by Oracle Corporation. It was created in 1977 by Lawrence Ellison and other
engineers. It is one of the most popular relational database engines in the IT market for storing,
organizing, and retrieving data.
Oracle database was the first DB that designed for enterprise grid computing and data warehousing.
Enterprise grid computing provides the most flexible and cost-effective way to manage information and
applications. It uses SQL queries as a language for interacting with the database.
4. FEATURES OF ORACLE
Oracle database manages data with the help of an open, complete, and integrated approach. The
following are features that complete the demand for powerful database management:
Availability: It is never offline or out of service that means supported 24*7 availability of the
database. It provides high availability of databases because of the Oracle Data Guard functionality.
This functionality allows using of the secondary database as a copy of the primary database during
any failure. As a result, all normal processes such as backups and partial failures do not interrupt
the database from being used.
Security: Oracle has a mechanism for controlling and accessing the database to prevent
unauthorized access. It provides high security because of the Oracle Advanced Security features. It
offers two solutions to protect databases that are TDE (Transparent Data Encryption) and Data
Redaction. TDE supports data encryption both at the source and after export. Redaction is
performed at the application level. Oracle has some other security features like Oracle Database
Vault that regulates user privileges and Oracle Label Security.
Scalability: It provides features like RAC (Real Application Cluster) and Portability, which makes
an Oracle database scalable based on usage. In a clustered environment, it includes capabilities
such as rolling instance migrations, performing upgrades, maintaining application continuity,
quality of service management, etc.
Oracle Advanced Analytics: It is a technical combination of Oracle R Enterprise and Oracle Data
Mining that assists customers in determining predictive business models through data and text
mining, as well as statistical data computation.
Management: Oracle Multitenant is a database management tool that combines a single container
database with many pluggable databases in a consolidated design.
1. Insert Oracle CD , the autorun window opens automatically. If you are installing from network
or hard disk, click setup.exe in the installation folder.
2. The Oracle Universal Installer (OUI) will run and display the Select Installation
Method Window.
3. Choose Basic Installation:
Select this option to quickly install Oracle Database 10g. This method requires minimal user
input. It installs the software and optionally creates a general-purpose database based on the
information you provide.
For basic installation, you specify the following:
Oracle Home Location — Enter the directory in which to install the Oracle Database 10g
software. You must specify a new Oracle home directory for each new installation of Oracle
Database 10g. Use the default value, which is : c:\oracle\product\10.2.0\db_1
5. A summary screen appears showing information such as your global settings, space
requirements and the new products to be installed. Click Install to start the installation
6. The Install window appears showing installation progress.
7. At the end of the installation phase, the Configuration Assistants window appears. This
window lists the configuration assistants that are started automatically.
If you are creating a database, then the Database Configuration Assistant starts automatically in
a separate window.
At the end of database creation, you are prompted to unlock user accounts to make the
accounts accessible. The SYS and SYSTEM accounts are already unlocked. Click OK to
bypass password management.
8. Your installation and database creation is now complete. The End of Installation window
displays several important URLs, one of which is for Enterprise Manager.
You can navigate to this URL in your browser and log in as the SYS user with the associated
password, and connect as SYSDBA. You use Enterprise Manager to perform common database
administration tasks
PRACTICAL NO.2
AIM: Data Types , Creating Tables, Retrieval of Rows using Select Statement ,
Conditional Retrieval of rows , Alter and Drop Statements.
DATATYPES:
Data Types are means to identify the type of data and associated operations for handling it. A value’s
datatype associates a fixed set of properties of with value. Some commonly used data types supported by
SQL are:
CREATE TABLE
Tables are defined with CREATE TABLE command. When a table is created, its columns are named, data
types and sizes are supplied for each column.
RETRIEVAL OF ROWS
The SELECT statement is used to pull information from a table. To retrieve all the rows of any table, we
just need to specify asterisk in select-list
ALTER TABLE
The ALTER TABLE command is used to change definitions of existing tables. Usually , it is used to add
columns to an existing , but sometimes it is used to delete or modify columns as well.
AIM: Working with Null Values, matching a Pattern from a Table, Ordering the Result of a
Query, Aggregate Functions, Grouping the Result of a Query, Update and Delete
Statements.
1. NOT NULL
By default, a column can take NULL value if no value is provided. To avoid this, we can make use
of NOT NULL constraint specifying that NULL is now not an allowable value
2. PATTERN MATCHING
SQL also includes a string-matching operator, LIKE, for comparisons on character strings using
patterns. Patterns are described using two special wildcard characters:
3. ORDER BY
The ORDER BY clause is used to sort selected results in ascending or descending order.
4. BETWEEN
The BETWEEN operator defines a range of values that the column values must fall in to make the
condition true.
5. GROUP BY
The GROUP BY statement groups rows that have the same values into summary rows
6. AGGREGATE FUNCTIONS:
SQL also supports group functions which operate upon a group of rows.
Some of those functions are:
e. Sum: This function returns the sum of all values from a column
7. UPDATE ROWS
The UPDATE statement is used to modify the existing records in a table.
8. DELETE ROWS
The DELETE statement is used to delete existing records in a table
3. INTERSECT:The intersect operator combines two select statements and return only
those rows that are returned by both queries.
SYNTAX:SQL>SELECT INTERSECT SELECT ;
4. MINUS:It combines the result of two queries and returns only those values that are
selected by first query but not in second query.
SYNTAX :SQL>SELECT MINUS SELECT ;
l Nested Queries: A sub query is a form of an SQL statement that appears inside another
SQL statement. It is also termed as nested query. The statement containing a sub query is
called a parent statement. The parent statement uses the rows returned by the sub query. It
can be used by the following commands:
1. To insert records in a target table.
2. To create tables and insert records in the table created.
3. To update records in a target table.
4. To create views.
5. To provide values for conditions in WHERE, HAVING, IN etc. used with SELECT,
UPDATE, and DELETE statements.
TYPES OF SUB QUERIES
l SINGLE ROW
It returns one row from inner nested query.
EXAMPLE IS: SQL>SELECT deptno FROM emp WHERE ename =’MILLER’;
l MULTIPLE ROW
Subqueries that return more than one row called multiple row queries. Operators like
IN,ALL,ANY are used.
EXAMPLESQL>SELECT ename,sal,deptno FROM emp WHERE sal IN (SELECT
min(sal) FROM emp GROUP BY deptno);
l JOINS:A JOIN can be recognized in sql select statement if its has more than one table
after from keyword. This join condition is based on primary keys and foreign keys. There
must be n-1 join conditions for n joins to tables. If join condition is omitted then the result
is Cartesian product.
SYNTAX
SQL>SELECT “list of columns” FROM table1, table2 WHERE “condition”;
TYPES OF JOINS
1. EQUI JOIN : It returns all rows from tables where there is a match. Tables are joined on
columns that have the same datatype & size in table. It is also known as equality join or
simple join or inner join.
SYNTAX:
SELECTfield1,field2 FROM table1,table2 WHERE table1.field=table2.field;
2. CARTESION JOIN : When the join condition is omitted the result is Cartesian join of
two or more tables in which all the combinations of rows will be displayed. All the rows
are joined to all rows of the second table.
SYNTAX
SQL>SELECT field1, field2 FROM table1, table2;
EXAMPLE
SQL>SELECT ename, dname FROM emp, dept;
3. OUTER JOIN : While using equi join we see that if there exists certain rows in one table
which don’t have corresponding values in the second table thn those rows will not be
selected. We can forcefully select those rows by outer join. The rows for those columns
will have NULL values.
SYNTAX
SELECT table1.col, table2.col FROM table1, table2 WHERE table1.col (+) = table2.col;
EXAMPLE
SQL>SELECT empno, ename, emp.deptno, dname FROM emp, dept WHERE emp.deptno (+)
= dept.deptno;
4. SELF JOIN : The self join can be seen as join of two copies of the same table. The table
is not actually copied but sql performs the command as though it were.
EXAMPLE
SQL>SELECT e.ename, m.ename FROM emp e, emp m WHERE e.mgr=e.empno;
l Sequence :is a set of integers 1, 2, 3, … that are generated and supported by some
database systems to produce unique values on demand.
A sequence is a user defined schema bound object that generates a sequence of numeric
values.Sequences are frequently used in many databases because many applications
require each row in a table to contain a unique value and sequences provides an easy way
to generate them.
PRACTICAL NO.5
AIM: Views, Indexes, Database Security and Privileges: Grant and Revoke
Commands, Commit and Rollback Commands.
l VIEWS :A view is very commonly used database object that is derived at runtime. A
view contains data of its own. Its contents are derived from another table. The command
for creating view is CREATE VIEW command. Editing in the tables are automatically
reflected in the views. It is virtual table & does not have any data of its own.
SYNTAX TO CREATE A VIEW IS:
SQL>CREATE [OR REPLACE] VIEW view name AS sub query
EXAMPLE IS: SQL>CREATE VIEW monika AS SELECT empno, ename, sal, comm
FROM emp;
l TYPES OF VIEWS
1. JOIN VIEW :It is defined as view that has more than one table specified in from clause
and does not contain following clauses i.e. distinct, aggregation, group by. This type of
view allows update, insert and delete command to change data in table.
SYNTAX SQL>CREATE OR REPLACE VIEW monika AS SELECT ename, empno, sal
FROM emp, dept WHERE emp.deptno = dept.deptno;
2. INLINE VIEW:Oracle also offers an inline view that is very handy and inline view is
part of SQL statements. It allows you in body of SQL statement to define SQL for view
that SQL statement will use to resolve its query.
3. MATERIALIZED VIEW:Snapshot also called materialized view. It is defined as copy
of part of table or entire table. It reflects the current status of table that is being copied.
The original status table is also called master table. Two types are Read only and update.
Read- only does not allow changes to be made in view
l Indexes:An index can be created in a table to find data more quickly and efficiently the
users cannot see the indexes, they are just used to speed up searches/queries.
Syntax
Creates an index on a table. Duplicate values are allowed:
CREATE INDEX index_name ON table_name (column_name)
SQL CREATE UNIQUE INDEX Syntax
Creates a unique index on a table. Duplicate values are not allowed:
CREATE UNIQUE INDEX index_name ON table_name (column_name)
PRACTICAL 7
CREATE OR REPLACE
PROCEDURE [schema] procedure_name (argument {IN, OUT, IN OUT}
datatype){IS, AS} Variable declarations;
Constant declarations; BEGIN
PL/SQL subprogram body; EXCEPTION
Exception PL/SQL
block; END;
Types of Procedures:
1. Local Procedure
2. Stored Procedure
Local Procedure: These procedures are declared within the PL/SQL block and
called from the begin section of the PL/SQL block.
The following is a simple example of a procedure:
Program:
declare
a number; b number; c number; d number; e number; f number;
procedure process ( a in number, b in number, c out number, d out number,
e out number, f out number) is
begin c:=a+b; d:=a-b; e:=a*b; f:=a/b; end; begin
a:=&firstnumber; b:=&secondnumber;
process(a, b, c, d, e, f); DBMS_output.put_line(‘addition is’ || c);
DBMS_output.put_line(‘subtraction is’ || d);
DBMS_output.put_line(‘multiplication is’ || e);
DBMS_output.put_line(‘division is’ || f);
end;
OUTPUT
Syntax:
WHEN exception THEN statement;
Program:
PRACTICAL NO. 8
Types of Triggers
• Row Triggers: A row trigger is fired each time the table is affected by the triggering
statement, for example, if an UPDATE statement update multiple rows of a table, a row
trigger is fired once for each row affected by the UPDATE statement.
• Statement Triggers: A row trigger is fired once on behalf of the triggering statement,
independent of the number of rows the triggering statement affects.
• Before Triggers: Before triggers execute the trigger action before the triggering
statement. These types of triggers are commonly used in the following situations:
1. BEFORE triggers are used when the trigger action should determine whether or
not the triggering statement should be allowed to complete. By using BEFORE
trigger, user can eliminate unnecessary processing of the triggering statement.
2. BEFORE triggers are used to derive specific column values before completing
a triggering INSERT or UPDATE statement.
• After Triggers: After triggers execute the trigger after the triggering statement is
executed. These types of triggers are commonly used in the following situations:
1. AFTER triggers are used when the triggering statement should complete before
executing the trigger action.
2. If a BEFORE trigger is already present, an AFTER trigger can perform
different actions on the same triggering statement.
Combinations Triggers: Using the above triggers, four types of triggers could be
created. There are twelve combinations of triggers.
Before Statement Trigger: Before executing the triggering statement, the trigger action is
executed.
Before Row Trigger: Before modifying each row affected by the triggering
statement and BEFORE applying appropriate integrity constraints, the trigger is
executed.
After Statement Trigger: After executing the triggering statement and applying
and deferred integrity constraints, the trigger action is executed.
After Row Trigger: After modifying each row affected by the triggering statement
and applying appropriate integrity constraints, the trigger action is executed for the
current row. Unlike BEFORE row triggers, AFTER row triggers have rows locked.
Syntax:
Deleting a Trigger: To drop Trigger one can use DROP TRIGGER statement.
Syntax:
Example
+ + + + + +
| ID | NAME | AGE | ADDRESS | SALARY |
+ + + + + +
| 1 | Ramesh | 32 | Ahmedabad | 2000.00 |
| 2 | Khilan | 25 | Delhi | 1500.00 |
| 3 | kaushik | 23 | Kota | 2000.00 |
| 4 | Chaitali | 25 | Mumbai | 6500.00 |
| 5 | Hardik | 27 | Bhopal | 8500.00 |
| 6 | Komal | 22 | MP | 4500.00 |
+ + + + + +
The following program creates a row-level trigger for the customers table that
CUSTOMERS table.
This trigger will display the salary difference between the old values and new
When the above code is executed at the SQL prompt, it produces the following
Triggering a Trigger:
INSERT INTO CUSTOMERS (ID,NAME,AGE,ADDRESS,SALARY)
VALUES (7, ',Kriti', 22, 'HP', 7500.00 ); When a record is
created in the CUSTOMERS table, the above create trigger,
display_salary_changes will be fired and it will display the following result – Old
salary:
New salary: 7500 Salary difference:
Because this is a new record, old salary is not available and the above result
comes as null.
The UPDATE statement will update an existing record in the table –
UPDATE customers
SET salary = salary
+ 500 WHERE id
= 2;
When a record is updated in the CUSTOMERS table, the above create trigger,
CURSORS:
Oracle DBA uses a work area for its internal processing. This work area is private
to SQL’s operations and is called Cursor. The data that is stored in the cursor is
called the Active Data Set. The size of the cursor in memory is the size required to
hold the number of rows in the active data set.
A PL/SQL block of code includes the Procedural code for looping and branching
along with the SQL statement. If records from a record set created using a select
statement are to be evaluated and processed one at a time, then the only method
available is by using Explicit Cursors.
TYPES OF CURSORS:
Implicitcursors.
Explicitcursors.
cursor_name%FOUND;
3. %ISOPEN: Oracle automatically closes the SQL cursor after executing its associated
SQL statement. As a result, sql%isopenalways evaluates to FALSE. If the explicit
cursor is open then it returns true otherwise false.
Syntax:
cursor_name%ISOPEN;
Syntax:
cursor_name%ROWCOUNT;
IMPLICIT CURSORS:
Oracle implicitly opens a cursor to process each SQL statement not associated
with an explicitly declared cursor. PL/SQL lets us refer to the most recent implicit
cursor as the SQL cursor. So, although we cannot use the open, fetch, and
closestatements to control an implicit cursor, we can still use cursor attributes to
access information about the most recently executed SQL statement.
Use cursor to select the five highest paid employees from the emp table showing
tables also.
Input Table:
Declare
cursor c1 isselectename, empno, sal from emp order by saldesc; -- start with highest
paid employee my_enamevarchar2(10);
my_empno
number(4);
my_sal
number(7,2);