DBMS Record
DBMS Record
DBMS Record
: 1 BASIC SQL
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.
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.
Aim
To create a student database and write sql queries for different operations using basic SQL command.
Theory
SQL
SQL SELECT
Program/Procedure :
Database Creation:
use University;
Table Creation:
GradeP :
Student :
CourseD :
Values Insertion:
GradeP Table:
Student Table:
CourseD Table:
Answer:
Result:
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.
Aim
To create university database and write sql queries for different operations using basic SQL command.
Theory
SQL
SQL SELECT
The COUNT() function returns the number of rows that matches a specified criteria.
Syntax -
The MIN() function returns the smallest value of the selected column.
The MAX() function returns the largest value of the selected column.
Syntax –
Syntax –
Syntax –
Program/Procedure
Database Creation:
use InsurDB;
Table Creation:
Person:
Car:
Acc:
Owns:
Participated:
Values Insertion:
Person Table :
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'));
Result
Insurance database created and successfully executed and query results obtained.
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, employee’s 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
SQL can delete records from a database
SQL can create new databases
SQL can create new tables in a database
SQL SELECT
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.
Program/Procedure
Database Creation:
Table Creation:
Center:
Employee:
Customer:
Inventory :
Services:
Purchase :
Billing:
Values Insertion:
Center Table:
Customer Table:
Billing Table:
Purchase Table :
insert into Billing (Cu_id, Pname,Pid, Paid_amt, Service_Chrg, Cus_Bal) values ('Cu20', 'Pandu', 'P000',2000,
100, 2100);
b)
Result:
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, employee’s 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 SELECT
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.
use shroy_db;
Table Creation:
Center:
Employee:
Customer:
Inventory :
Services:
Purchase :
Billing:
Values Insertion:
Center Table:
Employee Table:
Customer Table:
Inventory Table:
Billing Table:
Purchase Table :
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
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 –
Syntax –
Syntax –
DDL
Data Definition Language (DDL) statements are used to define the database structure or schema. Some
examples:
TRUNCATE - remove all records from a table, including all spaces allocated for the records are
removed.
DML
Data Manipulation Language (DML) statements are used for managing data within schema objects. Some
examples:
DELETE - deletes all records from a table, the space for the records remain
DCL
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.
SAVEPOINT - identify a point in a transaction to which you can later roll back
SET TRANSACTION - Change transaction options like isolation level and what rollback
segment to use.
Program/Procedure
Database Creation
use online_store;
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 (sid int, barcode int,quantity int,foreign key(sid) references sale(sid), foreign key(barcode) references
product(barcode));
Product Table :
Customer Table :
Answers
Result:
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 −
Triggers can be defined on the table, view, schema, or database with which the event is associated.
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 auto_increment,primary key(cid),phone varchar(20),email varchar(50), fname
varchar(50), lname varchar(50));
Sale :
create table sale(sid int auto_increment, primary key(sid), 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 :
Saleitem Table :
Answer :
Begin
update product
set quantityInStock=product.quantityInStock-NEW.quantity
where product.barcode=New.barcode;
end;
Result:
Date: 17.02.17
Aim:
To draw the ER diagram for the company databse.
Theory:
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 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
Result:
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.
PATIENT(Patno,Patname,Gpno,Gpname,Appdate,Consultant,Conaddr,Sample)
Supplier(sno,sname,saddress,(partno, partdesc,(custid,custname,custaddr,quantity)))
sno -> sname,saddr
partno -> partdesc
sno,partno,custid -> quantity
sname -> sno
custid -> custname,custaddr
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.
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.
Procedure:
1) Functional Dependencies
a) Patno -> Patname,Gpno,Gpname,Adddate,Consultant,Conaddr,Sample
b) Patname -> Patno,Gpno,Gpname,Adddate,Consultant,Conaddr,Sample
c) Gpno -> Patname,Patno,Gpname,Adddate,Consultant,Conaddr,Sample
d) Gpname -> Patname,Gpno,Patno,Adddate,Consultant,Conaddr,Sample
e) Appdate->Consultant,Consaddr
f) Consultant->Appdate,Conaddr
g) Conaddr->Appdate,Consultant
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
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.
Appdate->Consultant,Consaddr
Consultant->Consaddr
Consaddr->Appdate
Table 1:
(Pat no.,Patname,Gpno,Gpname,Appdate,sample)
Table 2:
(Appdate,Consultant,consaddr)
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.
a) Sno.->Sname,Saddr
b)Partno->Partdesc
c)Sno.,partno,custid->quantity
d)Sname->sno.
e)Custid->Custname,Custaddr
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
TABLE1:Supplier(Sno.,Sname,Saddr)
TABLE2:Supplier2(,(paartno.,part desc,sno.,custid,custname,custaddr,quantity))
TABLE1:Supplier(Sno.,Sname,Saddr)
TABLE2:Supplier2(,(paartno.,part desc,sno)
TABLE1:
a) Sno.->Sname,Saddr
d)Sname->sno
TABLE2: b)Partno->Partdesc
TABLE3: c)Sno.,partno,custid->quantity
TABLE1: Sno.,Sname,Saddr
TABLE2: Partno.,Sno.
TABLE3: Partno,Partdesc
TABLE4:partno.,custid,quantity
TABLE5:custid,custname,custaddr,Sno.
Finally it is in 2NF
Sno.->Sname,Saddr
Sname->Sno,
Partno->partdesc
Sno.,partno.,custid->quantity
Custid->custname,custaddr
Answer
1) For making given relation in 3NF we need to decompose into:
RELATION2(Appdate,Consultant,consaddr)
Result:
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
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.
Hadoop System: Bring the power of Apache Hadoop to the enterprise with application
accelerators, analytics, visualization, development tools, performance and security features.
Information Integration & Governance: Build confidence in big data with the ability to
integrate, understand, manage and govern data appropriately across its lifecycle.
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.
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
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.
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.
The Hadoop Map/Reduce framework spawns one map task for each InputSplit 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.
Apache Hadoop
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 hand—to 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.
Hadoop Common – contains libraries and utilities needed by other Hadoop modules.
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.
Hadoop Environment:
1. The hadoop name node information runs on /localhost:50070. To start the hadoop environment, run
start-dfs.sh and start-yarn.sh.
2. Add files to the hadoop file system using
Hadoop dfs -CopyToLocal </hadoop_directory> </OS_directory>
“This is the Hadoop Distributed File System running. This is an example for word count. Keerthi
Vasan Gounder.”
import java.io.IOException;
import java.util.StringTokenizer;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.*;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
Output: