Sei sulla pagina 1di 76

# Ex. No.

: 1 BASIC SQL

## Relation for Student Scenario

Suppose you are given a relation grade points(grade, points), which provides a conversion
from letter grades in the takes relation to numeric scores; for example an A grade could be
specified to correspond to 4 points, an A to 3.7 points, a B+ to 3.3 points, a B to 3 points, and
so on. The grade points earned by a student for a course offering (section) is defined as the number of
credits for the course multiplied by the numeric points for the grade that the student received. Given
the above relation, and our university schema, write each of the following queries in SQL. You can
assume for simplicity that no takes tuple has the null value for grade.

## For the above schema, perform the following

a. Find the total grade-points earned by the student with ID 12345, across all courses taken
by the student.

b. Find the grade-point average (GPA) for the above student, that is, the total grade-points
divided by the total credits for the associated courses.

## c. Find the ID and the grade-point average of every student.

Aim

To create a student database and write sql queries for different operations using basic SQL
command.

Theory

SQL

## SQL stands for Structured Query Language

SQL lets you access and manipulate databases
SQL is an ANSI (American National Standards Institute) standard
SQL can execute queries against a database
SQL can retrieve data from a database
SQL can insert records in a database
SQL can update records in a database
SQL can delete records from a database
SQL can create new databases

## CSP602: Advanced Database Management System MTECH CSE| NITPY 1

SQL can create new tables in a database
SQL can create stored procedures in a database
SQL can create views in a database
SQL can set permissions on tables, procedures, and views

Syntax-

## CREATE TABLE table_name (

column1 datatype,
column2 datatype,
column3 datatype,
....);

## The INSERT INTO statement is used to insert new records in a table.

Syntax -
INSERT INTO table_name VALUES (value1, value2, value3, ...);

SQL SELECT

## The SELECT statement is used to select data from a database.

The data returned is stored in a result table, called the result-set.
Syntax
SELECT column1, column2, ... FROM table_name;
To select whole table
SELECT * FROM table_name;

Program/Procedure :

Database Creation:

use University;

Table Creation:

Student :

## create table Student(ID varchar(10),Name varchar(20),Sub1G varchar(3),Sub2G varchar(3),Sub3G

varchar(3),Sub4G varchar(3),Primary key(ID));

CourseD :

## create table CourseD(CName varchar(5),MaxCredits int,Primary Key(CName));

Values Insertion:

Student Table:

CourseD Table:

## a) select ((g1.Points/5) * c1.MaxCredits)+ ((g2.Points/5) * c2.MaxCredits)+((g3.Points/5)

*c3.MaxCredits) +((g4.Points/5) * c4.MaxCredits)as Credits

g4,CourseD c4

## CSP602: Advanced Database Management System MTECH CSE| NITPY 5

b) select (((g1.Points/5) * c1.MaxCredits)+((g2.Points/5) * c2.MaxCredits)+((g3.Points/5) *
c3.MaxCredits) +((g4.Points/5) * c4.MaxCredits)) / (c1.MaxCredits+c2.MaxCredits
+c3.MaxCredits+c4.MaxCredits) as AGPA

g4,CourseD c4

## c) select ID,(((g1.Points/5) * c1.MaxCredits)+((g2.Points/5) * c2.MaxCredits)+((g3.Points/5) *

c3.MaxCredits)+((g4.Points/5) * c4.MaxCredits)) /(c1.MaxCredits+c2.MaxCredits
+c3.MaxCredits+c4.MaxCredits) as AGPA

g4,CourseD c4

Result:

## CSP602: Advanced Database Management System MTECH CSE| NITPY 7

Ex. No.: 2 BASIC SQL

## For the above schema, perform the following

a. Find the total number of people who owned cars that were involved in accidents in
2009.

b. Add a new accident to the database; assume any values for required attributes.

## c. Delete the Mazda belonging to John Smith.

Aim

To create university database and write sql queries for different operations using basic SQL
command.

Theory

SQL

## SQL stands for Structured Query Language

SQL lets you access and manipulate databases
SQL is an ANSI (American National Standards Institute) standard
SQL can execute queries against a database
SQL can retrieve data from a database
SQL can insert records in a database
SQL can update records in a database
SQL can delete records from a database
SQL can create new databases
SQL can create new tables in a database

## CSP602: Advanced Database Management System MTECH CSE| NITPY 8

SQL can create stored procedures in a database
SQL can create views in a database
SQL can set permissions on tables, procedures, and views

Syntax-

## CREATE TABLE table_name (

column1 datatype,
column2 datatype,
column3 datatype,
....
);
SQL INSERT INTO

## The INSERT INTO statement is used to insert new records in a table.

Syntax -
INSERT INTO table_name VALUES (value1, value2, value3, ...);

SQL SELECT

## The SELECT statement is used to select data from a database.

The data returned is stored in a result table, called the result-set.
Syntax
SELECT column1, column2, ... FROM table_name;
To select whole table
SELECT * FROM table_name;

## SQL COUNT() Function

The COUNT() function returns the number of rows that matches a specified criteria.

Syntax -

## SQL MIN() and MAX() Functions

The MIN() function returns the smallest value of the selected column.

## CSP602: Advanced Database Management System MTECH CSE| NITPY 9

The MAX() function returns the largest value of the selected column.

Syntax

Syntax

Syntax

## SELECT SUM(column_name) FROM table_name WHERE condition;

Program/Procedure

Database Creation:

use InsurDB;

Table Creation:

Person:

(Driver_ID));

Car:

Acc:

Owns:

## CSP602: Advanced Database Management System MTECH CSE| NITPY 10

create table Owns(Driver_ID varchar(20),License varchar(20),foreign key (Driver_ID) references

Participated:

foreign

## alter table Participated add foreign key(Driver_ID) references Person(Driver_ID);

Values Insertion:

Person Table :

## CSP602: Advanced Database Management System MTECH CSE| NITPY 11

insert into Person values('Driv13','Shashank','Ranga Reddy');

Car Table :

## CSP602: Advanced Database Management System MTECH CSE| NITPY 12

insert into Car values('Hun103','Hundai',2010);

Acc Table:

## CSP602: Advanced Database Management System MTECH CSE| NITPY 13

insert into Acc values(10,'2016-08-08','Karaikal');

Owns Table:

## CSP602: Advanced Database Management System MTECH CSE| NITPY 14

Participatedicipated Table :

## a) Select count(distinct Driver_ID)as People from Acc,Participated where year(Dat)=2009 and

Participated.ReportNo=Acc.ReportNo;

## CSP602: Advanced Database Management System MTECH CSE| NITPY 16

c) Deleting Mazda belonging "John Smith"

delete from Car where Model='Mazda' and license in (select License from Owns where Driver_ID
in(select Driver_ID from Person where Name='John Smith'));

## select * from Car;

Result

Insurance database created and successfully executed and query results obtained.

## CSP602: Advanced Database Management System MTECH CSE| NITPY 17

Ex. No.: 3 BASIC SQL

## Employee (ename, eno, center_id, address, gen_workhours, working_hours, designation, salary)

Customer (cname, cu_id, address, join_date, phone_no)
Centre (cename, center_id, location address, no_of_employees)
Services (pname, it_id, center_id, service_date, scharge)
Inventory (iname, it_id, idate, quantity, cost)
Purchase (pname, pid, owner, pdate, center_id, it_id, price)
Billing (invoice_num, cu_id, pname, pid, paid_amt, service_chrg, cus_bal)

Database Question

The SHROY computer service center is owned by the SPARK computer dealer; the
SHROY services only SPARK computers. Five SHROY computer service center provide services
and assembles for the entire state. Each center is independently managed by a shop manager; a
receptionist and at least ten service engineers. Each center also maintains about, the history of
services made, parts used, costs, service dates, owner etc. Also there is a need to keep track about
inventory, purchasing, billing, employees hours and payroll.

CREATING SEQUENCE
Write a query that will automatically produce customer id and invoice number as
sequence in customer and billing relations respectively.

Aim:
To create an Employee database and execute SQL queries to achieve the desired results.

Theory

SQL
SQL is a standard language for accessing and manipulating databases.
SQL stands for Structured Query Language
SQL lets you access and manipulate databases
SQL is an ANSI (American National Standards Institute) standard
SQL can execute queries against a database
SQL can retrieve data from a database
SQL can insert records in a database
SQL can update records in a database

## CSP602: Advanced Database Management System MTECH CSE| NITPY 18

SQL can delete records from a database
SQL can create new databases
SQL can create new tables in a database
SQL can create stored procedures in a database
SQL can create views in a database
SQL can set permissions on tables, procedures, and views

Syntax-

## CREATE TABLE table_name (

column1 datatype,
column2 datatype,
column3 datatype,
....);

## SQL INSERT INTO

The INSERT INTO statement is used to insert new records in a table.
Syntax -
INSERT INTO table_name VALUES (value1, value2, value3, ...);

SQL SELECT

## The SELECT statement is used to select data from a database.

The data returned is stored in a result table, called the result-set.
Syntax
SELECT column1, column2, ... FROM table_name;
To select whole table
SELECT * FROM table_name;

## SQL PRIMARY KEY

The PRIMARY KEY constraint uniquely identifies each record in a database table.
Primary keys must contain UNIQUE values, and cannot contain NULL values.
A table can have only one primary key, which may consist of single or multiple fields.

## A FOREIGN KEY is a key used to link two tables together.

A FOREIGN KEY in a table points to a PRIMARY KEY in another table.

## CSP602: Advanced Database Management System MTECH CSE| NITPY 19

Program/Procedure

Database Creation:

use shroy_db;

Table Creation:

Center:

## create table center (center_id varchar(10),cename varchar(50), location_address varchar(50),

no_of_employees int, primary key(center_id));

Employee:

## create table employee (Ename char(20),Eno int,center_id varchar(20),Address

varchar(50),gen_workhours float,working_hours float,Designation char(20),Salary int,Primary
Key(Eno));

Customer:

## create table Customer(Cname Char(20),Cu_id int,Address varchar(50),Join_Date date,Phone_no

bigint,primary key (Cu_id));

Inventory :

Services:

int);

Purchase :

## Create table Purchase(Pname char(20),Pid varchar(20),Owner char(30),Pdate date,Center_id

varchar(20), Primary Key(Pid));

Billing:

## create table Billing(Invoice_num int,Cu_id varchar(10),Pname char(20),Pid varchar(20),Paid_amt

int,Service_Chrg int,Cus_Bal int,primary key (Invoice_num));

Values Insertion:

Center Table:

Employee Table:

Customer Table:

Inventory Table:

Billing Table:

Services Table:

Purchase Table :

## CSP602: Advanced Database Management System MTECH CSE| NITPY 23

select * from Purchase;

## a) alter table Billing modify Invoice_num int auto_increment;

insert into Billing (Cu_id, Pname,Pid, Paid_amt, Service_Chrg, Cus_Bal) values ('Cu20', 'Pandu',
'P000',2000, 100, 2100);

## CSP602: Advanced Database Management System MTECH CSE| NITPY 24

insert into Customer(Cname, Address,Join_Date,Phone_no) Values ('Vidya', 'Bombai','2015-07-
07', 9685321467);

Result:

## CSP602: Advanced Database Management System MTECH CSE| NITPY 25

Ex. No.: 4 INTERMEDIATE SQL

## Employee (ename, eno, center_id, address, gen_workhours, working_hours, designation,

salary)
Customer (cname, cu_id, address, join_date, phone_no)
Centre (cename, center_id, location address, no_of_employees)
Services (pname, it_id, center_id, service_date, scharge)
Inventory (iname, it_id, idate, quantity, cost)
Purchase (pname, pid, owner, pdate, center_id, it_id, price)
Billing (invoice_num, cu_id, pname, pid, paid_amt, service_chrg, cus_bal)

Database Question

The SHROY computer service center is owned by the SPARK computer dealer; the
SHROY services only SPARK computers. Five SHROY computer service center provide services
and assembles for the entire state. Each center is independently managed by a shop manager; a
receptionist and at least ten service engineers. Each center also maintains about, the history of
services made, parts used, costs, service dates, owner etc. Also there is a need to keep track about
inventory, purchasing, billing, employees hours and payroll.

Creating Sequence
To create a table for center relation which should automatically fix the value for an
attribute no_of_emp as some default value (say 20) in all the tuples.

Aim:
To create an Employee database and execute SQL queries to achieve the desired results.

Theory
SQL
SQL is a standard language for accessing and manipulating databases.

## SQL stands for Structured Query Language

SQL lets you access and manipulate databases
SQL is an ANSI (American National Standards Institute) standard
SQL can execute queries against a database
SQL can retrieve data from a database
SQL can insert records in a database

## CSP602: Advanced Database Management System MTECH CSE| NITPY 26

SQL can update records in a database
SQL can delete records from a database
SQL can create new databases
SQL can create new tables in a database
SQL can create stored procedures in a database
SQL can create views in a database
SQL can set permissions on tables, procedures, and views

Syntax-

## CREATE TABLE table_name (

column1 datatype,
column2 datatype,
column3 datatype,
....);

## SQL INSERT INTO

The INSERT INTO statement is used to insert new records in a table.
Syntax -
INSERT INTO table_name VALUES (value1, value2, value3, ...);

SQL SELECT

## The SELECT statement is used to select data from a database.

The data returned is stored in a result table, called the result-set.
Syntax
SELECT column1, column2, ... FROM table_name;
To select whole table
SELECT * FROM table_name;

## SQL PRIMARY KEY

The PRIMARY KEY constraint uniquely identifies each record in a database table.
Primary keys must contain UNIQUE values, and cannot contain NULL values.
A table can have only one primary key, which may consist of single or multiple fields.

## CSP602: Advanced Database Management System MTECH CSE| NITPY 27

A FOREIGN KEY in a table points to a PRIMARY KEY in another table.

Program/Procedure:
Database Creation:

use shroy_db;

Table Creation:

Center:

## create table center (center_id varchar(10),cename varchar(50), location_address varchar(50),

no_of_employees int, primary key(center_id));

Employee:

## create table employee (Ename char(20),Eno int,center_id varchar(20),Address

varchar(50),gen_workhours float,working_hours float,Designation char(20),Salary int,Primary
Key(Eno));

Customer:

Inventory :

Services:

int);

Purchase :

## Create table Purchase(Pname char(20),Pid varchar(20),Owner char(30),Pdate date,Center_id

varchar(20), Primary Key(Pid));

Billing:

## create table Billing(Invoice_num int,Cu_id varchar(10),Pname char(20),Pid varchar(20),Paid_amt

int,Service_Chrg int,Cus_Bal int,primary key (Invoice_num));

## CSP602: Advanced Database Management System MTECH CSE| NITPY 28

Values Insertion:

Center Table:

Employee Table:

Customer Table:

Inventory Table:

Billing Table:

Services Table:

Purchase Table :

## create table Centerr(Cename char(20),Center_id varchar(10) , Location_Address

varchar(30),No_of_employees int DEFAULT 20,

Result:

## CSP602: Advanced Database Management System MTECH CSE| NITPY 33

Ex. No.: 5 INTERMEDIATE SQL

## For the above schema, perform the following

1. List all products, with barcode and name, and the sale id and quantity for the sales
containing that product, if any (products that were never sold should still be listed in your result)
Hint: use a left join
2. Create a view called AllProductsSales based on the query in the Q.No.1.
3. Write a query against the AllProductsSales view as if it was a table:
select everything from the AllProductsSales, ordered
by BarCode and SaleID.
5. Write a query on the ProductProfit view to show the top 5 products based on total profit
on order of highest profit to lowest profit.

Aim:

To create an Online Store Database and execute SQL queries to achieve the desired results.

Theory

SQL VIEWS

## In SQL, a view is a virtual table based on the result-set of an SQL statement.

A view contains rows and columns, just like a real table. The fields in a view are fields from one
or more real tables in the database

You can add SQL functions, WHERE, and JOIN statements to a view and present the data as if the
data were coming from one single table.

CREATE VIEW

Syntax

## CSP602: Advanced Database Management System MTECH CSE| NITPY 34

CREATE VIEW view_name AS

Syntax

Syntax

## DROP VIEW view_name;

DDL

Data Definition Language (DDL) statements are used to define the database structure or schema.
Some examples:

## DROP - delete objects from the database

TRUNCATE - remove all records from a table, including all spaces allocated for the records are

removed.

## RENAME - rename an object

DML

Data Manipulation Language (DML) statements are used for managing data within schema
objects. Some examples:

## CSP602: Advanced Database Management System MTECH CSE| NITPY 35

DELETE - deletes all records from a table, the space for the records remain

DCL

## REVOKE - withdraw access privileges given with the GRANT command

TCL

Transaction Control (TCL) statements are used to manage the changes made by DML statements.
It allows statements to be grouped together into logical transactions.

## COMMIT - save work done

SAVEPOINT - identify a point in a transaction to which you can later roll back

## ROLLBACK - restore database to original since the last COMMIT

SET TRANSACTION - Change transaction options like isolation level and what rollback

segment to use.

Program/Procedure

Database Creation

## create database online_store;

use online_store;

Table Creation:

Product :

## create table product(barcode int, primary key(barcode), pname varchar(50),price float,

quantityInStock int);

## CSP602: Advanced Database Management System MTECH CSE| NITPY 36

Customer :

create table customer (cid int ,primary key(cid),phone varchar(20),email varchar(50), fname
varchar(50), lname varchar(50));

Sale :

## create table sale(sidd int , primary key(sidd), deliveryAddress varchar(50), credit_card

varchar(50), cid int, foreign key(cid) references customer(cid));

Saleitem :

create table saleitem (sid int, barcode int,quantity int,foreign key(sid) references sale(sid), foreign
key(barcode) references product(barcode));

Values Insertion:

Product Table :

Customer Table :

Sale Table :

Saleitem Table :

join saleitem;

saleitem;

Result:

## Product(BarCode, PName, Price, QuantityInStock)

Customer (CustomerID, Phone, Email, FirstName, LastName)
SaleItem (SaleID, BarCode, Quantity)

## Write MYSQL code queries to accomplish the following tasks.

Create a trigger called updateAvailableQuantity that updates the quantity in stock in the
Product table, for every product sold. The trigger should be executed after each insert operation on
the SaleItemtable: for the product with the given barcode (the one inserted into SaleItem), update
the available quantity in Product table to be the old quantity minus the sold quantity.

Aim:
To create an Online Store Database and execute SQL queries to achieve the desired results.

Theory:
Triggers
Triggers are stored programs, which are automatically executed or fired when some events occur.
Triggers are, in fact, written to be executed in response to any of the following events

## A database operation (SERVERERROR, LOGON, LOGOFF, STARTUP, or SHUTDOWN).

Triggers can be defined on the table, view, schema, or database with which the event is associated.

## Generating some derived column values automatically

Enforcing referential integrity
Event logging and storing information on table access
Auditing
Synchronous replication of tables
Imposing security authorizations

## CSP602: Advanced Database Management System MTECH CSE| NITPY 42

Preventing invalid transactions

## CREATE [OR REPLACE ] TRIGGER trigger_name

{BEFORE | AFTER | INSTEAD OF }
{INSERT [OR] | UPDATE [OR] | DELETE}
[OF col_name]
ON table_name
[REFERENCING OLD AS o NEW AS n]
[FOR EACH ROW]
WHEN (condition)
DECLARE
Declaration-statements
BEGIN
Executable-statements
EXCEPTION
Exception-handling-statements
END;

Program/Procedure :

Table Creation:

Product :
create table product(barcode int, primary key(barcode), pname varchar(50),price float,
quantityInStock int);

Customer :

create table customer (cid int ,primary key(cid),phone varchar(20),email varchar(50), fname
varchar(50), lname varchar(50));

Sale :

create table sale(sidd int, primary key(sidd), deliveryAddress varchar(50), credit_card varchar(50),
cid int, foreign key(cid) references customer(cid));

Saleitem :

create table saleitem (sidd int, barcode int,quantity int,foreign key(sid) references sale(sidd),
foreign key(barcode) references product(barcode));

## CSP602: Advanced Database Management System MTECH CSE| NITPY 43

Values Insertion:

Product Table :

Customer Table :

Sale Table :

Saleitem Table :

## AFTER insert on saleitem for each row

Begin

update product

set quantityInStock=product.quantityInStock-NEW.quantity

where product.barcode=New.barcode;

end;

Result:

## CSP602: Advanced Database Management System MTECH CSE| NITPY 47

Ex. No.: 7 E-R DIAGRAM

Date: 17.02.17

Aim:
To draw the ER diagram for the company databse.

## Construct an ER diagram for Company having following Details:

Company organized into DEPARTMENT. Each department has unique name,
number and a particular employee who manages the department. Start date for the
manager is recorded. Department may have several locations.
A department controls number of PROJECTS. Projects have a unique name,
number and a single location.
Companys EMPLOYEE name, ssno, address, salary, gender and birth date are
recorded. An employee is assigned to one department, but may work for several
projects (not necessarily controlled by her dept). Number of hours week an
employee works on each project is recorded by the immediate supervisor for the
employee.
Employees DEPENDENT are tracked for health insurance purposes (dependent
name, birth date, relationship to employee).

Theory:

## An entity relationship model, also called an entity-relationship (ER) diagram, is a graphical

representation of entities and their relationships to each other, typically used in computing in
regard to the organization of data within databases or information systems. An entity is a piece of
data-an object or concept about which data is stored.

A relationship is how the data is shared between entities. There are three types of relationships
between entities:
1. One-to-One
One instance of an entity (A) is associated with one other instance of another entity (B). For
example, in a database of employees, each employee name (A) is associated with only one social
security number (B).
2. One-to-Many
One instance of an entity (A) is associated with zero, one or many instances of another entity (B),
but for one instance of entity B there is only one instance of entity A. For example, for a company

## CSP602: Advanced Database Management System MTECH CSE| NITPY 48

with all employees working in one building, the building name (A) is associated with many
different employees (B), but those employees all share the same singular association with entity A.
3. Many-to-Many
One instance of an entity (A) is associated with one, zero or many instances of another entity (B),
and one instance of entity B is associated with one, zero or many instances of entity A. For
example, for a company in which all of its employees work on multiple projects, each instance of
an employee (A) is associated with many instances of a project (B), and at the same time, each
instance of a project (B) has multiple employees (A) associated with it.
ER Diagram Symbols
In an ER diagram, symbols are commonly used to to represent the types of information. Most
diagrams will use the following:
Boxes represent entities.
Diamonds represent relationships
Circles (ovals) represent attributes.
Procedure
Entities and Attributes
1. Department
Name
Number
Location
Primary Key(s): Name, number
2. Project
Name
Number
Location
Primary Key(s): Name, number
3. Employee
ssnumber
name
salary
gender
birthdate
Primary Key: ssnumber
4. Dependant
Name

Birthdate
Relationship

Result:

## CSP602: Advanced Database Management System MTECH CSE| NITPY 50

Ex. No.: 8 NORMALIZATION

Date: 10.03.17

Question: Reduce the following to BCNF, showing all the steps involved.

Identify any repeating groups and functional dependences in the PATIENT relation.
Show all the intermediate steps to derive the third normal form for PATIENT.

## Patno Patname Gpno Gpname Appdate Consultant Consaddr Sample

1 Rosy 10 Robinson 31.3.2016 Sreenath Karaikal Blood
2 Jacky 11 Ruby 30.3.2016 Russell Puducherry None
3 John 12 Tane 01.4.2016 Peter Nagoor None
4 Jane 13 Alex 31.3.2016 Sreenath Karaikal Blood
5 Daniel 14 Raju 31.3.2016 Sreenath Karaikal Sputum
6 Edward 15 July 01.4.2016 Peter Nagoor Blood

partno -> partdesc
sno,partno,custid -> quantity
sname -> sno
Suppliers supply many parts to many customers. Each customer deals with only one supplier.
Supplier names are unique. Customer names are not unique.

Aim
To perform normalization on the given database table.

Theory

1NF:
An attribute (column) of a table cannot hold multiple values. It should hold only atomic
values.
2NF:
A table is said to be in 2NF if both the following conditions hold.
Table is 1NF
No non-prime attribute is dependent on the proper subset of any candidate key of table.

## CSP602: Advanced Database Management System MTECH CSE| NITPY 51

3NF:
A table design is said to be in 3NF if both the following conditions hold.
Table must be in 2NF
Transitive functional dependency of non-prime attribute on any super key should be
removed.
BCNF:
It is an advanced version of 3NF thats why it is also referred as 3.5NF, BCNF is stricter
than 3NF.A table compiles with BCNF if it is in 3NF and for every functional dependency X-
>Y,X should be the super key of the table.

Procedure:
1) Functional Dependencies

In the above F.D a,b,c,d are in 2 NF,as the keys in LHS are not part of any key
In the above F.D e,f,g there are no attributes which are part of any key.
So.it is in 2NF
3>For 3NF-If it satisfies 2 NF & there is no transitive dependency.
A FD X->Y in a relation schema R is a transitive dependency if there exists a set of attributes Z in
R that is neither a candidate key nor a subset of any key of R & both X->Y &Z->Y hold.
Therefore we can say that either the LHS should be Superkey or RHS should be prime attribute
In F.D a,b,c,d ,LHS is a key, so that they are in 3NF.But on the other hand in e,f,g LHS is not a
key ,hence they are not in 3NF& there is transitive dependency also.
We need to decompose tables-

Table 1:
(Pat no.,Patname,Gpno,Gpname,Appdate,sample)

## CSP602: Advanced Database Management System MTECH CSE| NITPY 52

Table 2:
Again we will check for 3NF in decomposed tables-In table1(a,b,c,d)are F.D which are in 3NF as
they have keys on LHS.
In table2(e,f,g) are F.D which are in 3NF as they have keys on LHS.

## 2) for any realtion to be in BCNF,it should be in 1NF,No partial dependency,No transitive

dependency,If whenever a non-trivial FD X->A holds In R then X is superkey of R.
Therefore,in the given FDs above
1)1NF->No multivalued or composite attributes.So, it is in 1 NF.
2)In the given FDsSno.,part no.,cust id->quantity.sno. is apart of key
But in other there is no partial dependency
b)Partno->Partdesc
c)Sno.,partno,custid->quantity
d)Sname->sno.
There are composite attributes which is not in 1NF.So,the composite attributes (paartno.,part
desc,custid,custname,custaddr,quantity)) should be separated from the supplier realtion
Again table 2 is not in 2 NF
So,composite attributes need to be separated.
TABLE2:Supplier2(,(paartno.,part desc,sno)
3)checking whether any partial dependency
TABLE1:

## CSP602: Advanced Database Management System MTECH CSE| NITPY 53

d)Sname->sno
no partial dependency as Sno. Is a key& sname is unique.
TABLE2: b)Partno->Partdesc
Partial dependency is ther.
TABLE3: c)Sno.,partno,custid->quantity
Left side is a key,so no partial dependency.
So,supplier 2&3 are again need to be decomposed to make them in 2 NF
TABLE2: Partno.,Sno.
TABLE3: Partno,Partdesc
TABLE4:partno.,custid,quantity
Finally it is in 2NF
4)check whether they are in 3NF
Sname->Sno,
Partno->partdesc
Sno.,partno.,custid->quantity

1) For making given relation in 3NF we need to decompose into:
RELATION1 (Pat no.,Patname,Gpno,Gpname,Appdate,sample)
Therefore ,table have been formed in 3 NF successfully.

## CSP602: Advanced Database Management System MTECH CSE| NITPY 54

Result:
Converted to 3NF and BCNF from given table and Output verified successfully.

## CSP602: Advanced Database Management System MTECH CSE| NITPY 55

Ex. No.: 09 WEB APPLICATION USING PHP AND MYSQL
Date: 17.03.17 Restaurant Management System

Aim
To develop a web application using PHP and MySQL for Restaurant managent system.

Theroy
PHP is a server-side scripting language designed primarily for web development but also
used as a general-purpose programming language. Originally created by Rasmus Lerdorf in
1994,the PHP reference implementation is now produced by The PHP Development Team.[5]
PHP originally stood for Personal Home Page, but it now stands for the recursive acronym PHP:
Hypertext Preprocessor.
PHP code may be embedded into HTML or HTML5 markup, or it can be used in
combination with various web template systems, web content management systems and web
frameworks. PHP code is usually processed by a PHP interpreter implemented as a module in the
web server or as a Common Gateway Interface (CGI) executable. The web server software
combines the results of the interpreted and executed PHP code, which may be any type of data,
including images, with the generated web page. PHP code may also be executed with a command-
line interface (CLI) and can be used to implement standalone graphical applications.
The standard PHP interpreter, powered by the Zend Engine, is free software released under
the PHP License. PHP has been widely ported and can be deployed on most web servers on almost
every operating system and platform, free of charge.
The PHP language evolved without a written formal specification or standard until 2014,
leaving the canonical PHP interpreter as a de facto standard. Since 2014 work has gone on to
create a formal PHP specification.
PHP stores integers in a platform-dependent range, either a 64-bit or 32-bit signed integer
equivalent to the C-language long type. Unsigned integers are converted to signed values in certain
situations; this behavior is different from that of other programming languages.Integer variables
can be assigned using decimal (positive and negative), octal, hexadecimal, and binary notations.

Floating point numbers are also stored in a platform-specific range. They can be specified
using floating point notation, or two forms of scientific notation.PHP has a native Boolean type
that is similar to the native Boolean types in Java and C++. Using the Boolean type conversion
rules, non-zero values are interpreted as true and zero as false, as in Perl and C++.
The null data type represents a variable that has no value; NULL is the only allowed value for this
data type.
Variables of the "resource" type represent references to resources from external sources. These are
typically created by functions from a particular extension, and can only be processed by functions
from the same extension; examples include file, image, and database resources.

## CSP602: Advanced Database Management System MTECH CSE| NITPY 56

Arrays can contain elements of any type that PHP can handle, including resources, objects, and
other arrays. Order is preserved in lists of values and in hashes with both keys and values, and the
two can be intermingled.PHP also supports strings, which can be used with single quotes, double
quotes, nowdoc or heredoc syntax.
The Standard PHP Library (SPL) attempts to solve standard problems and implements
efficient data access interfaces and classes.
MySQL
MySQL (officially pronounced as /ma skjul/ "My S-Q-L",[6]) is an open-source
relational database management system (RDBMS). Its name is a combination of "My", the name
of co-founder Michael Widenius' daughter, and "SQL", the abbreviation for Structured Query
Language. The MySQL development project has made its source code available under the terms of
the GNU General Public License, as well as under a variety of proprietary agreements. MySQL
was owned and sponsored by a single for-profit firm, the Swedish company MySQL AB, now
owned by Oracle Corporation. For proprietary use, several paid editions are available, and offer
MySQL is written in C and C++. Its SQL parser is written in yacc, but it uses a home-
brewed lexical analyzer. MySQL works on many system platforms, including AIX, BSDi,
FreeBSD, HP-UX, eComStation, i5/OS, IRIX, Linux, macOS, Microsoft Windows, NetBSD,
Novell NetWare, OpenBSD, OpenSolaris, OS/2 Warp, QNX, Oracle Solaris, Symbian, SunOS,
SCO OpenServer, SCO UnixWare, Sanos and Tru64. A port of MySQL to OpenVMS also exists.
The MySQL server software itself and the client libraries use dual-licensing distribution.
They are offered under GPL version 2,beginning from 28 June 2000 (which in 2009 has been
extended with a FLOSS License Exception) or to use a proprietary license.

Program:
Home.php :
<?php require_once 'db_connection.php';
if(isset(\$_SESSION['email']))
redirectTo('food.php');
?>
<html>
<title>NITPY Restaurant:Home</title>
<div id="company_name">NITPY Restaurant</div>

## CSP602: Advanced Database Management System MTECH CSE| NITPY 57

<li><a href="Home.php">Home</a></li>
</ul> </div> <div id="center"> <h1><center>Welcome</center></h1>
<div class="body_text">
Order your food today from the Food Zone and it will be delivered at your door step. Jump in to
our weekly special deals in the Special Deals menu. Register an account with us to enjoy fast
ordering, delivery, and convenient payment of your food. Start now by logging in below or
registering if you don't have an account with us:
</div> <table align="center" width="100%">
<tr align="center"> <td style="text-align:center;"> <div style="border:black solid
<table width="290" border="0" align="center" cellpadding="2" cellspacing="0">
</td></tr><tr>
</tr> <tr> <td width="112"><b>Email</b></td>
<td width="188"><font color="#FF0000">* </font><input name="login" type="text"
<td><input type="reset" value="Clear"/>
</td><td> <input type="submit" name="Submit" value="Login" /></td></tr>
<tr><td>&nbsp;</td></tr>
</table> </form>
</div><div>
<td style="text-align:center;">
<div style="border:#bd6f2f solid 1px;padding:4px 6px 2px 6px;">

## CSP602: Advanced Database Management System MTECH CSE| NITPY 58

<table width="450" border="0" align="center" cellpadding="2" cellspacing="0"><tr>
<td colspan="2" style="text-align:center;"><h1 style="color:purple">Register Here</h1>
</td></tr><tr> <td colspan="2" style="text-align:center;"><font color="#FF0000">*
</font><input name="fname" type="text" class="textfield" id="fname" /></td> </tr> <tr>
<th>Last Name </th><td><font color="#FF0000">* </font><input name="lname" type="text"
class="textfield" id="lname" /></td></tr> <tr>5 <th width="124">Email</th> <td
width="168"><font color="#FF0000">* </font><input name="email" type="text"
class="textfield" id="email" /></td></tr> <tr>
<td ><input type="reset" value="Clear"/></td><td>
<input type="submit" name="Submit" value="Register" /></td></tr> <tr><td>&nbsp;</td></tr>
</table>
</form> </div> </body> </html>

## CSP602: Advanced Database Management System MTECH CSE| NITPY 59

<?php session_start(); ?>
<?php session_start(); ?>
<?php
require_once 'db_connection.php';
if(isset(\$_POST['Submit']))
{
\$conn=OpenCon();
\$qur = mysqli_query(\$conn,"select * from `user` where `Email` = '\$Email' and
if(mysqli_num_rows(\$qur) > 0) {
\$ar = mysqli_fetch_array(\$qur,MYSQLI_ASSOC);
\$_SESSION['email'] = \$Email;
\$_SESSION['user'] = \$ar['FName'];
redirectTo('food.php');}
else
redirectTo('Home.php?failed');}
?>

## CSP602: Advanced Database Management System MTECH CSE| NITPY 60

food.php
<?php session_start(); ?>
<html>
<title>Food Items</title>
<body>
<div id="page">
<div id="company_name">NITPY Restaurant</div></div>
</ul></div>
<div id="center">

## CSP602: Advanced Database Management System MTECH CSE| NITPY 61

<h1>ORDER YOUR FOOD:<br> Welcome <?php //echo \$_SESSION['user']; ?></h1>
<hr> <div style="border:#bd6f2f solid 1px;padding:4px 6px 2px 6px">
<table width="860" height="auto" style="text-align:center;" border="1">
<tr> <th>Food Photo</th> <th>Food Name</th> <th>Food Category</th> <th>Food Price</th>
<th>Quantity</th> <th>Action(s)</th> </tr> <tr> <td> <tr>
<td><img src='images/dosa.jpg' width="80" height="70"/></td> <td>Dosa</td>
<td>Breakfast</td> <td>30.00</td> <form action="MyAccount.php" method="post"><input
type="hidden" name="name" value="dosa" />
<input type="hidden" name="price" value="30" />
<input type="hidden" name="cat" value="Breakfast" />
<td><select name="count" ><option value="1" selected>1</option> <option value="2"
selected>2</option></select> <td><input type="submit" name="Submit" value="Order"
/></td></td> </form> </td></tr> <tr> <td><img src=images/Idli.jpg width="80"
height="70"/></td> <td>Idli</td> <td>Breakfast</td> <td>20.00</td><form
action="MyAccount.php" method="post"><input type="hidden" name="name" value="Idli"
/><input type="hidden" name="price" value="20" /> <input type="hidden" name="cat"
value="Breakfast" /> <td><select name="count" > <option value="1" selected>1</option>
<option value="2" selected>2</option> </select> <td><input type="submit" name="Submit"
value="Order" /></td></td> </td> </tr><tr>
<td><img src=images/Samosa.jpg width="80" height="70"/></td> <td>Samosaa</td>
<td>Breakfast</td> <td>10.00</td>
<form action="MyAccount.php" method="post"><input type="hidden" name="name"
value="Samosa" /> <input type="hidden" name="price" value="10" />
<input type="hidden" name="cat" value="Breakfast" />
<td><select name="count" > <option value="1" selected>1</option> <option value="2"
selected>2</option> </select>
<td><input type="submit" name="Submit" value="Order" /></td></td> </td>
</tr><tr> <td><img src=images/Ukma.jpg width="80" height="70"/></td> <td>Ukma</td>
<td>Breakfast</td> <td>40.00</td>

## CSP602: Advanced Database Management System MTECH CSE| NITPY 62

<form action="MyAccount.php" method="post"><input type="hidden" name="name"
value="Ukma" /> <input type="hidden" name="price" value="40" />
<input type="hidden" name="cat" value="Breakfast" /> <td><select name="count" >
<option value="1" selected>1</option> <option value="2" selected>2</option>
</select> <td><input type="submit" name="Submit" value="Order" /></td></td> </td>
</tr><tr> <td><img src=images/lemonrice.jpg width="80" height="70"/></td>
<td>lemonrice</td> <td>Breakfast</td> <td>40.00</td>
<form action="MyAccount.php" method="post"><input type="hidden" name="name"
value="Lemonrice" /> <input type="hidden" name="price" value="40" />
<input type="hidden" name="cat" value="Breakfast" /> <td><select name="count" >
<option value="1" selected>1</option> <option value="2" selected>2</option></select>
<td><input type="submit" name="Submit" value="Order" /></td></td> </td> </tr><tr>
<td><img src=images/bonda.jpg width="80" height="70"/></td> <td>Bonda</td>
<td>Breakfast</td><td>35.00</td>
<form action="MyAccount.php" method="post"><input type="hidden" name="name"
value="Bonda" /> <input type="hidden" name="price" value="35" /> <input type="hidden"
name="cat" value="Breakfast" /> <td><select name="count" > <option value="1"
selected>1</option> <option value="2" selected>2</option> </select> <td><input type="submit"
name="Submit" value="Order" /></td></td> </td> </tr><tr>
<td>Breakfast</td><td>40.00</td> <form action="MyAccount.php" method="post">
<input type="hidden" name="price" value="40" /> <input type="hidden" name="cat"
value="Breakfast" /> <td><select name="count" > <option value="1" selected>1</option>
<option value="2" selected>2</option> </select> <td><input type="submit" name="Submit"
value="Order" /></td></td> </td> </tr> <tr> <td><img src=images/eggB.jpg width="80"
height="70"/></td> <td>Egg Biryani</td> <td>Lunch/Dinner</td> <td>120.00</td>
<form action="MyAccount.php" method="post"><input type="hidden" name="name" value="Egg

## CSP602: Advanced Database Management System MTECH CSE| NITPY 63

Biryani" /><input type="hidden" name="price" value="120" />
<input type="hidden" name="cat" value="Lunch/Dinner" /> <td><select name="count" >
<option value="1" selected>1</option> <option value="2" selected>2</option>
</select> <td><input type="submit" name="Submit" value="Order" /></td></td></td> </tr> <tr>
<td><img src=images/chickenB.jpg width="80" height="70"/></td> <td>Chicken Biryani</td>
<td>Lunch/Dinner</td> <td>180.00</td> <form action="MyAccount.php" method="post"><input
type="hidden" name="name" value="Chicken Biryani" /> <input type="hidden" name="price"
value="180" /> <input type="hidden" name="cat" value="Lunch/Dinner" /><td><select
name="count" > <option value="1" selected>1</option> <option value="2" selected>2</option>
</select> <td><input type="submit" name="Submit" value="Order" /></td></td>
</td></tr></td></tr> </table> </div></div> </div></div></body></html>

Food Page
MyAccount.php
<?php session_start(); ?>

## CSP602: Advanced Database Management System MTECH CSE| NITPY 64

<title>User Home</title>
<div id=page>
<div id=company_name>NITPY Restaurant</div> </div>
<li><a href=Home.php>Home</a></li>
<li><a href=logout.php>Log out</a></li> </ul></div>
<div id=center><h1>Welcome
<h3><a href=food.php>Order More Food!</a></h3><hr>
<table border=0 width=910 style=text-align:center;>
<CAPTION><h2>ORDER HISTORY</h2></CAPTION>
<tr><th>user ID</th> <th>Food Name</th>
<th>Food Category</th> <th>Food Price</th>
<th>Quantity</th> <th>Total Cost</th></tr>
<?php
require_once db_connection.php;
\$email = \$_SESSION[email];
\$query = select * from `ordertbl` where uid=\$email;
if(isset(\$_POST[Submit])) {
\$name =\$_POST[name] ;
\$qnt= \$_POST[count];
\$cat = \$_POST[cat];
\$price = \$_POST[price];
\$total = \$price * \$qnt ;

## CSP602: Advanced Database Management System MTECH CSE| NITPY 65

\$uid = \$_SESSION[email];
\$q = INSERT INTO `ordertbl`(`food`, `cat`, `price`, `qnt`, `total`, `uid`) VALUES
(\$name,\$cat,\$price,\$qnt,\$total,\$uid);
\$c = OpenCon();
mysqli_query(\$c,\$q);
}
\$c = OpenCon();
\$exec = mysqli_query(\$c,\$query);
//loop through all table rows
//gets active currency
\$total=0;
while (\$row=mysqli_fetch_array(\$exec,MYSQLI_ASSOC)){
echo <tr>;
echo <td> . \$row[uid].</td>;
echo <td> . \$row[food].</td>;
echo <td> . \$row[cat].</td>;
echo <td> . \$row[price].</td>;
echo <td> . \$row[qnt].</td>;
echo <td> . \$row[total].</td>;
echo </tr>;
\$total=\$total+\$row[total];
}
echo <tr><td></td><td><td></td><td><td></td><td></td><td>Total Price:.
\$total.</td></tr>;
?></table></div></div></div></body></html>

## CSP602: Advanced Database Management System MTECH CSE| NITPY 66

Order Page
db_connection.php
<?php
function OpenCon()
{
\$dbhost = "localhost";
\$dbuser = "root";
\$dbpass = "";
\$db = "Restaurant";
\$conn = new mysqli(\$dbhost, \$dbuser, \$dbpass,\$db) or die("Connect failed: %s\n". \$conn ->
error);
return \$conn;
}
function CloseCon(\$conn)
{
\$conn -> close();
}

## CSP602: Advanced Database Management System MTECH CSE| NITPY 67

} ?>

Logout.php
<?php
require_once('db_connection.php');
\$_SESSION[] = array();
}
session_destroy();
redirectTo('Home.php?done');
?>

Register.php
<?php
require_once 'db_connection.php';

if(isset(\$_POST['Submit']))
{
\$conn=OpenCon();
\$Fname=\$_POST['fname'];
\$Lname=\$_POST['lname'];
\$Email=\$_POST['email'];
//if(!\$_POST['Submit']) {
\$qur = mysqli_query(\$conn,"INSERT INTO `user`(`FName`,`LName`,

## CSP602: Advanced Database Management System MTECH CSE| NITPY 68

redirectTo('Home.php?page=home');
}
?>

Registration Page
Result:
Developed restaurant management system successfully and output verified.

## CSP602: Advanced Database Management System MTECH CSE| NITPY 69

Ex. No.: 10 STUDY OF BIG DATA TECHNIQUES (HADOOP)
Date: 01.04.17

Aim
This practical intends to explain the basic concept of big data techniques and also
implementation of mapper and reducer program.

Software Required

## Desktop PC, 4 GB ram, Ubuntu, MS SQL server 2000, Hadoop 2.6.0

Theory

Big Data is the application of specialized techniques and technologies to process very
large sets of data. These data sets are often so large and complex that it becomes difficult to
process using on-hand database management tools. Examples include web logs, call records,
medical records, military surveillance, photography archives, video archives and large-scale e-
commerce.

## Data Management & Warehouse: Gain industry-leading database performance

server costs; Realize extreme speed with capabilities optimized for analytics
that can be up and running in hours.

Hadoop System: Bring the power of Apache Hadoop to the enterprise with
application accelerators, analytics, visualization, development tools, performance
and security features.

## Stream Computing: Efficiently deliver real-time analytic processing on constantly

changing data in motion and enable descriptive and predictive analytics to support
real- time decisions. Capture and analyze all data, all the time, just in time. With
stream computing, store less, analyze more and make better decisions faster.

## Content Management: Enable comprehensive content lifecycle and document

management with cost-effective control of existing and new types of content with
scale, security and stability.

## CSP602: Advanced Database Management System MTECH CSE| NITPY 70

Information Integration & Governance: Build confidence in big data with the
ability to integrate, understand, manage and govern data appropriately across its
lifecycle.

## Problem Scope: Hadoop is a large-scale distributed batch processing

infrastructure. While it can be used on a single machine, its true power lies in its
ability to scale to hundreds or thousands of computers, each with several processor
cores. Hadoop is also designed to efficiently distribute large amounts of work
across a set of machines.

## Support large-scale, multi-structured data processing and analysis with application

accelerators, visualization, dashboards, development tools and security features.
Quickly analyze diverse data sets such as text, log records, social media, news
feeds, email and electronic sensor output.
Augment your data warehouse environment with a flexible archive for storing,
processing and querying big data.

## Hadoop is designed to efficiently process large volumes of information by connecting

many commodity computers together to work in parallel. The theoretical 1000-CPU machine
described earlier would cost a very large amount of money, far more than 1,000 single-CPU or 250
quad-core machines. Hadoop will tie these smaller and more reasonably priced machines together
into a single cost-effective compute cluster.

## Comparison to Existing Techniques:

Performing computation on large volumes of data has been done before, usually in a
distributed setting. What makes Hadoop unique is its simplified programming model which allows
the user to quickly write and test distributed systems, and its efficient, automatic distribution of
data and work across machines and in turn utilizing the underlying parallelism of the CPU cores.

Data Distribution

In a Hadoop cluster, data is distributed to all the nodes of the cluster as it is being loaded
in. The Hadoop Distributed File System (HDFS) will split large data files into chunks which are
managed by different nodes in the cluster. In addition to this each chunk is replicated across
several machines, so that a single machine failure does not result in any data being unavailable. An
active monitoring system then re-replicates the data in response to system failures which can result
in Participatedial storage. Even though the file chunks are replicated and distributed across several
machines, they form a single namespace, so their contents are universally accessible.

## CSP602: Advanced Database Management System MTECH CSE| NITPY 71

Data is conceptually record-oriented in the Hadoop programming framework. Individual
input files are broken into lines or into other formats specific to the application logic. Each process
running on a node in the cluster then processes a subset of these records. The Hadoop framework
then schedules these processes in proximity to the location of data/records using knowledge from
the distributed file system. Since files are spread across the distributed file system as chunks, each
compute process running on a node operates on a subset of the data. Which data operated on by a
node is chosen based on its locality to the node: most data is read from the local disk straight into
the CPU, alleviating strain on network bandwidth and preventing unnecessary network transfers.
This strategy of moving computation to the data, instead of moving the data to the computation
allows Hadoop to achieve high data locality which in turn results in high performance.

MapReduce
Hadoop limits the amount of communication which can be performed by the processes, as
each individual record is processed by a task in isolation from one another. While this sounds like
a major limitation at first, it makes the whole framework much more reliable. Hadoop will not run
just any program and distribute it across a cluster. Programs must be written to conform to a
Particular programming model, named "MapReduce". In MapReduce, records are processed in
isolation by tasks called Mappers. The output from the Mappers is then brought together into a
second set of tasks called Reducers, where results different mappers can be merged. Separate
nodes in a Hadoop cluster still communicate with one another. However, in contrast to more
conventional distributed systems where application developers explicitly marshal byte streams
from node to node over sockets or through MPI buffers, communication in Hadoop is performed
implicitly. Pieces of data can be tagged with key names which inform Hadoop how to send related
bits of information to a common destination node. Hadoop internally manages all of the data
transfer and cluster topology issues.

Flat Scalability

One of the major benefits of using Hadoop in contrast to other distributed systems is its flat
scalability curve. Executing Hadoop on a limited amount of data on a small number of nodes may
not demonstrate Particularly stellar performance as the overhead involved in starting Hadoop
programs is relatively high. Other parallel/distributed programming paradigms such as MPI
(Message Passing Interface) may perform much better on two, four, or perhaps a dozen machines.

A program written in distributed frameworks other than Hadoop may require large amounts of
refactoring when scaling from ten to one hundred or one thousand machines.

Procedure/ Program

Mapper:
Mapper maps input key/value pairs to a set of intermediate key/value pairs.

## CSP602: Advanced Database Management System MTECH CSE| NITPY 72

Maps are the individual tasks that transform input records into intermediate records.
The transformed intermediate records do not need to be of the same type as the
input records. A given input pair may map to zero or many output pairs.

generated by the InputFormat for the job.

Reducer:

Reducer reduces a set of intermediate values which share a key to a smaller set of
values.

The number of reduces for the job is set by the user via
JobConf.setNumReduceTasks(int). Overall, Reducer implementations are passed
the JobConf for the job via the JobConfigurable.configure(JobConf) method and
can override it to initialize themselves.

## The framework then calls reduce(WritableComparable, Iterator, OutputCollector,

Reporter) method for each <key, (list of values)> pair in the grouped inputs.
Applications can then override the Closeable.close() method to perform any
required cleanup.

## Apache Hadoop is an open-source software framework written in Java for distributed

storage and distributed processing of very large data sets on computer clusters built from
commodity hardware. All the modules in Hadoop are designed with a fundamental assumption that
hardware failures (of individual machines or racks of machines) are commonplace and thus should
be automatically handled in software by the framework.

The core of Apache Hadoop consists of a storage Part (Hadoop Distributed File System
(HDFS)) and a processing Part (MapReduce). Hadoop splits files into large blocks and distributes
them amongst the nodes in the cluster. To process the data, Hadoop MapReduce transfers
packaged code for nodes to process in parallel, based on the data each node needs to process. This
approach takes advantage of data locality - nodes manipulating the data that they have on handto
allow the data to be processed faster and more efficiently than it would be in a more conventional
supercomputer architecture that relies on a parallel file system where computation and data are
connected via high-speed networking.

## CSP602: Advanced Database Management System MTECH CSE| NITPY 73

Hadoop Distributed File System (HDFS) a distributed file-system that stores data on
commodity machines, providing very high aggregate bandwidth across the cluster.
Hadoop YARN a resource-management platform responsible for managing computing
resources in clusters and using them for scheduling of users' applications.
Hadoop MapReduce a programming model for large scale data processing.

The term "Hadoop" has come to refer not just to the base modules above, but also to the
"ecosystem" or collection of additional software packages that can be installed on top of or
alongside Hadoop, such as Apache Pig, Apache Hive, Apache HBase, Apache Spark, and others.

The Hadoop framework itself is mostly written in the Java programming language, with
some native code in C and command line utilities written as Shell script. For end-users, though
MapReduce Java code is common, any programming language can be used with "Hadoop
Streaming" to implement the "map" and "reduce" Parts of the user's program.
Sample Program

1. The hadoop name node information runs on /localhost:50070. To start the hadoop
environment, run start-dfs.sh and start-yarn.sh.

## 3. Create a file named File.txt in hdfs.

This is the Hadoop Distributed File System running. This is an example for word count.
Keerthi Vasan Gounder.

## 4. Run the map reduce job as

hadoop jar wordcount.jar WordCount <file_location> <result_location>
5. View result of job as

## CSP602: Advanced Database Management System MTECH CSE| NITPY 74

Map Reduce Code:

import java.io.IOException;
import java.util.StringTokenizer;
public class WordCount {
public static class Map
extends Mapper<LongWritable, Text, Text, IntWritable>{
private final static IntWritable one = new IntWritable(1); // type of output value
private Text word = new Text(); // type of output key
public void map(LongWritable key, Text value, Context context
) throws IOException, InterruptedException {
StringTokenizer itr = new StringTokenizer(value.toString()); // line to string
while (itr.hasMoreTokens()) {
word.set(itr.nextToken()); // set word as each input keyword
context.write(word, one); }}}
public static class Reduce
extends Reducer<Text,IntWritable,Text,IntWritable> {
private IntWritable result = new IntWritable();
public void reduce(Text key, Iterable<IntWritable> values,
Context context ) throws IOException, InterruptedException {
int sum = 0; // initialize the sum for each keyword

## CSP602: Advanced Database Management System MTECH CSE| NITPY 75

for (IntWritable val : values) { sum += val.get(); }
result.set(sum);
context.write(key, result); // create a pair <keyword, number of occurences> } }
public static void main(String[] args) throws Exception {
Configuration conf = new Configuration();
String[] otherArgs = new GenericOptionsParser(conf, args).get RemainingArgs();
if (otherArgs.length != 2) {
System.err.println("Usage: WordCount <in> <out>");
System.exit(2); }
Job job = new Job(conf, "wordcount");
job.setJarByClass(WordCount.class);
job.setMapperClass(Map.class);
job.setReducerClass(Reduce.class);
job.setCombinerClass(Reduce.class);
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(IntWritable.class);