Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 42

DBMS Laboratory with

Mini Project (21CSL55)


Database 2
Consider the following schema for Order Database:
SALESMAN(Sid, SName, City, Commission)
CUSTOMER(Cid, CName, City, Grade, Sid)
ORDERS(Orderno, Purchase_Amt, Ord_Date, Cid, Sid)
ER Diagram
Schema Diagram
Create Tables
create table salesman create table customer
( (
sid int, cid int,
sname varchar(15), cname varchar(15),
city varchar(15), city varchar(15),
commission int, grade int,
primary key(sid) sid int,
); primary key(cid),
foreign key(sid) references salesman(sid)
on delete cascade
);
Create Tables
create table orders
(
orderno int,
purchaseamt int,
orddate date,
cid int,
sid int,
primary key(orderno),
foreign key(cid) references customer(cid) on delete cascade,
foreign key(sid) references salesman(sid) on delete cascade
);
Insert Values
insert into salesman values(1000,'arun','mangalore',15);
insert into salesman values(2000,'harsha','bangalore',25);
insert into salesman values(3000,'sagar','mysore',20);
insert into salesman values(4000,'priya','mangalore',30);
insert into salesman values(5000,'divya','delhi',15);
insert into customer values(10,'ravi','bangalore',100,1000);
insert into customer values(12,'shwetha','mangalore',300,1000);
insert into customer values(14,'shalini','chenai',500,2000);
insert into customer values(16,'sushmitha','bangalore',700,2000);
insert into customer values(18,'pramya','bangalore',500,3000);
insert into orders values(60,5000, '2017-01-05',10,1000);
insert into orders values(62,3000,'2017-01-17',10,2000);
insert into orders values(64,4000,'2017-02-17',12,2000);
insert into orders values(66,450,'2017-03-17',14,3000);
insert into orders values(68,2000,'2017-02-17',16,1000);
Query 2: Find the name and numbers of
all salesman who had more than one
customer.
select s.sid, s.sname, count(cid)
from salesman s, customer c
where s.sid = c.sid
group by (s.sname) having count(cid)>1;
Aggregate functions in SQL
count()
Min()
max()
Sum()
Avg()
Example for Sub-Query

Retrieve the name of the employee whose salary is


more the average salary of the company

EMPLOYEE(Ename, SSN, DOB, Salary)

Select Ename
From Employee
Where salary > (Select avg(salary) from employee );
1.Count the customers with grades above Bangalore’s
average.
select grade, count(*) as COUNT
from customer where grade >
(select avg(grade)
from customer
where city ='bangalore');
3. List all the salesman and indicate those who have and don’t have
customers in their cities (Use UNION operation.)
select sname, 'exists' as samecity
from salesman s
where city in
(select city
from customer c
where s.sid = c.sid)
union
select sname, 'not exists' as samecity
from salesman s where
city not in
(select city
from customer c
where s.sid = c.sid);
In SQL, View is called as Virtual table
View a qurey result for which we give a separate name
Student(name, usn, sem, Dob, Phone Address, Marks, feestatus)

Create view Fee_Status as


(select name, usn, sem
Output
from student
A 12 7
where feestatus = ‘No’); B 111 3
Select * From Fee_Status;
4. Create a view that finds the salesman who has the customer with the highest
order of a day.
create view highest_order as
select s.sid, s.sname, o.purchaseamt, o.orddate
from salesman s,orders o
where s.sid = o.sid and
o.purchaseamt =
(select max(purchaseamt)
from orders c
where c.orddate =o. orddate);

select * from highest_order;


5.Demonstrate the DELETE operation by
removing salesman with id 1000. All his orders
must also be deleted.
SQL> delete from salesman where sid=1000;

Additional Query:
1. Select the name of the salesmans who gets highest commission.
2. Count the number of orders that each customers have done.
3. More than 3 orders.
CONSTRAINT
• MySQL CONSTRAINT is used to define rules to allow or restrict what
values can be stored in columns.
• The purpose of inducing constraints is to enforce the integrity of a
database.
• MySQL CONSTRAINTS are used to limit the type of data that can be
inserted into a table.
• MySQL CONSTRAINTS can be classified into two types - column level
and table level.
• The column level constraints can apply only to one column where as
table level constraints are applied to the entire table.
• MySQL CONSTRAINT is declared at the time of creating a table.
MySQL CONSTRAINTs are :
• NOT NULL
• UNIQUE
• PRIMARY KEY
• FOREIGN KEY
• CHECK
• DEFAULT
CONSTRAINT DESCRIPTION
NOT NULL In MySQL NOT NULL constraint allows to specify that a column can not contain any NULL
value. MySQL NOT NULL can be used to CREATE and ALTER a table.
The UNIQUE constraint in MySQL does not allow to insert a duplicate value in a column.
UNIQUE The UNIQUE constraint maintains the uniqueness of a column in a table. More than one
UNIQUE column can be used in a table.

PRIMARY KEY A PRIMARY KEY constraint for a table enforces the table to accept unique data for a specific
column and this constraint creates a unique index for accessing the table faster.

A FOREIGN KEY in MySQL creates a link between two tables by one specific column of both
FOREIGN KEY tables. The specified column in one table must be a PRIMARY KEY and referred by the
column of another table known as FOREIGN KEY.

A CHECK constraint controls the values in the associated column. The CHECK constraint
CHECK
determines whether the value is valid or not from a logical expression.
In a MySQL table, each column must contain a value ( including a NULL). While inserting
DEFAULT data into a table, if no value is supplied to a column, then the column gets the value set as
DEFAULT.
NOT NULL Constaint
CREATE TABLE Student(s_id int NOT NULL, Name varchar(60), Age int);

UNIQUE Constaint
CREATE TABLE Student(s_id int NOT NULL UNIQUE, Name
varchar(60), Age int);
PRIMARY KEY Constraint
CREATE table Student (s_id int PRIMARY KEY, Name
varchar(60) NOT NULL, Age int);

ALTER table Student ADD PRIMARY KEY (s_id);


FOREIGN KEY Constraint

CREATE table Order_Detail( order_id int PRIMARY KEY,


order_name varchar(60) NOT NULL, c_id int FOREIGN KEY
REFERENCES Customer_Detail(c_id) );

ALTER table Order_Detail ADD FOREIGN KEY (c_id) REFERENCES


Customer_Detail(c_id);
Check Constraint
CREATE table Student( s_id int NOT NULL
CHECK(s_id > 0), Name varchar(60) NOT NULL,
Age int );

ALTER table Student ADD CHECK(s_id > 0);


REFERENTIAL INTEGRITY OPTIONS
• Since Database Management System enforces referential constraints, it must
ensure data integrity if rows in a referenced table are to be deleted (or
updated).
• If dependent rows in referencing tables still exist, those references have to
be considered.
• SQL specifies following different referential actions that shall take place in
such occurrences:
o CASCADE
o RESTRICT
o SET NULL
o SET DEFAULT
CASCADE
• Whenever rows in the master (referenced) table are deleted, the
respective rows of the child (referencing) table with a matching
foreign key column will get deleted as well. This is called a
cascade delete.
• Example Tables: Customer(customer_id, cname, caddress) and
Order(customer_id, product_id, payment)
• Customer is the master table and Order is the child table, where
'customer_id' is the foreign key in Order table and represents the
customer who placed the order.
• When a row of Customer is deleted, any Order row matching the
deleted Customer's customer_id will also be deleted.
RESTRICT
• A value cannot be updated or deleted when a row exists in a foreign
key table that references the value in the referenced table.
• Similarly, a row cannot be deleted as long as there is a reference to it
from a foreign key table.

SET NULL
• The foreign key values in the referencing row are set to NULL when
the referenced row is updated or deleted.
• This is only possible if the respective columns in the referencing
table are nullable.
• Due to the semantics of NULL, a referencing row with NULLs in
the foreign key columns does not require a referenced row.

SET DEFAULT
• Similar to SET NULL, the foreign key values in the referencing row
are set to the column default value when the referenced row is
DEPARTMENT
Dno DeptName Location

10 Finance Charlotte
20 Infosys New York
30 Marketing Woodbridge
40 Accountant California

PROJECT
PID PName Dept_no

1 Product-X 10
2 Product-Y 10
3 Product-Z 20
4 Product-A 30
5 Product-B 40
SELECT * FROM user_constraints WHERE table_name = '<your table name>'
AND constraint_name = '<your constraint name>';
Data Retrieval:
SELECT [DISTINCT | ALL] attribute-list
FROM tablename-list – DISTINCT will eliminate
[WHERE condition ] duplication in the output
[GROUP BY expr [, expr]] while ALL will keep all
[HAVING condition] duplicated rows.
[ORDER BY {expr} [ASC | DESC]]
• select * from dept; • select max(salary)
from emp
• select deptname group by positionid;
from dept
where deptid='10'; • select deptname
from dept,emp
• select lname,fname
where dept.deptid=emp.deptid and
from emp
order by lname desc; emp.empid='111';
Figure: SQL statement processing
order
Customer
CustomerID CustomerName ContactName Address City PostalCode Country

1 Alfreds Futterkiste Maria Anders Obere Str. 57 Berlin 12209 Germany

2 Ana Trujillo Ana Trujillo Avda. de la México D.F. 05021 Mexico


Emparedados y Constitución
helados 2222
3 Antonio Moreno Antonio Mataderos 2312 México D.F. 05023 Mexico
Taquería Moreno

4 Around the Horn Thomas 120 Hanover Sq. London WA1 1DP UK
Hardy

5 Berglunds Christina Berguvsvägen 8 Luleå S-958 22 Sweden


snabbköp Berglund
Group By Clause
Query: Lists the number of customers in each country

SELECT COUNT(CustomerID), Country


FROM Customer
GROUP BY Country;
Student

Employee
Database 1:
Consider the following schema for a Library Database:
BOOK (Book_id, Title, Publisher_Name, Pub_Year)
BOOK_AUTHORS (Book_id, Author_Name)
PUBLISHER (Name, Address, Phone)
BOOK_COPIES (Book_id, Branch_id, No-of_Copies)
BOOK_LENDING (Book_id, Branch_id, Card_No,
Date_Out, Due_Date)
LIBRARY_BRANCH (Branch_id, Branch_Name, Address)

You might also like