23CS1312 DBMS LAB
CONSTRAINTS
PROGRAM
mysql> use dbms
Database changed
mysql> DELIMITER $$
mysql>
mysql> CREATE PROCEDURE create_table_with_constraintss(
-> IN min_salary INT,
-> IN max_salary INT
-> )
-> BEGIN
-> DECLARE table_name VARCHAR(255);
-> SET table_name = 'employees';
->
-> -- Drop the table if it already exists
-> DROP TABLE IF EXISTS employees;
->
-> -- Create the table with the specified constraints
-> SET @create_table_sql = CONCAT(
-> 'CREATE TABLE ', table_name, ' (
'> id INT AUTO_INCREMENT PRIMARY KEY,
'> name VARCHAR(255) NOT NULL,
'> salary INT NOT NULL,
'> CHECK (salary >= ', min_salary, '),
'> CHECK (salary <= ', max_salary, ')
'> )'
-> );
->
-> PREPARE stmt FROM @create_table_sql;
-> EXECUTE stmt;
-> DEALLOCATE PREPARE stmt;
->
-> -- Print success message
-> SELECT 'Constraint created successfully!' AS message;
-> END$$
Query OK, 0 rows affected (0.00 sec)
mysql>
mysql> DELIMITER ;
ROLLNO:211423243113 EDWARD EUGHENE TIMOTHY
23CS1312 DBMS LAB
OUTPUT
mysql> CALL create_table_with_constraintss(30000, 80000);
+ +
| message |
+ +
| Constraint created successfully! |
+ +
1 row in set (0.03 sec)
Query OK, 0 rows affected (0.05 sec)
RESULT
ROLLNO:211423243113 EDWARD EUGHENE TIMOTHY
23CS1312 DBMS LAB
EXCEPTION HANDLING
PROGRAM
mysql> DELIMITER //
mysql>
mysql> CREATE PROCEDURE CalculateDiscount(
-> IN p_product_id INT,
-> IN p_discount_percentage DECIMAL(5,2),
-> OUT p_original_price DECIMAL(10,2),
-> OUT p_discounted_price DECIMAL(10,2)n
-> )
-> BEGIN
-> DECLARE msg VARCHAR(255);
->
-> -- Initialize output variables
-> SET p_original_price = 0;
-> SET p_discounted_price = 0;
->
-> -- Get the original price for the product
-> SELECT ORIGINAL_PRICE INTO p_original_price
-> FROM Products
-> WHERE PRODUCT_ID = p_product_id;
->
-> -- Exception handling for product not found
-> IF p_original_price IS NULL THEN
-> SET msg = CONCAT('Product with ID ', p_product_id, ' does not exist.');
-> SIGNAL SQLSTATE '45000' SET MESSAGE_TEXT = msg;
-> ELSE
-> -- Calculate discounted price
-> SET p_discounted_price = p_original_price - (p_original_price * (p_discount_percentage /
100));
-> END IF;
-> END //
Query OK, 0 rows affected (0.00 sec)
mysql>
mysql> DELIMITER ;
mysql> CREATE TABLE Products (
-> PRODUCT_ID INT PRIMARY KEY,
-> ORIGINAL_PRICE DECIMAL(10,2)
-> );
Query OK, 0 rows affected (0.03 sec)
mysql> INSERT INTO Products (PRODUCT_ID, ORIGINAL_PRICE) VALUES
-> (1, 100),
ROLLNO:211423243113 EDWARD EUGHENE TIMOTHY
23CS1312 DBMS LAB
-> (2, 200),
-> (3, 150);
Query OK, 3 rows affected (0.00 sec)
Records: 3 Duplicates: 0 Warnings: 0
mysql> -- Declare variables to hold the results
mysql> SET @original_price = 0;
Query OK, 0 rows affected (0.00 sec)
mysql> SET @discounted_price = 0;
Query OK, 0 rows affected (0.00 sec)
mysql>
mysql> -- Call the procedure for Product ID 1
mysql> CALL CalculateDiscount(1, 10, @original_price, @discounted_price);
Query OK, 1 row affected (0.00 sec)
mysql> SELECT
-> 'Product ID: 1' AS Product_ID,
-> CONCAT('Original Price: ', @original_price) AS Original_Price,
-> CONCAT('Discount Percentage: 10') AS Discount_Percentage,
-> CONCAT('Discounted Price: ', @discounted_price) AS Discounted_Price;
+ + + + +
| Product_ID | Original_Price | Discount_Percentage | Discounted_Price |
+ + + + +
| Product ID: 1 | Original Price: 100.00 | Discount Percentage: 10 | Discounted Price: 90.00 |
+ + + + +
1 row in set (0.00 sec)
mysql> -- Call the procedure for Product ID 2
mysql> CALL CalculateDiscount(2, 20, @original_price, @discounted_price);
Query OK, 1 row affected (0.00 sec)
mysql> SELECT
-> 'Product ID: 2' AS Product_ID,
-> CONCAT('Original Price: ', @original_price) AS Original_Price,
-> CONCAT('Discount Percentage: 20') AS Discount_Percentage,
-> CONCAT('Discounted Price: ', @discounted_price) AS Discounted_Price;
+ + + + +
| Product_ID | Original_Price | Discount_Percentage | Discounted_Price |
+ + + + +
| Product ID: 2 | Original Price: 200.00 | Discount Percentage: 20 | Discounted Price: 160.00 |
+ + + + +
1 row in set (0.00 sec)
mysql>
mysql> -- Call the procedure for Product ID 3
mysql> CALL CalculateDiscount(3, 15, @original_price, @discounted_price);
ROLLNO:211423243113 EDWARD EUGHENE TIMOTHY
23CS1312 DBMS LAB
Query OK, 1 row affected (0.00 sec)
mysql> SELECT
-> 'Product ID: 3' AS Product_ID,
-> CONCAT('Original Price: ', @original_price) AS Original_Price,
-> CONCAT('Discount Percentage: 15') AS Discount_Percentage,
-> CONCAT('Discounted Price: ', @discounted_price) AS Discounted_Price;
+ + + + +
| Product_ID | Original_Price | Discount_Percentage | Discounted_Price |
+ + + + +
| Product ID: 3 | Original Price: 150.00 | Discount Percentage: 15 | Discounted Price: 127.50 |
+ + + + +
1 row in set (0.00 sec)
RESULT
ROLLNO:211423243113 EDWARD EUGHENE TIMOTHY
23CS1312 DBMS LAB
FUNCTIONS
PROGRAM
mysql> DELIMITER //
mysql> CREATE FUNCTION CalculateCircleArea6(radius DECIMAL(10,2))
-> RETURNS DECIMAL(10,2)
-> NO SQL
-> BEGIN
-> DECLARE area DECIMAL(10,2);
-> -- Calculate the area
-> SET area = PI() * POWER(radius, 2);
-> RETURN area;
-> END //
Query OK, 0 rows affected (0.00 sec)
mysql>
mysql> DELIMITER ;
ROLLNO:211423243113 EDWARD EUGHENE TIMOTHY
23CS1312 DBMS LAB
OUTPUT
mysql> SELECT CONCAT('Area of the circle with radius 6 is ', FORMAT(CalculateCircleArea6(6), 6))
AS Output;
+ +
| Output |
+ +
| Area of the circle with radius 6 is 113.100000 |
+ +
1 row in set, 1 warning (0.00 sec)
RESULT
ROLLNO:211423243113 EDWARD EUGHENE TIMOTHY
23CS1312 DBMS LAB
IMPLICT CURSOR
PROGRAM
mysql> CREATE TABLE customers (
-> ID INT PRIMARY KEY,
-> NAME VARCHAR(50),
-> AGE INT,
-> ADDRESS VARCHAR(100),
-> SALARY DECIMAL(10,2)
-> );
Query OK, 0 rows affected (0.05 sec)
mysql> INSERT INTO customers (ID, NAME, AGE, ADDRESS, SALARY) VALUES
-> (1, 'Ramesh', 23, 'Allahabad', 20000),
-> (2, 'Suresh', 22, 'Kanpur', 22000),
-> (3, 'Mahesh', 24, 'Ghaziabad', 24000),
-> (4, 'Chandan', 25, 'Noida', 26000),
-> (5, 'Alex', 21, 'Paris', 28000),
-> (6, 'Sunita', 20, 'Delhi', 30000);
Query OK, 6 rows affected (0.02 sec)
Records: 6 Duplicates: 0 Warnings: 0
mysql> DELIMITER //
mysql> CREATE PROCEDURE UpdateCustomerSalariess()
-> BEGIN
-> -- Update the salary of all customers by adding 10000
-> UPDATE customers SET SALARY = SALARY + 10000;
-> -- Output the number of rows updated
-> SELECT CONCAT(ROW_COUNT(), ' customers updated') AS Message;
-> END //
Query OK, 0 rows affected (0.00 sec)
mysql> DELIMITER ;
mysql> CALL UpdateCustomerSalariess();
+ +
| Message |
+ +
| 6 customers updated |
+ +
1 row in set (0.00 sec)
Query OK, 0 rows affected (0.02 sec)
ROLLNO:211423243113 EDWARD EUGHENE TIMOTHY
23CS1312 DBMS LAB
OUTPUT
mysql> SELECT * FROM customers;
+ + + + + +
| ID | NAME | AGE | ADDRESS | SALARY |
+ + + + + +
| 1 | Ramesh | 23 | Allahabad | 30000.00 |
| 2 | Suresh | 22 | Kanpur | 32000.00 |
| 3 | Mahesh | 24 | Ghaziabad | 34000.00 |
| 4 | Chandan | 25 | Noida | 36000.00 |
| 5 | Alex | 21 | Paris | 38000.00 |
| 6 | Sunita | 20 | Delhi | 40000.00 |
+ + + + + +
6 rows in set (0.00 sec)
RESULT
ROLLNO:211423243113 EDWARD EUGHENE TIMOTHY
23CS1312 DBMS LAB
EXPLICT CURSOR
PROGRAM
mysql> CREATE TABLE customers (
-> ID INT PRIMARY KEY,
-> NAME VARCHAR(50),
-> AGE INT,
-> ADDRESS VARCHAR(100),
-> SALARY DECIMAL(10,2)
-> );
Query OK, 0 rows affected (0.00 sec)
mysql> INSERT INTO customers (ID, NAME, AGE, ADDRESS, SALARY) VALUES
-> (1, 'Ramesh', 23, 'Allahabad', 20000),
-> (2, 'Suresh', 22, 'Kanpur', 22000),
-> (3, 'Mahesh', 24, 'Ghaziabad', 24000),
-> (4, 'Chandan', 25, 'Noida', 26000),
-> (5, 'Alex', 21, 'Paris', 28000),
-> (6, 'Sunita', 20, 'Delhi', 30000);
Query OK, 6 rows affected (0.02 sec)
Records: 6 Duplicates: 0 Warnings: 0
mysql> DELIMITER //
mysql> CREATE PROCEDURE ListCustomerDetailss()
-> BEGIN
-> DECLARE done INT DEFAULT FALSE;
-> DECLARE v_id INT;
-> DECLARE v_name VARCHAR(50);
-> DECLARE v_address VARCHAR(100);
-> -- Declare the cursor
-> DECLARE cur CURSOR FOR
-> SELECT ID, NAME, ADDRESS
-> FROM customers;
-> -- Declare the handler for when no more rows are found
-> DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;
-> -- Open the cursor
-> OPEN cur;
-> -- Loop through all rows
-> read_loop: LOOP
-> FETCH cur INTO v_id, v_name, v_address;
-> IF done THEN
-> LEAVE read_loop;
-> END IF;
-> -- Output the row details
-> SELECT v_id AS 'ID', v_name AS 'Name', v_address AS 'Address';
-> END LOOP;
-> -- Close the cursor
ROLLNO:211423243113 EDWARD EUGHENE TIMOTHY
23CS1312 DBMS LAB
-> CLOSE cur;
-> END //
Query OK, 0 rows affected (0.01 sec)
mysql> DELIMITER ;
OUTPUT
mysql> CALL ListCustomerDetailss();
+ + + +
| ID | Name | Address |
+ + + +
| 1 | Ramesh | Allahabad |
+ + + +
1 row in set (0.03 sec)
+ + + +
| ID | Name | Address |
+ + + +
| 2 | Suresh | Kanpur |
+ + + +
1 row in set (0.03 sec)
+ + + +
| ID | Name | Address |
+ + + +
| 3 | Mahesh | Ghaziabad |
+ + + +
1 row in set (0.05 sec)
+ + + +
| ID | Name | Address |
+ + + +
| 4 | Chandan | Noida |
+ + + +
1 row in set (0.06 sec)
+ + + +
| ID | Name | Address |
+ + + +
| 5 | Alex | Paris |
+ + + +
1 row in set (0.06 sec)
+ + + +
ROLLNO:211423243113 EDWARD EUGHENE TIMOTHY
23CS1312 DBMS LAB
| ID | Name | Address |
+ + + +
| 6 | Sunita | Delhi |
+ + + +
1 row in set (0.07 sec)
Query OK, 0 rows affected, 1 warning (0.08 sec)
RESULT
ROLLNO:211423243113 EDWARD EUGHENE TIMOTHY
23CS1312 DBMS LAB
VIEWS, INDEXING, SYNONYMNS, SEQUENCES AND SAVEPOINT
PROGRAM
mysql> create table employees (id int primary key,name varchar(20),department varchar(10),salary
decimal(10,2));
Query OK, 0 rows affected (0.03 sec)
mysql> desc employees;
+ + + + + + +
| Field | Type | Null | Key | Default | Extra |
+ + + + + + +
| id | int(11) | NO | PRI | NULL | |
| name | varchar(20) | YES | | NULL | |
| department | varchar(10) | YES | | NULL | |
| salary | decimal(10,2) | YES | | NULL | |
+ + + + + + +
4 rows in set (0.02 sec)
mysql> INSERT INTO employees (id, name, department, salary) VALUES (1,'john doe','sales',50000);
Query OK, 1 row affected (0.03 sec)
mysql> INSERT INTO employees (id, name, department, salary) VALUES (2,'tamil','engine',60000);
Query OK, 1 row affected (0.02 sec)
mysql> INSERT INTO employees (id, name, department, salary) VALUES (3,'selvi','market',70000);
Query OK, 1 row affected (0.03 sec)
mysql> INSERT INTO employees (id, name, department, salary) VALUES (4,'naveen','dancer',80000);
Query OK, 1 row affected (0.03 sec)
mysql> select * from employees;
+ + + + +
| id | name | department | salary |
+ + + + +
| 1 | john doe | sales | 50000.00 |
| 2 | tamil | engine | 60000.00 |
| 3 | selvi | market | 70000.00 |
| 4 | naveen | dancer | 80000.00 |
+ + + + +
4 rows in set (0.00 sec)
ROLLNO:211423243113 EDWARD EUGHENE TIMOTHY
23CS1312 DBMS LAB
mysql> CREATE VIEW sales_employees AS
-> SELECT id, name, salary
-> FROM employees
-> WHERE department ='sales';
Query OK, 0 rows affected (0.03 sec)
mysql> SELECT * FROM sales_employees;
+ + + +
| id | name | salary |
+ + + +
| 1 | john doe | 50000.00 |
+ + + +
1 row in set (0.02 sec)
SEQUENCES
mysql> CREATE TABLE example1 (id INT AUTO_INCREMENT, name VARCHAR(100),PRIMARY
KEY (id));
Query OK, 0 rows affected (0.03 sec)
mysql> INSERT INTO example1 (name) VALUES ('banu'),('parveen');
Query OK, 2 rows affected (0.03 sec)
Records: 2 Duplicates: 0 Warnings: 0
mysql> select * from example1;
+ + +
| id | name |
+ + +
| 1 | banu |
| 2 | parveen |
+ + +
2 rows in set (0.00 sec)
mysql> CREATE TABLE sequence ( seq_name VARCHAR(50) PRIMARY KEY, seq_value INT NOT
-> NULL );
Query OK, 0 rows affected (0.00 sec)
mysql> INSERT INTO sequence (seq_name, seq_value) VALUES ('my_sequence', 0);
Query OK, 1 row affected (0.02 sec)
mysql> select * from sequence;
+ + +
| seq_name | seq_value |
+ + +
| my_sequence | 0|
+ + +
1 row in set (0.01 sec)
ROLLNO:211423243113 EDWARD EUGHENE TIMOTHY
23CS1312 DBMS LAB
SYNONYMS
mysql> CREATE VIEW staff AS SELECT*FROM employees;
Query OK, 0 rows affected (0.02 sec)
mysql> SELECT * FROM staff;
+ + + + +
| id | name | department | salary |
+ + + + +
| 1 | john doe | sales | 50000.00 |
| 2 | tamil | engine | 60000.00 |
| 3 | selvi | market | 70000.00 |
| 4 | naveen | dancer | 80000.00 |
+ + + + +
4 rows in set (0.00 sec)
INDEXING
mysql> create unique index emp_index on employees(id);
Query OK, 0 rows affected (0.04 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> show index from employees;
+ + + + + + + + + +
+ + + +
| Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part |
Packed | Null | Index_type | Comment | Index_comment |
+ + + + + + + + + +
+ + + +
| employees | 0 | PRIMARY | 1 | id |A | 2 | NULL | NULL | |
BTREE | | |
| employees | 0 | emp_index | 1 | id |A | 4 | NULL | NULL | | BTREE
| | |
+ + + + + + + + + +
+ + + +
2 rows in set (0.03 sec)
mysql> drop index emp_index on employees;
Query OK, 0 rows affected (0.03 sec)
Records: 0 Duplicates: 0 Warnings: 0
ROLLNO:211423243113 EDWARD EUGHENE TIMOTHY
23CS1312 DBMS LAB
mysql> show index from employees;
+ + + + + + + + + +
+ + + +
| Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part |
Packed | Null | Index_type | Comment | Index_comment |
+ + + + + + + + + +
+ + + +
| employees | 0 | PRIMARY | 1 | id |A | 4 | NULL | NULL | | BTREE
| | |
+ + + + + + + + + +
+ + + +
1 row in set (0.00 sec)
RESULT
ROLLNO:211423243113 EDWARD EUGHENE TIMOTHY
23CS1312 DBMS LAB
JOINS
PROGRAM
create table salesman(salesman_id int primary key,name varchar(50),city varchar(50),commission
decimal(5,2));
Query OK, 0 rows affected (0.03 sec)
mysql> desc salesman;
+ + + + + + +
| Field | Type | Null | Key | Default | Extra |
+ + + + + + +
| salesman_id | int(11) | NO | PRI | NULL | |
| name | varchar(50) | YES | | NULL | |
| city | varchar(50) | YES | | NULL | |
| commission | decimal(5,2) | YES | | NULL | |
+ + + + + + +
4 rows in set (0.00 sec)
mysql> create table customer(customer_id int primary key,cust_name varchar(50),city varchar(50),grade
int,salesman_id int,foreign key (salesman_id) references salesman(salesman_id));
Query OK, 0 rows affected (0.03 sec)
mysql> desc customer;
+ + + + + + +
| Field | Type | Null | Key | Default | Extra |
+ + + + + + +
| customer_id | int(11) | NO | PRI | NULL | |
| cust_name | varchar(50) | YES | | NULL | |
| city | varchar(50) | YES | | NULL | |
| grade | int(11) | YES | | NULL | |
| salesman_id | int(11) | YES | MUL | NULL | |
+ + + + + + +
5 rows in set (0.03 sec)
mysql> insert into salesman(salesman_id,name,city,commission) values (5001,'james','new york',0.15),
-> (5002,'nail','paris',0.13),
-> (5005,'alex','london',0.11),
-> (5006,'tamil','paris',0.14),
-> (5007,'selvi','chennai',0.13),
-> (5003,'naveen','kanchi',0.12);
Query OK, 6 rows affected (0.00 sec)
Records: 6 Duplicates: 0 Warnings: 0
mysql> select * from salesman;
+ + + + +
| salesman_id | name | city | commission |
+ + + + +
ROLLNO:211423243113 EDWARD EUGHENE TIMOTHY
23CS1312 DBMS LAB
| 5001 | james | new york | 0.15 |
| 5002 | nail | paris | 0.13 |
| 5003 | naveen | kanchi | 0.12 |
| 5005 | alex | london | 0.11 |
| 5006 | tamil | paris | 0.14 |
| 5007 | selvi | chennai | 0.13 |
+ + + + +
6 rows in set (0.00 sec)
mysql> insert into customer(customer_id,cust_name,city,grade,salesman_id) values (3002,'kumar','new
york',100,5001),
-> (3007,'brad','new york',200,5001),
-> (3005,'ajith','london',300,5002),
-> (3004,'vijay','paris',300,5006),
-> (3009,'ajay','kanchi',100,5007),
-> (3003,'ammoi','chennai',300,5003);
Query OK, 6 rows affected (0.02 sec)
Records: 6 Duplicates: 0 Warnings: 0
mysql> select * from customer;
+ + + + + +
| customer_id | cust_name | city | grade | salesman_id |
+ + + + + +
| 3002 | kumar | new york | 100 | 5001 |
| 3003 | ammoi | chennai | 300 | 5003 |
| 3004 | vijay | paris | 300 | 5006 |
| 3005 | ajith | london | 300 | 5002 |
| 3007 | brad | new york | 200 | 5001 |
| 3009 | ajay | kanchi | 100 | 5007 |
+ + + + + +
6 rows in set (0.01 sec)
INNER JOIN
mysql> select salesman.salesman_id,salesman.name,customer.cust_name,customer.city,customer.grade
-> from salesman
-> inner join customer on salesman.salesman_id=customer.salesman_id;
+ + + + + +
| salesman_id | name | cust_name | city | grade |
+ + + + + +
| 5001 | james | kumar | new york | 100 |
| 5001 | james | brad | new york | 200 |
| 5002 | nail | ajith | london | 300 |
| 5003 | naveen | ammoi | chennai | 300 |
| 5006 | tamil | vijay | paris | 300 |
| 5007 | selvi | ajay | kanchi | 100 |
+ + + + + +
6 rows in set (0.03 sec)
ROLLNO:211423243113 EDWARD EUGHENE TIMOTHY
23CS1312 DBMS LAB
LEFT JOIN
mysql> select salesman.salesman_id,salesman.name,customer.cust_name,customer.city,customer.grade
-> from salesman
-> left join customer on salesman.salesman_id=customer.salesman_id;
+ + + + + +
| salesman_id | name | cust_name | city | grade |
+ + + + + +
| 5001 | james | kumar | new york | 100 |
| 5001 | james | brad | new york | 200 |
| 5002 | nail | ajith | london | 300 |
| 5003 | naveen | ammoi | chennai | 300 |
| 5005 | alex | NULL | NULL | NULL |
| 5006 | tamil | vijay | paris | 300 |
| 5007 | selvi | ajay | kanchi | 100 |
+ + + + + +
7 rows in set (0.02 sec)
RIGHT JOIN
mysql> select salesman.salesman_id,salesman.name,customer.cust_name,customer.city,customer.grade
-> from salesman
-> right join customer on salesman.salesman_id=customer.salesman_id;
+ + + + + +
| salesman_id | name | cust_name | city | grade |
+ + + + + +
| 5001 | james | kumar | new york | 100 |
| 5003 | naveen | ammoi | chennai | 300 |
| 5006 | tamil | vijay | paris | 300 |
| 5002 | nail | ajith | london | 300 |
| 5001 | james | brad | new york | 200 |
| 5007 | selvi | ajay | kanchi | 100 |
+ + + + + +
6 rows in set (0.00 sec)
FULL OUTER JOIN
mysql> select salesman.salesman_id,salesman.name,customer.cust_name,customer.city,customer.grade
-> from salesman
-> left join customer on salesman.salesman_id=customer.salesman_id
-> union
-> select salesman.salesman_id,salesman.name,customer.cust_name,customer.city,customer.grade
-> from salesman
-> right join customer on salesman.salesman_id=customer.salesman_id;
+ + + + + +
| salesman_id | name | cust_name | city | grade |
+ + + + + +
ROLLNO:211423243113 EDWARD EUGHENE TIMOTHY
23CS1312 DBMS LAB
| 5001 | james | kumar | new york | 100 |
| 5001 | james | brad | new york | 200 |
| 5002 | nail | ajith | london | 300 |
| 5003 | naveen | ammoi | chennai | 300 |
| 5005 | alex | NULL | NULL | NULL |
| 5006 | tamil | vijay | paris | 300 |
| 5007 | selvi | ajay | kanchi | 100 |
+ + + + + +
7 rows in set (0.02 sec)
CROSS JOIN
mysql> select salesman.salesman_id,salesman.name,customer.cust_name,customer.city,customer.grade
-> from salesman
-> cross join customer;
+ + + + + +
| salesman_id | name | cust_name | city | grade |
+ + + + + +
| 5001 | james | kumar | new york | 100 |
| 5002 | nail | kumar | new york | 100 |
| 5003 | naveen | kumar | new york | 100 |
| 5005 | alex | kumar | new york | 100 |
| 5006 | tamil | kumar | new york | 100 |
| 5007 | selvi | kumar | new york | 100 |
| 5001 | james | ammoi | chennai | 300 |
| 5002 | nail | ammoi | chennai | 300 |
| 5003 | naveen | ammoi | chennai | 300 |
| 5005 | alex | ammoi | chennai | 300 |
| 5006 | tamil | ammoi | chennai | 300 |
| 5007 | selvi | ammoi | chennai | 300 |
| 5001 | james | vijay | paris | 300 |
| 5002 | nail | vijay | paris | 300 |
| 5003 | naveen | vijay | paris | 300 |
| 5005 | alex | vijay | paris | 300 |
| 5006 | tamil | vijay | paris | 300 |
| 5007 | selvi | vijay | paris | 300 |
| 5001 | james | ajith | london | 300 |
| 5002 | nail | ajith | london | 300 |
| 5003 | naveen | ajith | london | 300 |
| 5005 | alex | ajith | london | 300 |
| 5006 | tamil | ajith | london | 300 |
| 5007 | selvi | ajith | london | 300 |
| 5001 | james | brad | new york | 200 |
| 5002 | nail | brad | new york | 200 |
| 5003 | naveen | brad | new york | 200 |
| 5005 | alex | brad | new york | 200 |
| 5006 | tamil | brad | new york | 200 |
| 5007 | selvi | brad | new york | 200 |
| 5001 | james | ajay | kanchi | 100 |
| 5002 | nail | ajay | kanchi | 100 |
ROLLNO:211423243113 EDWARD EUGHENE TIMOTHY
23CS1312 DBMS LAB
| 5003 | naveen | ajay | kanchi | 100 |
| 5005 | alex | ajay | kanchi | 100 |
| 5006 | tamil | ajay | kanchi | 100 |
| 5007 | selvi | ajay | kanchi | 100 |
+ + + + + +
36 rows in set (0.00 sec)
RESULT
ROLLNO:211423243113 EDWARD EUGHENE TIMOTHY
23CS1312 DBMS LAB
TRIGGER
PROGRAM
mysql> CREATE TABLE customers (
ID INT PRIMARY KEY,
NAME VARCHAR(50),
AGE INT,
ADDRESS VARCHAR(100),
-> );
SALARY DECIMAL(10,2)
Query OK, 0 rows affected (0.03 sec)
mysql> INSERT INTO customers (ID, NAME, AGE, ADDRESS, SALARY) VALUES
-> (1, 'Ramesh', 23, 'Allahabad', 20000),
-> (2, 'Suresh', 22, 'Kanpur', 22000),
-> (3, 'Mahesh', 24, Ghaziabad', 24000),
-> (4, Chandan', 25, 'Noida', 26000),
-> (5, 'Alex', 21, 'Paris', 28000),
-> (6, 'Sunita', 20, 'Delhi', 30000);
Query OK, 6 rows affected (0.01 sec)
Records: 6 Duplicates: 0 Warnings: 0
mysql> DELIMITER //
mysql>
mysql
> CREATE TRIGGER before_salary_update
-> BEFORE UPDATE ON customers
> FOR EACH ROW
> BEGIN
DECLARE old_salary DECIMAL(10,2);
DECLARE new_salary DECIMAL(10,2);
DECLARE salary_difference DECIMAL (10,2);
Set old and new salary values
SET old_salary OLD.SALARY;
SET new_salary NEW.SALARY;
SET salary_difference new_salary old_salary;
-- Insert the change into the log table
INSERT INTO salary_changes_log (customer_id, old_salary, new_salary, salary_difference)
VALUES (OLD.ID, old_salary, new_salary, salary_difference);
-> END //
Query OK, 0 rows affected (0.01 sec)
mysql>
mysql > DELIMITER;
mysql> UPDATE customers
-> SET SALARY SALARY + 5000
-> WHERE ID = 1;
Query OK, 1 row affected (0.01 sec)
Rows matched: 1 Changed: 1 Warnings: 0
ROLLNO:211423243113 EDWARD EUGHENE TIMOTHY
23CS1312 DBMS LAB
OUTPUT
mysql> SELECT * FROM salary_changes_log;
| change_id | customer_id | old_salary | new_salary | salary_difference| change_time |
|1 |1 | 20000.00 | 25000.00 |5000.00 |2024-09-21 09:47:04|
1 row in set (0.00 sec)
RESULT
ROLLNO:211423243113 EDWARD EUGHENE TIMOTHY
23CS1312 DBMS LAB
PACKAGES
PROGRAM
mysql> CREATE TABLE customers (
ID INT PRIMARY KEY,
NAME VARCHAR(50),
AGE INT,
ADDRESS VARCHAR(100),
SALARY DECIMAL(10,2));
Query OK, ℗ rows affected (0.03 sec)
mysql> DELIMITER //
mysql>
mysql> CREATE PROCEDURE addCustomer(
-> IN c_id INT,
-> IN c_nameVARCHAR(50),
-> IN c_age INT,
-> IN c_addrVARCHAR(100),
-> IN c_sal DECIMAL(18, 2)
->)
-> BEGIN
INSERT INTO customers (ID, NAME, AGE, ADDRESS, SALARY)
VALUES (c_id, c_name, c_age, c_addr, c_sal);
-> END //
Query OK, ▸ rows affected (0.00 sec)
mysql>
mysql> DELIMITER;
mysql> DELIMITER //
mysql>
mysql> CREATE PROCEDURE delCustomer(IN c_id INT)
-> BEGIN
-> DELETE FROM customers WHERE ID = c_id;
-> END //
Query OK, ▸ rows affected (0.01 sec)
mysql>
mysql> DELIMITER;
mysql> DELIMITER //
mysql> CREATE PROCEDURE listCustomer()
-> BEGIN
-> DECLARE done INT DEFAULT FALSE;
-> DECLARE v_id INT;
-> DECLARE v_nameVARCHAR(50);
-> DECLARE cur CURSOR FOR SELECT ID, NAME FROM customers;
-> DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;
-> OPEN cur;
-> read_loop: LOOP
ROLLNO:211423243113 EDWARD EUGHENE TIMOTHY
23CS1312 DBMS LAB
-> FETCH cur INTO v_id, v_name;
-> IF done THEN
-> LEAVE read_loop;
-> END IF;
-> -- Print the results
-> SELECT CONCAT('Customer(', v_id, '): ', v_name) AS Customer_Details;
-> END LOOP;
-> CLOSE cur;
-> END //
Query OK, rows affected (0.01 sec)
mysql>
mysql>DELIMITER ;
mysql> CALL addCustomer(1, 'Ramesh', 32, 'Ahmedabad', 3000.00);
Query OK, 1 row affected (0.01 sec)
mysql> CALL addCustomer(2, 'Khilan', 25, 'Delhi', 3000.00);
Query OK, 1 row affected (0.00 sec)
mysql> CALL addCustomer(3, 'Kaushik', 23, 'Kota', 3000.00);
Query OK, 1 row affected (0.00 sec)
mysql> CALL addCustomer(4, 'Chaitali', 25, 'Mumbai', 7500.00);
Query OK, 1 row affected (0.00 sec)
mysql> CALL addCustomer (5, 'Hardik', 27, 'Bhopal', 9500.00);
Query OK, 1 row affected (0.00 sec)
mysql> CALL addCustomer(6, 'Komal', 22, 'MP', 5500.00);
Query OK, 1 row affected (0.00 sec)
mysql> CALL listCustomer();
| Customer_Details |
| Customer(1): Ramesh |
1 row in set (0.00 sec)
| Customer_Details |
| Customer(2): Khilan |
1 row in set (0.01 sec)
| Customer_Details |
| Customer(3): Kaushik |
1 row in set (0.02 sec)
| Customer_Details |
| Customer(4): Chaitali |
1 row in set (0.03 sec)
| Customer_Details |
| Customer(5): Hardik |
ROLLNO:211423243113 EDWARD EUGHENE TIMOTHY
23CS1312 DBMS LAB
1 row in set (0.03 sec)
| Customer_Details |
| Customer(6): Komal |
1 row in set (0.03 sec)
Query OK, 0 rows affected (0.04 sec)
mysql> +-
-> | Customer_Details
-> |Customer(1): Ramesh |
-> |Customer(2): Khilan |
-> |Customer(3): Kaushik |
-> |Customer(4): Chaitali |
-> |Customer(5): Hardik |
-> |Customer(6): Komal |
-> ^C
mysql> CALL delCustomer(4); ---Deleting customer with ID 4
Query OK, 1 row affected (0.01 sec)
mysql> CALL listCustomer();
| Customer_Details
| Customer(1): Ramesh
1 row in set (0.00 sec)
| Customer_Details
| Customer(2): Khilan
1 row in set (0.01 sec)
| Customer_Details |
| Customer(3): Kaushik |
1 row in set (0.02 sec)
| Customer_Details |
| Customer(5): Hardik |
1 row in set (0.03 sec)
| Customer_Details |
| Customer(6): Komal|
1 row in set (0.03 sec)
Query OK, rows affected (0.04 sec)
mysql>
->Customer_Details
->|Customer (1): Ramesh |
->|Customer(2): Khilan |
->|Customer(3): Kaushik |
->| Customer(5): Hardik |
->| Customer(6): Komal |
ROLLNO:211423243113 EDWARD EUGHENE TIMOTHY
23CS1312 DBMS LAB
RESULT
ROLLNO:211423243113 EDWARD EUGHENE TIMOTHY
23CS1312 DBMS LAB
BASIC COMMANDS USING CASSANDRA/MONGO DB
PROGRAM
>_MONGOSH
> use admin
< switched to db admin
> show dbs
< admin 40.00 KiB
config 12.00 KiB
local 40.00 KiB
> use myDatabase
< switched to dbmyDatabase
> show collections
>db.createCollection ("myCollection")
<{ ok: 1 }
>db.users.insertOne({ name: "Alice", age: 25, email: "[email protected]" })
<{
acknowledged: true,
insertedId:ObjectId('66ee33afe11096a355c828fe')
}
>db.products.insertMany([
{ name: "Laptop", price: 1200 },
{ name: "Smartphone", price: 800 }
])
<{
acknowledged: true,
insertedIds: {
'0': ObjectId('66ee34bde11096a355c828ff'),
'1': ObjectId('66ee34bde11096a355c82900')
}
>db.users.findOne({ name: "Alice" })
<{
_id: ObjectId('66ee33afe11096a355c828fe'),
name: 'Alice',
age: 25,
email: '[email protected]'
}
>db.products.find({ price: { $gt: 500 } })
<{
_id: ObjectId('66ee34bde11096a355c828ff'),
name: 'Laptop',
price: 1200
}
{
_id: ObjectId('66ee34bde11096a355c82900'),
name: 'Smartphone',
price: 800
ROLLNO:211423243113 EDWARD EUGHENE TIMOTHY
23CS1312 DBMS LAB
}
>db.users.updateOne(
{ name: "Alice" },
{ $set: { age: 26 } }
)
<{
acknowledged: true,
insertedId: null,
matchedCount: 1,
modifiedCount: 1,
upsertedCount: 0
>db.products.updateMany(
{ price: { $lt: 1000} },
{ $set: {onSale: true } }
)
<{
acknowledged: true,
insertedId: null,
matchedCount: 1,
modifiedCount: 1,
upsertedCount: 0
}
>db.users.deleteOne({ name: "Alice" })
<{
acknowledged: true,
deletedCount: 1
}
>db.products.deleteMany ({ price: { $lt: 500 } })
<{
acknowledged: true,
deletedCount: 0
}
>db.orders.aggregate([
{ $match: { status: "shipped" } },
{ $group: { _id: "$customerId", totalAmount: { $sum: "$amount" } } },
{ $sort: { totalAmount: -1} }
])
>db.users.drop()
< true
>db.dropDatabase()
<{ ok: 1, dropped: 'myDatabase' }
myDatabase> Ś
RESULT
ROLLNO:211423243113 EDWARD EUGHENE TIMOTHY
23CS1312 DBMS LAB
DATA MODEL IN NOSQL
PROGRAM
>_MONGOSH
>use admin
<Switched to db admin
> use myDatabase
< switched to dbmyDatabase
>db.users.insertOne({
"_id": ObjectId("609b2e5d6c73724c5c8bf20e"),
"username": "user123",
"email": "[email protected]",
"password": "hashed_password",
"profile": {
"name": "John Doe",
"bio": "Software Engineer | Blogger | MongoDB Enthusiast",
"avatar_url": https://example.com/avatar.jpg
},
"created_at": ISODate("2023-05-01T08:00:00Z")
})
<{
acknowledged: true,
insertedId: ObjectId('609b2e5d6c73724c5c8bf20e')
}
>db.products.insertOne({
"_id": ObjectId("609b2e5d6c73724c5c8bf20f"),
"name": "Laptop",
"description": "A powerful laptop for all your needs",
"price": 1099.99,
"stock_quantity": 100
})
<{
acknowledged: true,
insertedId: ObjectId('609b2e5d6c73724c5c8bf20f')
}
>_MONGOSH
>db.orders.insertOne({
"_id": ObjectId("609b2e5d6c73724c5c8bf210"),
"user_id":
ObjectId("609b2e5d6c73724c5c8bf20e"),
"order_date": ISODate("2023-05-14T10:00:00Z"),
"items": [
{
"product_id": ObjectId("609b2e5d6c73724c5c8bf20f"),
"quantity": 2
}
],
ROLLNO:211423243113 EDWARD EUGHENE TIMOTHY
23CS1312 DBMS LAB
"total_amount": 1999.98
})
<{
acknowledged: true,
insertedId: ObjectId('609b2e5d6c73724c5c8bf210')
}
>db.users.findOne({ username: "user123" })
<{
_ id: ObjectId('609b2e5d6c73724c5c8bf20e'),
username: 'user123',
email: '[email protected]',
password: 'hashed_password',
profile: {
name: 'John Doe',
bio: 'Software Engineer | Blogger | MongoDB Enthusiast',
avatar_url: 'https://example.com/avatar.jpg'
},
created_at: 2023-05-01T08:00:00.000Z
}
>db.products.findOne({ name: "Laptop" })
<{
_ id: ObjectId('609b2e5d6c73724c5c8bf20f'),
name: 'Laptop',
description: 'A powerful laptop for all your needs',
price: 1099.99,
stock_quantity: 100
}
>db.orders.find({ user_id: ObjectId("609b2e5d6c73724c5c8bf20e") })
<{
_ id: ObjectId('609b2e5d6c73724c5c8bf210'),
user_id: ObjectId('609b2e5d6c73724c5c8bf20e'),
order_date: 2023-05-14T10:00:00.000Z,
items: [
{
product_id: ObjectId('609b2e5d6c73724c5c8bf20f'),
quantity: 2
}
],
total_amount: 1999.98
}
>db.orders.aggregate([
{ $unwind: "$items" },
{ $lookup: {
from: "products",
localField: "items.product_id",
foreignField: "_id",
as: "product_details"
}
ROLLNO:211423243113 EDWARD EUGHENE TIMOTHY
23CS1312 DBMS LAB
},
{ $unwind: "$product_details" },
{ $project: {
id: 1,
order_date: 1,
"items.product_id": 1,
"items.quantity": 1,
"product_details.name": 1,
"product_details.price": 1
}
}
])
<{
_ id: ObjectId('609b2e5d6c73724c5c8bf210'),
order_date: 2023-05-14T10:00:00.000Z,
items: {
product_id: ObjectId('609b2e5d6c73724c5c8bf20f'),
quantity: 2
},
product_details: {
name: 'Laptop',
price: 1099.99
}
}
>db.users.updateOne(
{ username: "user123" },
{ $set: { email: "[email protected]" } }
)
<{
acknowledged: true,
insertedId: null,
matchedCount: 1,
modifiedCount: 1,
upsertedCount: 0
}
> a. Delete a User
>db.users.deleteOne({ username: "user123" })
<{
acknowledged: true,
deletedCount: 1
}
>db.orders.deleteOne({ _id: ObjectId("609b2e5d6c73724c5c8bf210") })
<{
acknowledged: true,
deletedCount: 1
ROLLNO:211423243113 EDWARD EUGHENE TIMOTHY
23CS1312 DBMS LAB
RESULT
ROLLNO:211423243113 EDWARD EUGHENE TIMOTHY
23CS1312 DBMS LAB
AGGREGATION IN NOSQL
PROGRAM
>_MONGOSH
> use admin
< switched to db admin
> use yourDatabaseName
< switched to dbyourDatabaseName
>db.movies.aggregate([
// First Stage: Projection
{ $project: { _id: 0, genres: 1, imdb: 1, title: 1 } },
// Second Stage: Unwind genres
{ $unwind: "$genres" },
// Third Stage: Group by genre and calculate average rating
{ $group: {
_id: "$genres",
averageGenreRating: { $avg: "$imdb.rating" }
}
},
// Fourth Stage: Sort by average rating in descending order
{ $sort: { averageGenreRating: -1 } }
])
<
>{
"_id": "Sci-Fi",
"averageGenreRating": 8.2
}
RESULT
ROLLNO:211423243113 EDWARD EUGHENE TIMOTHY
23CS1312 DBMS LAB
INDEXES IN MYSQL
PROGRAM
2024-09-23T08:18:22.515+0530 I CONTROL [initandlisten]
2024-09-23T08:18:22.515+0530 I CONTROL [initandlisten] ** WARNING: Access control
is not enabled for the database.
2024-09-23T08:18:22.516+0530 I CONTROL [initandlisten] ** Read and write access to data and
configuration is unrestricted
2024-09-23T08:18:22.517+0530 I CONTROL [initandlisten]
2024-09-23T08:18:22.517+0530 I CONTROL [initandlisten] ** WARNING: This server
is bound to localhost.
2024-09-23T08:18:22.518+0530 I CONTROL [initandlisten] ** Remote system will be unable to
connect to this server.
2024-09-23T08:18:22.519+0530 I CONTROL [initandlisten] ** Start the server with --
bind_ip<address> to specify which IP
2024-09-23T08:18:22.519+0530 I CONTROL [initandlisten] ** addresses it should serve responses
from, or with --bind_ip_all to
2024-09-23T08:18:22.520+0530 I CONTROL [initandlisten] ** bind to all interfaces. If this behavior is
desired, start the
2024-09-23T08:18:22.521+0530 I CONTROL [initandlisten] ** server with--bind_ip 127.0.0.1 to
disable this warning.
2024-09-23T08:18:22.521+0530 I CONTROL [initandlisten]
2024-09-23T08:18:22.522+0530 I CONTROL [initandlisten] Hotfix KB2731284 or late
rupdate is not installed, will zero-out data files.
2024-09-23T08:18:22.522+0530 I CONTROL [initandlisten]
2024-09-23T08:18:22.523+0530 I CONTROL [initandlisten] ** WARNING: The file system cache of
this machine is configured to be greater than 40% of the total nemory. This can lead to increased
memory pressure and poor performance.
2024-09-23T08:18:22.523+0530 I CONTROL [initandlisten] See http://dochub.mongod
b.org/core/wt-windows-systen-file-cache
2024-09-23T08:18:22.524+0530 I CONTROL [initandlisten]
>db.blog.createIndex<
...{
... content:"text",
... "users.comments": "text",
... "users.profiles": "text"
. . . },
...{
... name: "Interactions Text Index"
. . . })
{
"createdCollectionAutomatically" : true,
"numIndexes Before": 1,
"numIndexesAfter": 2,
"ok": 1
}
ROLLNO:211423243113 EDWARD EUGHENE TIMOTHY
23CS1312 DBMS LAB
RESULT
ROLLNO:211423243113 EDWARD EUGHENE TIMOTHY
23CS1312 DBMS LAB
INTRODUCTION SQL-SQL*PLUS
PROGRAM
mysql> CREATE TABLE employee (
->empno INT PRIMARY KEY,
-> name VARCHAR(50),
-> role VARCHAR(50),
-> commission DECIMAL(10, 2),
-> department VARCHAR(50) -- added department column
->);
Query OK, 0 rows affected (0.07 sec)
mysql>
mysql> -- Step 2: Insert records into the employee table
mysql> INSERT INTO employee (empno, name, role, commission, department) VALUES (
-> 10, 'John', 'manager', 800, 'Sales');
Query OK, 1 row affected (0.03 sec)
mysql> INSERT INTO employee (empno, name, role, commission, department) VALUES (
-> 26, 'Mark', 'lead', 700, 'Development');
Query OK, 1 row affected (0.02 sec)
mysql>INSERT INTO employee (empno, name, role, commission, department) VALUES (
-> 30, 'Lilly', 'manager', 4000, 'Sales');
Query OK, 1 row affected (0.03 sec)
mysql> INSERT INTO employee (empno, name, role, commission, department) VALUES (
-> 40, 'Thomson', 'supervisor', 100, 'HR');
Query OK, 1 row affected (0.02 sec)
mysql> INSERT INTO employee (empno, name, role, commission, department) VALUES (
-> 50, 'Kelly', 'junior lead', 650, 'Development');
Query OK, 1 row affected (0.03 sec)
mysql>
mysql> -- Step 3: Update the commission of all managers to Rs 1000
mysql> UPDATE employee SET commission = 1000 WHERE role = 'manager';
Query OK, 2 rows affected (0.08 sec)
Rows matched: 2 Changed: 2 Warnings: 0
mysql>
mysql> -- Step 4: Delete employees who are supervisors
mysql> DELETE FROM employee WHERE role = 'supervisor';
Query OK, 1 row affected (0.03 sec)
mysql>
ROLLNO:211423243113 EDWARD EUGHENE TIMOTHY
23CS1312 DBMS LAB
mysql> -- Step 5: Alter table to add a new column (e.g., 'location')
mysql> ALTER TABLE employee ADD COLUMN location VARCHAR(50);
Query OK, 4 rows affected (0.22 sec)
Records: 4 Duplicates: 0 Warnings: 0
mysql> -- Step 6: (Optional) Modify the new column if needed
mysql> ALTER TABLE employee MODIFY location VARCHAR(100);
Query OK, 4 rows affected (0.24 sec)
Records: 4 Duplicates: 0 Warnings: 0
mysql> -- Step 7: Query to get total commission by role
mysql> SELECT role, SUM(commission) AS total_commission
-> FROM employee
-> GROUP BY role
-> HAVING SUM(commission) > 10000
-> ORDER BY total_commission DESC;
Empty set (0.00 sec)
mysql> SELECT role, SUM(commission) AS total_commission
-> FROM employee
-> GROUP BY role
-> HAVING SUM(commission) > 10000
-> ORDER BY total_commission DESC;
Empty set (0.00 sec)
mysql> SELECT role, SUM(commission) AS total_commission
-> FROM employee
-> GROUP BY role
-> HAVING SUM(commission) > 1000
-> ORDER BY total_commission DESC;
+ + +
| role | total_commission |
+ + +
| manager | 2000.00 |
+ + +
1 row in set (0.00 sec)
RESULT
ROLLNO:211423243113 EDWARD EUGHENE TIMOTHY