DBMsnew
DBMsnew
AIM- Consider the following relational database.Give expressions in QBE for each of the
following SQL
queries.Employee(personname,street,city)Works(personname,companyname,salary)Company(
companyname,city)Manages(personname,managername)Modify the database so that Jones
now lives in Newtown.Give all employees of First Bank Corporation a 10 % raise.Give all
mangers in the database a 10% raise.Give all mangers in the database a 10 % raise unless the
salary would be greater than Rs.100,000. In such a case give only a 3% raise.
SOFTWARE USED- MySQL
//CODE
CREATE DATABASE assignmnet1;
use assignmnet1;
CREATE TABLE Employee_12 (
personname VARCHAR(50),
street VARCHAR(50),
city VARCHAR(50),
PRIMARY KEY (personname));
-- Create the Works table
CREATE TABLE Works1 (
personname VARCHAR(50),
companyname VARCHAR(50),
salary DECIMAL(10, 2),
FOREIGN KEY (personname) REFERENCES Employee_12(personname));
-- Create the Company table
CREATE TABLE Company1(
companyname VARCHAR(50),
city VARCHAR(50),
PRIMARY KEY (companyname));
-- Create the Manages table
CREATE TABLE Manages1(
personname VARCHAR(50),
managername VARCHAR(50),
FOREIGN KEY (personname) REFERENCES Employee_12(personname),
FOREIGN KEY (managername) REFERENCES Employee_12(personname));
-- Insert data into the Employee table
INSERT INTO Employee_12 (personname, street, city) VALUES
('John', '123 Elm St', 'Chicago'),
('Alice', '456 Oak St', 'Boston'),
('Jones', '789 Maple St', 'New York'),
('Robert', '111 Pine St', 'San Francisco');
--Insert data into the Works table
INSERT INTO Works1 (personname, companyname, salary) VALUES
('John', 'First Bank Corporation', 50000),
('Alice', 'Tech Solutions', 80000),
('Jones', 'First Bank Corporation', 60000),
('Robert', 'Retail Corp', 90000);
-- Insert data into the Company table
INSERT INTO Company1 (companyname, city) VALUES
('First Bank Corporation', 'New York'),
('Tech Solutions', 'Boston'),
('Retail Corp', 'San Francisco');
-- Insert data into the Manages table
INSERT INTO Manages1 (personname, managername) VALUES
('John', 'Jones'),
('Alice', 'Robert');
-- Update Jones' city to Newtown
UPDATE Employee_12
SET city = 'Newtown'
WHERE personname = 'Jones';
SELECT * FROM Employee_12;
AIM- Explore and explain various types of database languages such as DDL, DML, DCL & TCL.
THEORY-
->DATA DEFINITION LANGUAGE (DDL)- This is used to create tables, indexes, schemas and
constraints. With the help of DDL we can define the dbms internal structure and pattern of the
database.
//multi-line comment
->DATA CONTROL LANGUAGE (DCL) -these commands are used to retrieve the saved data
from the database.
TRANSACTIONAL CONTROL LANGUAGE(TCL)- commands are used to run the changes made
by the DML commands one more thing is TCL can be grouped into a logical transaction.
1.COMMIT: It is used to save the transaction on the Database. These are very useful in the
banking sector.
2.ROLLBACK: It is used to restore the database to its original state from the last commit. This
command also plays an important role in Banking Sectors.
THEORY-
PRIMARY KEY- A primary key in a table that uniquely identifies each row and column or set of
columns in the table. The primary key is an attribute or a set of attributes that help to
uniquely identify the tuples(records) in the relational table. The primary key which refers to a
nonnull value that is required for the identification of the tuple (record) means that any
attribute can not contain the non-null value in DBMS. The primary key in which value can not
be duplicated in any of the rows or tuples of the relation.
FOREIGN KEY- A Foreign Key is a column in one table that matches a Primary Key in another
table, allowing the two tables to be connected together . A foreign key also maintains
referential integrity between two tables, making it impossible to drop the table containing the
primary key. In a foreign key reference, a link is created between two tables when the column
or columns that hold the primary key value for one table are referenced by the column or
columns in another table. This column becomes a foreign key in the second table.
RESULT- The query has been successfully implemented.
EXPERIMENT 9
AIM- Create a database called COMPANY consisting of two tables - EMP & DEPT EMP. Perform
the given queries on the tables just created.
//CODE
USE DBMS1;
ENAME VARCHAR(50),
JOB CHAR(10),
MGR CHAR(50),
HIREDATE DATE,
SAL CHAR(50),
COMM CHAR(59),
DEPTNO CHAR(40));
DNAME VARCHAR(50),
LOC VARCHAR(50));
-- Step 11: List employee names for those who have joined between 30 June and 31 Dec. 1981
FROM EMP;
-- Step 13: List the names of employees who are not eligible for commission
SELECT ENAME
FROM EMP
-- Step 14: List the name and designation of the employee who does not report to anybody
FROM EMP
-- Step 16: List the employees who are eligible for commission
-- Step 17: List employees whose names either start or end with “S”
SELECT ENAME
FROM EMP
-- Step 18: List names of employees whose names have “i” as the second character
SELECT ENAME
FROM EMP
-- Step 19: List the number of employees working with the company
SELECT COUNT(*)
FROM EMP;
-- Step 20: List the number of designations available in the EMP table
FROM EMP;
-- Step 21: List the total salaries paid to the employees
SELECT SUM(SAL)
FROM EMP;
-- Step 22: List the maximum, minimum, and average salary in the company
FROM EMP;
SELECT MAX(SAL)
AIM- To explore and implement SQL functions and understand their role in data manipulation.
THEORY-
SQL Functions are built-in programs that are used to perform different operations on the
database.
There are two types of functions in SQL:
1.Aggregate Functions
2.Scalar Functions.
1. AGGREGATE FUNCTIONS- SQL functions can operate on a data group and return a singular output.
2. SQL Scalar Functions are built-in functions that operate on a single value and return a single
value.
OUTPUT-
RESULT- The query has been implemented successfully.
EXPERIMENT- 4
//CODE
USE MEMORY;
ADDRESS1 varchar(30),
ADDRESS2 varchar(30),
CITY varchar(15),
PINCODE int,
STATE varchar(15),
BALDUE decimal(10,2));
INSERT INTO Client_Master_11 (ClientNo, Name, City, PinCode, State, BalDue) VALUES
('C00001', 'Ivan Bayross', 'Mumbai', 400054, 'Maharashtra', 15000);
INSERT INTO Client_Master_11 (ClientNo, Name, City, PinCode, State, BalDue) VALUES
('C00002', 'Mamta Muzumdar', 'Madras', 780001, 'Tamil Nadu', 0);
INSERT INTO Client_Master_11 (ClientNo, Name, City, Pincode, State, BalDue) VALUES
('C00003', 'Chhaya Bankar', 'Mumbai', 400057, 'Maharashtra', 5000);
INSERT INTO Client_Master_11 (ClientNo, Name, City, PinCode, State, BalDue)VALUES
('C00004', 'Ashwini Joshi', 'Bangalore', 560001, 'Karnataka', 0);
INSERT INTO Client_Master_11 (ClientNo, Name, City, PinCode, State, BalDue) VALUES
('C00006', 'Deepak Sharma', 'Mangalore', 560050, 'Karnataka', 0);
SELECT Name ,Address1, Address2, City, State, PinCode FROM Client_Master_11 WHERE
ClientNo
FROM Client_Master_11
WHERE ClientNo IN (
SELECT ClientNo
FROM Sales_Order
PRODUCTNO varchar(6),
DESCRIPTION varchar(15),
PROFITPERCENT decimal(4,2),
UNITMEASURE varchar(10),
INSERT INTO Product_Master_121 VALUES ('P00001', 'T-Shirts', 5, 'Piece', 200, 50, 350, 250);
INSERT INTO Product_Master_121 VALUES ('P03453', 'Shirts', 6, 'Piece', 150, 50, 500, 350);
INSERT INTO Product_Master_121 VALUES ('P06734', 'Cotton Jeans', 5, 'Piece', 100, 20, 600,
450);
INSERT INTO Product_Master_121 VALUES ('P07865', 'Jeans', 5, 'Piece', 100, 20, 750, 500);
INSERT INTO Product_Master_121 VALUES ('P07868', 'Trousers', 2, 'Piece', 150, 50, 850, 550);
INSERT INTO Product_Master_121 VALUES ('P07885', 'Pull Overs', 2.5, 'Piece', 80, 30, 700, 450);
INSERT INTO Product_Master_121 VALUES ('P07965', 'Denim Shirts', 4, 'Piece', 100, 40, 350,
250);
INSERT INTO Product_Master_121 VALUES ('P07975', 'Lycra Tops', 5, 'Piece', 70, 30, 300, 175);
INSERT INTO Product_Master_121 VALUES ('P08865', 'Skirts', 5, 'Piece', 75, 30, 450, 300);
/*salesman table*/
REMARKS varchar(60));
INSERT INTO Salesman_Master_11 VALUES ('S00003', 'Raj', 'P-7', 'Bandra', 'Mumbai', 400032,
'Maharashtra', 3000, 200, 100, 'Good');
INSERT INTO Salesman_Master_11 VALUES ('S00004', 'Ashish', 'A/5', 'Juhu', 'Bombay', 400044,
'Maharashtra', 3500, 200, 150, 'Good');
SELECT * FROM SALESMAN_MASTER_11;
/*salesorder table*/
CONSTRAINT ck_ord_status
VALUES ('O19001', '2002-06-12', 'C00001', 'F', 'N', 'S00001', '2002-07-20', 'In Process');
VALUES ('O19008', '2002-05-24', 'C00005', 'F', 'N', 'S00004', '1996-07-26', 'In Process');
Client_Master CM
Client_Master_11 CM
Client_Master_11 CM
GROUP BY Description;
SELECT Sales_Order_Details.ProductNo, Product_Master.Description,
Product'
GROUP BY SO.OrderNo;
OUTPUT-
1. Finding the non- moving products .i.e. products not being sold.
2. Finding the name and complete address for the customer who has placed order number
‘O19001’.
3. Finding the clients who have placed orders before the month of May'02.
4. Find out if the product 'Lycra Tops' has been ordered by any client and print the ClientNo,
Name to whom it was sold.
5. Find the names of clients who have placed orders worth Rs. 10000 or more.
THEORY- The GROUP BY Statement in SQL is used to arrange identical data into groups with
the help of some functions. i.e. if a particular column has the same values in different rows
then it will arrange these rows in a group.
//CODE
USE MEMORY;
ADDRESS1 varchar(30),
ADDRESS2 varchar(30),
CITY varchar(15),
PINCODE int,
STATE varchar(15),
BALDUE decimal(10,2));
INSERT INTO Client_Master_11 (ClientNo, Name, City, PinCode, State, BalDue) VALUES
('C00001', 'Ivan Bayross', 'Mumbai', 400054, 'Maharashtra', 15000);
INSERT INTO Client_Master_11 (ClientNo, Name, City, PinCode, State, BalDue) VALUES
('C00002', 'Mamta Muzumdar', 'Madras', 780001, 'Tamil Nadu', 0);
INSERT INTO Client_Master_11 (ClientNo, Name, City, Pincode, State, BalDue) VALUES
('C00003', 'Chhaya Bankar', 'Mumbai', 400057, 'Maharashtra', 5000);
INSERT INTO Client_Master_11 (ClientNo, Name, City, PinCode, State, BalDue) VALUES
('C00006', 'Deepak Sharma', 'Mangalore', 560050, 'Karnataka', 0);
SELECT Name ,Address1, Address2, City, State, PinCode FROM Client_Master_11 WHERE
ClientNo
FROM Client_Master_11
WHERE ClientNo IN (
SELECT ClientNo
FROM Sales_Order
PRODUCTNO varchar(6),
DESCRIPTION varchar(15),
PROFITPERCENT decimal(4,2),
UNITMEASURE varchar(10),
INSERT INTO Product_Master_121 VALUES ('P00001', 'T-Shirts', 5, 'Piece', 200, 50, 350, 250);
INSERT INTO Product_Master_121 VALUES ('P03453', 'Shirts', 6, 'Piece', 150, 50, 500, 350);
INSERT INTO Product_Master_121 VALUES ('P06734', 'Cotton Jeans', 5, 'Piece', 100, 20, 600,
450);
INSERT INTO Product_Master_121 VALUES ('P07865', 'Jeans', 5, 'Piece', 100, 20, 750, 500);
INSERT INTO Product_Master_121 VALUES ('P07868', 'Trousers', 2, 'Piece', 150, 50, 850, 550);
INSERT INTO Product_Master_121 VALUES ('P07885', 'Pull Overs', 2.5, 'Piece', 80, 30, 700, 450);
INSERT INTO Product_Master_121 VALUES ('P07965', 'Denim Shirts', 4, 'Piece', 100, 40, 350,
250);
INSERT INTO Product_Master_121 VALUES ('P07975', 'Lycra Tops', 5, 'Piece', 70, 30, 300, 175);
INSERT INTO Product_Master_121 VALUES ('P08865', 'Skirts', 5, 'Piece', 75, 30, 450, 300);
/*salesman table*/
REMARKS varchar(60));
INSERT INTO Salesman_Master_11 VALUES ('S00004', 'Ashish', 'A/5', 'Juhu', 'Bombay', 400044,
'Maharashtra', 3500, 200, 150, 'Good');
/*salesorder table*/
CONSTRAINT ck_ord_status
VALUES ('O19001', '2002-06-12', 'C00001', 'F', 'N', 'S00001', '2002-07-20', 'In Process');
VALUES ('O19008', '2002-05-24', 'C00005', 'F', 'N', 'S00004', '1996-07-26', 'In Process');
Client_Master_11 CM
Client_Master_11 CM
GROUP BY Description;
Product'
GROUP BY SO.OrderNo;
OUTPUT-
1. Printing the description and total quantity sold for each product.
2. Finding the value of each product sold.
3. Calculating the average quantity sold for each client that has a maximum order value of
15000.00.
4. Finding out the total of all the billed orders for the month of June.
RESULT- The query is successfully implemented.
EXPERIMENT -6
THEORY- A join in SQL is used to combine rows from two or more tables based on a related
column between them. Joins are fundamental in relational databases as they allow you to
retrieve data spread across multiple tables in a meaningful way. There are several types of
joins, each serving different purposes for how tables are combined.
//CODE
USE MEMORY;
ADDRESS1 varchar(30),
ADDRESS2 varchar(30),
CITY varchar(15),
PINCODE int,
STATE varchar(15),
BALDUE decimal(10,2));
INSERT INTO Client_Master_11 (ClientNo, Name, City, PinCode, State, BalDue) VALUES
('C00001', 'Ivan Bayross', 'Mumbai', 400054, 'Maharashtra', 15000);
INSERT INTO Client_Master_11 (ClientNo, Name, City, PinCode, State, BalDue) VALUES
('C00002', 'Mamta Muzumdar', 'Madras', 780001, 'Tamil Nadu', 0);
INSERT INTO Client_Master_11 (ClientNo, Name, City, Pincode, State, BalDue) VALUES
('C00003', 'Chhaya Bankar', 'Mumbai', 400057, 'Maharashtra', 5000);
INSERT INTO Client_Master_11 (ClientNo, Name, City, PinCode, State, BalDue) VALUES
('C00006', 'Deepak Sharma', 'Mangalore', 560050, 'Karnataka', 0);
SELECT Name ,Address1, Address2, City, State, PinCode FROM Client_Master_11 WHERE
ClientNo
FROM Client_Master_11
WHERE ClientNo IN (
SELECT ClientNo
FROM Sales_Order
PRODUCTNO varchar(6),
DESCRIPTION varchar(15),
PROFITPERCENT decimal(4,2),
UNITMEASURE varchar(10),
INSERT INTO Product_Master_121 VALUES ('P00001', 'T-Shirts', 5, 'Piece', 200, 50, 350, 250);
INSERT INTO Product_Master_121 VALUES ('P03453', 'Shirts', 6, 'Piece', 150, 50, 500, 350);
INSERT INTO Product_Master_121 VALUES ('P06734', 'Cotton Jeans', 5, 'Piece', 100, 20, 600,
450);
INSERT INTO Product_Master_121 VALUES ('P07865', 'Jeans', 5, 'Piece', 100, 20, 750, 500);
INSERT INTO Product_Master_121 VALUES ('P07868', 'Trousers', 2, 'Piece', 150, 50, 850, 550);
INSERT INTO Product_Master_121 VALUES ('P07885', 'Pull Overs', 2.5, 'Piece', 80, 30, 700, 450);
INSERT INTO Product_Master_121 VALUES ('P07965', 'Denim Shirts', 4, 'Piece', 100, 40, 350,
250);
INSERT INTO Product_Master_121 VALUES ('P07975', 'Lycra Tops', 5, 'Piece', 70, 30, 300, 175);
INSERT INTO Product_Master_121 VALUES ('P08865', 'Skirts', 5, 'Piece', 75, 30, 450, 300);
/*salesman table*/
REMARKS varchar(60));
INSERT INTO Salesman_Master_11 VALUES ('S00004', 'Ashish', 'A/5', 'Juhu', 'Bombay', 400044,
'Maharashtra', 3500, 200, 150, 'Good');
/*salesorder table*/
CONSTRAINT ck_ord_status
VALUES ('O19001', '2002-06-12', 'C00001', 'F', 'N', 'S00001', '2002-07-20', 'In Process');
VALUES ('O19008', '2002-05-24', 'C00005', 'F', 'N', 'S00004', '1996-07-26', 'In Process');
Client_Master_11 CM
Client_Master_11 CM
GROUP BY Description;
Product'
GROUP BY SO.OrderNo;
OUTPUT-
1. Find out the products, which have been sold to 'Ivan Bayross'.
2. Listing the ProductNo and description of constantly sold (i.e. rapidly moving) products.
4. Listing the products and orders from customers who have ordered less than 5 units of 'Pull
Overs'.
5. Finding the products and their quantities for the orders placed by 'Ivan Bayross' and 'Mamta
Muzumdar'.
6. Finding the products and their quantities for the orders placed by ClientNo 'C00001' and
'C00002'.
AIM- Create the tables EMPLOYEE, WORKS, COMPANY, MANAGES. Specify primary and foreign
keys. Enter at least 5 tuples in each table with relevant data and solve the given queries.
THEORY- A primary key is used to ensure that data in the specific column is unique. A column
cannot have NULL values. It is either an existing table column or a column that is specifically
generated by the database according to a defined sequence.
A foreign key is a column or group of columns in a relational database table that provides a
link between data in two tables. It is a column (or columns) that references a column (most
often the primary key) of another table.
//CODE
use dbmsfile1;
street varchar(20),
city varchar(20));
city varchar(50));
employeename varchar(100),
companyname varchar(100),
salary double,
managername varchar(20),
on e.employeename=e.employeename
order by companyname;
select "--------------------------------------------------";
select e.employeename,e.street,e.city
where e.employeename=w.employeename
and salary>10000;
select e.employeename
where e.employeename=w.employeename
and w.companyname=c.companyname
and e.city=c.city;
from works12
group by companyname;
select e.employeename
where e.employeename=r.employeename
and m.employeename=r.managername
and e.city=m.city
and e.street=m.street;
select employeename
from works12
select employeename
from works12
where salary>
(select max(salary)
from works12
select e.employeename,e.street,e.city
where e.employeename=w.employeename
where e.employeename=w.employeename
and w.companyname=c.companyname
and e.city=c.city;
select employeename
from works12
where salary>
(select max(salary)
from works12