Beruflich Dokumente
Kultur Dokumente
LAB
MANUAL
EXPERIMENT NO: 1
AIM: Preparing an ER diagram for given database and Conversion from ER diagram
to tables
THEORY:
• Database :
A Database is a collection of interrelated data and a Database Management
System is a a software system that enables users to define, create and maintain the database
and which provides controlled access to the database.
• ER- Diagram :
It is an Entity –Relationship diagram which is used to represent the
relationship between different entities. An entity is an object in the real world which is
distinguishable from other objects. The overall logical structure of a database can be
expressed graphically by an ER diagram, which is built up from following components.
• Mapping Cardinalities :
It expresses the number of entities to which another entity can be associated via a
relationship set. For a binary relationship set R between entity sets A and B. The
Mapping Cardinalities must be one of the following.
• One to one
• One to many
• Many to one
• Many to many
For each regular(strong) entity type E in the ER schema create a relation R that
includes all the simple attributes of E. Include only the simple component attributes of a
composite ,the set of simple attributes that form it will together form the primary key of R.
For each binary 1:1 relationship type R in the ER schema, identify the relations S
and T that correspond to the Entity Types participating in R. Choose one of the relations- S,
say – and include as a Foreign key in S the primary key of T. It is better to choose an entity
type with total participation in R in the role of S. Include all the simple attributes (or simple
components of a composite attributes) of the 1:1 relationship type R as attributes of S.
For each regular binary 1:N relationship type R, identifying the relation S that
represent the participating Entity Type at the N side of the relationship type. Include as
Foreign key in S the primary key of T that represents the other entity type participating in R;
this is done because each entity instance on the N side is related to at most one entity instance
on the 1-side of the relationship Type. Include any simple attributes (or simple components of
composite attributes) of the 1:N relationship type as attributes of S.
For each binary M:N relationship type R, create a new relation S to represent R.
Include the primary key of the participating entity types; their combination will form the
primary key of S. Also include any simple attributes of the M:N relationship types (or
simple components of a composite attributes) as attributes of S.
For each Multivalued Attribute A, create a new relation R. This relation R will
include an attribute corresponding to A, plus the primary key attribute K- of the relation that
represents the entity type or relationship type that has A as an attribute. The primary key of R
is the combination of A and K. If the multivalued attribute is composite, we include its
simple components.
For each N-ary relationship type R, where N>2, create a new relation S to represent
R. Include the primary keys of the relations that represent the participating entity types. Also
include any simple attributes of the N-ary relationship types (or simple components of a
composite attributes) as attributes of S.
Conclusion:
Referring to the problem statement the ER diagram was designed and converted into
the tables.
EXPERIMENT NO: 2
THEORY:
SQL: It is structured query language, basically used to pass the query to retrieve and
manipulate the information from database. Depending upon the nature of query, SQL is
divided into different components:
DDL: The Data Definition Language (DDL) is used to create the database (i.e. tables,
keys, relationships etc), maintain the structure of the database and destroy databases and
database objects.
Eg. Create, Drop, Alter, Describe, Truncate
Syntax:
Syntax:
Syntax:
Syntax:
DESC table_name;
Syntax :
CONCLUSION:
2. What is Schema?
THEORY:
INSERT:
INSERT statement adds one or more records to any single table in a relational database.
Syntax:
UPDATE:
UPDATE statement that changes the data of one or more records in a table. Either all the
rows can be updated, or a subset may be chosen using a condition.
Syntax:
DELETE:
DELETE statement removes one or more records from a table. A subset may be defined for
deletion using a condition, otherwise all records are removed.
Syntax:
Syntax:
SELECT<attribute list>
FROM<table list>
WHERE<condition>
Where
• Attribute list is a list of attribute name whose values to be retrieved by the query.
• Table list is a list of table name required to process query.
• Condition is a Boolean expression that identifies the tuples to be retrieved by query.
CONCLUSION:
Implemented various DML commands and also executed simple SQL queries.
EXPERIMENT NO: 4
THEORY:
Constraints are the business Rules which are enforced on the data being stored in a table
are called Constraints.
Syntax:
Syntax:
Syntax:
ON DELETE CASCADE is set then DELETE operation in master table will trigger
the DELETE operation for corresponding records in the detail table.
Syntax:
Note: The NOT NULL constraint can only be applied at column level.
EXPERIMENT NO: 5
THEORY:
Subquery is a form of SQL statement that appears inside another SQL statement. It is also
called as Nested Query.
The statement containing a subquery is called a Parent Statement.
The parent statement uses the rows returned by the subquery
CORRRELATED SUBQUERY
A subquery which executes once for each and every row of main query is called correlated
subquery.
It is mainly used for row by row process
The main difference between Correlated subquery and subquery is that in
Subquery(child query),Main Query is executed first.
THEORY:
Multiple queries can be put together and their output combined using the union, intersects,
minus clause. The union clause merges the output of two or more queries into a single set of
rows and columns. The intersect clause outputs only rows produced by both the queries
intersected. The minus clause outputs the rows produced by the first query, after filtering the
rows retrieved by the second query.
Syntax:
THEORY:
PLSQL stands for "Procedural Language extensions to SQL", and can be used in Oracle
databases. PLSQL is closely integrated into the SQL language, yet it adds programming
constructs that are not native to SQL.
Advantages of PL/SQL
1. PL/SQL supports not only SQL data manipulation but also provides facilities of
conditional checking, branching and looping.
2. PL/SQL sends an entire block of statements to the Oracle engine at one time.
3. PL/SQL permits dealing with errors and displaying user friendly messages when the error
occurs.
4. Via PL/SQL all sorts of calculations can be done quickly without the use of Oracle Engine
5. Applications written in PL/SQL are portable to any computer hardware and operating
system where Oracle is operational.
DECLARE SECTION
Declarations of memory variables, constants, cursors etc in PL/SQL
BEGIN
SQL executable statements
PL/SQL executable statements
EXCEPTION
SQL or PL/SQL code to handle errors that may arise during the execution of the code block
EXPERIMENT NO: 8
THEORY:
The oracle engine uses a work area for its internal processing in order to execute an SQL
statement. This work area is private to SQL’s operations and is called a cursor. A cursor is a
mechanism by which you can assign a name to a "select statement" and manipulate the
information within that SQL statement.
Implicit cursor attributes can be used to access information about status of last insert, update,
delete or single - row select statement. This can be done by preceding the implicit cursor
attribute with cursor name (i.e. sql)
Explicit cursor is defined in the declarative part of a pl/sql block. This is done by naming the
cursor and mapping it to a query.
The commands used to control the cursor are DECLARE, OPEN, FETCH and CLOSE.
A cursor is a SELECT statement that is defined within the declaration section of your
PLSQL code. We'll take a look at three different syntaxes for cursors.
CURSOR cursor_name
IS
SELECT_statement;
CURSOR cursor_name
RETURN field%ROWTYPE
IS
SELECT_statement;
Once you've declared your cursor, the next step is to open the cursor.
OPEN cursor_name;
The purpose of using a cursor, in most cases, is to retrieve the rows from your cursor so that
some type of operation can be performed on the data. After declaring and opening your
cursor, the next step is to FETCH the rows from your cursor.
The final step of working with cursors is to close the cursor once you have finished using it.
CLOSE cursor_name;
THEORY:
Procedure is a subprogram used to perform a specific action. A subprogram is a
named block of PL/SQL. There are two types of subprograms in PL/SQL namely Procedures
and Functions. Every subprogram will have a declarative part, an executable part or body,
and an exception handling part, which is optional. Declarative part contains variable
declarations. Body of a subprogram contains executable statements of SQL and PL/SQL.
Statements to handle exceptions are written in exception part.
Procedure specification begins with CREATE and ends with procedure name or
parameters list. Procedures that do not take parameters are written without a parenthesis. The
body of the procedure starts after the keyword IS or AS and ends with keyword END.
A procedure may be passed multiple parameters. IN | OUT | IN OUT specifies the mode of
the parameter. Type specifies the datatype of the parameter.
IN - The parameter can be referenced by the procedure or function. The value of the
parameter cannot be overwritten by the procedure or function.
OUT - The parameter cannot be referenced by the procedure or function, but the value of the
parameter can be overwritten by the procedure or function.
IN OUT - The parameter can be referenced by the procedure or function and the value of the
parameter can be overwritten by the procedure or function.
Procedure body contains the SQL and PL/SQL statements to perform the procedure's task.
Exception Section:
The Exception section of a PL/SQL Block starts with the reserved keyword
EXCEPTION. This section is optional. Any errors in the program can be handled in this
section, so that the PL/SQL Blocks terminates gracefully. If the PL/SQL Block contains
exceptions that cannot be handled, the Block terminates abruptly with errors.
Advantages:
When client executes a procedure or a function, the processing is done in the server.
Server is likely to me more powerful than the clients which in turn mean that stored
procedures should run faster. This reduces network traffic.
As the procedures/functions are stored in the Oracle database there is no need to
transfer the code from the clients to the database server or to transfer intermediate results
from the server to the clients. This results in much less network traffic and again improves
scalability
The subprograms are compiled and stored in the Oracle database as stored programs
and can be invoked whenever required. As they are stored in compiled form when called,
they only need to be executed. Hence they save time needed for compilation. They allow
breaking the program into manageable modules. They provide reusability and maintainability
for the code.
Disadvantages:
There is an overhead involved in embedding or calling PL/SQL procedures from SQL
in Oracle due to the context switching that the Oracle database has to perform. This may be
significant in terms of performance but usually this overhead is outweighed by performance
advantages of using PL/SQL.
More memory may be required on the Oracle database server when using Oracle
PL/SQL packages as the whole package is loaded into memory as soon as any object in the
package is accessed
Conclusion:
Stored procedures should be used to improve the performance and scalability of applications.
The disadvantages are easily outweighed by the advantages and the use of packages to
combine related objects (procedures, functions and variables) into one physical unit enhances
these advantages.
EXPERIMENT NO: 10
THEORY :
THEORY:
A database triggers is stored PL/SQL program unit associated with a specific database
table or view. The code in the trigger defines the action the database needs to perform
whenever some database manipulation (INSERT, UPDATE, and DELETE) takes place.
Unlike the stored procedure and functions, which have to be called explicitly, the database
triggers are fired (executed) or called implicitly whenever the table is affected by any of the
above said DML operations. Till oracle 7.0 only 12 triggers could be associated with a given
table, but in higher versions of Oracle there is no such limitation. A database trigger fires
with the privileges of owner not that of user
1. A triggering event
3. Trigger action
Syntax:
Trigger
A BEFORE INSERT Trigger means that Oracle will fire this trigger before the INSERT
operation is executed.
Restrictions:
Drop a Trigger
Disable a Trigger
Enable a Trigger
THEORY:
A transaction begins with the first executable SQL statement after a connection made
to the Oracle engine. All changes made to an Oracle table data via unit a transaction are made
or undone at one instance.
Transaction Control Language (TCL) consists of various commands which are as
follows.
COMMIT
ROLLBACK
SAVEPOINT
COMMIT:
A commit ends the current transaction and makes permanent any changes made
during the transactions. A commit will also release any existing savepoint that may be in use.
Syntax:
COMMIT;
ROLLBACK:
A ROLLBACK does exactly the opposite of COMMIT. It ends the transaction but
undoes any changes made during the transaction.
Syntax:
ROLLBACK TO [SAVEPOINT] savepointname;
Where,
SAVEPOINT: is optional and is used to rollback a partial transaction, as far
the specified savepoint.
Savepointname: is a savepoint created in current transaction.
SAVEPOINT:
SAVEPOINT marks and saves current point in the processing of a transaction. When
a SAVEPOINT is used with a ROLLBACK statement, parts of a transaction can be undone.
An active savepoint is the one that is specified since the last COMMIT or ROLLBACK.
Syntax:
SAVEPOINT savepointname;
ROLLBACK can be performed with or without the SAVEPOINT clause. The
implication of each is described below:
A ROLLBACK operation performed without the SAVEPONT clause amounts to the
following:
• Ends the transaction.
• Undoes all the changes in the current transaction.
• Erases all savepoints in that transaction.
A ROLLBACK operation performed with the TO SAVEPOINT clause amounts to
the following:
• A predetermined portion of the transaction is rolled back.
• Retains the savepoint rolled back to, but losses those create after the
named savepoint.
Conclusion:
TCL statements are used to save or erase changes made in the database during current
transaction.
EXPERIMENT NO:13
THEORY:
Data Control Language (DCL) consists of various commands which are related to
data sharing and security of data in database.
They are
GRANT
REVOKE
Granting Privileges:
Objects that are created by a user are owned and controlled by that user. If user
wishes to access any of the objects belonging to another user, the owner of the object will
have to give permissions for such access. This is called Granting of Privileges.
Granting privileges using the GRANT statements:
The GRANT statements provide various types of access to database objects such as
tables, views.
Syntax:
GRANT {object privileges}
ON object name
TO username;
Object Privileges:
each object privilege that is granted authorizes the grantee to perform some operation
on the object. The user can grant all the privileges or grant only specific object privileges.
The list of object privileges is as follows:
• ALTER: allows the grantee to change the table definitions with the ALTER table
command.
• DELETE: allows the grantee to remove the records from the table with the DELETE
command.
• INDEX: allows the grantee to create an index on the table with the CREATE INDEX
command.
• INSERT: allows the grantee to add records to the table with the INSERT command.
• SELECT: allows the grantee to query the table with SELECT command.
• UPDATE: allows the grantee to modify the records in the table with the UPDATE
command.
Conclusion:
DCL statements are used to provide data sharing of data in database.