Lab Manual File: Course Coordinator
Lab Manual File: Course Coordinator
DATABASEMANAGEMENT SYSTEM(LAB)[CSL402]
Course Coordinator
MS.POOJA PRAJAPATI
Assistant Professor
Department of Artificial Intelligence & Data Science
2021-2022
Mahavir Education Trust's
SHAH & ANCHOR KUTCHHI ENGINEERING COLLEGE
Chembur, Mumbai - 400 088
UG Program in Artificial Intelligence & Data Science
Apply different rules for Creating and updating database and tables with 3
2
different DDL and DML statements.
Page no.
Sr. No. Title
Name: Signature:
Date:
Experiment No. 1
Entity:An entity can be a real-world object, either animate or inanimate, that can be
easily identifiable.
Degree of Relationship
Mapping Cardinalities
Cardinality defines the number of entities in one entity set, which can be
associated with the number of entities of other set via relationship set.
One-to-one
One-to-many
Many-to-one
Many-to-
many ER Diagram
Notations:
Symbol Meaning
Entity
Weak Entity
Relationship
Attribute
Key Attribute
Multivalued
Attribute
Composite
Attribute
Derived Attribute
E1 R E2
Total Participation
1 N
E1 R E2 Cardinality
Enhanced Entity Relationship (EER):
1. Generalization:
Generalization is a bottom-up approach in which two lower level
entities combine to form a higher level entity.
Account
ISA
Saving Current
2. Specialization:
Staff
ISA
Teaching Non-Teaching
3. Aggregation:
COMPANY IN APPLICANT
COMPANY INT APPLICANT
RES JOB_OFFER
Conclusion: The ER and EER for the given problem statement is studied and drawn
EXPERIMENT NO : 2
AIM-
THEORY:
For each weak entity type W in the ER schema with owner entity type E,
create a relation R and include all simple attributes(or simple component of
composite attributes)of W as attributes of R.In addition include as foreign key
attributes of R the Primary key attribute(s) of the relation(s) that correspond to the
owner entity type(s); this takes care of the identifying relationship type of W. The
primary key of R is the combination of the primary key(s) of the owner(s) and the
partial key of the Weak Entity Type W, if any.
If there is a weak entity type E2, whose owner is also a weak entity type
E1, should be mapped before E2 to determine its primary key first.
For Example : We create the relation DEPENDENT to correspond to the
weak entity type DEPENDENT. We include the primary key ENO as a foreign key
attribute of DEPENDENT; we renamed it EENO, although this is not
necessary.Theprimary key of the DEPENDENT relation is combination {EENO,
DEPENDENT NAME} because DEPENDENT_NAME is the partial key of
DEPENDENT.
Step 3 : Mapping of Binary 1:1 Relationship Types :
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. There are
three possible approaches given below.
1. Foreign key approach : 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 example: we map the 1:1 relationship type MANAGES, by choosing the
participating entity type DEPARTMENT to serve in the role of S, because its
participation in the MANAGES relationship type is total(every department has a
manager). We include the includes all the simple attributes Primary key of the
EMPLOYEE relation as foreign key in the DEPARTMENT relation and rename it
MGRENO. We also include the simple attribute STARTDATE of the MANAGES
relationship type in the DEPARTMENT relation and rename it MGRSTARTDATE.
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 example : we map the M:N Relationship type WORKS_ON from fig by
creating the relation WORKS_ON in fig. We include the primary keys of the
PROJECT and EMPLOYEE relations
as Foreign keys in WORKS_ON and rename them PNO and EENO,respectively.
We also include an attribute HOURS in WORKS_ON to represent the HOURS
attribute of the relationship type. The primary key of the WORKS_ON relation is
the combination of the foreign key attributes {EENO,PNO}
For each N-ary relationship type R, where N>2, create a new relation S to
represent R. Include as Foreign key attributes in S 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. . The primary key of S is usually a combination of all the foreign
keys that reference the relations representing the participating entity types.
However, if the cardinality constraints on any of the entity types E participating in
R is 1, then the primary key of S should not include the foreign key attribute that
references the relation E’ corresponding to E is composite, we include its simple
components.
For example: primary key of SUPPLIER relation is SNAME, primary key of
PROJECT relation is PROJNAME ,primary key of PART relation is PARTNO,
then primary key of SUPPLY relation is the combination of the three Foreign keys
{SNAME, PROJNAME, PARTNO}.
Conclusion:
Referring to the problem statement the ER diagram was designed and converted into
the tables.
EXPERIMENT NO-3
1. CREATE Command
2. DROP Command
3. ALTER Command
4. TRUNCATE Command
5. RENAME Command
CREATE Command
DROP Command
DROP is a DDL command used to delete/remove the database objects
from the SQL database. We can easily remove the entire table, view, or
index from the database using this DDL command.
ALTER Command
TRUNCATE Command
RENAME Command
CONCLUSION
Implemented various DDL and DML command and also applied various
constraints on the table.
EXPERIMENT NO.-4
1. Domain Constraint
2. Entity Constraint
3. Referential Integrity Constraint
4. Key Constraint
1. Domain constraints
Example:
Syntax
CREATE TABLE table_name (column1 datatype constraint,column2
datatype constraint,column3 datatype constraint, ....);
o The entity integrity constraint states that primary key value can't be
null.
o This is because the primary key value is used to identify individual
rows in relation and if the primary key has a null value, then we
can't identify those rows.
o A table can contain a null value other than the primary key field.
Syntax:
Example
Example:
Systax:
4. Key constraints
o Keys are the entity set that is used to identify an entity within its
entity set uniquely.
o An entity set can have multiple keys, but out of which one key will
be the primary key. A primary key can contain a unique and null
value in the relational table.
Example:
35.5M
824
OOPs Concepts in Java
Syntax:
Definition
Each check constraint has to be defined in the CREATE
TABLE or ALTER TABLE statement using the syntax:
CREATE TABLE table_name (
...,
CONSTRAINT constraint_name CHECK ( predicate ),
...
)
ALTER TABLE table_name
ADD CONSTRAINT constraint_name CHECK ( predicate )
If the check constraint refers to a single column only, it is possible to
specify the constraint as part of the column definition.
CREATE TABLE table_name (
...
column_name type CHECK ( predicate ),
...
)
NOT NULL constraint
A NOT NULL constraint is functionally equivalent to the following
check constraint with an IS NOT NULL predicate:
CHECK (column IS NOT NULL)
Some relational database management systems are able to optimize
performance when the NOT NULL constraint syntax is used as opposed
to the CHECK constraint syntax given above.
CONCLUSION:
Various constraint can be applied on the table for better design and as per
the requirement of the user.
EXPERIMENT NO.-5
Example
Example
SELECT COUNT(CustomerID), Country
FROM Customers
GROUP BY Country
HAVING COUNT(CustomerID) > 5;
ORDER BY
The SQL ORDER BY Keyword
The ORDER BY keyword is used to sort the result-set in ascending or
descending order.
The ORDER BY keyword sorts the records in ascending order by default.
To sort the records in descending order, use the DESC keyword.
ORDER BY Syntax
SELECT column1, column2, ...
FROM table_name
ORDER BY column1, column2, ... ASC|DESC;
ORDER BY Example
The following SQL statement selects all customers from the "Customers"
table, sorted by the "Country" column:
Example
Example
CONCLUSION-
Thus we have design the queries of simple and string manipulation operations
EXPERIMENT NO.6
AIM-Write Nested queries and Complex queries for the given statement of
problem.
THEORY-
Nested Queries -In nested queries, a query is written inside a query. The result
of inner query is used in execution of outer query. We will use STUDENT,
COURSE, STUDENT_COURSE tables for understanding nested queries.
STUDENT
COURSE
C_ID C_NAME
C1 DSA
C2 Programming
C3 DBMS
STUDENT_COURSE
S_ID C_ID
S1 C1
S1 C3
S2 C1
S3 C2
S4 C2
S4 C3
There are mainly two types of nested queries:
Independent Nested Queries: In independent nested queries, query
execution starts from innermost query to outermost queries. The execution
of inner query is independent of outer query, but the result of inner query
is used in execution of outer query. Various operators like IN, NOT IN,
ANY, ALL etc are used in writing independent nested queries.
IN: If we want to find out S_ID who are enrolled in C_NAME ‘DSA’ or
‘DBMS’, we can write it with the help of independent nested query and
IN operator. From COURSE table, we can find
out C_ID for C_NAME ‘DSA’ or DBMS’ and we can use these C_IDs
for finding S_IDs from STUDENT_COURSE TABLE.
STEP 1: Finding C_ID for C_NAME =’DSA’ or ‘DBMS’
Select C_ID from COURSE where C_NAME = ‘DSA’ or C_NAME =
‘DBMS’
STEP 2: Using C_ID of step 1 for finding S_ID
Select S_ID from STUDENT_COURSE where C_ID IN
(SELECT C_ID from COURSE where C_NAME = ‘DSA’
or C_NAME=’DBMS’);
The inner query will return a set with members C1 and C3 and outer
query will return those S_IDs for which C_ID is equal to any member of
set (C1 and C3 in this case). So, it will return S1, S2 and S4.
NOT IN: If we want to find out S_IDs of STUDENTs who have neither
enrolled in ‘DSA’ nor in ‘DBMS’, it can be done as:
Select S_ID from STUDENT where S_ID NOT IN
(Select S_ID from STUDENT_COURSE where C_ID IN
(SELECT C_ID from COURSE where C_NAME=’DSA’
or C_NAME=’DBMS’));
The innermost query will return a set with members C1 and C3. Second
inner query will return those S_IDs for which C_ID is equal to any
member of set (C1 and C3 in this case) which are S1, S2 and S4. The
outermost query will return those S_IDs where S_ID is not a member of
set (S1, S2 and S4). So it will return S3.
CONCLUSION-
Implemented various nested and complex queris and executed
successfully
.
EXPERIMENT NO.-7
THEORY- SQL:JOINS
Description
SQL JOINS are used to retrieve data from multiple tables. A SQL JOIN
is performed whenever two or more tables are listed in a SQL statement.
There are 4 different types of SQL joins:
Sample Table-EMPLOYEE
PROJECT
101 1 Testing
102 2 Development
103 3 Designing
104 4 Development
1. INNER JOIN
In SQL, INNER JOIN selects records that have matching values in both
tables as long as the condition is satisfied. It returns the combination of
all rows from both the tables where the condition satisfies.It is the most
common type of SQL join. SQL INNER JOINS return all rows from
multiple tables where the join condition is met.
Syntax
Query
2. FROM EMPLOYEE
3. INNER JOIN PROJECT
4. ON PROJECT.EMP_ID = EMPLOYEE.EMP_ID;
Output
EMP_NAME DEPARTMENT
Angelina Testing
Robert Development
Christian Designing
Kristen Development
2. LEFT JOIN
The SQL left join returns all the values from left table and the matching
values from the right table. If there is no matching join value, it will
return NULL.This type of join returns all rows from the LEFT-hand table
specified in the ON condition and only those rows from the other table
where the joined fields are equal (join condition is met).
Syntax
Query
2. FROM EMPLOYEE
3. LEFT JOIN PROJECT
4. ON PROJECT.EMP_ID = EMPLOYEE.EMP_ID;
Output
EMP_NAME DEPARTMENT
Angelina Testing
Robert Development
Christian Designing
Kristen Development
Russell NULL
Marry NULL
3. RIGHT JOIN
In SQL, RIGHT JOIN returns all the values from the values from the
rows of right table and the matched values from the left table. If there is
no matching in both tables, it will return NULL.This type of join returns
all rows from the RIGHT-hand table specified in the ON condition
and only those rows from the other table where the joined fields are equal
(join condition is met).
Syntax
1. SELECT table1.column1, table1.column2, table2.column1,....
2. FROM table1
3. RIGHT JOIN table2
4. ON table1.matching_column = table2.matching_column;
Query
2. FROM EMPLOYEE
3. RIGHT JOIN PROJECT
4. ON PROJECT.EMP_ID = EMPLOYEE.EMP_ID;
Output
EMP_NAME DEPARTMENT
Angelina Testing
Robert Development
Christian Designing
Kristen Development
4. FULL JOIN
In SQL, FULL JOIN is the result of a combination of both left and right
outer join. Join tables have all the records from both tables. It puts NULL
on the place of matches not found. This type of join returns all rows from
the RIGHT-hand table specified in the ON condition and only those rows
from the other table where the joined fields are equal (join condition is
met).
Syntax
Query
2. FROM EMPLOYEE
3. FULL JOIN PROJECT
4. ON PROJECT.EMP_ID = EMPLOYEE.EMP_ID;
Output
EMP_NAME DEPARTMENT
Angelina Testing
Robert Development
Christian Designing
Kristen Development
Russell NULL
Marry NULL
CONCLUSION-
Implemented various nested and complex queris and executed successfully.
Experiment No. 08
Aim: Write Views and Triggers for the given statement of problem.
Theory:
Concept of a View:
A view in SQL terminology is a single table that is derived from other
tables. These other tables could be base tables or previously defined
views. A view does not necessarily exist in physical form; it is considered
a virtual table, in contrast to base tables whose tuples are actually stored
in the database. This limits the possible update operations that can be
applied to views, but it does not provide any limitations on querying a
view.
Specification of Views:
The command to specify a view is CREATE VIEW. The view is given a
(virtual) table name (or view name), a list of attribute names, and a query
to specify the contents of the view. If none of the view attributes result
from applying functions or arithmetic operations, we do not have to
specify attribute names for the view, as they would be the same as the
names of the attributes of the defining tables in the default case.
Syntax:
CREATE VIEW <view name>[ (<column name> { , <column
name> } ) ]
AS <SELECT statement>;
Querying View:
We can now specify SQL queries on a view or virtual table in the same
way we specify queries involving base tables.
Updating View:
A view is always up to date; if we modify the tuples in the base tables on
which the view is defined, the view must automatically reflect these
changes. Hence, the view is not realized at the time of view definition but
rather at the time we specify a query on the view. It is the responsibility
of the DBMS and not the user to make sure that the view is up to date.
Updating of views is complicated and can be ambiguous. In general, an
update on a view defined on a single table without any aggregate
functions can be mapped to an update on the underlying base
table.Dropping View: If we do not need a view any more, we can use the
DROP VIEW command to dispose of it.
Syntax:
DROP VIEW <view name>;
Advantages:
● View simplifies the specification of certain queries.
● Views are used as a security and authorization mechanism.
Concept of Trigger:
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
A database trigger has three parts
1. A triggering event
2. A trigger constraint (Optional)
3. Trigger action
A triggering event can be an insert, update, or delete statement or a
instance shutdown or startup etc. The trigger fires automatically when
any of these events occur on the specified table or a view. A trigger
constraint specifies a Boolean expression that must be true for the trigger
to fire. This condition is specified using the WHEN clause. The trigger
action is a procedure that contains the code to be executed when the
trigger fires.
Syntax:
BEFORE INSERT Trigger
A BEFORE INSERT Trigger means that Oracle will fire this trigger
before the INSERT operation is executed.
CREATE or REPLACE TRIGGER trigger_name
BEFORE INSERT ON table_name
[ FOR EACH ROW ]
DECLARE
-- variable declarationsBEGIN
-- trigger code
EXCEPTION
WHEN ...
-- exception handling
END;
trigger_name is the name of the trigger to create.
Restrictions:
● One can not create a BEFORE trigger on a view.
● One can update the :NEW values.
● One can not update the :OLD values.
AFTER INSERT Trigger
An AFTER INSERT Trigger means that Oracle will fire this trigger after
the INSERT operation
is executed.
The syntax for an AFTER INSERT Trigger is:
CREATE or REPLACE TRIGGER trigger_name
AFTER INSERT
ON table_name
[ FOR EACH ROW ]
DECLARE
-- variable declarations
BEGIN
-- trigger code
EXCEPTION
WHEN ...
-- exception handling
END;
trigger_name is the name of the trigger to create.
AFTER UPDATE Trigger
An AFTER UPDATE Trigger means that Oracle will fire this trigger
after the UPDATE
operation is executed.
The syntax for an AFTER UPDATE Trigger is:
CREATE or REPLACE TRIGGER trigger_name
AFTER UPDATE
ON table_name
[ FOR EACH ROW ]
DECLARE
-- variable declarationsBEGIN
-- trigger code
EXCEPTION
WHEN ...
-- exception handling
END;
trigger_name is the name of the trigger to create.
Drop a Trigger
The syntax for a dropping a Trigger is:
DROP TRIGGER trigger_name;
Disable a Trigger
The syntax for a disabling a Trigger is:
ALTER TRIGGER trigger_name DISABLE;
Enable a Trigger
The syntax for a enabling a Trigger is:
ALTER TRIGGER trigger_name ENABLE;
Conclusion:
● A view with a single defining table is updatable if the view attributes
contain the primary key (or possibly some other candidate key) of the
base relation, because this maps each (virtual) view tuple to a single base
tuple.
● Views defined on multiple tables using joins are generally notupdatable.
● Views defined using grouping and aggregate functions are notupdatable.
● Trigger generates some derived column values automatically also
enforces referential integrity
Experiment No. 09
Aim : Write Functions and procedure for the given statement of problem.
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.
Syntax for Procedure:
CREATE [OR REPLACE] PROCEDURE procedure_name
[(parameter_name [IN | OUT | IN OUT] type [, ...])]
{IS | AS}
BEGIN
procedure_body
EXCEPTION
Exception handling
END procedure_name;
Procedure is created using CREATE PROCEDURE statement.
OR REPLACE specifies the procedure is to replace an existing
procedure if present. One can use this option to modify an existing
procedure.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 A function is a named PL/SQL Block which is similar to a
procedure. The major difference between a procedure and a function is, a
function must always return a value, but a procedure may or may not
return a value.
Syntax for Function:
CREATE [OR REPLACE] FUNCTION function_name [parameters]
RETURN return_datatype;
IS
Declaration_section
BEGIN
Execution_section
Return return_variable;
EXCEPTION
exception section
Return return_variable; END;
Return Type:
The header section defines the return type of the function. The return
datatype can be any of the oracle datatype like varchar, number etc. The
execution and exception section both should return a value which is of the
datatype defined in the header section.
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. Functions are PL/SQL stored programming units,
providing a way to hide implementation details in a programming unit.
Functions can be used as right operands in PL/SQL and be called from
SQL statements, while procedures cannot.
EXPERIMENT NO.-10
TCL Commands
1. COMMIT
This command is used to make a transaction permanent in a database. So
it can be said that commit command saves the work done as it ends the
current transaction by making permanent changes during the transaction.
The syntax for this command is as below.
COMMIT;
For instance, we want to update the location of an employee in the table
“EMPLOYEE”. Sample EMPLOYEE table is given below:
ROLLBACK;
Also, the ROLLBACK command is used along with savepoint command
to leap to a save point in a transaction. The syntax for the same is as
below:
ROLLBACK TO <savepoint_name>;
Let us take the example of the EMPLOYEE table as cited above. Let us
consider that we have updated EMP_LOC for Raju to Bangalore later and
realize that the update was done mistakenly as below. Then we can
restore the EMP_LOC for ‘Raju’ to Hyderabad again by using the
Rollback command as below.
Query:
UPDATE EMPLOYEE SET EMP_LOC= 'Bangalore' WHERE
EMP_NAME = 'Raju';
ROLLBACK;
After the wrong update the table is as below:
4. SET TRANSACTION
SET TRANSACTION is used for placing a name on a transaction.
Syntax:
Concurrency Control
Lock-Based Protocol
In this type of protocol, any transaction cannot read or write data until it
acquires an appropriate lock on it. There are two types of lock:
1. Shared lock:
2. Exclusive lock:
o In the exclusive lock, the data item can be both reads as well as
written by the transaction.
o This lock is exclusive, and in this lock, multiple transactions do not
modify the same data simultaneously.
Timestamp-based Protocols
Timestamp based Protocol in DBMS is an algorithm which uses the
System Time or Logical Counter as a timestamp to serialize the execution
of concurrent transactions. The Timestamp-based protocol ensures that
every conflicting read and write operations are executed in a timestamp
order.
The older transaction is always given priority in this method. It uses
system time to determine the time stamp of the transaction. This is the
most commonly used concurrency protocol.
Example:
CONCLUSION-
concurrency schedule is parallel execution of multiple transaction.while
executing transaction concurrently if execution does not execute in
current sequence,that they may lead to inconsistency in database.thus to
maintain consistence to database,different protocol are required to
use.locking which we have implemented is part of concurrency handling
Experiment No. 11
.Conclusion:
Thus, we have written a java program to make JDBC connection and
retrieve the data from the respective table.