Professional Documents
Culture Documents
Dbms-Lab-Hemshu Final
Dbms-Lab-Hemshu Final
FOR
ACADEMIC YEAR 2022-2023
(BIET JHANSI)-284128
1. Installing oracle
As a student, many of you may be wondering with some of the questions in your
mind regarding the subject and exactly what has been tried is to answer through
this manual.
2. Creating Entity Relationship Diagram using case tools
As you may be aware that MGM has already been awarded with ISO 9001:2000
3. Writing SQL statements Using ORACLE /MYSQL:
certification and it is our endure to technically equip our students taking the
advantage of the procedural aspects
a)Writing basic of ISOstatements.
SQL SELECT 9001:2000 Certification.
b) Restricting and sorting data.
4. Normalization
Dr. S.D.Deshmukh
Principal
1. Lab Exercise
Aim: Installation of oracle on windows.
Steps:
Theory:
An entity-relationship (ER) diagram is a specialized graphic that
illustrates the interrelationships between entities in a database. ER diagrams often
use symbols to represent three different types of information. Boxes are commonly
used to represent entities. Diamonds are normally used to represent relationships and
ovals are used to represent attributes.
1 Entity
An entity is a person, place, concept, or thing about which the business
needs data.
2 Relationship
A relationship is an association between entity types
Example of ER Diagram
we have identified three entity types (Employee, Department, Division) and tworelationships
among these entity types (manages, contains). Now we can begin to representthe problem in
the language of ER modeling.
ER models are usually represented graphically. The language we are going to userepresents
entity types as rectangles and relationships as diamonds. Below is the representation of the
situation we are working with.
Notice that the contains relationship is drawn between the two entities that it is associated
with. Similarly for the manages relationship. This (simplified) ER model tells us that:
• Division is related to department through a relationship called contains.
• Departments are related to employees through a relationship called
manages.
• Employees are not directly related to divisions.
Certainly, we know more about the problem than this. Consider the relationship between
divisions and departments. We know that divisions have multiple departments and
departments can only be contained within one division. Or, for every one division there can
be many departments. In the
language of ER modeling this is called a 1: M (read: “one to many”) relationship.
A. One to One
B. One to Many
C. Many to Many
D. Many to One
A. One-to-one
One-to-one table relationships are a little more interesting and more underused than either
of the other two types of relationships. The key indicator of a possible need for a one-to-
one relationship is a table that contains fields that are only used for a certain subset of
the records in that table.
Let's take a look at building a Catalog table for the items that your store sells. Odds are
that you need to store some information about the individual items like catalog numbers,
weight, and other common data. But if you're selling different kinds of items, books and
CDs for example, you may want some item- specific information in the database. For
example, you may want a page count, author, publish date, and ISBN for books, while
you want playing time, number of tracks, artist, and label for the CDs. You could come
up with some way to fit both sets of data into the same structure, but then when
management decides you're also selling pet supplies, your system willprobably break!
A better solution would be a one-to-one relationship between the Item table and another table
of item-specific data for each type of item. The resulting structure is essentially one "master"
table (CatalogItems) with one or more "subtables" (CDs and Books in this example). You link
the two subtables to the master table through the primary key of the master table.
Catalog Table
CDs
Books
The one-to-one relationship has saved us from doubling the number of fields in the
Catalog table and, more importantly, helped us break the database into more discrete
entities. In this scenario, we can get all the general information about an item from the
Catalog table and can use the primary key of that table to pull up the appropriate
information from the subtable.
B. One-to-many
The one-to-many relationship is the workhorse of relational databases as well as being
the easiest relationship to understand. Let us say you need to build a shopping cart
application for an e-commerce site. Your first draft of the database has columns for Item1,
Item2, and Item3 with the corresponding Quantity1, Quantity2, and Quantity3 fields.
Of course, this immediately starts to break down with more than three orders! Any time you
find yourself designing a database and adding similar fields like this to the same table, you
need to break the table into two (or more!) related tables using a one-to-many relationship.
OrderID ShippingInfo
OrderItem Table
The two tables are linked together using the OrderID field. The contents of any order in the
Order table can easily be found by finding all the items with that value in the OrderID field.
There is also the added advantage that the two pieces of data are independent and can easily
be modified. If we now want to add an ItemNumber to the OrderItem table, we add a single
column; in our
original monolithic data table, we'd be adding ItemNumber1, ItemNumber2, etc.
C. Many-to-many
Finally, there is the many-to-many table. This relationship is a little more complex than the
one-to-many because, in addition to the two tables of data, we need another table to join the
two tables of interest together. That's right, we're adding a table to the database -- but it is a
simple table and saves us lots of effort down the road. As an example, let's say you want to
add the ability to search for CDs by the musicians on any given song. From the musician side,
you have one musician related to many songs.
Musician Table
MusicianID MusicianName
44 Paul McCartney
Song Table
SongID SongName
200 Sgt. Pepper's Lonely Heart's Club Band
Musician Table
This has saved us nothing -- in fact, it has complicated the structure by introducing lots
of redundant data to manage. The way to handle this situation is to create two one-to-
many relationships involving a linking table which we'll call Song_Musician, since it links
those tables. We create a one-to-many from Song to Song_Musician since one song will
have 0-N musicians and then another one-to-many from Musician to Song_Musician
since any one musician will be in one or more songs. The results look like the following:
Musician Table
MusicianID MusicianName
43 John Lennon
44 Paul McCartney
Song_Musician Table
SongID MusicianID
200 43
200 44
201 44
Song Table
SongID SongName
200 Sgt. Pepper's Lonely Heart's Club Band
201 Ebony and Ivory
This time around, all of the redundant data is in the Song_Musician table, which areonly
two columns of integers. Any changes to the structure of the Song or Musiciantable
remain independent of their relationship, which is precisely what we're after.
D. Many to One
An employee can work in only one department; this relationship is single- valued for
employees. On the other hand, one department can have many employees; this relationship
is multi-valued for departments. The relationship between employees (single-valued) and
departments (multi-valued) is a one-to- many relationship.
To define tables for each one-to-many and each many-to-one relationship:
1. Group all the relationships for which the "many" side of the
relationship is the same entity.
2. Define a single table for all the relationships in thegroup.
In the following example, the "many" side of the first and second relationships is
"employees" so an EMPLOYEE table is defined.
are assigned
Employees departments
to
DEPTNO ADMRDEPT
C01 A00
D01 A00
D11 D01
3. Lab Exercise
Theory:
You can restrict the rows returned from the query by using the WHERE clause. A WHERE clause
contains a condition that must be met, and it directly follows the FROM clause. If the condition is
true, the row meeting the condition is returned.
In the syntax:
WHERE restricts the query to rows that meet a condition
condition is composed of column names, expressions,constants and a comparison operator
The WHERE clause can compare values in columns, literal values, arithmetic expressions, or functions. It
consists of three elements:
Column name
Comparison condition
Column name, constant, or list of values
Example:
SELECT employee_id, last_name, job_id, department_id FROM employees WHERE department_id = 90
Examples:
▪ … WHERE hire_date=’01-JAN-95′
▪ … WHERE salary>=6000
▪ … WHERE last_name=’Smith’
▪ An alias cannot be used in the WHERE clause.
BETWEEN Condition:
You can display rows based on a range of values using the BETWEEN range condition. The range that
you specify contains a lower limit and an upper limit.
The SELECT statement on the slide returns rows from the EMPLOYEES table for any employee whose
salary is between $2,500 and $3,500.
Values specified with the BETWEEN condition are inclusive. You must specify the lower limit first.
Example:
SELECT last_name, salary FROM employees WHERE salary BETWEEN 2500 AND 3500;
Like condition:
Use the LIKE condition to perform wildcard searches of valid search string values.
▪ Search conditions can contain either literal characters or numbers:
▪ % denotes zero or many characters.
▪ _ denotes one character.
Example:
SELECT first_name FROM employees WHERE first_name LIKE ‘S%’;
Sorting:
Sort rows with the ORDER BY clause
ASC: ascending order, default
DESC: descending order
The ORDER BY clause comes last in the SELECT statement.
Example:
SELECT last_name, job_id, department_id, hire_date FROM employees ORDER BY hire_date;
3. Displaying data from multiple tables
In real-life applications, you would need to fetch data from multiple tables to
achieve your goals. To do so, you would need to use SQL joins.
In order to understand joins, you should first understand the concept of a Cartesian
product. In mathematics, this is the product of two sets. For example, a set of two
items multiplied by a set of six items will produce a set of twelve items.
In databases, a Cartesian product is the result of joining every row of one input table
to all rows of another table. Therefore, the product of a table of two rows and a table
of three rows would be a set of six rows.
orderid itemid
1 1
2 2
Result: Each row of orders would multiply by each row of items, and the result would be
as below.
orderID itemid
1 1
1 2
1 3
2 1
2 2
2 3
In SQL, to fetch data from multiple tables, the join operator is used. The join operator
adds or removes rows in the virtual table that is used by SQL server to process data
before the other steps of the query consume the data. Joins can be of the following
categories:
1. A cross join, also known as a Cartesian product, adds all possible combinations of
the two input tables' rows to the virtual table. All data filtering of the rows will be
done in the where clause.
2. The inner join operator first creates a Cartesian product, and then filters the results
using the predicate supplied in the ON clause, removing any rows from the virtual
table that do not satisfy the predicate. It is the most commonly used join type.
3. An outer join operator (LEFT OUTER JOIN, RIGHT OUTER JOIN, FULL OUTER
JOIN) first creates a Cartesian product, then filters the results to find rows that match
in each table. The difference is that all rows from one table are preserved and added
back to the virtual table after the initial filter is applied. All null values are placed on
attributes where no matching values are found.
4. Aggregating data using group function:
A group function returns a result based on group of rows. Some are purely mathematical
function.
A. Avg function :
It returns average of values of the column specified in the arguments in the column.
SYNTAX: Select avg(column_name whose avg to find) from <table_name> where condition;
B. min function:
This function will give the least value of the column present in the argument.
SYNTAX: Select min(column_name whose min to find) from <table_name> where condition;
C. max function:
This function will give the maximum value of the column present in the argument.
Select min(column_name whose max to find) from <table_name> where condition;
D. sum:
This function will give the sum of value of the column present in the argument.
SYNTAX: Select sum(column_name whose sum to find) from <table_name> where condition;
E. count:
This functionis used to count the number of rows in function .
SYNTAX: Select count (*) from <table_name> ;
5. Manipulating data
INSERT statement
The INSERT command is used to store data in tables.There are two different forms of the INSERT
command. The first form is used if a new row will have a value inserted into each column of the row.
The second form of the INSERT command is used to insert rows where some of the column data is
unknown or defaulted from another business logic.This form of the INSERT command requires that
you specify column names for which data are being stored.
Syntax:
The below syntax can be followed if the values for all the columns in the table is definite and known.
INSERT INTO table
VALUES (column1 value, column2 value, ...);
UPDATE statement
The UPDATE command modifies the data stored in a column.It can update single or multiple rows at a
time depending on the result set filtered by conditions specified in WHERE clause. Note that Updating
columns is different from altering columns..The UPDATE command changes data in the table, not the
table structure.
Syntax:
UPDATE table
SET column = value [, column = value ...]
[WHERE condition]
DELETE statement
The DELETE command is one of the simplest of the SQL statements. It removes one or more rows from
a table. Multiple table delete operations are not allowed in SQL.The syntax of the DELETE command is
as below.
DELETE FROM table_name
[WHERE condition];
The DELETE command deletes all rows in the table that satisfy the condition in the optional WHERE
clause. Since the WHERE clause is optional, one can easily delete all rows from a table by omitting a
WHERE clause since the WHERE clause limits the scope of the DELETE operation.
Creating a basic table involves naming the table and defining its columns and each column's data type.
The SQL CREATE TABLE statement is used to create a new table.
Syntax
The basic syntax of the CREATE TABLE statement is as follows −
CREATE TABLE table_name(
column1 datatype,
column2 datatype,
.....
columnN datatype,
PRIMARY KEY( one or more columns )
);
CREATE TABLE is the keyword telling the database system what you want to do. In this case, you want
to create a new table. The unique name or identifier for the table follows the CREATE TABLE statement.
Then in brackets comes the list defining each column in the table and what sort of data type it is.
MANAGING TABLE:
Managing table structures include adding new column(s), and modifying the definition or deleting existing
columns. In general, these operations are done by ALTER TABLE command with different clauses.
• Modify column definitions. Use MODIFY (<column name>, <new definition>) clause.
For example, the following command expands the length of the COMMENT column in employee table.
SQL> ALTER TABLE employee MODIFY (comment VARCHAR2(200));
Theory:
C. Transitive dependency
A transitive dependency is an indirect functional dependency, one in which
X→Z only by virtue of X→Y and Y→Z.
D. Multivalued dependency
A multivalued dependency is a constraint according to which the presence of
certain rows in a table implies the presence of certain other rows.
E. Join dependency
A table T is subject to a join dependency if T can always be recreated by joining
multiple tables each having a subset of the attributes of T.
F. Superkey
A superkey is an attribute or set of attributes that uniquely identifies
rows within a table; in other words, two distinct rows are always guaranteed to
have distinct superkeys. {Employee ID, Employee Address, Skill} would be a
superkey for the "Employees' Skills" table; {Employee ID, Skill} would also be a
superkey.
G. Candidate key
A candidate key is a minimal superkey, that is, a superkey for which we can say
that no proper subset of it is also a superkey. {Employee Id, Skill} would be a
candidate key for the "Employees' Skills" table.
H. Non-prime attribute
A non-prime attribute is an attribute that does not occur in any candidate key.
Employee Address would be a non-prime attribute in the "Employees' Skills" table.
F. Primary key
Most DBMSs require a table to be defined as having a single unique key, rather
than a number of possible unique keys. A primary key is a key which thedatabase
designer has designated for this purpose.
EXPERMENT PART:
What is Normalization?
o Normalization is the process of organizing the data in the database.
o Normalization is used to minimize the redundancy from a relation or set of relations. It is also used
to eliminate undesirable characteristics like Insertion, Update, and Deletion Anomalies.
o Normalization divides the larger table into smaller and links them using relationships.
o The normal form is used to reduce redundancy from the database table.
TYPES:
1st Normal Form (1NF)
• A table is referred to as being in its First Normal Form if atomicity of the table is 1.
• Here, atomicity states that a single cell cannot hold multiple values. It must hold only a single-
valued attribute.
• The First normal form disallows the multi-valued attribute, composite attribute, and their
combinations.
Now you will understand the First Normal Form with the help of an example.
Below is a students’ record table that has information about student roll number, student name, student
course, and age of the student.
In the studentsrecord table, you can see that the course column has two values. Thus it does not follow
the First Normal Form. Now, if you use the First Normal Form to the above table, you get the below
table as a result.
By applying the First Normal Form, you achieve atomicity, and also every column has unique values.
The first condition for the table to be in Second Normal Form is that the table has to be in First Normal
Form. The table should not possess partial dependency. The partial dependency here means the proper
subset of the candidate key should give a non-prime attribute.
Now understand the Second Normal Form with the help of an example.
The Location table possesses a composite primary key cust_id, storeid. The non-key attribute is
store_location. In this case, store_location only depends on storeid, which is a part of the primary key.
Hence, this table does not fulfill the second normal form.
To bring the table to Second Normal Form, you need to split the table into two parts. This will give you
the below tables:
As you have removed the partial functional dependency from the location table, the column
store_location entirely depends on the primary key of that table, storeid.
• The first condition for the table to be in Third Normal Form is that the table should be in the Second
Normal Form.
• The second condition is that there should be no transitive dependency for non-prime attributes, which
indicates that non-prime attributes (which are not a part of the candidate key) should not depend on
other non-prime attributes in a table. Therefore, a transitive dependency is a functional dependency in
which A → C (A determines C) indirectly, because of A → B and B → C (where it is not the case that
B → A).
• The third Normal Form ensures the reduction of data duplication. It is also used to achieve data
integrity.
Below is a student table that has student id, student name, subject id, subject name, and address of the
student as its columns.
In the above student table, stu_id determines subid, and subid determines sub. Therefore, stu_id
determines sub via subid. This implies that the table possesses a transitive functional dependency, and
it does not fulfill the third normal form criteria.
Now to change the table to the third normal form, you need to divide the table as shown below:
As you can see in both the tables, all the non-key attributes are now fully functional, dependent only
on the primary key. In the first table, columns name, subid, and addresses only depend on stu_id. In the
second table, the sub only depends on subid.
Boyce Codd Normal Form is also known as 3.5 NF. It is the superior version of 3NF and was developed
by Raymond F. Boyce and Edgar F. Codd to tackle certain types of anomalies which were not resolved
with 3NF.
The first condition for the table to be in Boyce Codd Normal Form is that the table should be in the third
normal form. Secondly, every Right-Hand Side (RHS) attribute of the functional dependencies should
depend on the super key of that particular table.
For example :
You have a functional dependency X → Y. In the particular functional dependency, X has to be the part
of the super key of the provided table.