Académique Documents
Professionnel Documents
Culture Documents
: - 1
Aim: - Introduction to DBMS.
Introduction:
Database management system (DBMS) is computer software designed for the purpose of
managing databases based on a variety of data models.
A DBMS is a complex set of software programs that controls the organization, storage,
management, and retrieval of data in a database. DBMS are categorized according to their
data structures or types. It is a set of prewritten programs that are used to store, update
and retrieve a Database.
Based Systems
Conventionally, before the Database systems evolved, data in software systems
was stored in and represented using flat files.
• Database Systems
Database Systems evolved in the late 1960s to address common issues in
applications handling large volumes of data which are also data intensive. Some of
these issues could be traced back to the following disadvantages of File-based
systems.
Functions of a DBMS
The functions performed by a typical DBMS are the following:
• Data Definition
The DBMS provides functions to define the structure of the data in the application.
These include defining and modifying the record structure, the type and size of
fields and the various constraints/conditions to be satisfied by the data in each
field.
• Data Manipulation
Once the data structure is defined, data needs to be inserted, modified or deleted.
The functions which perform these operations are also part of the DBMS. These
function can handle planned and unplanned data manipulation needs. Planned
queries are those which form part of the application. Unplanned queries are ad-hoc
queries which are performed on a need basis.
• Data Security & Integrity
The DBMS contains functions which handle the security and integrity of data in the
application. These can be easily invoked by the application and hence the
application programmer need not code these functions in his/her programs.
• Data Recovery & Concurrency
Recovery of data after a system failure and concurrent access of records by
multiple users are also handled by the DBMS.
• Data Dictionary Maintenance
Maintaining the Data Dictionary which contains the data definition of the
application is also one of the functions of a DBMS.
• Performance
Optimizing the performance of the queries is one of the important functions of a
DBMS. Hence the DBMS has a set of programs forming the Query Optimizer which
evaluates the different implementations of a query and chooses the best among
them.
Thus the DBMS provides an environment that is both convenient and efficient to
use when there is a large volume of data and many transactions to be processed.
When a computer user wants to store data electronically they must do so by placing data
in files. Files are stored in specific locations on the hard disk (directories). The user can
create new files to place data in, delete a file that contains data, rename the file, etc -- all
known as file management; a function provided by the Operating System (OS).
If the user wishes to perform some operation on the data he has placed in the file, such as
viewing a list of his friends that celebrate their birthday in June, he has to scroll through all
the data by himself in order to see the data he is interested in. Moreover, he has to know
where he put the files that contain the data, and if there are multiple files he has to
remember to go through each one of them.
Disadvantages of DBMS :
i) High cost of DBMS
ii) High hardware cost
iii) Complex
iv) Backup and Recovery are more difficult
v) Does not support client server architecture
vi) Does not support distributed data base.
External level is the view that the individual user of the database has. This view is often a
restricted view of the database and the same database may provide a number of different
views for different classes of users. In general, the end users and even the applications
programmers are only interested in a subset of the database. For example, a department
head may only be interested in the departmental finances and student enrolments but not
the library information. The librarian would not be expected to have any interest in the
information about academic staff. The payroll office would have no interest in student
enrolments.
The conceptual view is the information model of the enterprise and contains the view of
the whole enterprise without any concern for the physical implementation. This view is
normally more stable than the other two views. In a database, it may be desirable to
change the internal view to improve performance while there has been no change in the
conceptual view of the database. The conceptual view is the overall community view of the
database and it includes all the information that is going to be represented in the
database. The conceptual view is defined by the conceptual schema which includes
definitions of search of the various types of data.
The internal view is the view about the actual physical storage of data. It tells us what data
is stored in the database and how. At least the following aspects are considered at this
level:
Efficiency considerations are the most important at this level and the data structures are
chosen to provide an efficient database. The internal view does not deal with the physical
devices directly. Instead it views a physical device as a collection of physical pages and
allocates space in terms of logical pages.
Experiment No. : - 4
Aim: Describe the SQL fundamentals
Introduction:
SQL, Structured Query Language, is the standard language used to communicate with a
relational database. The prototype was originally developed by IBM using Dr. E.F. Codd's
paper ("A Relational Model of Data for Large Shared Data Banks") as a model. In 1979, not
long after IBM's prototype, the first SQL product, ORACLE, was released by Relational
Software, Incorporated (it was later renamed Oracle Corporation). It is, today, one of the
distinguished leaders in relational database technologies.
DML
DDL
DDL is abbreviation of Data Definition Language. It is used to create and modify the
structure of database objects in database.
DCL
TCL
TCL is abbreviation of Transactional Control Language. It is used to manage different
transactions occurring within a database.
Embedded SQL
The ANSI SQL standards committee defined the embedded SQL standard in two steps: a
formalism called Module Language was defined, then the embedded SQL standard was
derived from Module Language. The SQL standard defines embedding of SQL as embedded
SQL and the language in which SQL queries are embedded is referred to as the host
language. A popular host language is C. The mixed C and embedded SQL is called Pro*C in
Oracle and Sybase database management systems. In the PostgreSQL database
management system this precompiler is called ECPG. Other embedded SQL precompilers
are Pro*Ada, Pro*COBOL, Pro*FORTRAN, Pro*Pascal, and Pro*PL/I.
Experiment No. : - 5
Aim: Introduction to Oracle data base.
Each column value and constant in a SQL statement has a datatype, which is associated
with a specific storage format, constraints, and a valid range of values. When you create a
table, you must specify a datatype for each of its columns.
• Character Datatypes
• Numeric Datatypes
• DATE Datatype
• LOB Datatypes
• RAW and LONG RAW Datatypes
• ROWID and UROWID Datatypes
Character Datatypes
The character datatypes store character (alphanumeric) data in strings, with byte values
corresponding to the character encoding scheme, generally called a character set or code
page.
The database's character set is established when you create the database. Examples of
character sets are 7-bit ASCII (American Standard Code for Information Interchange),
EBCDIC (Extended Binary Coded Decimal Interchange Code), Code Page 500, Japan
Extended UNIX, and Unicode UTF-8. Oracle supports both single-byte and multibyte
encoding schemes.
Numeric Datatypes
The numeric datatypes store positive and negative fixed and floating-point numbers, zero,
infinity, and values that are the undefined result of an operation (that is, is "not a number"
or NAN).
DATE Datatype
The DATE datatype stores point-in-time values (dates and times) in a table. The DATE
datatype stores the year (including the century), the month, the day, the hours, the
minutes, and the seconds (after midnight).
Oracle Database can store dates in the Julian era, ranging from January 1, 4712 BCE
through December 31, 9999 CE (Common Era, or 'AD'). Unless BCE ('BC' in the format
mask) is specifically used, CE date entries are the default.
Oracle Database uses its own internal format to store dates. Date data is stored in fixed-
length fields of seven bytes each, corresponding to century, year, month, day, hour,
minute, and second.
For input and output of dates, the standard Oracle date format is DD-MON-YY, as follows:
'13-NOV-92'
You can change this default date format for an instance with the parameter
NLS_DATE_FORMAT. You can also change it during a user session with the ALTER SESSION
statement. To enter dates that are not in standard Oracle date format, use the TO_DATE
function with a format mask:
Oracle Database stores time in 24-hour format—HH:MI:SS. By default, the time in a date
field is 00:00:00 A.M. (midnight) if no time portion is entered. In a time-only entry, the date
portion defaults to the first day of the current month. To enter the time portion of a date,
use the TO_DATE function with a format mask indicating the time portion, as in:
LOB Datatypes
The LOB datatypes BLOB, CLOB, NCLOB, and BFILE enable you to store and manipulate
large blocks of unstructured data (such as text, graphic images, video clips, and sound
waveforms) in binary or character format. They provide efficient, random, piece-wise
access to the data. Oracle recommends that you always use LOB datatypes over LONG
datatypes. You can perform parallel queries (but not parallel DML or DDL) on LOB columns.
LOB datatypes differ from LONG and LONG RAW datatypes in several ways. For example:
• A table can contain multiple LOB columns but only one LONG column.
• A table containing one or more LOB columns can be partitioned, but a table
containing a LONG column cannot be partitioned.
• The maximum size of a LOB is 128 terabytes depending on database block size,
and the maximum size of a LONG is only 2 gigabytes.
• LOBs support random access to data, but LONGs support only sequential access.
• LOB datatypes (except NCLOB) can be attributes of a user-defined object type but
LONG datatypes cannot.
• Temporary LOBs that act like local variables can be used to perform
transformations on LOB data. Temporary internal LOBs (BLOBs, CLOBs, and
NCLOBs) are created in a temporary tablespace and are independent of tables. For
LONG datatypes, however, no temporary structures are available.
• Tables with LOB columns can be replicated, but tables with LONG columns cannot.
SQL statements define LOB columns in a table and LOB attributes in a user-defined object
type. When defining LOBs in a table, you can explicitly specify the tablespace and storage
characteristics for each LOB.
LOB datatypes can be stored inline (within a table), out-of-line (within a tablespace, using
a LOB locator), or in an external file (BFILE datatypes). With compatibility set to Oracle9i or
higher, you can use LOBs with SQL VARCHAR operators and functions.
• The LONG RAW datatype is provided for backward compatibility with existing
applications. For new applications, use the BLOB and BFILE datatypes for large
amounts of binary data.
• Oracle also recommends that you convert existing LONG RAW columns to LOB
columns. LOB columns are subject to far fewer restrictions than LONG columns.
Further, LOB functionality is enhanced in every release, whereas LONG RAW
functionality has been static for several releases.
• The RAW and LONG RAW datatypes are used for data that is not to be interpreted
(not converted when moving data between different systems) by Oracle Database.
These datatypes are intended for binary data or byte strings. For example, LONG
RAW can be used to store graphics, sound, documents, or arrays of binary data.
The interpretation depends on the use.
• RAW is a variable-length datatype like the VARCHAR2 character datatype, except
Oracle Net Services (which connects user sessions to the instance) and the Import
and Export utilities do not perform character conversion when transmitting RAW or
LONG RAW data. In contrast, Oracle Net Services and Import/Export automatically
convert CHAR, VARCHAR2, and LONG data between the database character set and
the user session character set, if the two character sets are different.
• When Oracle Database automatically converts RAW or LONG RAW data to and from
CHAR data, the binary data is represented in hexadecimal form with one
hexadecimal character representing every four bits of RAW data. For example, one
byte of RAW data with bits 11001011 is displayed and entered as 'CB'.
• LONG RAW data cannot be indexed, but RAW data can be indexed.
Oracle Database uses a ROWID datatype to store the address (rowid) of every row in the
database.
• Physical rowids store the addresses of rows in ordinary tables (excluding index-
organized tables), clustered tables, table partitions and subpartitions, indexes, and
index partitions and subpartitions.
• Logical rowids store the addresses of rows in index-organized tables.
A single datatype called the universal rowid, or UROWID, supports both logical and
physical rowids, as well as rowids of foreign tables such as non-Oracle tables accessed
through a gateway.
A column of the UROWID datatype can store all kinds of rowids. The value of the
COMPATIBLE initialization parameter (for file format compatibility) must be set to 8.1 or
higher to use UROWID columns.
Experiment No. : - 6
Aim: Describe the data models
Data Model:
• Flat model: This may not strictly qualify as a data model. The flat (or table) model
consists of a single, two-dimensional array of data elements, where all members of
a given column are assumed to be similar values, and all members of a row are
assumed to be related to one another.
• Hierarchical model: In this model data is organized into a tree-like structure,
implying a single upward link in each record to describe the
nesting, and a sort field to keep the records in a particular
order in each same-level list.
• Network model: This model organizes data using two fundamental constructs,
called records and sets. Records contain fields, and sets define one-to-many
relationships between records: one owner, many members.
• Star schema: It is the simplest style of data warehouse schema. The star schema
consists of a few "fact tables" (possibly only one, justifying the name) referencing
any number of "dimension tables". The star schema is considered an important
special case of the snowflake schema.
Experiment No. : - 7
Aim: Describe the data models.
Entity-relationship model:
Entity:
An entity may be a physical object such as a house or a car, an event such as a house sale
or a car service, or a concept such as a customer transaction or order. Although the term
entity is the one most commonly used, following Chen we should really distinguish
between an entity and an entity-type. An entity-type is a category. An entity, strictly
speaking, is an instance of a given entity-type. There are usually many instances of an
entity-type. Because the term entity-type is somewhat cumbersome, most people tend to
use the term entity as a synonym for this term.
Entities can be thought of as nouns. Examples: a computer, an employee, a song, a
mathematical theorem. Entities are represented as rectangles.
Relationship:
A relationship captures how two or more entities are related to one another.
Relationships can be thought of as verbs, linking two or more nouns.
Examples: an owns relationship between a company and a computer, a
supervises relationship between an employee and a department, a
performs relationship between an artist and a song, a proved relationship between a
mathematician and a theorem. Relationships are represented as diamonds, connected by
lines to each of the entities in the relationship.
Entities and relationships can both have attributes. Examples: an employee entity might
have a Social Security Number (SSN) attribute; the proved relationship may have a date
attribute. Attributes are represented as ellipses connected to their owning entity sets by a
line.
Primary key:
Every entity (unless it is a weak entity) must have a minimal set of uniquely identifying
attributes, which is called the entity's primary key.
A sample ER diagram
Experiment No. : - 8
Aim: Describe about Select statement.
The SQL SELECT statement queries data from tables in the database. The statement
begins with the SELECT keyword. The basic SELECT statement has 3 clauses:
• SELECT
• FROM
• WHERE
For example
name
Mario
• FROM Clause -- specifies the tables to be accessed.
• WHERE Clause -- specifies which rows in the FROM tables to use.
The SELECT statement is used to select data from a database. The result is stored in a
result table, called the result-set.
FROM table_name
And
Now we want to select the content of the columns named "LastName" and "FirstName"
from the table above.
LastName FirstName
Hansen Ola
Svendson Tove
Pettersen Kari
SELECT * Example
Now we want to select all the columns from the "Persons" table.
SQL*Plus is a client terminal software allowing users to interact with Oracle server to
manipulate data and data structures. Users type in SQL statements in SQL*Plus that send
statements to Oracle server. Oracle server then validates and executes the statements on
its databases. The query results are returned to SQL*Plus and displayed to the user.
Besides sending SQL statements to the server, SQL*Plus also saves them into a local
buffer and allow users to view and change the statements. The following figure illustrates
the process.
Experiment No. : - 10
Aim: Introduction to Group functions in SQL.
SQL GROUP Functions
Group functions are built-in SQL functions that operate on groups of rows and return one
value for the entire group. These functions are: COUNT, MAX, MIN, AVG, SUM,
DISTINCT
SQL COUNT (): This function returns the number of rows in the table that satisfies the
condition specified in the WHERE condition. If the WHERE condition is not specified, then
the query returns the total number of rows in the table.
For Example: If you want the number of employees in a particular department, the query
would be:
If you want the total number of employees in all the department, the query would take the
form:
For Example: If you want to select all distinct department names from employee table,
the query would be:
To get the count of employees with unique name, the query would be:
SQL MAX(): This function is used to get the maximum value from a column.
To get the maximum salary drawn by an employee, the query would be:
SELECT MAX (salary) FROM employee;
SQL MIN(): This function is used to get the minimum value from a column.
SQL AVG(): This function is used to get the average value of a numeric column.
SQL SUM(): This function is used to get the sum of a numeric column
Syntax:
SELECT column, group_function (column) FROM table
[WHERE condition]
[GROUP BY group_by_experssion]
[ORDER BY column];
Here, group_by_experssion specifies columns whose value determines the basis for
grouping rows.
For example, if we have to find total salary of each department manually, first we group
the records on the basis of department no and then we apply the sum function on salary of
each group to obtain the required result.
Similarly in SQL, we apply the GROUP BY clause on department no and then calculate the
total salary for each group by sum (sal) function as shown below:
The following figure shows the grouping and the execution of the query:
DEPTN SUM(SA
O L)
10 8750
20 10875
30 9400
Experiment No. : - 12
Aim: Joining of tables for multiple queries.
Types of Joins:
While different implementations have many ways of joining tables, you concentrate on the
most common joins in this lesson. The types of joins that you learn are
i) EQUIJOINS
ii) NATURAL JOINS
iii) NON-EQUIJOINS
iv) OUTER JOINS
v) SELF JOINS
Joins of Equality:
Perhaps the most used and important of the joins is the EQUIJOIN, also referred to as an
INNER JOIN. The EQUIJOIN joins two tables with a common column in which each is usually
the primary key.
Example:
SELECT EMPLOYEE_TBL.EMP_ID,
EMPLOYEE_PAY_TBL.DATE_HIRE
FROM EMPLOYEE_TBL,
EMPLOYEE_PAY_TBL
WHERE EMPLOYEE_TBL.EMP_ID = EMPLOYEE_PAY_TBL.EMP_ID;
Natural Joins:
A NATURAL JOIN is nearly the same as the EQUIJOIN; however, the NATURAL JOIN differs
from the EQUIJOIN by eliminating duplicate columns in the joining columns. The JOIN
condition is the same, but the columns selected differ.
The syntax:
SELECT TABLE1.*, TABLE2.COLUMN_NAME
[ TABLE3.COLUMN_NAME]
FROM TABLE1, TABLE2 [ TABLE3 ]
WHERE TABLE1.COLUMN_NAME = TABLE2.COLUMN_NAME
[ AND TABLE1.COLUMN_NAME = TABLE3.COLUMN ]
Example:
SELECT EMPLOYEE_TBL.*, EMPLOYEE_PAY_TBL.SALARY
FROM EMPLOYEE_TBL,
EMPLOYEE_PAY_TBL
WHERE EMPLOYEE_TBL.EMP_ID = EMPLOYEE_PAY_TBL.EMP_ID;
Joins of Non-Equality:
NON-EQUIJOIN joins two or more tables based on a specified column value not equaling a
specified column value in another table.
The syntax for the NON-EQUIJOIN:
FROM TABLE1, TABLE2 [, TABLE3 ]
WHERE TABLE1.COLUMN_NAME != TABLE2.COLUMN_NAME
[ AND TABLE1.COLUMN_NAME != TABLE2.COLUMN_NAME ]
Example:
SELECT EMPLOYEE_TBL.EMP_ID, EMPLOYEE_PAY_TBL.DATE_HIRE
FROM EMPLOYEE_TBL,
EMPLOYEE_PAY_TBL
WHERE EMPLOYEE_TBL.EMP_ID != EMPLOYEE_PAY_TBL.EMP_ID;
Outer Joins:
An OUTER JOIN is used to return all rows that exist in one table, even though
corresponding rows do not exist in the joined table. The (+) symbol is used to denote an
OUTER JOIN in a query. The (+) is placed at the end of the table name in the WHERE
clause. The table with the (+) should be the table that does not have matching rows. In
many implementations, the OUTER JOIN is broken down into joins called LEFT OUTER JOIN,
RIGHT OUTER JOIN, and FULL OUTER JOIN. The OUTER JOIN in these implementations is
normally optional.
The general syntax:
FROM TABLE1
{RIGHT | LEFT | FULL} [OUTER] JOIN
ON TABLE2
The Oracle syntax:
FROM TABLE1, TABLE2 [, TABLE3 ]
WHERE TABLE1.COLUMN_NAME[(+)] = TABLE2.COLUMN_NAME[(+)]
[ AND TABLE1.COLUMN_NAME[(+)] = TABLE3.COLUMN_NAME[(+)]]
Self Joins:
The SELF JOIN is used to join a table to itself, as if the table were two tables, temporarily
renaming at least one table
The syntax is:
In the SQL statement.
SELECT A.COLUMN_NAME, B.COLUMN_NAME, [ C.COLUMN_NAME ]
FROM TABLE1 A, TABLE2 B [, TABLE3 C ]
WHERE A.COLUMN_NAME = B.COLUMN_NAME
[ AND A.COLUMN_NAME = C.COLUMN_NAME ]
Example:
SELECT A.LAST_NAME, B.LAST_NAME, A.FIRST_NAME
FROM EMPLOYEE_TBL A,
EMPLOYEE_TBL B
WHERE A.LAST_NAME = B.LAST_NAME;
Experiment No. : - 13
Aim: SQL commands using set operators.
Introduction:
Set operators can be used to select data from multiple data base. Set operators are
basically combine the result of two queries in to one. These queries are known as
compound queries. All set operators have equal precedence; when multiple set of
operators are present in a single query, they are evaluated from left to right unless
specified otherwise with parentheses. The data types of the resulting columns should
match in both the queries. Oracle has 4 set operators which are shown below:
Operator Description
UNION Returns all unique rows selected by either query
UNION ALL Return all rows, including duplicates selected by
either query
INTERSECT Returns rows selected from both queries
MINUS Returns unique rows selected by the first query
but not the rows selected by the second query.
• Union operator: The union operator merges the output of two or more queries in
a single set of rows or columns. It eliminates the duplicate values between two or
more queries.
Syntax:
SELECT <statements1>
UNION
SELECT < statements2>
[ORDER BY clause];
e.g.
SQL> Select job from emp where deptno=20
UNION
Select job from emp where deptno=30;
• UNION ALL
The union all operator merges the output of two or more queries in a single set of rows and
columns. It does not eliminate the duplicate values between two or more queries.
Syntax:
SELECT<statements1>
UNION ALL
SELECT <statements2>
[ORDER BY clause];
e.g.
SQL> select job from emp where deptno=20
UNION ALL
Select job from emp where deptno=30
The output is :
JOB
CLERK
MANAGER
ANALYST
CLERK
ANALYST
SALESMAN
• INTERSECT OPERATOR : It gives only those rows in the output which have
common records among them.
Syntax:
SELECT <statements1>
INTERSECT
SELECT<statements2>
[ORDER BY clause];
e.g. SQL> select jobs from emp where deptno =20
INTERSECT
Select jobs from emp where deptno=30
The Output is :
JOB
CLERK
MANAGER
• Minus Operator : The minus operator returns the rows which are unique to the
first query. It must be remembered that in case of minus A-B is different from B-A.
Syntax:
SELECT <statements1>
MINUS
SELECT<statements2>
[ORDER BY clause];
e.g. SQL> select jobs from emp where deptno =20
INTERSECT
Select jobs from emp where deptno=30
The output is :
JOB
ANALYST
Experiment No. : - 14
Aim: SQL commands using sub queries to solve a problem.
Sub queries:
A sub query is a query that appears within another SQL command. SQL DML commands
(SELECT, DELETE, and UPDATE) support sub queries, though the rules and reasons for
using them vary. Some sub queries stand alone; you can run the sub query independent of
the command that contains it. Other sub queries rely on fields from the containing
command—these sub queries are said to be correlated.
A sub query is a complete query, but cannot contain the TO or INTO clause. Sub queries
are enclosed in parentheses in the containing query. Sub queries can appear in the WHERE
clause of SELECT, UPDATE, and DELETE.subqueries can also be used in the field list of
SELECT, in the SET clause of UPDATE, and in the FROM clause of SELECT, UPDATE, and
DELETE. a sub query cannot contain another sub query.
TYPE DESCRIPTION
Single row sub queries Queries that return only one row from inner SELECT
statement.
Multiple row sub Queries that returns more than one rows from inner SELECT
queries statements.
Multiple column sub Queries that return more than one column from the inner
queries SELECT statement.
1. Single sub queries : Queries that return only one row from inner SELECT
statement.
e.g.
SQL> SELECT ename,job from emp
Where job=(SELECT job from emp WHERE empno=7369);
The output is :
ENAM JOB
E
JAMES CLERK
SMITH CLERK
ADAMS CLERK
MILLER CLERK
2. Multiple row sub query : Queries that returns more than one rows from inner
SELECT statements.
OPERATO MEANING
R
IN Equal to any number in the list.
ANY Compare value to each value returned by
subquery
ALL Compare value to every value returned by
subquery.
3. Multiple Column Sub queries : Queries that return more than one column from
the inner SELECT statement.
Syntax :
SELECT column, column…
FROM table
WHERE (column, column…..)
(SELECT column, column…
FROM table
WHERE cond);
e.g. SELECT ordid, prodid, qty FROM item
WHERE (prodid,qty) IN
(SELECT prodid,qty FROM item
WHERE ordid=605) AND ordid< >605;
The output is :