├── dbms_out ├── files ├── trigger3 ├── cursor │ ├── cursor2.sql │ ├── out_table_last.sql │ ├── cursor1.sql │ └── cursor3.sql ├── trigger2 ├── trigger5 ├── trigger4 ├── package.sql ├── expt9out ├── view.sql ├── Expt_6_BuiltIntxt.txt ├── tcl command.sql ├── dcl command.sql ├── expt7 ├── expt5 └── expt8 grpby havin ├── images ├── syllabus ├── WhatsApp Image 2023-01-04 at 20.03.54.jpeg └── WhatsApp Image 2023-01-04 at 20.03.54 (1).jpeg ├── Expt No 1.pdf ├── PLSQL ├── additionplsql.sql ├── functionPl.sql ├── arrayEx.sql ├── caseUse.sql ├── trigger2.sql ├── largestAmongTwo.sql ├── trigger3.sql ├── sortingPl.sql ├── loopSql.sql ├── Expt_10_stud.sql ├── trigger5.sql ├── sqlop.sql └── trigger4.sql ├── factorial.sql ├── lab4.sql ├── Mock_test ├── triggerEmployee.sql └── cursorEmployee.sql ├── binarysearch.sql ├── employeelab3.sql ├── Expt_9_actor.sql ├── Expt_5_student.sql ├── Expt_7_Aggreate.sql ├── Expt_6_BuiltIn.sql ├── README.md ├── Employee.sql ├── student.sql └── Expt_8_grpby_orderby.sql /dbms_out/files: -------------------------------------------------------------------------------- 1 | OThers 2 | -------------------------------------------------------------------------------- /images/syllabus: -------------------------------------------------------------------------------- 1 | This folder has our syllabus 2 | -------------------------------------------------------------------------------- /Expt No 1.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PaulThomas20002/S5-DATABASE-MANAGEMENT-SYSTEMS-LAB/HEAD/Expt No 1.pdf -------------------------------------------------------------------------------- /images/WhatsApp Image 2023-01-04 at 20.03.54.jpeg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PaulThomas20002/S5-DATABASE-MANAGEMENT-SYSTEMS-LAB/HEAD/images/WhatsApp Image 2023-01-04 at 20.03.54.jpeg -------------------------------------------------------------------------------- /images/WhatsApp Image 2023-01-04 at 20.03.54 (1).jpeg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PaulThomas20002/S5-DATABASE-MANAGEMENT-SYSTEMS-LAB/HEAD/images/WhatsApp Image 2023-01-04 at 20.03.54 (1).jpeg -------------------------------------------------------------------------------- /PLSQL/additionplsql.sql: -------------------------------------------------------------------------------- 1 | /* addition of two numbers */ 2 | DECLARE 3 | a integer := &a; 4 | b integer := &b; 5 | res int; 6 | BEGIN 7 | res := a + b; 8 | dbms_output.put_line('result = ' || res); 9 | END; 10 | -------------------------------------------------------------------------------- /factorial.sql: -------------------------------------------------------------------------------- 1 | DECLARE 2 | n number := 5; 3 | f number := 1; 4 | BEGIN 5 | WHILE N > 0 LOOP 6 | f := f * n; 7 | n := n-1; 8 | END LOOP; 9 | dbms_output.put_line('factorial is: '|| f ); 10 | END; 11 | -------------------------------------------------------------------------------- /dbms_out/trigger3: -------------------------------------------------------------------------------- 1 | SQL> @trigger3 2 | Table dropped. 3 | Table created. 4 | 1 row created. 5 | Trigger created. 6 | SQL> insert into Student values(103, 'Alice', 'Address2'); 7 | insert into Student values(103, 'Alice', 'Address2') 8 | * 9 | ERROR at line 1: 10 | ORA-20000: Cannot insert record on weekdays 11 | ORA-06512: at "SYSTEM.TRIGGER3", line 4 12 | ORA-04088: error during execution of trigger 'SYSTEM.TRIGGER3' 13 | SQL> -------------------------------------------------------------------------------- /lab4.sql: -------------------------------------------------------------------------------- 1 | use dbms_exp; 2 | create table Student( 3 | stud_id INT PRIMARY KEY, 4 | stud_fname VARCHAR(20), 5 | stud_lname VARCHAR(20), 6 | stud_email VARCHAR(20), 7 | stud_ph VARCHAR(10)); 8 | 9 | show tables; 10 | 11 | select * from Student; 12 | 13 | LOAD DATA INFILE '/var/lib/mysql-files/student.csv' INTO TABLE Student FIELDS TERMINATED BY ',' ENCLOSED BY '"' LINES TERMINATED BY '\n' IGNORE 1 ROWS; 14 | 15 | select * from Student; 16 | -------------------------------------------------------------------------------- /PLSQL/functionPl.sql: -------------------------------------------------------------------------------- 1 | /* Use of function in pl/sql */ 2 | /* Program - findMax value*/ 3 | DECLARE 4 | a int; 5 | b int; 6 | c number; 7 | FUNCTION findMax(x int, y int) 8 | RETURN int 9 | IS 10 | z int; 11 | BEGIN 12 | IF x > y THEN 13 | z:= x; 14 | ELSE 15 | Z:= y; 16 | END IF; 17 | RETURN z; 18 | END; 19 | 20 | BEGIN 21 | a := &a; 22 | b := &b; 23 | 24 | c := findMax(a, b); 25 | dbms_output.put_line(' Maximum of (' || a ||',' ||b || '): ' || c); 26 | END; 27 | -------------------------------------------------------------------------------- /Mock_test/triggerEmployee.sql: -------------------------------------------------------------------------------- 1 | drop trigger triggerem; 2 | CREATE TRIGGER triggerem 3 | BEFORE INSERT OR UPDATE OF salary ON em 4 | FOR EACH ROW 5 | DECLARE 6 | old_sal integer; 7 | BEGIN 8 | old_sal := :old.salary; 9 | dbms_output.put_line(old_sal); 10 | IF (:new.salary <= old_sal ) 11 | THEN 12 | dbms_output.put_line('Error..! Salary must be greater than old salary'); 13 | rollback; 14 | END IF; 15 | END; 16 | / 17 | 18 | update em set salary = 19 where id = 101; 19 | select * from em; 20 | -------------------------------------------------------------------------------- /PLSQL/arrayEx.sql: -------------------------------------------------------------------------------- 1 | /* use of array*/ 2 | /* index starts with 1 .. not 0 */ 3 | DECLARE 4 | type intArray IS VARRAY(10) OF INTEGER; 5 | type namesArray IS VARRAY(5) OF VARCHAR2(5); 6 | 7 | arr intArray; 8 | names namesArray; 9 | i integer; 10 | 11 | BEGIN 12 | arr := intArray(1,5,2,3,6,7,4,8,9,10); 13 | names := namesArray('Alice', 'Bob', 'Cindy', 'Sam', 'Eric'); 14 | dbms_output.put_line('Integer array'); 15 | --dbms_output.put_line('arr[0]' || arr(0)); 16 | dbms_output.put_line('arr[1]' || arr(1)); 17 | dbms_output.put_line('arr[2]' || arr(2)); 18 | dbms_output.put_line('arr[3]' || arr(3)); 19 | END; 20 | -------------------------------------------------------------------------------- /PLSQL/caseUse.sql: -------------------------------------------------------------------------------- 1 | /* Case statement */ 2 | /* 3 | Grading ..! 4 | A- > print "Excellent" 5 | B- > print "Very good" 6 | C- > print "Well done" 7 | D- > print "You passed" 8 | F- > print "Better try again" 9 | others - "No such grade" 10 | */ 11 | DECLARE 12 | c char(1) := '&c'; 13 | BEGIN 14 | case c 15 | when 'A' then dbms_output.put_line('Excellent'); 16 | when 'B' then dbms_output.put_line('Very good'); 17 | when 'C' then dbms_output.put_line('Well done'); 18 | when 'D' then dbms_output.put_line('You passed'); 19 | when 'F' then dbms_output.put_line('Better try again'); 20 | else dbms_output.put_line('No such grade'); 21 | end case; 22 | END; 23 | -------------------------------------------------------------------------------- /dbms_out/cursor/cursor2.sql: -------------------------------------------------------------------------------- 1 | EMPLOYEE ID:10 2 | EMPLOYEE NAME:Skove 3 | SALARY:100000 4 | ********************* 5 | EMPLOYEE ID:9 6 | EMPLOYEE NAME:Timo 7 | SALARY:90000 8 | ********************* 9 | EMPLOYEE ID:8 10 | EMPLOYEE NAME:Sari 11 | SALARY:80000 12 | ********************* 13 | EMPLOYEE ID:7 14 | EMPLOYEE NAME:John 15 | SALARY:70000 16 | ********************* 17 | EMPLOYEE ID:6 18 | EMPLOYEE NAME:Tom 19 | SALARY:60000 20 | ********************* 21 | TOTAL SALARY:400000 22 | 23 | PL/SQL procedure successfully completed. 24 | 25 | 26 | 27 | /////// 28 | connect 29 | username : system 30 | password : password 31 | //eof / 32 | 33 | @cursor1 34 | -------------------------------------------------------------------------------- /PLSQL/trigger2.sql: -------------------------------------------------------------------------------- 1 | drop table Account; 2 | create table Account(id int primary key, name varchar(20), bal int); 3 | drop trigger trigger2; 4 | 5 | CREATE TRIGGER trigger2 6 | BEFORE INSERT ON Account 7 | FOR EACH ROW 8 | DECLARE 9 | min_bal integer := 0; 10 | BEGIN 11 | IF (:new.bal < min_bal ) THEN 12 | dbms_output.put_line('Error..! Balance cannot be less than zero'); 13 | raise_application_error(-20000, 'Balance cannot be less than zero'); 14 | END IF; 15 | END; 16 | 17 | insert into Account values(100, 'Alice', 200); 18 | insert into Account values(104, 'Bob', 500); 19 | 20 | select * from Account; 21 | insert into Account values(102, 'Cindy', -500); 22 | -------------------------------------------------------------------------------- /Mock_test/cursorEmployee.sql: -------------------------------------------------------------------------------- 1 | declare cursor cursor0 is 2 | select * from employee order by salary desc; 3 | e_id employee.id%type; 4 | e_name employee.name%type; 5 | e_age employee.age%type; 6 | e_salary employee.salary%type; 7 | 8 | begin 9 | 10 | open cursor0; 11 | for i in 1 .. 3 12 | loop 13 | fetch cursor0 into e_id,e_name, e_age, e_salary; 14 | 15 | exit when cursor0%notfound; 16 | dbms_output.put_line('Emp id : '|| e_id); 17 | dbms_output.put_line('Emp name : '|| e_name); 18 | dbms_output.put_line('Emp age : '|| e_age); 19 | dbms_output.put_line('Emp salary : '|| e_salary); 20 | end loop; 21 | close cursor0; 22 | end; 23 | / 24 | select * from employee; 25 | -------------------------------------------------------------------------------- /dbms_out/trigger2: -------------------------------------------------------------------------------- 1 | SQL> @trigger2 2 | Table dropped. 3 | Table created. 4 | Trigger created. 5 | SQL> insert into Account values(100, 'Alice', 200); 6 | 1 row created. 7 | SQL> insert into Account values(104, 'Bob', 500); 8 | 1 row created. 9 | SQL> select * from Account; 10 | 11 | ID NAME BAL 12 | ---------- -------------------- ---------- 13 | 100 Alice 200 14 | 104 Bob 500 15 | SQL> insert into Account values(102, 'Cindy', -500); 16 | insert into Account values(102, 'Cindy', -500) 17 | * 18 | ERROR at line 1: 19 | ORA-20000: Balance cannot be less than zero 20 | ORA-06512: at "SYSTEM.TRIGGER2", line 6 21 | ORA-04088: error during execution of trigger 'SYSTEM.TRIGGER2' 22 | SQL> -------------------------------------------------------------------------------- /PLSQL/largestAmongTwo.sql: -------------------------------------------------------------------------------- 1 | -- find the largest of two numbers 2 | DECLARE 3 | a integer := &a; 4 | b integer := &b; 5 | BEGIN 6 | 7 | if (a > b) then 8 | dbms_output.put_line(a || ' is the largest number'); 9 | else 10 | dbms_output.put_line(b || ' is the largest number'); 11 | end if; 12 | END; 13 | 14 | 15 | /* Use of if elsif ladder */ 16 | /* calculate the range of input number */ 17 | DECLARE 18 | c integer := &c; 19 | BEGIN 20 | if (c >= 0 and c < 10) then 21 | dbms_output.put_line(' is less than 10'); 22 | elsif (c >= 10 and c < 20) then 23 | dbms_output.put_line(' is less than 20'); 24 | elsif (c >= 20 and c < 30) then 25 | dbms_output.put_line(' is less than 30'); 26 | else 27 | dbms_output.put_line(' is grater than or equal 30'); 28 | end if; 29 | END; -------------------------------------------------------------------------------- /PLSQL/trigger3.sql: -------------------------------------------------------------------------------- 1 | /* 2 | Test 2: 3 | Write a PL/SQL program to create a trigger before insert 4 | for each row and not allowing transaction on weekends. 5 | */ 6 | 7 | drop table Student; 8 | create table Student(Rollno int primary key, name varchar(20), address varchar(50)); 9 | insert into Student values(104, 'Bob', 'Address1'); 10 | 11 | drop trigger trigger3; 12 | CREATE OR REPLACE TRIGGER trigger3 13 | BEFORE INSERT ON Student 14 | REFERENCING NEW AS NEW OLD AS OLD 15 | FOR EACH ROW 16 | BEGIN 17 | IF TO_CHAR(SYSDATE, 'D') <> '7' THEN 18 | dbms_output.put_line('Error..! Cannot insert record on weekdays'); 19 | RAISE_APPLICATION_ERROR(-20000, 'Cannot insert record on weekdays'); 20 | END IF; 21 | END; 22 | 23 | insert into Student values(103, 'Alice', 'Address2'); 24 | -------------------------------------------------------------------------------- /dbms_out/trigger5: -------------------------------------------------------------------------------- 1 | SQL> @trigger5 2 | Table dropped. 3 | Table created. 4 | 1 row created. 5 | 1 row created. 6 | 1 row created. 7 | 1 row created. 8 | 1 row created. 9 | 10 | ID NAME AGE ADDRESS SAL 11 | ---------- ---------------- ---------- ---------------- ---------- 12 | 1 Alice 20 Address1 10000 13 | 2 Bob 30 Address2 20000 14 | 3 Cindy 40 Address3 30000 15 | 16 | ID NAME AGE ADDRESS SAL 17 | ---------- ---------------- ---------- ---------------- ---------- 18 | 4 Sam 50 Address4 40000 19 | 5 Eric 60 Address5 50000 20 | 21 | Warning: Trigger created with compilation errors. 22 | SQL> -------------------------------------------------------------------------------- /dbms_out/trigger4: -------------------------------------------------------------------------------- 1 | SQL> @trigger4 2 | Table dropped. 3 | Table dropped. 4 | Table dropped. 5 | Table created. 6 | Table created. 7 | Table created. 8 | 1 row created. 9 | 1 row created. 10 | 1 row created. 11 | 1 row created. 12 | 1 row created. 13 | 1 row created. 14 | ST_ID BOOK_ID ISSUE_DA RETURN_D 15 | ---------- ---------- -------- -------- 16 | 100 1 01-01-22 01-02-22 17 | 101 2 01-01-22 01-03-22 18 | 19 | 20 | ST_ID NAME CLASS FINE 21 | ---------- -------------------- ---------- ---------- 22 | 100 Alice CSE 0 23 | 101 Bob CSE 0 24 | 25 | 26 | BOOKID TITLE NO_OF_COPIES PRICE 27 | ---------- -------------------- ------------ ---------- 28 | 1 Data Structure 10 1000 29 | 2 Java - Complete ref 10 1000 30 | 31 | Trigger created. 32 | SQL> -------------------------------------------------------------------------------- /PLSQL/sortingPl.sql: -------------------------------------------------------------------------------- 1 | /* Sorting algorithm...! */ 2 | DECLARE 3 | type intArray IS VARRAY(10) OF INTEGER; 4 | arr1 intArray; 5 | i int; 6 | j int; 7 | c int; 8 | temp INTEGER := 0; 9 | FUNCTION sortArray(arr IN OUT intArray, len integer) 10 | RETURN int 11 | IS 12 | ret int; 13 | BEGIN 14 | ret := 1; 15 | dbms_output.new_line; 16 | for i in 1 .. len loop 17 | dbms_output.put_line('arr[' || i ||'] =' ||arr(i)); 18 | end loop; 19 | 20 | for i in 1 .. len loop 21 | for j in 1+i .. len loop 22 | if (arr(i) > arr(j)) then 23 | temp := arr(i); 24 | arr(i) := arr(j); 25 | arr(j) := temp; 26 | end if; 27 | end loop; 28 | end loop; 29 | 30 | for i in 1 .. len loop 31 | dbms_output.put_line('arr[' || i ||'] =' ||arr(i)); 32 | end loop; 33 | 34 | return ret; 35 | END; 36 | 37 | BEGIN 38 | arr1 := intArray(1,5,2,3,6,7,4,8,9,10); 39 | c := sortArray(arr1, 10); 40 | END; -------------------------------------------------------------------------------- /PLSQL/loopSql.sql: -------------------------------------------------------------------------------- 1 | /* Use of loops */ 2 | DECLARE 3 | type intArray IS VARRAY(10) OF INTEGER; 4 | type namesArray IS VARRAY(5) OF VARCHAR2(5); 5 | 6 | arr intArray; 7 | names namesArray; 8 | i integer; 9 | 10 | BEGIN 11 | arr := intArray(1,5,2,3,6,7,4,8,9,10); 12 | names := namesArray('Alice', 'Bob', 'Cindy', 'Sam', 'Eric'); 13 | 14 | i := 1; 15 | /*While loop...! */ 16 | while( i <= 10) loop 17 | dbms_output.put_line('arr[' || i ||'] =' ||arr(i)); 18 | i := i+1; 19 | end loop; 20 | 21 | /*For loop...! */ 22 | for i in 1 .. 10 loop 23 | dbms_output.put_line('arr[' || i ||'] =' ||arr(i)); 24 | end loop; 25 | 26 | i := 1; 27 | while( i <= 5) loop 28 | dbms_output.put_line('names[' || i ||'] =' ||names(i)); 29 | i := i+1; 30 | end loop; 31 | 32 | /*For loop...! */ 33 | for i in 1 .. 5 loop 34 | dbms_output.put_line('names[' || i ||'] =' ||names(i)); 35 | end loop; 36 | END; -------------------------------------------------------------------------------- /binarysearch.sql: -------------------------------------------------------------------------------- 1 | DECLARE 2 | TYPE intarray IS VARRAY(10) OF INTEGER; 3 | a intarray; 4 | first INTEGER; 5 | mid INTEGER; 6 | last INTEGER; 7 | flag BOOLEAN; 8 | key INTEGER; 9 | BEGIN 10 | key := 10; 11 | first :=0; 12 | last :=10; 13 | flag := FALSE; 14 | a := intarray(1,2,3,4,5,6,7,8,9,10); 15 | 16 | WHILE(first <= last) LOOP 17 | BEGIN 18 | mid := (first + last) / 2; 19 | IF(a(mid) = key) 20 | THEN 21 | dbms_output.put('number found @ '); 22 | dbms_output.put_line(mid); 23 | flag := TRUE; 24 | EXIT; 25 | END IF; 26 | 27 | IF(a(mid) > key) 28 | THEN 29 | last := mid -1; 30 | ELSE 31 | first := mid + 1; 32 | END IF; 33 | END; 34 | END LOOP; 35 | 36 | IF(NOT flag) 37 | THEN 38 | dbms_output.put_line('number not found'); 39 | END IF; 40 | 41 | END; 42 | -------------------------------------------------------------------------------- /PLSQL/Expt_10_stud.sql: -------------------------------------------------------------------------------- 1 | SET SERVEROUTPUT ON 2 | 3 | select user from dual; 4 | SELECT NAME FROM v$database; 5 | select table_name from dba_tables; 6 | select owner, table_name from all_tables; 7 | select table_name FROM user_tables; 8 | SELECT table_name FROM user_tables ORDER BY table_name; 9 | 10 | create table Stud(rollno int primary key, 11 | name char(10), 12 | mark1 float, 13 | mark2 float, 14 | mark3 float); 15 | 16 | /* ROLLNO NAME MARK1 MARK2 MARK3 17 | ---------- ---------- --------- ---------- ---------- 18 | 1 aparna 80 90 78 19 | 2 amritha 90 92 81 20 | 3 binuja 23 18 20 21 | 4 cathy 49 50 50 22 | 5 danish 60 62 61 23 | 6 fayas 76 62 74 */ 24 | 25 | insert into Stud values(1,'aparna', 80, 90, 78); 26 | insert into Stud values(&rollno, '&name', &mark1, &mark2, &mark3); 27 | select * from Stud; 28 | select name from Stud where rollno=1; 29 | 30 | 31 | DECLARE 32 | age integer; 33 | name VARCHAR(20); 34 | BEGIN 35 | dbms_output.put_line('Hello world'); 36 | --dbms_output.put_line('age = ' || age); 37 | --dbms_output.put_line('name = ' || name); 38 | --insert into Stud values(&rollno, '&name', &mark1, &mark2, &mark3); 39 | END; 40 | 41 | select * from Stud; 42 | delete from Stud; 43 | drop table Stud purge; 44 | -------------------------------------------------------------------------------- /dbms_out/cursor/out_table_last.sql: -------------------------------------------------------------------------------- 1 | ID NAME AGE ADDRESS 2 | ---------- -------------------- ---------- ------------------------------ 3 | SAL HIREDATE 4 | ---------- -------- 5 | 1 Alice 20 Address1 6 | 11000 01-01-21 7 | 8 | 2 Bob 30 Address2 9 | 21000 01-01-20 10 | 11 | 3 Cindy 40 Address3 12 | 31000 01-01-19 13 | 14 | 15 | ID NAME AGE ADDRESS 16 | ---------- -------------------- ---------- ------------------------------ 17 | SAL HIREDATE 18 | ---------- -------- 19 | 4 Sam 50 Address4 20 | 40500 01-01-18 21 | 22 | 5 Eric 60 Address5 23 | 50500 01-01-17 24 | 25 | 6 Tom 20 Address6 26 | 60500 01-01-16 27 | 28 | 29 | ID NAME AGE ADDRESS 30 | ---------- -------------------- ---------- ------------------------------ 31 | SAL HIREDATE 32 | ---------- -------- 33 | 7 John 30 Address7 34 | 70500 01-01-15 35 | 36 | 8 Sari 40 Address8 37 | 80500 01-01-14 38 | 39 | 9 Timo 50 Address9 40 | 90500 01-01-13 41 | 42 | 43 | ID NAME AGE ADDRESS 44 | ---------- -------------------- ---------- ------------------------------ 45 | SAL HIREDATE 46 | ---------- -------- 47 | 10 Skove 60 Address10 48 | 100500 01-01-12 49 | 50 | 51 | 10 rows selected. 52 | 53 | -------------------------------------------------------------------------------- /dbms_out/cursor/cursor1.sql: -------------------------------------------------------------------------------- 1 | Employee Id:1 2 | Employee Name:Alice 3 | Employee Age:20 4 | Salary :10000 5 | Hire Date:01-01-21 6 | ============================== 7 | Employee Id:2 8 | Employee Name:Bob 9 | Employee Age:30 10 | Salary :20000 11 | Hire Date:01-01-20 12 | ============================== 13 | Employee Id:3 14 | Employee Name:Cindy 15 | Employee Age:40 16 | Salary :30000 17 | Hire Date:01-01-19 18 | ============================== 19 | Employee Id:4 20 | Employee Name:Sam 21 | Employee Age:50 22 | Salary :40000 23 | Hire Date:01-01-18 24 | ============================== 25 | Employee Id:5 26 | Employee Name:Eric 27 | Employee Age:60 28 | Salary :50000 29 | Hire Date:01-01-17 30 | ============================== 31 | Employee Id:6 32 | Employee Name:Tom 33 | Employee Age:20 34 | Salary :60000 35 | Hire Date:01-01-16 36 | ============================== 37 | Employee Id:7 38 | Employee Name:John 39 | Employee Age:30 40 | Salary :70000 41 | Hire Date:01-01-15 42 | ============================== 43 | Employee Id:8 44 | Employee Name:Sari 45 | Employee Age:40 46 | Salary :80000 47 | Hire Date:01-01-14 48 | ============================== 49 | Employee Id:9 50 | Employee Name:Timo 51 | Employee Age:50 52 | Salary :90000 53 | Hire Date:01-01-13 54 | ============================== 55 | Employee Id:10 56 | Employee Name:Skove 57 | Employee Age:60 58 | Salary :100000 59 | Hire Date:01-01-12 60 | ============================== 61 | 62 | PL/SQL procedure successfully completed. 63 | -------------------------------------------------------------------------------- /employeelab3.sql: -------------------------------------------------------------------------------- 1 | show databases; 2 | create database dbms_exp3; 3 | use dbms_exp3; 4 | CREATE TABLE employee ( 5 | fname VARCHAR(15), minit char, 6 | lname varchar(15), ssn char(9) PRIMARY KEY, 7 | bdate date, address varchar(30), 8 | sex char, salary int(10), 9 | super_ssn char(9), dno int(4) ); 10 | 11 | CREATE TABLE department ( 12 | dnumber int(4) PRIMARY KEY, 13 | dname varchar(15), 14 | mgrssn char(9), 15 | mgrstartdate date ); 16 | 17 | CREATE TABLE dept_locations ( 18 | dnumber int(4), 19 | dlocation varchar(15), 20 | primary key (dnumber,dlocation) ); 21 | 22 | CREATE TABLE project ( 23 | pname varchar(15), 24 | pnumber int(4) PRIMARY KEY, 25 | plocation varchar(15), 26 | dnum int(4) ); 27 | 28 | CREATE TABLE works_on ( 29 | essn char(9), 30 | pno int(4), 31 | hours int(4), 32 | primary key (essn,pno) ); 33 | 34 | CREATE TABLE dependent ( 35 | essn char(9), 36 | dependent_name varchar(15), 37 | sex char, 38 | bdate date, 39 | relationship varchar(8), 40 | primary key (essn,dependent_name)); 41 | 42 | ALTER TABLE employee ADD FOREIGN KEY (super_ssn) references employee(ssn); 43 | ALTER TABLE employee ADD FOREIGN KEY (dno) references department(dnumber); 44 | ALTER TABLE department ADD FOREIGN KEY (mgrssn) references employee(ssn); 45 | ALTER TABLE dept_locations ADD FOREIGN KEY (dnumber) references department(dnumber); 46 | ALTER TABLE project ADD FOREIGN KEY (dnum) references department(dnumber); 47 | ALTER TABLE works_on ADD FOREIGN KEY (essn) references employee(ssn); 48 | ALTER TABLE works_on ADD FOREIGN KEY (pno) references project(pnumber); 49 | ALTER TABLE dependent ADD FOREIGN KEY (essn) references employee(ssn); 50 | 51 | show tables; 52 | -------------------------------------------------------------------------------- /PLSQL/trigger5.sql: -------------------------------------------------------------------------------- 1 | /* 2 | Sapple 4 3 | Create a Table 4 | Customer(id int primary key, name varchar(20), 5 | age int, address varchar(30), sal int); 6 | 7 | Create a trigger to display the salary difference between new and old values 8 | before - insert, delete or update of values on the table 9 | */ 10 | drop table Customer; 11 | create table Customer(id int primary key, 12 | name varchar(20), age int, address varchar(30), sal int); 13 | 14 | insert into Customer values(1, 'Alice', 20, 'Address1', 10000); 15 | insert into Customer values(2, 'Bob', 30, 'Address2', 20000); 16 | insert into Customer values(3, 'Cindy', 40, 'Address3', 30000); 17 | insert into Customer values(4, 'Sam', 50, 'Address4', 40000); 18 | insert into Customer values(5, 'Eric', 60, 'Address5', 50000); 19 | 20 | select * from Customer; 21 | 22 | drop trigger trigger5; 23 | CREATE OR REPLACE TRIGGER trigger5 24 | BEFORE DELETE OR INSERT OR UPDATE ON Customer 25 | REFERENCING NEW AS NEW OLD AS OLD 26 | FOR EACH ROW 27 | DECLARE 28 | sal_diff int; 29 | BEGIN 30 | dbms_output.put_line('TRIGGER 5'); 31 | if (:new.id <= 0) then 32 | dbms_output.put_line('Error ...! : Invaid ID'); 33 | RAISE_APPLICATION_ERROR(-20000, 'Error ...! : Invaid ID '); 34 | else 35 | sal_diff := :new.sal - :old.sal; 36 | dbms_output.put_line('Old Salary = ' || :old.sal); 37 | dbms_output.put_line('New Salary = ' || :new.sal); 38 | dbms_output.put_line('Salary diff = ' || sal_diff); 39 | end if; 40 | END; 41 | 42 | /* Update statement to test trigger */ 43 | update Customer set sal=30000 where id=1; 44 | select * from Customer; 45 | 46 | /* You can also write a sample program to test this trigger */ 47 | DECLARE 48 | new_sal int; 49 | cust_id int; 50 | BEGIN 51 | cust_id := &cust_id; 52 | new_sal := &new_sal; 53 | dbms_output.put_line('new_sal = ' || new_sal); 54 | if (cust_id <= 0) then 55 | RAISE_APPLICATION_ERROR(-20000, 'Error ...! : Invaid ID '); 56 | else 57 | update Customer set sal=new_sal where id=cust_id; 58 | end if; 59 | END; 60 | / -------------------------------------------------------------------------------- /dbms_out/package.sql: -------------------------------------------------------------------------------- 1 | 2 | SQL> @package 3 | 4 | Package created. 5 | 6 | 7 | Package body created. 8 | 9 | SQL> @testprogram 10 | Enter value for a1: 2 11 | old 2: e_id number := &a1; 12 | new 2: e_id number := 2; 13 | Customer corresponding to 2 is Bob 14 | Annual Salary of Bob is : 252000 15 | 16 | PL/SQL procedure successfully completed. 17 | 18 | SQL> @testprogram 19 | Enter value for a1: 5 20 | old 2: e_id number := &a1; 21 | new 2: e_id number := 5; 22 | Customer corresponding to 5 is Eric 23 | Annual Salary of Eric is : 606000 24 | 25 | PL/SQL procedure successfully completed. 26 | /////////////////////////////////////////////////////////////////////////////////////////////// 27 | CREATE OR REPLACE PACKAGE Customer_package AS 28 | PROCEDURE Get_emp_name(c_id IN NUMBER, c_name OUT VARCHAR); 29 | FUNCTION Get_annual_salary(c_id IN NUMBER) RETURN NUMBER; 30 | END Customer_package; 31 | / 32 | -- package body 33 | CREATE OR REPLACE PACKAGE BODY Customer_package AS 34 | -- first procedure definition 35 | PROCEDURE Get_emp_name(c_id IN NUMBER, c_name OUT VARCHAR) 36 | IS 37 | BEGIN 38 | SELECT name INTO c_name from Customer where id = c_id; 39 | END Get_emp_name; 40 | -- second function definition 41 | FUNCTION Get_annual_salary (c_id IN NUMBER) 42 | RETURN NUMBER 43 | IS 44 | c_sal NUMBER :=0; 45 | annual_sal NUMBER; 46 | BEGIN 47 | SELECT sal INTO c_sal FROM Customer WHERE id = c_id; 48 | annual_sal := 12 * c_sal; 49 | RETURN (annual_sal); 50 | END; 51 | END Customer_package; 52 | / 53 | 54 | ///////////////////////////////////////////////////////////////////////////////////////////////// 55 | 56 | declare 57 | e_id number := &a1; 58 | e_name varchar(10); 59 | sal number :=0; 60 | begin 61 | Customer_package.Get_emp_name(e_id, e_name); 62 | dbms_output.put_line('Customer corresponding to '||e_id || ' is '|| e_name); 63 | sal := Customer_package.Get_annual_salary(e_id); 64 | dbms_output.put_line('Annual Salary of '||e_name || ' is : '|| sal); 65 | end; 66 | / 67 | 68 | ////////////////////////////////////////////////////////////////////////////////////////////////////// 69 | -------------------------------------------------------------------------------- /PLSQL/sqlop.sql: -------------------------------------------------------------------------------- 1 | /* SQL Operations */ 2 | /* ROLLNO NAME MARK1 MARK2 MARK3 3 | ---------- ---------- --------- ---------- ---------- 4 | 1 aparna 80 90 78 5 | 2 amritha 90 92 81 6 | 3 binuja 23 18 20 7 | 4 cathy 49 50 50 8 | 5 danish 60 62 61 9 | 6 fayas 76 62 74 */ 10 | create table Stud(rollno int primary key, 11 | name char(10), 12 | mark1 float, 13 | mark2 float, 14 | mark3 float); 15 | insert into Stud values(1,'aparna', 80, 90, 78); 16 | insert into Stud values(2,'amritha', 90, 92, 81); 17 | insert into Stud values(3,'binuja', 23, 18, 20); 18 | insert into Stud values(4,'cathy', 49, 50, 50); 19 | insert into Stud values(5,'danish', 60, 62, 61); 20 | insert into Stud values(6,'fayas', 76, 62, 74); 21 | 22 | select * from Stud; 23 | 24 | /* 25 | Write a PL/SQL program to grade the student according to the following rules 26 | Student(name,rollno,mark1,mark2,mark3) 27 | TOTAL MARKS GRADE 28 | >=250 Distinction 29 | 180-250 First Class 30 | 120-179 Second Class 31 | 80-119 Third Class 32 | <80 Fail 33 | The result should be in the following Format 34 | STUDENT NAME: 35 | ROLL NO : 36 | TOTAL MARKS : 37 | GRADE : 38 | */ 39 | DECLARE 40 | roleNo integer; 41 | No integer; 42 | Name varchar(20); 43 | total integer; 44 | BEGIN 45 | -- get role no from user 46 | roleNo := &roleNo; 47 | 48 | --execute sql query 49 | select rollno,name,(mark1+mark2+mark3) into No, Name, total from Stud where 50 | lno=roleNo; 51 | 52 | --print output 53 | dbms_output.put_line('---------------------------'); 54 | dbms_output.put_line('Roll no : ' || No); 55 | dbms_output.put_line('Name : ' || Name); 56 | dbms_output.put_line('Total Marks : ' || total); 57 | dbms_output.put_line('---------------------------'); 58 | 59 | --calculate & print grade 60 | if (total >=250 ) then 61 | dbms_output.put_line('Grade = Distinction'); 62 | elsif (total < 250 and total >= 180) then 63 | dbms_output.put_line('Grade = First Class'); 64 | elsif (total < 180 and total >= 120) then 65 | dbms_output.put_line('Grade = Second Class'); 66 | elsif (total < 120 and total >= 80) then 67 | dbms_output.put_line('Grade = Third Class'); 68 | else 69 | dbms_output.put_line('Grade = FAIL'); 70 | end if; 71 | END; 72 | / -------------------------------------------------------------------------------- /dbms_out/cursor/cursor3.sql: -------------------------------------------------------------------------------- 1 | Employee Id:1 2 | Employee Name:Alice 3 | Salary :10000 4 | Hire Date:01-01-21 5 | Employee name:Alice is Grater that 5 6 | Years 7 | ----------------------------- 8 | New Salary : 11000 9 | ============================== 10 | Employee Id:2 11 | Employee Name:Bob 12 | Salary :20000 13 | Hire Date:01-01-20 14 | Employee name:Bob is Grater that 5 15 | Years 16 | ----------------------------- 17 | New Salary : 21000 18 | ============================== 19 | Employee Id:3 20 | Employee Name:Cindy 21 | Salary :30000 22 | Hire Date:01-01-19 23 | Employee name:Cindy is Grater that 5 24 | Years 25 | ----------------------------- 26 | New Salary : 31000 27 | ============================== 28 | Employee Id:4 29 | Employee Name:Sam 30 | Salary :40000 31 | Hire Date:01-01-18 32 | Employee name:Sam is Less that 5 33 | Years 34 | ----------------------------- 35 | New Salary : 40500 36 | ============================== 37 | Employee Id:5 38 | Employee Name:Eric 39 | Salary :50000 40 | Hire Date:01-01-17 41 | Employee name:Eric is Less that 5 42 | Years 43 | ----------------------------- 44 | New Salary : 50500 45 | ============================== 46 | Employee Id:6 47 | Employee Name:Tom 48 | Salary :60000 49 | Hire Date:01-01-16 50 | Employee name:Tom is Less that 5 51 | Years 52 | ----------------------------- 53 | New Salary : 60500 54 | ============================== 55 | Employee Id:7 56 | Employee Name:John 57 | Salary :70000 58 | Hire Date:01-01-15 59 | Employee name:John is Less that 5 60 | Years 61 | ----------------------------- 62 | New Salary : 70500 63 | ============================== 64 | Employee Id:8 65 | Employee Name:Sari 66 | Salary :80000 67 | Hire Date:01-01-14 68 | Employee name:Sari is Less that 5 69 | Years 70 | ----------------------------- 71 | New Salary : 80500 72 | ============================== 73 | Employee Id:9 74 | Employee Name:Timo 75 | Salary :90000 76 | Hire Date:01-01-13 77 | Employee name:Timo is Less that 5 78 | Years 79 | ----------------------------- 80 | New Salary : 90500 81 | ============================== 82 | Employee Id:10 83 | Employee Name:Skove 84 | Salary :100000 85 | Hire Date:01-01-12 86 | Employee name:Skove is Less that 5 87 | Years 88 | ----------------------------- 89 | New Salary : 100500 90 | ============================== 91 | 92 | PL/SQL procedure successfully completed. 93 | -------------------------------------------------------------------------------- /PLSQL/trigger4.sql: -------------------------------------------------------------------------------- 1 | /* 2 | Sapple 3 3 | Book_avail (bookid, title, no_of _copies, price) 4 | Student (st_id,name,class,fine) 5 | Issue_tab (st_id, book_id, issuedate, returndate) 6 | 7 | Create a database trigger to calculate the fine based on the rules given below. 8 | After 1 month 5% of price 9 | After 2 month 10% of price 10 | After 3 month 20% of price. 11 | */ 12 | drop table Book_avail; 13 | drop table Student; 14 | drop table Issue_tab; 15 | create table Book_avail(bookid int primary key, title varchar(20), no_of_copies int, 16 | ice int); 17 | create table Student(st_id int primary key, name varchar(20), class varchar(10), fine int 18 | 19 | create table Issue_tab(st_id int, book_id int, issue_date date , return_date date, 20 | imary key(st_id, book_id)); 21 | 22 | insert into Student values(100, 'Alice', 'CSE', 0); 23 | insert into Student values(101, 'Bob', 'CSE', 0); 24 | insert into Book_avail values(1, 'Data Structure', 10, 1000); 25 | insert into Book_avail values(2, 'Java - Complete ref', 10, 1000); 26 | 27 | --TO_DATE('2003/05/03 21:02:44', 'yyyy/mm/dd hh24:mi:ss') 28 | insert into Issue_tab values(100, 1, TO_DATE('2022/01/01','%yyyy-%mm-%dd'), TO_DATE( 29 | 022/02/01','yyyy-%mm-%dd')); 30 | insert into Issue_tab values(101, 2, TO_DATE('2022/01/01','%yyyy-%mm-%dd'), TO_DATE( 31 | 022/03/01','yyyy-%mm-%dd')); 32 | select * from Issue_tab; 33 | select * from Student; 34 | select * from Book_avail; 35 | drop trigger trigger4; 36 | 37 | CREATE OR REPLACE TRIGGER trigger4 38 | BEFORE UPDATE ON Issue_tab 39 | REFERENCING NEW AS NEW OLD AS OLD 40 | FOR EACH ROW 41 | DECLARE 42 | bprice int; 43 | months int; 44 | latefine int; 45 | BEGIN 46 | select price into bprice from Book_avail where bookid=1; 47 | months:=months_between(:new.return_date,:old.issue_date); 48 | dbms_output.put_line('months = ' || months); 49 | 50 | -- Calculate fine while update issue_tab 51 | if months>=1 and months<2 then 52 | latefine := bprice*0.05; 53 | else if months>=2 and months<3 then 54 | latefine := bprice*0.01; 55 | else if months>=3 then 56 | latefine := bprice*0.2; 57 | end if; 58 | end if; 59 | end if; 60 | 61 | -- Update fine into Student table while update issue_tab 62 | dbms_output.put_line('latefine = ' || latefine); 63 | update Student set fine=latefine where st_id=:old.st_id; 64 | END; 65 | 66 | /* Update statement to test trigger */ 67 | update Issue_tab set return_date=TO_DATE('2022/03/02','%yyyy-%mm-%dd') where st_id=100; 68 | select * from Student; 69 | -------------------------------------------------------------------------------- /Expt_9_actor.sql: -------------------------------------------------------------------------------- 1 | show databases; 2 | create database lab9_tables; 3 | use lab9_tables; 4 | CREATE TABLE ACTOR ( 5 | ACT_ID INT, 6 | ACT_NAME VARCHAR (20), 7 | ACT_GENDER CHAR (1), 8 | PRIMARY KEY (ACT_ID) 9 | ); 10 | CREATE TABLE DIRECTOR ( 11 | DIR_ID INT, 12 | DIR_NAME VARCHAR (20), 13 | DIR_PHONE NUMERIC (10), 14 | PRIMARY KEY (DIR_ID) 15 | ); 16 | CREATE TABLE MOVIES ( 17 | MOV_ID INT, 18 | MOV_TITLE VARCHAR (25), 19 | MOV_YEAR INT, 20 | MOV_LANG VARCHAR (12), 21 | DIR_ID INT, 22 | PRIMARY KEY (MOV_ID), 23 | FOREIGN KEY (DIR_ID) REFERENCES DIRECTOR (DIR_ID) 24 | ); 25 | CREATE TABLE MOVIE_CAST ( 26 | ACT_ID INT, 27 | MOV_ID INT, 28 | ROLE VARCHAR(10), 29 | PRIMARY KEY (ACT_ID, MOV_ID), 30 | FOREIGN KEY (ACT_ID) REFERENCES ACTOR (ACT_ID), 31 | FOREIGN KEY (MOV_ID) REFERENCES MOVIES (MOV_ID) 32 | ); 33 | CREATE TABLE RATING ( 34 | MOV_ID INT, 35 | REV_STARS VARCHAR (25), 36 | PRIMARY KEY (MOV_ID), 37 | FOREIGN KEY (MOV_ID) REFERENCES MOVIES (MOV_ID) 38 | ); 39 | 40 | INSERT INTO ACTOR VALUES (301,'ANUSHKA','F'); 41 | INSERT INTO ACTOR VALUES (302,'PRABHAS','M'); 42 | INSERT INTO ACTOR VALUES (303,'PUNITH','M'); 43 | INSERT INTO ACTOR VALUES (304,'JERMY','M'); 44 | INSERT INTO DIRECTOR VALUES (60,'RAJAMOULI', 8751611001); 45 | INSERT INTO DIRECTOR VALUES (61,'HITCHCOCK', 7766138911); 46 | INSERT INTO DIRECTOR VALUES (62,'FARAN', 9986776531); 47 | INSERT INTO DIRECTOR VALUES (63,'STEVEN SPIELBERG', 8989776530); 48 | 49 | INSERT INTO MOVIES VALUES (1001,'BAHUBALI-2', 2017, 'TELAGU', 60); 50 | INSERT INTO MOVIES VALUES (1002,'BAHUBALI-1', 2015, 'TELAGU', 60); 51 | INSERT INTO MOVIES VALUES (1003,'AKASH', 2008, 'KANNADA', 61); 52 | INSERT INTO MOVIES VALUES (1004,'WAR HORSE', 2011, 'ENGLISH', 63); 53 | INSERT INTO MOVIE_CAST VALUES (301, 1002, 'HEROINE'); 54 | INSERT INTO MOVIE_CAST VALUES (301, 1001, 'HEROINE'); 55 | INSERT INTO MOVIE_CAST VALUES (303, 1003, 'HERO'); 56 | INSERT INTO MOVIE_CAST VALUES (303, 1002, 'GUEST'); 57 | INSERT INTO MOVIE_CAST VALUES (304, 1004, 'HERO'); 58 | INSERT INTO RATING VALUES (1001,4); 59 | INSERT INTO RATING VALUES (1002,2); 60 | INSERT INTO RATING VALUES (1003, 5); 61 | INSERT INTO RATING VALUES (1004, 4); 62 | select * from ACTOR; 63 | select * from DIRECTOR; 64 | select * from MOVIES; 65 | select * from MOVIE_CAST; 66 | select * from RATING; 67 | QUERIES 68 | 1) List the titles of all movies directed by‘Hitchcock’ 69 | SELECT MOV_TITLE FROM MOVIES WHERE DIR_ID IN ( 70 | SELECT DIR_ID FROM DIRECTOR WHERE DIR_NAME = 'HITCHCOCK'); 71 | 2) Find the movie names where one or more actors acted in two or 72 | moremovie 73 | SELECT MOV_TITLE FROM MOVIES M, MOVIE_CAST MV 74 | WHERE M.MOV_ID=MV.MOV_ID AND ACT_ID IN ( 75 | SELECT ACT_ID FROM MOVIE_CAST 76 | GROUP BY ACT_ID HAVING COUNT(ACT_ID)>1) 77 | GROUP BY MOV_TITLE HAVING COUNT(*)>1; 78 | 3) List all actors who acted in a movie before 2000 and also in a movie 79 | after 2015 (use 80 | JOIN operation). 81 | i) SELECT ACT_NAME, MOV_TITLE, MOV_YEAR FROM ACTOR A 82 | JOIN MOVIE_CAST C ON A.ACT_ID=C.ACT_ID 83 | JOIN MOVIES M ON C.MOV_ID=M.MOV_ID 84 | WHERE M.MOV_YEAR NOT BETWEEN 2000 AND 2015; 85 | OR 86 | ii) SELECT A.ACT_NAME, C.MOV_TITLE, C.MOV_YEAR FROM ACTOR A, MOVIES C, 87 | MOVIE_CAST B 88 | WHERE A.ACT_ID=B.ACT_ID AND B.MOV_ID=C.MOV_ID 89 | AND C.MOV_YEAR NOT BETWEEN 2000 AND 2015; 90 | 4) Find the title of movies and number of stars for each movie that has at 91 | least one rating and 92 | find the highest number of stars that movie received. Sort the result by 93 | movie title. 94 | SELECT MOV_TITLE, MAX(REV_STARS) FROM MOVIES 95 | INNER JOIN RATING USING(MOV_ID) 96 | 97 | GROUP BY MOV_TITLE HAVING MAX(REV_STARS)>0 98 | ORDER BY MOV_TITLE; 99 | 5) Update rating of all movies directed by ‘Steven Spielberg’ to 5 100 | UPDATE RATING SET REV_STARS=5 101 | WHERE MOV_ID IN (SELECT MOV_ID FROM MOVIES WHERE DIR_ID IN ( 102 | SELECT DIR_ID FROM DIRECTOR WHERE DIR_NAME ='STEVEN SPIELBERG')); 103 | -------------------------------------------------------------------------------- /Expt_5_student.sql: -------------------------------------------------------------------------------- 1 | 2 | create table Student( 3 | stud_id INT AUTO_INCREMENT PRIMARY KEY DEFAULT 100, 4 | stud_fname VARCHAR(20), 5 | stud_lname VARCHAR(20), 6 | stud_email VARCHAR(20), 7 | stud_ph VARCHAR(10)); 8 | ALTER TABLE Student AUTO_INCREMENT=100; 9 | 10 | create table Subject( 11 | sub_id INT NOT NULL AUTO_INCREMENT PRIMARY KEY, 12 | sub_name VARCHAR(20)); 13 | ALTER TABLE Subject AUTO_INCREMENT=200; 14 | 15 | create table Marks( 16 | sub_id INT, 17 | stud_id INT, 18 | marks INT, 19 | PRIMARY KEY(sub_id,stud_id)); 20 | 21 | desc Student; 22 | insert into Student(stud_fname,stud_lname,stud_email,stud_ph) values('shanti','vasan','shantiv@gmail.com',9677483824); 23 | insert into Student(stud_fname,stud_lname,stud_email,stud_ph) values('anjitha','k','anjithak@gmail.com',9574884993); 24 | insert into Student(stud_fname,stud_lname,stud_email,stud_ph) values('riya','khan','riyakhan@gmail.com',9637833993); 25 | select * from student; 26 | 27 | desc Subject; 28 | insert into Subject(sub_name) values('chemistry'); 29 | insert into Subject(sub_name) values('physics'); 30 | insert into Subject(sub_name) values('maths'); 31 | select * from Subject; 32 | 33 | desc Marks; 34 | insert into Marks(sub_id,stud_id,marks) values(200,100,75); 35 | insert into Marks(sub_id,stud_id,marks) values(200,101,94); 36 | insert into Marks(sub_id,stud_id,marks) values(200,102,60); 37 | insert into Marks(sub_id,stud_id,marks) values(201,100,85); 38 | insert into Marks(sub_id,stud_id,marks) values(201,101,98); 39 | insert into Marks(sub_id,stud_id,marks) values(201,102,70); 40 | insert into Marks(sub_id,stud_id,marks) values(202,100,50); 41 | insert into Marks(sub_id,stud_id,marks) values(202,101,96); 42 | insert into Marks(sub_id,stud_id,marks) values(202,102,45); 43 | select * from Marks; 44 | 45 | update Student set stud_lname='rajan' where stud_id=100; 46 | select * from Student where stud_id=100; 47 | 48 | update Subject set sub_name='mathematics' where sub_id=202; 49 | select * from Subject where sub_id=202; 50 | 51 | update Marks set marks=83 where sub_id=200 AND stud_id=102; 52 | select * from Marks where sub_id=200 and stud_id=102; 53 | 54 | select * from Student; 55 | select * from Subject; 56 | select * from Marks; 57 | 58 | select *from Student where stud_id=102; 59 | 60 | # Write a query to display student name, subject and marks of student ordered by marks. 61 | select stud_fname, sub_name, marks from Student, Subject, Marks where 62 | Student.stud_id=Marks.stud_id and 63 | Subject.sub_id=Marks.sub_id 64 | order by marks; 65 | 66 | # Write a query to display student name, subject and marks of students who have marks greater than 70 in physics. 67 | select stud_fname, sub_name, marks from Student,Subject,Marks where 68 | Student.stud_id=Marks.stud_id and 69 | Subject.sub_id=Marks.sub_id and 70 | marks >70 and sub_name ='physics'; 71 | 72 | # Write a query to display average marks in each subject. 73 | select sub_name, avg(marks) from Student, Subject, Marks where 74 | Student.stud_id=Marks.stud_id and 75 | Subject.sub_id=Marks.sub_id group by sub_name; 76 | 77 | # Write a query to display the number of students. 78 | select count(stud_id) from Student; 79 | 80 | # Write a query to display the maximum and minimum marks obtained by students in each subject. 81 | select sub_name, max(marks), min(marks) from Student, Subject, Marks where 82 | Student.stud_id=Marks.stud_id and Subject.sub_id=Marks.sub_id group by sub_name; 83 | 84 | # Write a query to display the details of a student whose name begins with S. 85 | select * from Student where stud_fname like 's%'; 86 | 87 | # Write a query to display the details of a student whose first name contains a in the fourth place. 88 | select * from Student where stud_fname like '___a'; 89 | 90 | # Write a query to display the name,subject and marks of students having marks between 50 and 75. 91 | select stud_fname, sub_name, marks from Student, Subject, Marks where 92 | Student.stud_id=Marks.stud_id and 93 | Subject.sub_id=Marks.sub_id and 94 | marks between 50 and 75; 95 | 96 | #Create a view to display student name and marks 97 | create view student_view as select stud_fname, stud_lname, sub_name,marks from 98 | Student, Subject, Marks where 99 | Student.stud_id=Marks.stud_id and 100 | Subject.sub_id=Marks.sub_id; 101 | 102 | select * from student_view; 103 | -------------------------------------------------------------------------------- /dbms_out/expt9out: -------------------------------------------------------------------------------- 1 | Expt 9 2 | 3 | mysql> select * from ACTOR; 4 | +--------+----------+------------+ 5 | | ACT_ID | ACT_NAME | ACT_GENDER | 6 | +--------+----------+------------+ 7 | | 301 | ANUSHKA | F | 8 | | 302 | PRABHAS | M | 9 | | 303 | PUNITH | M | 10 | | 304 | JERMY | M | 11 | +--------+----------+------------+ 12 | 4 rows in set (0.00 sec) 13 | 14 | mysql> select * from DIRECTOR; 15 | +--------+------------------+------------+ 16 | | DIR_ID | DIR_NAME | DIR_PHONE | 17 | +--------+------------------+------------+ 18 | | 60 | RAJAMOULI | 8751611001 | 19 | | 61 | HITCHCOCK | 7766138911 | 20 | | 62 | FARAN | 9986776531 | 21 | | 63 | STEVEN SPIELBERG | 8989776530 | 22 | +--------+------------------+------------+ 23 | 4 rows in set (0.00 sec) 24 | 25 | mysql> select * from MOVIES; 26 | +--------+------------+----------+----------+--------+ 27 | | MOV_ID | MOV_TITLE | MOV_YEAR | MOV_LANG | DIR_ID | 28 | +--------+------------+----------+----------+--------+ 29 | | 1001 | BAHUBALI-2 | 2017 | TELAGU | 60 | 30 | | 1002 | BAHUBALI-1 | 2015 | TELAGU | 60 | 31 | | 1003 | AKASH | 2008 | KANNADA | 61 | 32 | | 1004 | WAR HORSE | 2011 | ENGLISH | 63 | 33 | +--------+------------+----------+----------+--------+ 34 | 4 rows in set (0.00 sec) 35 | 36 | mysql> select * from MOVIE_CAST; 37 | +--------+--------+---------+ 38 | | ACT_ID | MOV_ID | ROLE | 39 | +--------+--------+---------+ 40 | | 301 | 1001 | HEROINE | 41 | | 301 | 1002 | HEROINE | 42 | | 303 | 1002 | GUEST | 43 | | 303 | 1003 | HERO | 44 | | 304 | 1004 | HERO | 45 | +--------+--------+---------+ 46 | 5 rows in set (0.00 sec) 47 | 48 | mysql> select * from RATING; 49 | +--------+-----------+ 50 | | MOV_ID | REV_STARS | 51 | +--------+-----------+ 52 | | 1001 | 4 | 53 | | 1002 | 2 | 54 | | 1003 | 5 | 55 | | 1004 | 4 | 56 | +--------+-----------+ 57 | 4 rows in set (0.00 sec) 58 | 59 | 1) List the titles of all movies directed by‘Hitchcock’ 60 | SELECT MOV_TITLE FROM MOVIES WHERE DIR_ID IN (SELECT DIR_ID FROM DIRECTOR WHERE DIR_NAME = 'HITCHCOCK'); 61 | +-----------+ 62 | | MOV_TITLE | 63 | +-----------+ 64 | | AKASH | 65 | +-----------+ 66 | 67 | 68 | 2) Find the movie names where one or more actors acted in two or 69 | moremovie 70 | SELECT MOV_TITLE FROM MOVIES M, MOVIE_CAST MV WHERE M.MOV_ID=MV.MOV_ID AND ACT_ID IN (SELECT ACT_ID FROM MOVIE_CAST GROUP BY ACT_ID HAVING COUNT(ACT_ID)>1) GROUP BY MOV_TITLE HAVING COUNT(*)>1; 71 | +------------+ 72 | | MOV_TITLE | 73 | +------------+ 74 | | BAHUBALI-1 | 75 | +------------+ 76 | 77 | 78 | 79 | 3) List all actors who acted in a movie before 2000 and also in a movie 80 | after 2015 (use 81 | JOIN operation). 82 | 83 | i) 84 | 85 | SELECT ACT_NAME, MOV_TITLE, MOV_YEAR FROM ACTOR A JOIN MOVIE_CAST C ON A.ACT_ID=C.ACT_ID JOIN MOVIES M ON C.MOV_ID=M.MOV_ID WHERE M.MOV_YEAR NOT BETWEEN 2000 AND 2015; 86 | OR 87 | ii) SELECT A.ACT_NAME, C.MOV_TITLE, C.MOV_YEAR FROM ACTOR A, MOVIES C, 88 | MOVIE_CAST B 89 | WHERE A.ACT_ID=B.ACT_ID AND B.MOV_ID=C.MOV_ID 90 | AND C.MOV_YEAR NOT BETWEEN 2000 AND 2015; 91 | +----------+------------+----------+ 92 | | ACT_NAME | MOV_TITLE | MOV_YEAR | 93 | +----------+------------+----------+ 94 | | ANUSHKA | BAHUBALI-2 | 2017 | 95 | +----------+------------+----------+ 96 | 97 | 98 | 4) Find the title of movies and number of stars for each movie that has at 99 | least one rating and 100 | find the highest number of stars that movie received. Sort the result by 101 | movie title. 102 | 103 | 104 | SELECT MOV_TITLE, MAX(REV_STARS) FROM MOVIES INNER JOIN RATING USING(MOV_ID) GROUP BY MOV_TITLE 105 | HAVING MAX(REV_STARS)>0 ORDER BY MOV_TITLE; 106 | 107 | +------------+----------------+ 108 | | MOV_TITLE | MAX(REV_STARS) | 109 | +------------+----------------+ 110 | | AKASH | 5 | 111 | | BAHUBALI-1 | 2 | 112 | | BAHUBALI-2 | 4 | 113 | | WAR HORSE | 4 | 114 | +------------+----------------+ 115 | 116 | 117 | 5) Update rating of all movies directed by ‘Steven Spielberg’ to 5 118 | 119 | UPDATE RATING SET REV_STARS=5 WHERE MOV_ID IN (SELECT MOV_ID FROM MOVIES WHERE DIR_ID IN (SELECT DIR_ID FROM DIRECTOR WHERE DIR_NAME ='STEVEN SPIELBERG')); 120 | 121 | +--------+-----------+ 122 | | MOV_ID | REV_STARS | 123 | +--------+-----------+ 124 | | 1004 | 5 | 125 | +--------+-----------+ 126 | 1 row in set (0.00 sec) 127 | 128 | 129 | 130 | 131 | -------------------------------------------------------------------------------- /dbms_out/view.sql: -------------------------------------------------------------------------------- 1 | mysql> use db; 2 | Reading table information for completion of table and column names 3 | You can turn off this feature to get a quicker startup with -A 4 | 5 | Database changed 6 | mysql> # create table Emp 7 | mysql> create table Emp(EMP_no int primary key, Emp_name varchar(10), Job 8 | -> varchar(10), Hiredata date, Salary float, Comm Float, Depno int); 9 | ERROR 1050 (42S01): Table 'Emp' already exists 10 | mysql> # insert data 11 | mysql> INSERT INTO Emp VALUES(1,'Steven', 'Marketing', STR_TO_DATE('06-jan- 12 | '> 1995', '%d-%M-%Y'),24000, NULL,2); 13 | ERROR 1062 (23000): Duplicate entry '1' for key 'Emp.PRIMARY' 14 | mysql> INSERT INTO Emp VALUES(2,'Neena', 'FI_ACCOUNT', STR_TO_DATE('06- 15 | '> feb-1987', '%d-%M-%Y'),34000, NULL,1); 16 | ERROR 1062 (23000): Duplicate entry '2' for key 'Emp.PRIMARY' 17 | mysql> INSERT INTO Emp VALUES(3,'Lex', 'FI_MGR', STR_TO_DATE('06-jan-1980', 18 | -> '%d-%M-%Y'),240000, NULL,1); 19 | ERROR 1062 (23000): Duplicate entry '3' for key 'Emp.PRIMARY' 20 | mysql> INSERT INTO Emp VALUES(4,'Alexander', 'Sa_Rep', STR_TO_DATE('06-jun- 21 | '> 1987', '%d-%M-%Y'),20000, NULL,4); 22 | ERROR 1062 (23000): Duplicate entry '4' for key 'Emp.PRIMARY' 23 | mysql> INSERT INTO Emp VALUES(5,'Bruce', 'IT_PROG',STR_TO_DATE('06-jul- 24 | '> 1990', '%d-%M-%Y'),24000, NULL,4); 25 | ERROR 1062 (23000): Duplicate entry '5' for key 'Emp.PRIMARY' 26 | mysql> # create view 27 | mysql> create view emp_view as select EMP_no, Emp_name, Salary from Emp where 28 | -> Salary>20000; 29 | Query OK, 0 rows affected (0.11 sec) 30 | 31 | mysql> select * from emp_view ; 32 | +--------+----------+--------+ 33 | | EMP_no | Emp_name | Salary | 34 | +--------+----------+--------+ 35 | | 1 | Steven | 24000 | 36 | | 2 | Neena | 34000 | 37 | | 3 | Lex | 240000 | 38 | | 5 | Bruce | 24000 | 39 | | 6 | Jack | 241000 | 40 | +--------+----------+--------+ 41 | 5 rows in set (0.00 sec) 42 | 43 | mysql> # update salary in view 44 | mysql> update emp_view set Salary = 999; 45 | Query OK, 5 rows affected (0.06 sec) 46 | Rows matched: 5 Changed: 5 Warnings: 0 47 | 48 | mysql> # verify, the Emp table is updated 49 | mysql> select * from Emp; 50 | +--------+-----------+------------+------------+--------+------+-------+ 51 | | EMP_no | Emp_name | Job | Hiredata | Salary | Comm | Depno | 52 | +--------+-----------+------------+------------+--------+------+-------+ 53 | | 1 | Steven | Marketing | 1995-01-06 | 999 | NULL | 2 | 54 | | 2 | Neena | FI_ACCOUNT | 1987-02-06 | 999 | NULL | 1 | 55 | | 3 | Lex | FI_MGR | 1980-01-06 | 999 | NULL | 1 | 56 | | 4 | Alexander | Sa_Rep | 1987-06-06 | 20000 | NULL | 4 | 57 | | 5 | Bruce | IT_PROG | 1990-07-06 | 999 | NULL | 4 | 58 | | 6 | Jack | FI_MGR | 1980-08-06 | 999 | NULL | 5 | 59 | +--------+-----------+------------+------------+--------+------+-------+ 60 | 6 rows in set (0.00 sec) 61 | 62 | mysql> # create view 63 | mysql> create view job_view as (select Emp_name, Job from Emp ); 64 | Query OK, 0 rows affected (0.13 sec) 65 | 66 | mysql> # show view 67 | mysql> select * from job_view; 68 | +-----------+------------+ 69 | | Emp_name | Job | 70 | +-----------+------------+ 71 | | Steven | Marketing | 72 | | Neena | FI_ACCOUNT | 73 | | Lex | FI_MGR | 74 | | Alexander | Sa_Rep | 75 | | Bruce | IT_PROG | 76 | | Jack | FI_MGR | 77 | +-----------+------------+ 78 | 6 rows in set (0.00 sec) 79 | 80 | mysql> # update data in Emp 81 | mysql> update Emp set Job = 'MK_MGR' where Emp_name = 'Steven'; 82 | Query OK, 1 row affected (0.06 sec) 83 | Rows matched: 1 Changed: 1 Warnings: 0 84 | 85 | mysql> # show view and verify update is reflected 86 | mysql> select * from job_view; 87 | +-----------+------------+ 88 | | Emp_name | Job | 89 | +-----------+------------+ 90 | | Steven | MK_MGR | 91 | | Neena | FI_ACCOUNT | 92 | | Lex | FI_MGR | 93 | | Alexander | Sa_Rep | 94 | | Bruce | IT_PROG | 95 | | Jack | FI_MGR | 96 | +-----------+------------+ 97 | 6 rows in set (0.00 sec) 98 | 99 | mysql> # show Emp and verify update is reflected 100 | mysql> select * from Emp; 101 | +--------+-----------+------------+------------+--------+------+-------+ 102 | | EMP_no | Emp_name | Job | Hiredata | Salary | Comm | Depno | 103 | +--------+-----------+------------+------------+--------+------+-------+ 104 | | 1 | Steven | MK_MGR | 1995-01-06 | 999 | NULL | 2 | 105 | | 2 | Neena | FI_ACCOUNT | 1987-02-06 | 999 | NULL | 1 | 106 | | 3 | Lex | FI_MGR | 1980-01-06 | 999 | NULL | 1 | 107 | | 4 | Alexander | Sa_Rep | 1987-06-06 | 20000 | NULL | 4 | 108 | | 5 | Bruce | IT_PROG | 1990-07-06 | 999 | NULL | 4 | 109 | | 6 | Jack | FI_MGR | 1980-08-06 | 999 | NULL | 5 | 110 | +--------+-----------+------------+------------+--------+------+-------+ 111 | 6 rows in set (0.00 sec) 112 | 113 | mysql> 114 | -------------------------------------------------------------------------------- /Expt_7_Aggreate.sql: -------------------------------------------------------------------------------- 1 | drop database student; 2 | create database student; 3 | use student; 4 | create table Faculty (F_Code int Primary Key, F_Name varchar(15)); 5 | insert into Faculty values(101, 'Silgy'); 6 | insert into Faculty values(102, 'Bindu'); 7 | insert into Faculty values(103, 'Vidhya'); 8 | insert into Faculty values(104, 'Sangeetha'); 9 | insert into Faculty values(105, 'Jayakumar'); 10 | create table Subject (subjectcode varchar(5) primary key not null, 11 | subjectname char(15), 12 | maxmark int, 13 | faculty_code int, 14 | foreign key(faculty_code) references Faculty(F_code)); 15 | 16 | insert into Subject(subjectcode,subjectname,maxmark,faculty_code) values(501,'Maths', 150, 101); 17 | insert into Subject(subjectcode,subjectname,maxmark,faculty_code) values(502,'FCA', 100, 102); 18 | insert into Subject(subjectcode,subjectname,maxmark,faculty_code) values(503,'DBMS', 100, 105); 19 | insert into Subject(subjectcode,subjectname,maxmark,faculty_code) values(504, 'OS',75, 103); 20 | insert into Subject(subjectcode,subjectname,maxmark,faculty_code) values(505, 'DC',200, 104); 21 | insert into Subject(subjectcode,subjectname,maxmark,faculty_code) values(508, 'DBMSLab', 1001, 103); 22 | select * from Subject; 23 | create table Student(studentcode varchar(5) primary key not null, 24 | studentname char(15), 25 | dob date, 26 | studentbranch char(3), 27 | adate date, 28 | check(studentbranch in('cs','ec','ee','me'))); 29 | 30 | insert into Student values(1,'Amitha',STR_TO_DATE('12-jan-1987', '%d-%M-%Y'),'cs',STR_TO_DATE('1-jun-2000', '%d-%M-%Y')); 31 | insert into Student values(2,'vaidehi',STR_TO_DATE('25-dec-88', '%d-%M-%Y'),'cs',STR_TO_DATE('1-jun-2000', '%d-%M-%Y')); 32 | insert into Student values(3,'varun',STR_TO_DATE('2-oct-88', '%d-%M-%Y'),'cs',STR_TO_DATE('2-jun-2000', '%d-%M-%Y')); 33 | insert into Student values(4,'turner',STR_TO_DATE('5-sep-88', '%d-%M-%Y'),'cs',STR_TO_DATE('2-jun-2000', '%d-%M-%Y')); 34 | insert into Student values(5,'vani',STR_TO_DATE('20-jul-88', '%d-%M-%Y'),'cs',STR_TO_DATE('5-jun-2000', '%d-%M-%Y')); 35 | insert into Student values(6,'binu',STR_TO_DATE('13-aug-88', '%d-%M-%Y'),'cs',STR_TO_DATE('10-jun-2000', '%d-%M-%Y')); 36 | insert into Student values(7,'chitra',STR_TO_DATE('14-nov-86', '%d-%M-%Y'),'cs',STR_TO_DATE('9-jun-2000', '%d-%M-%Y')); 37 | insert into Student values(8,'dona',STR_TO_DATE('2-dec-91', '%d-%M-%Y'),'cs',STR_TO_DATE('2-jun-2000', '%d-%M-%Y')); 38 | insert into Student values(9,'elana',STR_TO_DATE('5-feb-90', '%d-%M-%Y'),'cs',STR_TO_DATE('2-jun-2000', '%d-%M-%Y')); 39 | insert into Student values(10,'fahan',STR_TO_DATE('20-mar-88', '%d-%M-%Y'),'cs',STR_TO_DATE('5-jun-2000', '%d-%M-%Y')); 40 | insert into Student values(11,'ginu',STR_TO_DATE('13-apr-88', '%d-%M-%Y'),'cs',STR_TO_DATE('10-jun-2000', '%d-%M-%Y')); 41 | insert into Student values(12,'hamna',STR_TO_DATE('14-may-85', '%d-%M-%Y'),'cs',STR_TO_DATE('9-jun-2000', '%d-%M-%Y')); 42 | create table M_mark( 43 | studentcode varchar(5) references Student(studentcode), 44 | subjectcode varchar(5) references 45 | Subject(subjectcode), 46 | mark int, 47 | primary key(studentcode,subjectcode)); 48 | insert into M_mark values(1,501,40); 49 | insert into M_mark values(1,502,70); 50 | insert into M_mark values(1,503,50); 51 | insert into M_mark values(1,504,80); 52 | insert into M_mark values(1,505,40); 53 | insert into M_mark values(1,508,70); 54 | insert into M_mark values(2,501,90); 55 | insert into M_mark values(2,502,89); 56 | insert into M_mark values(2,503,77); 57 | insert into M_mark values(2,504,95); 58 | insert into M_mark values(2,505,74); 59 | insert into M_mark values(2,508,98); 60 | insert into M_mark values(3,501,40); 61 | insert into M_mark values(3,502,43); 62 | insert into M_mark values(3,503,40); 63 | insert into M_mark values(3,504,40); 64 | insert into M_mark values(3,505,40); 65 | insert into M_mark values(3,508,35); 66 | insert into M_mark values(4,501,50); 67 | insert into M_mark values(5,501,60); 68 | insert into M_mark values(6,501,67); 69 | insert into M_mark values(7,501,23); 70 | insert into M_mark values(8,501,43); 71 | insert into M_mark values(9,501,42); 72 | insert into M_mark values(10,505,74); 73 | insert into M_mark values(11,508,98); 74 | insert into M_mark values(12,501,40); 75 | insert into M_mark values(5,502,43); 76 | insert into M_mark values(6,503,40); 77 | insert into M_mark values(7,504,40); 78 | insert into M_mark values(8,505,40); 79 | insert into M_mark values(9,508,35); 80 | insert into M_mark values(10,501,50); 81 | insert into M_mark values(11,501,60); 82 | insert into M_mark values(12,503,67); 83 | insert into M_mark values(5,504,23); 84 | insert into M_mark values(6,504,23); 85 | insert into M_mark values(9,504,1); 86 | insert into M_mark values(10,504,1); 87 | insert into M_mark values(6,502,43); 88 | insert into M_mark values(7,505,42); 89 | select count(*) "No of Faculty = " from Faculty; 90 | select studentname,sum(mark) "Total Mark" from M_mark,Student where Student.studentcode= M_mark.studentcode group by studentname; 91 | select subjectname,round(avg(mark),2) "Average mark" from Subject,M_mark where Subject.subjectcode= M_mark.subjectcode group by subjectname; 92 | select Subject.subjectname, count(studentname) "NO: OF STUDENTS" from Subject,M_mark, Student where Student.studentcode= M_mark.studentcode and M_mark.mark<(40* maxmark)/100 and Subject.subjectCode=M_mark.subjectcode group by 93 | Subject.Subjectname having count(distinct(M_mark.subjectcode))>=1; 94 | select studentname, subjectname,mark,maxmark,round((M_mark.mark/maxmark)*100,2) "Percentage" from Subject, Student, M_mark where mark<(40*maxmark/100) and 95 | Subject.subjectCode = M_mark. subjectcode and Student.studentcode =M_mark.studentcode; 96 | select Faculty.F_name,Subject.subjectname from Faculty, Subject where Faculty.F_code=Subject.faculty_code; 97 | select F_Name from Faculty where (select count(subjectcode) from Subject where Subject.faculty_code=Faculty.F_Code) > 1 group by Faculty.F_Name; 98 | select studentname,subjectname,mark from Student,Subject,M_mark where Student.studentcode=M_mark.studentcode and Subject.subjectcode=M_mark.subjectcode order by mark; 99 | -------------------------------------------------------------------------------- /Expt_6_BuiltIn.sql: -------------------------------------------------------------------------------- 1 | 1. Mathematical Functions 2 | 1.1 ABS 3 | mysql> select ABS(-100) from dual; 4 | +-----------+ 5 | | ABS(-100) | 6 | +-----------+ 7 | | 100 | 8 | +-----------+ 9 | 1.2 FLOOR 10 | mysql> select floor(3.14) from dual; 11 | +-------------+ 12 | | floor(3.14) | 13 | +-------------+ 14 | | 3 | 15 | 16 | 1.3 GREATEST 17 | mysql> select greatest (2,3) from dual; 18 | +----------------+ 19 | | greatest (2,3) | 20 | +----------------+ 21 | | 3 | 22 | +----------------+ 23 | 1.4 LEAST 24 | mysql> select least(6,3,6,3,7,1) from dual; 25 | +--------------------+ 26 | | least(6,3,6,3,7,1) | 27 | +--------------------+ 28 | | 1 | 29 | +--------------------+ 30 | 1.5 LENGTH 31 | mysql> select length('adishankara college') from dual; 32 | +-------------------------------+ 33 | | length('adishankara college') | 34 | +-------------------------------+ 35 | | 19 | 36 | +-------------------------------+ 37 | 38 | mysql> select length (123456) from dual; 39 | +-----------------+ 40 | | length (123456) | 41 | +-----------------+ 42 | | 6 | 43 | +-----------------+ 44 | 1.6 SQRT 45 | mysql> select sqrt(25) from dual; 46 | +----------+ 47 | | sqrt(25) | 48 | +----------+ 49 | | 5 | 50 | 1.7 POWER 51 | mysql> select power(5,2) from dual; 52 | +------------+ 53 | | power(5,2) | 54 | +------------+ 55 | | 25 | 56 | +------------+ 57 | 1.8 ROUND 58 | mysql> select round(3.5) from dual; 59 | +------------+ 60 | | round(3.5) | 61 | +------------+ 62 | | 4 | 63 | +------------+ 64 | mysql> select round(3.4) from dual; 65 | +------------+ 66 | | round(3.4) | 67 | +------------+ 68 | | 3 | 69 | 1.9 SIN, COS, TAN 70 | mysql> select sin(90) from dual; 71 | +--------------------+ 72 | | sin(90) | 73 | +--------------------+ 74 | | 0.8939966636005579 | 75 | +--------------------+ 76 | mysql> select cos(90) from dual; 77 | +---------------------+ 78 | | cos(90) | 79 | +---------------------+ 80 | | -0.4480736161291701 | 81 | +---------------------+ 82 | mysql> select tan(90) from dual; 83 | +--------------------+ 84 | | tan(90) | 85 | +--------------------+ 86 | | -1.995200412208242 | 87 | +--------------------+ 88 | 1.10 LN, LOG 89 | mysql> select ln(2) from dual; 90 | +--------------------+ 91 | | ln(2) | 92 | +--------------------+ 93 | | 0.6931471805599453 | 94 | +--------------------+ 95 | mysql> select log(2) from dual; 96 | +--------------------+ 97 | | log(2) | 98 | +--------------------+ 99 | | 0.6931471805599453 | 100 | +--------------------+ 101 | 1.11 MOD 102 | mysql> select mod(4,3) from dual; 103 | +----------+ 104 | | mod(4,3) | 105 | +----------+ 106 | | 1 | 107 | +----------+ 108 | 1 row in set (0.00 sec) 109 | mysql> select mod(4,2) from dual; 110 | +----------+ 111 | | mod(4,2) | 112 | +----------+ 113 | | 0 | 114 | +----------+ 115 | 1.10 EXP 116 | mysql> select exp(2) from dual; 117 | +------------------+ 118 | | exp(2) | 119 | +------------------+ 120 | | 7.38905609893065 | 121 | +------------------+ 122 | mysql> select exp(-2) from dual; 123 | +--------------------+ 124 | | exp(-2) | 125 | +--------------------+ 126 | | 0.1353352832366127 | 127 | +--------------------+ 128 | mysql> select exp(0) from dual; 129 | +--------+ 130 | | exp(0) | 131 | +--------+ 132 | | 1 | 133 | +--------+ 134 | mysql> select exp(0) from dual; 135 | +--------+ 136 | | exp(0) | 137 | +--------+ 138 | | 1 | 139 | +--------+ 140 | mysql> select exp(1) from dual; 141 | +-------------------+ 142 | | exp(1) | 143 | +-------------------+ 144 | | 2.718281828459045 | 145 | +-------------------+ 146 | 2.Date Functions 147 | mysql> SELECT CURRENT_DATE FROM DUAL; 148 | +--------------+ 149 | | CURRENT_DATE | 150 | +--------------+ 151 | | 2022-11-21 | 152 | +--------------+ 153 | mysql> SELECT EXTRACT(MONTH FROM 154 | CURRENT_DATE ) FROM DUAL; 155 | +-----------------------------------+ 156 | | EXTRACT(MONTH FROM CURRENT_DATE ) | 157 | +-----------------------------------+ 158 | | 11 | 159 | +-----------------------------------+ 160 | mysql> SELECT EXTRACT(YEAR FROM CURRENT_DATE) 161 | FROM DUAL; 162 | +---------------------------------+ 163 | | EXTRACT(YEAR FROM CURRENT_DATE) | 164 | +---------------------------------+ 165 | | 2022 | 166 | +---------------------------------+ 167 | mysql> SELECT CURRENT_DATE FROM DUAL; 168 | +--------------+ 169 | | CURRENT_DATE | 170 | +--------------+ 171 | | 2022-11-21 | 172 | +--------------+ 173 | mysql> SELECT EXTRACT(YEAR FROM CURRENT_DATE) 174 | FROM DUAL; 175 | +---------------------------------+ 176 | | EXTRACT(YEAR FROM CURRENT_DATE) | 177 | +---------------------------------+ 178 | | 2022 | 179 | +---------------------------------+ 180 | mysql> SELECT EXTRACT(DAY FROM CURRENT_DATE) 181 | FROM DUAL; 182 | +--------------------------------+ 183 | | EXTRACT(DAY FROM CURRENT_DATE) | 184 | +--------------------------------+ 185 | | 21 | 186 | +--------------------------------+ 187 | mysql> SELECT EXTRACT(MONTH FROM CURRENT_DATE) 188 | FROM DUAL; 189 | +----------------------------------+ 190 | | EXTRACT(MONTH FROM CURRENT_DATE) | 191 | +----------------------------------+ 192 | | 11 | 193 | 3. String Functions 194 | mysql> select ascii('t') from dual; 195 | +------------+ 196 | | ascii('t') | 197 | +------------+ 198 | | 116 | 199 | +------------+ 200 | mysql> select ascii('a') from dual; 201 | +------------+ 202 | | ascii('a') | 203 | +------------+ 204 | | 97 | 205 | +------------+ 206 | mysql> select ascii('A') from dual; 207 | +------------+ 208 | | ascii('A') | 209 | +------------+ 210 | | 65 | 211 | mysql> SELECT lower('ASIET - COLLEGE OF ENGINEERING..') 212 | from dual; 213 | +-------------------------------------------+ 214 | | lower('ASIET - COLLEGE OF ENGINEERING..') | 215 | +-------------------------------------------+ 216 | | asiet - college of engineering.. | 217 | mysql> SELECT REPLACE('HELLO','H','K') FROM DUAL; 218 | +--------------------------+ 219 | | REPLACE('HELLO','H','K') | 220 | +--------------------------+ 221 | | KELLO | 222 | +--------------------------+ 223 | mysql> SELECT TRIM('A' FROM 'ANACONDA') FROM DUAL; 224 | +---------------------------+ 225 | | TRIM('A' FROM 'ANACONDA') | 226 | +---------------------------+ 227 | | NACOND | 228 | +------------------------ 229 | -------------------------------------------------------------------------------- /dbms_out/Expt_6_BuiltIntxt.txt: -------------------------------------------------------------------------------- 1 | 1. Mathematical Functions 2 | 1.1 ABS 3 | mysql> select ABS(-100) from dual; 4 | +-----------+ 5 | | ABS(-100) | 6 | +-----------+ 7 | | 100 | 8 | +-----------+ 9 | 1.2 FLOOR 10 | mysql> select floor(3.14) from dual; 11 | +-------------+ 12 | | floor(3.14) | 13 | +-------------+ 14 | | 3 | 15 | 16 | 1.3 GREATEST 17 | mysql> select greatest (2,3) from dual; 18 | +----------------+ 19 | | greatest (2,3) | 20 | +----------------+ 21 | | 3 | 22 | +----------------+ 23 | 1.4 LEAST 24 | mysql> select least(6,3,6,3,7,1) from dual; 25 | +--------------------+ 26 | | least(6,3,6,3,7,1) | 27 | +--------------------+ 28 | | 1 | 29 | +--------------------+ 30 | 1.5 LENGTH 31 | mysql> select length('adishankara college') from dual; 32 | +-------------------------------+ 33 | | length('adishankara college') | 34 | +-------------------------------+ 35 | | 19 | 36 | +-------------------------------+ 37 | 38 | mysql> select length (123456) from dual; 39 | +-----------------+ 40 | | length (123456) | 41 | +-----------------+ 42 | | 6 | 43 | +-----------------+ 44 | 1.6 SQRT 45 | mysql> select sqrt(25) from dual; 46 | +----------+ 47 | | sqrt(25) | 48 | +----------+ 49 | | 5 | 50 | 1.7 POWER 51 | mysql> select power(5,2) from dual; 52 | +------------+ 53 | | power(5,2) | 54 | +------------+ 55 | | 25 | 56 | +------------+ 57 | 1.8 ROUND 58 | mysql> select round(3.5) from dual; 59 | +------------+ 60 | | round(3.5) | 61 | +------------+ 62 | | 4 | 63 | +------------+ 64 | mysql> select round(3.4) from dual; 65 | +------------+ 66 | | round(3.4) | 67 | +------------+ 68 | | 3 | 69 | 1.9 SIN, COS, TAN 70 | mysql> select sin(90) from dual; 71 | +--------------------+ 72 | | sin(90) | 73 | +--------------------+ 74 | | 0.8939966636005579 | 75 | +--------------------+ 76 | mysql> select cos(90) from dual; 77 | +---------------------+ 78 | | cos(90) | 79 | +---------------------+ 80 | | -0.4480736161291701 | 81 | +---------------------+ 82 | mysql> select tan(90) from dual; 83 | +--------------------+ 84 | | tan(90) | 85 | +--------------------+ 86 | | -1.995200412208242 | 87 | +--------------------+ 88 | 1.10 LN, LOG 89 | mysql> select ln(2) from dual; 90 | +--------------------+ 91 | | ln(2) | 92 | +--------------------+ 93 | | 0.6931471805599453 | 94 | +--------------------+ 95 | mysql> select log(2) from dual; 96 | +--------------------+ 97 | | log(2) | 98 | +--------------------+ 99 | | 0.6931471805599453 | 100 | +--------------------+ 101 | 1.11 MOD 102 | mysql> select mod(4,3) from dual; 103 | +----------+ 104 | | mod(4,3) | 105 | +----------+ 106 | | 1 | 107 | +----------+ 108 | 1 row in set (0.00 sec) 109 | mysql> select mod(4,2) from dual; 110 | +----------+ 111 | | mod(4,2) | 112 | +----------+ 113 | | 0 | 114 | +----------+ 115 | 1.10 EXP 116 | mysql> select exp(2) from dual; 117 | +------------------+ 118 | | exp(2) | 119 | +------------------+ 120 | | 7.38905609893065 | 121 | +------------------+ 122 | mysql> select exp(-2) from dual; 123 | +--------------------+ 124 | | exp(-2) | 125 | +--------------------+ 126 | | 0.1353352832366127 | 127 | +--------------------+ 128 | mysql> select exp(0) from dual; 129 | +--------+ 130 | | exp(0) | 131 | +--------+ 132 | | 1 | 133 | +--------+ 134 | mysql> select exp(0) from dual; 135 | +--------+ 136 | | exp(0) | 137 | +--------+ 138 | | 1 | 139 | +--------+ 140 | mysql> select exp(1) from dual; 141 | +-------------------+ 142 | | exp(1) | 143 | +-------------------+ 144 | | 2.718281828459045 | 145 | +-------------------+ 146 | 2.Date Functions 147 | mysql> SELECT CURRENT_DATE FROM DUAL; 148 | +--------------+ 149 | | CURRENT_DATE | 150 | +--------------+ 151 | | 2022-11-21 | 152 | +--------------+ 153 | mysql> SELECT EXTRACT(MONTH FROM 154 | CURRENT_DATE ) FROM DUAL; 155 | +-----------------------------------+ 156 | | EXTRACT(MONTH FROM CURRENT_DATE ) | 157 | +-----------------------------------+ 158 | | 11 | 159 | +-----------------------------------+ 160 | mysql> SELECT EXTRACT(YEAR FROM CURRENT_DATE) 161 | FROM DUAL; 162 | +---------------------------------+ 163 | | EXTRACT(YEAR FROM CURRENT_DATE) | 164 | +---------------------------------+ 165 | | 2022 | 166 | +---------------------------------+ 167 | mysql> SELECT CURRENT_DATE FROM DUAL; 168 | +--------------+ 169 | | CURRENT_DATE | 170 | +--------------+ 171 | | 2022-11-21 | 172 | +--------------+ 173 | mysql> SELECT EXTRACT(YEAR FROM CURRENT_DATE) 174 | FROM DUAL; 175 | +---------------------------------+ 176 | | EXTRACT(YEAR FROM CURRENT_DATE) | 177 | +---------------------------------+ 178 | | 2022 | 179 | +---------------------------------+ 180 | mysql> SELECT EXTRACT(DAY FROM CURRENT_DATE) 181 | FROM DUAL; 182 | +--------------------------------+ 183 | | EXTRACT(DAY FROM CURRENT_DATE) | 184 | +--------------------------------+ 185 | | 21 | 186 | +--------------------------------+ 187 | mysql> SELECT EXTRACT(MONTH FROM CURRENT_DATE) 188 | FROM DUAL; 189 | +----------------------------------+ 190 | | EXTRACT(MONTH FROM CURRENT_DATE) | 191 | +----------------------------------+ 192 | | 11 | 193 | 3. String Functions 194 | mysql> select ascii('t') from dual; 195 | +------------+ 196 | | ascii('t') | 197 | +------------+ 198 | | 116 | 199 | +------------+ 200 | mysql> select ascii('a') from dual; 201 | +------------+ 202 | | ascii('a') | 203 | +------------+ 204 | | 97 | 205 | +------------+ 206 | mysql> select ascii('A') from dual; 207 | +------------+ 208 | | ascii('A') | 209 | +------------+ 210 | | 65 | 211 | mysql> SELECT lower('ASIET - COLLEGE OF ENGINEERING..') 212 | from dual; 213 | +-------------------------------------------+ 214 | | lower('ASIET - COLLEGE OF ENGINEERING..') | 215 | +-------------------------------------------+ 216 | | asiet - college of engineering.. | 217 | mysql> SELECT REPLACE('HELLO','H','K') FROM DUAL; 218 | +--------------------------+ 219 | | REPLACE('HELLO','H','K') | 220 | +--------------------------+ 221 | | KELLO | 222 | +--------------------------+ 223 | mysql> SELECT TRIM('A' FROM 'ANACONDA') FROM DUAL; 224 | +---------------------------+ 225 | | TRIM('A' FROM 'ANACONDA') | 226 | +---------------------------+ 227 | | NACOND | 228 | +------------------------ 229 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 |

# S5-DATABASE-MANAGEMENT-SYSTEMS-LAB

2 | 3 | This course helps the learners to get practical exposure on database creation, SQL queries creation, transaction processing and NoSQL & MongoDB based operations. The course enables the students to create, manage and administer the databases, develop necessary tools for the design and development of the databases, and to understand emerging technologies to handle Big Data. 4 | 5 |

Syllabus

6 | 1. Design a database schema for an application with ER diagram from a problem description 7 | **. 8 |
2. Creation, modification, configuration, and deletion of databases using UI and SQL 9 | Commands **. 10 |
3. Creation of database schema - DDL (create tables, set constraints, enforce relationships, 11 | create indices, delete and modify tables). Export ER diagram from the database and verify 12 | relationships** (with the ER diagram designed in step 1). 13 |
4. Database initialization - Data insert, Data import to a database (bulk import using UI and 14 | SQL Commands)**. 15 |
5. Practice SQL commands for DML (insertion, updating, altering, deletion of data, and 16 | viewing/querying records
b. You may use a MongoDB local installation or cloud MongoDB services like 17 | MongoDB Atlas for this exercise 18 |
c. For documentation: Refer: https://docs.mongodb.com/manual/introduction/ 19 |
6. Implementation of built-in functions in RDBMS**. 20 |
7. Implementation of various aggregate functions in SQL**. 21 |
8. Implementation of Order By, Group By & Having clause **. 22 |
9. Implementation of set operators nested queries, and join queries **. 23 |
10. Implementation of queries using temp tables. 24 |
11. Practice of SQL TCL commands like Rollback, Commit, Savepoint **. 25 |
12. Practice of SQL DCL commands for granting and revoking user privileges **. 26 |
13. Practice of SQL commands for creation of views and assertions ** . 27 |
14. Implementation of various control structures like IF-THEN, IF-THEN-ELSE, IF-THEN- 28 |

ELSIF, CASE, WHILE using PL/SQL **. 29 |
15. Creation of Procedures, Triggers and Functions**. 30 |
16. Creation of Packages **. 31 |
17. Creation of Cursors **. 32 |
18. Creation of PL/SQL blocks for exception handling **. 33 |
19. Database backup and restore using commands. 34 |
20. Query analysis using Query Plan/Show Plan. 35 |
21. Familiarization of NoSQL Databases and CRUD operations**. 36 |
22. Design a database application using any front end tool for any problem selected. The 37 | application constructed should have five or more tables**. 38 |

** mandatory 39 | 40 |

EXERCISES

41 |
1. Create a normalized database design with proper tables, columns, column types, and 42 | constraints 43 |
2. Create an ER diagram for the above database design. 44 |
3. Write SQL commands to 45 |

a. Create a database by name Library. Drop the database and re-create it. 46 |
b. Create DDL statements and create the tables and constraints (from the design) in the 47 | database created in step-a (Library) 48 |

Notes: [ Create a script file and execute it. Create the script file in such a way that,,if the 49 | table exists, drop the tables and recreate )] 50 |
c. Create and execute DROP TABLE command in tables with and without FOREIGN 51 | KEY constraints. 52 |
d. Create and execute ALTER TABLE command in tables with data and without data. 53 |
e. Create and execute SQL commands to build indices on Member_Id and Book_Id on 54 | table Book_Issue. 55 |
f. Create and execute GRANT/REVOKE commands on tables. 56 |
g. Create and execute SQL commands to insert data into each of the tables designed 57 |
h. Learn and execute bulk import of data to tables from CSV files (insert 1000 records of 58 | books into the BOOK table from a CSV file). 59 |
i. Create and execute UPDATE/DELETE commands on tables. Try to update/delete 60 | rows with Primary and Foreign Keys. Try bulk updates or deletes using SQL 61 |
UPDATE statement 62 |
4. Write SQLQuery to retrieve the following information 63 |
a Get the number of books written by a given author 64 |
b. Get the list of publishers and the number of books published by each publisher 65 |
c. Get the names of authors who jointly wrote more than one book. 66 |
d. Get the list of books that are issued but not returned 67 |
e. Get the list of students who reads only ‘Malayalam’ books 68 |
f. Get the total fine collected for the current month and current quarter 69 |
g. Get the list of students who have overdue (not returned the books even on due date) 70 |
h. Calculate the fine (as of today) to be collected from each overdue book. 71 |

i. Members who joined after Jan 1 2021 but has not taken any books 72 |
5. Book return should insert an entry into the Book_Return table and also update the status in 73 | Book_Issue table as ‘Returned’. Create a database TRANSACTION to do this operation 74 | (stored procedure). 75 |
6. Create a database view ‘Available_Books’, which will list out books that are currently 76 | available in the library 77 |
7. Create a database procedure to add, update and delete a book to the Library database (use 78 | parameters). 79 |
8. Use cursors and create a procedure to print Books Issue Register (page wise – 20 rows in a 80 | page) 81 |
9. Create a history table (you may use the same structure without any keys) for the MEMBER 82 | table and copy the original values of the row being updated to the history table using a 83 | TRIGGER. 84 |
10. NoSQL Exercise 85 |
a. Practice Mongo DB CRUD operations. Refer: 86 | https://docs.mongodb.com/manual/crud/ 87 |
b. You may use a MongoDB local installation or cloud MongoDB services like 88 | MongoDB Atlas for this exercise 89 |
c. For documentation: Refer: https://docs.mongodb.com/manual/introduction/ 90 |
11.Application Development Problem examples: 91 |
1) Inventory Control System. 92 |
2) Material Requirement Processing. 93 |
3) Hospital Management System. 94 |
4) Railway Reservation System. 95 |
5) Personal Information System. 96 |
6) Web Based User Identification System. 97 |
7) Timetable Management System. 98 |
8) Hotel Management System. 99 | -------------------------------------------------------------------------------- /dbms_out/tcl command.sql: -------------------------------------------------------------------------------- 1 | 2 | mysql> set autocommit=0 3 | -> ; 4 | Query OK, 0 rows affected (0.00 sec) 5 | 6 | mysql> Experiment 1. Commit 7 | -> SET autocommit=0; 8 | ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'Experiment 1. Commit 9 | SET autocommit=0' at line 1 10 | mysql> create table Emp(EMP_no int primary key, Emp_name varchar(10), Job 11 | -> varchar(10), Hiredata date, Salary float, Comm Float, Depno int 12 | -> references Dept(department_id)); 13 | ERROR 1046 (3D000): No database selected 14 | mysql> show databases; 15 | +--------------------+ 16 | | Database | 17 | +--------------------+ 18 | | information_schema | 19 | | mysql | 20 | | performance_schema | 21 | | sys | 22 | | testdcl | 23 | +--------------------+ 24 | 5 rows in set (0.01 sec) 25 | 26 | mysql> create database db; 27 | Query OK, 1 row affected (0.17 sec) 28 | 29 | mysql> use db; 30 | Database changed 31 | mysql> Experiment 1. Commit 32 | -> SET autocommit=0; 33 | ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'Experiment 1. Commit 34 | SET autocommit=0' at line 1 35 | mysql> create table Emp(EMP_no int primary key, Emp_name varchar(10), Job 36 | -> varchar(10), Hiredata date, Salary float, Comm Float, Depno int 37 | -> references Dept(department_id)); 38 | Query OK, 0 rows affected (0.48 sec) 39 | 40 | mysql> INSERT INTO Emp VALUES(1,'Steven', 'Marketing', STR_TO_DATE('06- 41 | '> jan-1995', '%d-%M-%Y'),24000, NULL,2); 42 | Query OK, 1 row affected (0.00 sec) 43 | 44 | mysql> INSERT INTO Emp VALUES(2,'Neena', 'FI_ACCOUNT', 45 | -> STR_TO_DATE('06-feb-1987', '%d-%M-%Y'),34000, NULL,1); 46 | Query OK, 1 row affected (0.00 sec) 47 | 48 | mysql> INSERT INTO Emp VALUES(3,'Lex', 'FI_MGR', STR_TO_DATE('06-jan- 49 | '> 1980', '%d-%M-%Y'),240000, NULL,1); 50 | Query OK, 1 row affected (0.00 sec) 51 | 52 | mysql> INSERT INTO Emp VALUES(4,'Alexander', 'Sa_Rep', STR_TO_DATE('06- 53 | '> jun-1987', '%d-%M-%Y'),20000, NULL,4); 54 | Query OK, 1 row affected (0.00 sec) 55 | 56 | mysql> INSERT INTO Emp VALUES(5,'Bruce', 'IT_PROG',STR_TO_DATE('06- 57 | '> jul-1990', '%d-%M-%Y'),24000, NULL,4); 58 | Query OK, 1 row affected (0.00 sec) 59 | 60 | mysql> start transaction ; 61 | Query OK, 0 rows affected (0.11 sec) 62 | 63 | mysql> INSERT INTO Emp VALUES(6,'Jack','Clerk', STR_TO_DATE('06-aug- 64 | '> 1980', '%d-%M-%Y'),240000, NULL,5); 65 | Query OK, 1 row affected (0.00 sec) 66 | 67 | mysql> UPDATE Emp SET Job = 'FI_MGR' where EMP_no = 6; 68 | Query OK, 1 row affected (0.03 sec) 69 | Rows matched: 1 Changed: 1 Warnings: 0 70 | 71 | mysql> commit; 72 | Query OK, 0 rows affected (0.09 sec) 73 | 74 | mysql> select * from Emp; 75 | +--------+-----------+------------+------------+--------+------+-------+ 76 | | EMP_no | Emp_name | Job | Hiredata | Salary | Comm | Depno | 77 | +--------+-----------+------------+------------+--------+------+-------+ 78 | | 1 | Steven | Marketing | 1995-01-06 | 24000 | NULL | 2 | 79 | | 2 | Neena | FI_ACCOUNT | 1987-02-06 | 34000 | NULL | 1 | 80 | | 3 | Lex | FI_MGR | 1980-01-06 | 240000 | NULL | 1 | 81 | | 4 | Alexander | Sa_Rep | 1987-06-06 | 20000 | NULL | 4 | 82 | | 5 | Bruce | IT_PROG | 1990-07-06 | 24000 | NULL | 4 | 83 | | 6 | Jack | FI_MGR | 1980-08-06 | 240000 | NULL | 5 | 84 | +--------+-----------+------------+------------+--------+------+-------+ 85 | 6 rows in set (0.00 sec) 86 | 87 | mysql> SET autocommit=0; 88 | Query OK, 0 rows affected (0.00 sec) 89 | 90 | mysql> start transaction; 91 | Query OK, 0 rows affected (0.00 sec) 92 | 93 | mysql> update Emp set Salary = Salary + 1000 where EMP_no = 6; 94 | Query OK, 1 row affected (0.00 sec) 95 | Rows matched: 1 Changed: 1 Warnings: 0 96 | 97 | mysql> # create savepoint 98 | mysql> SAVEPOINT emp_save_point1; 99 | Query OK, 0 rows affected (0.00 sec) 100 | 101 | mysql> INSERT INTO Emp VALUES(7,'Girish','Clerk', STR_TO_DATE('06-aug- 102 | '> 1980', '%d-%M-%Y'),240000, NULL,5); 103 | Query OK, 1 row affected (0.00 sec) 104 | 105 | mysql> # verify Girish is added ; 106 | mysql> select * from Emp; 107 | +--------+-----------+------------+------------+--------+------+-------+ 108 | | EMP_no | Emp_name | Job | Hiredata | Salary | Comm | Depno | 109 | +--------+-----------+------------+------------+--------+------+-------+ 110 | | 1 | Steven | Marketing | 1995-01-06 | 24000 | NULL | 2 | 111 | | 2 | Neena | FI_ACCOUNT | 1987-02-06 | 34000 | NULL | 1 | 112 | | 3 | Lex | FI_MGR | 1980-01-06 | 240000 | NULL | 1 | 113 | | 4 | Alexander | Sa_Rep | 1987-06-06 | 20000 | NULL | 4 | 114 | | 5 | Bruce | IT_PROG | 1990-07-06 | 24000 | NULL | 4 | 115 | | 6 | Jack | FI_MGR | 1980-08-06 | 241000 | NULL | 5 | 116 | | 7 | Girish | Clerk | 1980-08-06 | 240000 | NULL | 5 | 117 | +--------+-----------+------------+------------+--------+------+-------+ 118 | 7 rows in set (0.00 sec) 119 | 120 | mysql> # rollback 121 | mysql> ROLLBACK TO SAVEPOINT emp_save_point1; 122 | Query OK, 0 rows affected (0.00 sec) 123 | 124 | mysql> #commit transaction 125 | mysql> commit; 126 | Query OK, 0 rows affected (0.09 sec) 127 | 128 | mysql> # verify Girish is removed on rollback; 129 | mysql> select * from Emp; 130 | +--------+-----------+------------+------------+--------+------+-------+ 131 | | EMP_no | Emp_name | Job | Hiredata | Salary | Comm | Depno | 132 | +--------+-----------+------------+------------+--------+------+-------+ 133 | | 1 | Steven | Marketing | 1995-01-06 | 24000 | NULL | 2 | 134 | | 2 | Neena | FI_ACCOUNT | 1987-02-06 | 34000 | NULL | 1 | 135 | | 3 | Lex | FI_MGR | 1980-01-06 | 240000 | NULL | 1 | 136 | | 4 | Alexander | Sa_Rep | 1987-06-06 | 20000 | NULL | 4 | 137 | | 5 | Bruce | IT_PROG | 1990-07-06 | 24000 | NULL | 4 | 138 | | 6 | Jack | FI_MGR | 1980-08-06 | 241000 | NULL | 5 | 139 | +--------+-----------+------------+------------+--------+------+-------+ 140 | 6 rows in set (0.00 sec) 141 | 142 | -------------------------------------------------------------------------------- /Employee.sql: -------------------------------------------------------------------------------- 1 | mysql -u root -p 2 | 3 | SHOW DATABASES; 4 | CREATE DATABASE db; 5 | 6 | USE db; 7 | 8 | CREATE TABLE employee( fname VARCHAR(25) NOT NULL, 9 | -> minit CHAR, 10 | -> lname VARCHAR(25) NOT NULL, 11 | -> ssn CHAR(9) NOT NULL, 12 | -> bdate DATE, 13 | -> address VARCHAR(30), 14 | -> sex CHAR, 15 | -> salary DECIMAL(10.2), 16 | -> super_ssn CHAR(9), 17 | -> dno INT NOT NULL, 18 | -> PRIMARY KEY (ssn), 19 | -> FOREIGN KEY (super_ssn)REFERENCES employee(ssn) 20 | -> ); 21 | show tables; 22 | 23 | describe employee; 24 | 25 | CREATE TABLE department( 26 | -> dname VARCHAR(15) NOT NULL, 27 | -> dnumber INT NOT NULL, 28 | -> mgr_ssn CHAR(9) NOT NULL, 29 | -> mgr_start_date DATE, 30 | -> PRIMARY KEY (dnumber), 31 | -> UNIQUE (dname), 32 | -> FOREIGN KEY (mgr_ssn)REFERENCES employee(ssn) 33 | -> ); 34 | 35 | describe department; 36 | 37 | ALTER TABLE employee ADD email VARCHAR(25); 38 | 39 | describe employee; 40 | ALTER TABLE employee MODIFY email VARCHAR(10) NOT NULL; 41 | show columns from employee; 42 | 43 | alter table department add deptlocation varchar(20); 44 | 45 | alter table employee add foreign key(dno) references departmenttable(dnumber); 46 | 47 | alter table departmenttable change deptlocation location varchar(20); 48 | 49 | create table project( 50 | -> pname varchar(15) not null, 51 | -> pnumber int not null auto_increment, 52 | -> plocation varchar(15), 53 | -> dnum int not null, 54 | -> primary key(pnumber), 55 | -> unique(pname), 56 | -> foreign key(dnum) references departmenttable(dnumber) 57 | -> ); 58 | 59 | alter table departmenttable drop column location; 60 | 61 | SET FOREIGN_KEY_CHECKS=0; 62 | 63 | insert into departmenttable values('Research','1','ssn2001','2021-05-30'); 64 | insert into departmenttable values('Admin','2','ssn3001','2021-06-30'); 65 | insert into departmenttable values('HQ','3','ssn4001','2021-07-30'); 66 | 67 | alter table employee drop column email; 68 | 69 | insert into employee values ('John','B','Smith','ssn2001','1970-05-30','New York','M','100000','ssn1001',1); 70 | mysql> insert into employee values ('Ramesh','A','Narayan','ssn3001','1971-05-30','New Delhi','M','100000','ssn 71 | Query OK, 1 row affected (0.05 sec) 72 | 73 | mysql> insert into employee values ('Sam','C','Eric','ssn4001','1972-05-30','Paris','M','100000','ssn4001',3); 74 | Query OK, 1 row affected (0.04 sec) 75 | mysql> insert into employee values ('James','D','Joe','ssn2002','1970-05-30','New York','M','50000','ssn2001',4); 76 | Query OK, 1 row affected (0.03 sec) 77 | 78 | mysql> insert into employee values ('Alice','E','Alice','ssn2003','1970-05-30','New York','M','100000','ssn1001 79 | Query OK, 1 row affected (0.04 sec) 80 | 81 | mysql> insert into employee values ('Bob','F','Bob','ssn2004','1970-05-30','New York','M','100000','ssn1001',1); 82 | Query OK, 1 row affected (0.04 sec) 83 | 84 | mysql> insert into employee values ('Ram','M','Ram','ssn2005','1970-05-30','New York','M','100000','ssn1001',1); 85 | Query OK, 1 row affected (0.04 sec) 86 | 87 | mysql> select * from employee; +--------+-------+---------+---------+------------+-----------+------+--------+-----------+-----+ 88 | | fname | minit | lname | ssn | bdate | address | sex | salary | super_ssn | dno | 89 | +--------+-------+---------+---------+------------+-----------+------+--------+-----------+-----+ 90 | | John | B | Smith | ssn2001 | 1970-05-30 | New York | M | 100000 | ssn1001 | 1 | 91 | | James | D | Joe | ssn2002 | 1970-05-30 | New York | M | 50000 | ssn2001 | 4 | 92 | | Alice | E | Alice | ssn2003 | 1970-05-30 | New York | M | 100000 | ssn1001 | 5 | 93 | | Bob | F | Bob | ssn2004 | 1970-05-30 | New York | M | 100000 | ssn1001 | 1 | 94 | | Ram | M | Ram | ssn2005 | 1970-05-30 | New York | M | 100000 | ssn1001 | 1 | 95 | | Ramesh | A | Narayan | ssn3001 | 1971-05-30 | New Delhi | M | 100000 | ssn3001 | 2 | 96 | | Sam | C | Eric | ssn4001 | 1972-05-30 | Paris | M | 100000 | ssn4001 | 3 | 97 | +--------+-------+---------+---------+------------+-----------+------+--------+-----------+-----+ 98 | 99 | insert into project values ('ProductX','1001','Cochin','1'); 100 | Query OK, 1 row affected (0.03 sec) 101 | 102 | mysql> insert into project (pname, plocation, dnum) values ('Computerization','Cochin','1'); 103 | Query OK, 1 row affected (0.04 sec) 104 | 105 | mysql> select * from project; 106 | +-----------------+---------+-----------+------+ 107 | | pname | pnumber | plocation | dnum | 108 | +-----------------+---------+-----------+------+ 109 | | ProductX | 1001 | Cochin | 1 | 110 | | Computerization | 1002 | Cochin | 1 | 111 | +-----------------+---------+-----------+------+ 112 | 2 rows in set (0.00 sec) 113 | 114 | mysql> insert into project (pname, plocation, dnum) values ('Jobfest','Cochin','3'); 115 | Query OK, 1 row affected (0.05 sec) 116 | 117 | mysql> select dno, count(*), avg(salary), from employee group by dno; 118 | ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'from employee group by dno' at line 1 119 | mysql> select dno, count(*), avg(salary) from employee group by dno; 120 | +-----+----------+---------------+ 121 | | dno | count(*) | avg(salary) | 122 | +-----+----------+---------------+ 123 | | 1 | 5 | 90000.000000 | 124 | | 2 | 1 | 100000.000000 | 125 | | 3 | 1 | 100000.000000 | 126 | +-----+----------+---------------+ 127 | 3 rows in set (0.03 sec) 128 | 129 | mysql> select fname , minit, lname, Address from employee where dno = (select dnnumber from departmenttable where dname='Research'); 130 | ERROR 1054 (42S22): Unknown column 'dnnumber' in 'field list' 131 | mysql> select fname , minit, lname, Address from employee where dno = (select dnumber from departmenttable where dname='Research'); 132 | +-------+-------+-------+----------+ 133 | | fname | minit | lname | Address | 134 | +-------+-------+-------+----------+ 135 | | John | B | Smith | New York | 136 | | James | D | Joe | New York | 137 | | Alice | E | Alice | New York | 138 | | Bob | F | Bob | New York | 139 | | Ram | M | Ram | New York | 140 | +-------+-------+-------+----------+ 141 | -------------------------------------------------------------------------------- /student.sql: -------------------------------------------------------------------------------- 1 | mysql -u root -p 2 | Enter password: 3 | Welcome to the MySQL monitor. Commands end with ; or \g. 4 | Your MySQL connection id is 6 5 | Server version: 5.7.40-0ubuntu0.18.04.1 (Ubuntu) 6 | 7 | Copyright (c) 2000, 2022, Oracle and/or its affiliates. 8 | 9 | Oracle is a registered trademark of Oracle Corporation and/or its 10 | affiliates. Other names may be trademarks of their respective 11 | owners. 12 | 13 | Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. 14 | 15 | mysql> create database stud; 16 | Query OK, 1 row affected (0.00 sec) 17 | 18 | mysql> use stud; 19 | Database changed 20 | mysql> create table student( 21 | -> rollno int primary key auto_increment, 22 | -> name varchar(25) not null, 23 | -> email varchar(25), 24 | -> phonenum int); 25 | Query OK, 0 rows affected (0.24 sec) 26 | 27 | mysql> describe student; 28 | +----------+-------------+------+-----+---------+----------------+ 29 | | Field | Type | Null | Key | Default | Extra | 30 | +----------+-------------+------+-----+---------+----------------+ 31 | | rollno | int(11) | NO | PRI | NULL | auto_increment | 32 | | name | varchar(25) | NO | | NULL | | 33 | | email | varchar(25) | YES | | NULL | | 34 | | phonenum | int(11) | YES | | NULL | | 35 | +----------+-------------+------+-----+---------+----------------+ 36 | 4 rows in set (0.00 sec) 37 | 38 | mysql> create table subject( 39 | -> subjectid int primary key, 40 | -> subjectname varchar(25) not null); 41 | Query OK, 0 rows affected (0.24 sec) 42 | 43 | mysql> describe student; 44 | +----------+-------------+------+-----+---------+----------------+ 45 | | Field | Type | Null | Key | Default | Extra | 46 | +----------+-------------+------+-----+---------+----------------+ 47 | | rollno | int(11) | NO | PRI | NULL | auto_increment | 48 | | name | varchar(25) | NO | | NULL | | 49 | | email | varchar(25) | YES | | NULL | | 50 | | phonenum | int(11) | YES | | NULL | | 51 | +----------+-------------+------+-----+---------+----------------+ 52 | 4 rows in set (0.00 sec) 53 | 54 | mysql> describe subject; 55 | +-------------+-------------+------+-----+---------+-------+ 56 | | Field | Type | Null | Key | Default | Extra | 57 | +-------------+-------------+------+-----+---------+-------+ 58 | | subjectid | int(11) | NO | PRI | NULL | | 59 | | subjectname | varchar(25) | NO | | NULL | | 60 | +-------------+-------------+------+-----+---------+-------+ 61 | 2 rows in set (0.00 sec) 62 | 63 | mysql> create table mark( 64 | -> sid int, 65 | -> rno int 66 | -> , 67 | -> totalmarks int, 68 | -> primary key(sid,rno)); 69 | Query OK, 0 rows affected (0.24 sec) 70 | 71 | mysql> describe marks; 72 | ERROR 1146 (42S02): Table 'stud.marks' doesn't exist 73 | mysql> describe mark; 74 | +------------+---------+------+-----+---------+-------+ 75 | | Field | Type | Null | Key | Default | Extra | 76 | +------------+---------+------+-----+---------+-------+ 77 | | sid | int(11) | NO | PRI | NULL | | 78 | | rno | int(11) | NO | PRI | NULL | | 79 | | totalmarks | int(11) | YES | | NULL | | 80 | +------------+---------+------+-----+---------+-------+ 81 | 3 rows in set (0.01 sec) 82 | 83 | mysql> 84 | mysql> alter table mark add foreign key(rno) references student(rollno); 85 | Query OK, 0 rows affected (0.63 sec) 86 | Records: 0 Duplicates: 0 Warnings: 0 87 | 88 | mysql> alter table mark add foreign key(sid) references subject(subjectid); 89 | Query OK, 0 rows affected (0.59 sec) 90 | Records: 0 Duplicates: 0 Warnings: 0 91 | 92 | mysql> describe mark; +------------+---------+------+-----+---------+-------+ 93 | | Field | Type | Null | Key | Default | Extra | 94 | +------------+---------+------+-----+---------+-------+ 95 | | sid | int(11) | NO | PRI | NULL | | 96 | | rno | int(11) | NO | PRI | NULL | | 97 | | totalmarks | int(11) | YES | | NULL | | 98 | +------------+---------+------+-----+---------+-------+ 99 | 3 rows in set (0.00 sec) 100 | 101 | mysql> describe student; +----------+-------------+------+-----+---------+----------------+ 102 | | Field | Type | Null | Key | Default | Extra | 103 | +----------+-------------+------+-----+---------+----------------+ 104 | | rollno | int(11) | NO | PRI | NULL | auto_increment | 105 | | name | varchar(25) | NO | | NULL | | 106 | | email | varchar(25) | YES | | NULL | | 107 | | phonenum | int(11) | YES | | NULL | | 108 | +----------+-------------+------+-----+---------+----------------+ 109 | 4 rows in set (0.00 sec) 110 | 111 | mysql> alter table student add address varchar(25); 112 | Query OK, 0 rows affected (0.41 sec) 113 | Records: 0 Duplicates: 0 Warnings: 0 114 | 115 | mysql> describe student; 116 | +----------+-------------+------+-----+---------+----------------+ 117 | | Field | Type | Null | Key | Default | Extra | 118 | +----------+-------------+------+-----+---------+----------------+ 119 | | rollno | int(11) | NO | PRI | NULL | auto_increment | 120 | | name | varchar(25) | NO | | NULL | | 121 | | email | varchar(25) | YES | | NULL | | 122 | | phonenum | int(11) | YES | | NULL | | 123 | | address | varchar(25) | YES | | NULL | | 124 | +----------+-------------+------+-----+---------+----------------+ 125 | 5 rows in set (0.00 sec) 126 | 127 | mysql> alter table mark modify totalmarks decimal(10,4); 128 | Query OK, 0 rows affected (0.61 sec) 129 | Records: 0 Duplicates: 0 Warnings: 0 130 | 131 | mysql> desc mark; 132 | +------------+---------------+------+-----+---------+-------+ 133 | | Field | Type | Null | Key | Default | Extra | 134 | +------------+---------------+------+-----+---------+-------+ 135 | | sid | int(11) | NO | PRI | NULL | | 136 | | rno | int(11) | NO | PRI | NULL | | 137 | | totalmarks | decimal(10,4) | YES | | NULL | | 138 | +------------+---------------+------+-----+---------+-------+ 139 | 3 rows in set (0.00 sec) 140 | 141 | mysql> describe mark; 142 | +------------+---------------+------+-----+---------+-------+ 143 | | Field | Type | Null | Key | Default | Extra | 144 | +------------+---------------+------+-----+---------+-------+ 145 | | sid | int(11) | NO | PRI | NULL | | 146 | | rno | int(11) | NO | PRI | NULL | | 147 | | totalmarks | decimal(10,4) | YES | | NULL | | 148 | +------------+---------------+------+-----+---------+-------+ 149 | 3 rows in set (0.00 sec) 150 | -------------------------------------------------------------------------------- /dbms_out/dcl command.sql: -------------------------------------------------------------------------------- 1 | ysql> select user(); 2 | +----------------+ 3 | | user() | 4 | +----------------+ 5 | | root@localhost | 6 | +----------------+ 7 | 1 row in set (0.04 sec) 8 | 9 | mysql> create database testdcl; 10 | Query OK, 1 row affected (0.28 sec) 11 | 12 | mysql> use testdcl; 13 | Database changed 14 | mysql> create table Emp(EMP_no int primary key, Emp_name varchar(10), Job 15 | -> varchar(10), Hiredata date, Salary float, Comm Float, Depno int ); 16 | Query OK, 0 rows affected (0.53 sec) 17 | 18 | mysql> show tables; 19 | +-------------------+ 20 | | Tables_in_testdcl | 21 | +-------------------+ 22 | | Emp | 23 | +-------------------+ 24 | 1 row in set (0.00 sec) 25 | 26 | mysql> # create a new user 27 | mysql> create user 'user_test@localhost' identified by "Test@123"; 28 | Query OK, 0 rows affected (0.19 sec) 29 | 30 | mysql> # list users list and verify user_test exists. 31 | mysql> select user from mysql.user; 32 | +---------------------+ 33 | | user | 34 | +---------------------+ 35 | | user_test@localhost | 36 | | debian-sys-maint | 37 | | mysql.infoschema | 38 | | mysql.session | 39 | | mysql.sys | 40 | | root | 41 | +---------------------+ 42 | 6 rows in set (0.00 sec) 43 | 44 | mysql> # check what all privileges allotted by default on creating new user. 45 | mysql> show grants for 'user_test@localhost'; 46 | +-------------------------------------------------+ 47 | | Grants for user_test@localhost@% | 48 | +-------------------------------------------------+ 49 | | GRANT USAGE ON *.* TO `user_test@localhost`@`%` | 50 | +-------------------------------------------------+ 51 | 1 row in set (0.00 sec) 52 | 53 | mysql> # grant all privileges to new user 54 | mysql> grant select on *.* to 'user_test@localhost'; 55 | Query OK, 0 rows affected (0.07 sec) 56 | 57 | mysql> # grant privileges to a specific table for (insert) 58 | mysql> grant insert on testdcl.Emp to 'user_test@localhost'; 59 | Query OK, 0 rows affected (0.09 sec) 60 | 61 | mysql> # grant update privilege to a specific attribute on a table 62 | mysql> grant update (Emp_name) on testdcl.Emp to 'user_test@localhost'; 63 | Query OK, 0 rows affected (0.14 sec) 64 | 65 | mysql> #show grants for new user 66 | mysql> show grants for 'user_test@localhost'; 67 | +-----------------------------------------------------------------------------------+ 68 | | Grants for user_test@localhost@% | 69 | +-----------------------------------------------------------------------------------+ 70 | | GRANT SELECT ON *.* TO `user_test@localhost`@`%` | 71 | | GRANT INSERT, UPDATE (`Emp_name`) ON `testdcl`.`Emp` TO `user_test@localhost`@`%` | 72 | +-----------------------------------------------------------------------------------+ 73 | 2 rows in set (0.00 sec) 74 | 75 | mysql> #Verify 76 | mysql> # logoff mysql and login using below command, it will prompt for 77 | mysql> password and enter the same password. 78 | -> ^C 79 | mysql> mysql -u 'user_test@localhost' -p 80 | -> # verify user is user_test 81 | -> slect user(); 82 | ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'mysql -u 'user_test@localhost' -p 83 | 84 | slect user()' at line 1 85 | mysql> # list the grants 86 | mysql> show grants for 'user_test@localhost'; 87 | +-----------------------------------------------------------------------------------+ 88 | | Grants for user_test@localhost@% | 89 | +-----------------------------------------------------------------------------------+ 90 | | GRANT SELECT ON *.* TO `user_test@localhost`@`%` | 91 | | GRANT INSERT, UPDATE (`Emp_name`) ON `testdcl`.`Emp` TO `user_test@localhost`@`%` | 92 | +-----------------------------------------------------------------------------------+ 93 | 2 rows in set (0.00 sec) 94 | 95 | mysql> # select databse and list tables 96 | mysql> show databases; 97 | +--------------------+ 98 | | Database | 99 | +--------------------+ 100 | | information_schema | 101 | | mysql | 102 | | performance_schema | 103 | | sys | 104 | | testdcl | 105 | +--------------------+ 106 | 5 rows in set (0.00 sec) 107 | 108 | mysql> use testdcl; 109 | Database changed 110 | mysql> show tables; 111 | +-------------------+ 112 | | Tables_in_testdcl | 113 | +-------------------+ 114 | | Emp | 115 | +-------------------+ 116 | 1 row in set (0.01 sec) 117 | 118 | mysql> # insert into tables 119 | mysql> INSERT INTO Emp VALUES(1,'Steven', 'Marketing', STR_TO_DATE('06- 120 | '> jan-1995', '%d-%M-%Y'),24000, NULL,2); 121 | Query OK, 1 row affected (0.07 sec) 122 | 123 | mysql> select * from Emp; 124 | +--------+----------+-----------+------------+--------+------+-------+ 125 | | EMP_no | Emp_name | Job | Hiredata | Salary | Comm | Depno | 126 | +--------+----------+-----------+------------+--------+------+-------+ 127 | | 1 | Steven | Marketing | 1995-01-06 | 24000 | NULL | 2 | 128 | +--------+----------+-----------+------------+--------+------+-------+ 129 | 1 row in set (0.00 sec) 130 | 131 | mysql> # logoff mysql and login to super user, 132 | mysql> mysql -u root -p 133 | -> #change password of new user 134 | -> alter user 'user_test@localhost' identified by '123456#'; 135 | ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'mysql -u root -p 136 | 137 | alter user 'user_test@localhost' identified by '123456#'' at line 1 138 | mysql> # verify by logging with new password. 139 | mysql> #logoff and login with user_test with newly modified password. 140 | mysql> mysql -u 'user_test@localhost' -p 141 | -> ^DBye 142 | asiet@asiet-ThinkCentre-E73:~$ mysql -u root -p 143 | Enter password: 144 | Welcome to the MySQL monitor. Commands end with ; or \g. 145 | Your MySQL connection id is 9 146 | Server version: 8.0.31-0ubuntu0.20.04.1 (Ubuntu) 147 | 148 | Copyright (c) 2000, 2022, Oracle and/or its affiliates. 149 | 150 | Oracle is a registered trademark of Oracle Corporation and/or its 151 | affiliates. Other names may be trademarks of their respective 152 | owners. 153 | 154 | Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. 155 | 156 | mysql> mysql -u 'user_test@localhost' -p 157 | -> ^DBye 158 | asiet@asiet-ThinkCentre-E73:~$ mysql -u 'user_test@localhost' -p 159 | Enter password: 160 | Welcome to the MySQL monitor. Commands end with ; or \g. 161 | Your MySQL connection id is 10 162 | Server version: 8.0.31-0ubuntu0.20.04.1 (Ubuntu) 163 | 164 | Copyright (c) 2000, 2022, Oracle and/or its affiliates. 165 | 166 | Oracle is a registered trademark of Oracle Corporation and/or its 167 | affiliates. Other names may be trademarks of their respective 168 | owners. 169 | 170 | Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. 171 | 172 | mysql> 173 | -------------------------------------------------------------------------------- /Expt_8_grpby_orderby.sql: -------------------------------------------------------------------------------- 1 | show databases; 2 | create database lab8_tables; 3 | use lab8_tables; 4 | 2. 5 | create table Dept(department_id int primary key , department_name VARCHAR(20) NOT 6 | NULL , manager_id int, loc varchar(10)); 7 | create table Emp(EMP_no int primary key, Emp_name varchar(10), Job varchar(10), 8 | Hiredata date, Salary float, Comm Float, Depno int references Dept(department_id)); 9 | 3. Insert Data into both tables 10 | INSERT INTO Emp VALUES(1,'Steven', 'Marketing', STR_TO_DATE('06-jan-1995', 11 | '%d-%M-%Y'),24000, NULL,2); 12 | INSERT INTO Emp VALUES(2,'Neena', 'FI_ACCOUNT', STR_TO_DATE('06-feb-1987', 13 | '%d-%M-%Y'),34000, NULL,1); 14 | INSERT INTO Emp VALUES(3,'Lex', 'FI_MGR', STR_TO_DATE('06-jan-1980', 15 | '%d-%M-%Y'),240000, NULL,1); 16 | INSERT INTO Emp VALUES(4,'Alexander', 'Sa_Rep', STR_TO_DATE('06-jun-1987', 17 | '%d-%M-%Y'),20000, NULL,4); 18 | INSERT INTO Emp VALUES(5,'Bruce', 'IT_PROG',STR_TO_DATE('06-jul-1990', 19 | '%d-%M-%Y'),24000, NULL,4); 20 | INSERT INTO Emp VALUES(6,'David', 'IT_PROG', STR_TO_DATE('06-sep-1991', 21 | '%d-%M-%Y'),22000, NULL,4); 22 | INSERT INTO Emp VALUES(7,'vipin', 'IT_PROG', STR_TO_DATE('16-nov-1987', 23 | '%d-%M-%Y'),28000, NULL,4); 24 | INSERT INTO Emp VALUES(8,'Diana', 'Pur_Man', STR_TO_DATE('26-jan-1987', 25 | '%d-%M-%Y'),24000, NULL,3); 26 | INSERT INTO Emp VALUES(9,'John', 'FI_ACCOUNT',STR_TO_DATE('1-dec-1992', 27 | '%d-%M-%Y'), 24000, NULL,1); 28 | INSERT INTO Emp VALUES(10,'Ismael', 'CLERK', STR_TO_DATE('29-mar-1994', '%d-%M-%Y'), 29 | 4000, NULL,3); 30 | INSERT INTO Emp VALUES(11,'Mathew', 'CLERK', STR_TO_DATE('12-oct-1992', '%d-%M-%Y'), 31 | 46000, 200,3); 32 | INSERT INTO Emp VALUES(12,'Hayes', 'Marketing', STR_TO_DATE('21-apr-1998', 33 | '%d-%M-%Y'), 14000, 1000,3); 34 | INSERT INTO Emp VALUES(13,'sarun', 'Marketing', STR_TO_DATE('18-may-1993', 35 | '%d-%M-%Y'), 18000, NULL,2); 36 | INSERT INTO Emp VALUES(14,'Henin', 'FI_MGR', STR_TO_DATE('06-aug-1980', '%d-%M-%Y'), 37 | 240000, NULL,1); 38 | INSERT INTO Emp VALUES(15,'Greesh','Clerk', STR_TO_DATE('06-aug-1980', 39 | '%d-%M-%Y'),240000, NULL,5); 40 | INSERT INTO Dept values(1, 'Administration', null, 'Boston'); 41 | INSERT INTO Dept values(2, 'Marketing', null, 'Boston'); 42 | INSERT INTO Dept values(3, 'Purchase', null, 'perryridge'); 43 | INSERT INTO Dept values(4, 'Programming',null, 'Hudson'); 44 | INSERT INTO Dept values(5, 'HR', null, 'Hudson'); 45 | 46 | 4. 47 | alter table Dept add foreign key(manager_id) references Emp(EMP_no); 48 | update Dept set manager_id=2 where department_id=1; 49 | update Dept set manager_id=1 where department_id=2; 50 | update Dept set manager_id=8 where department_id=3; 51 | update Dept set manager_id=7 where department_id=4; 52 | select * from Dept; 53 | 5. Do the following queries 54 | #1 Display the name and salary for all employees whose salary is not in the range of 5000 and 55 | 35000 56 | select Emp_name, Salary from Emp where Salary not between 5000 and 35000; 57 | #2 Display the employee name, job ID, and start date of employees hired between February 58 | # 20, 1990, and May 1, 1998. Order the query in ascending order by start date. 59 | select Emp_name, Salary, Job, Hiredate from Emp where Hiredate between '1990-02-20' and '1998- 60 | 05-01' order by Hiredate; 61 | #3 list the name and salary of employees who earn between 5,000 and12,000, and are in 62 | # department 2 or 4. Label the columns Employee and Monthly Salary,respectively. 63 | SELECT Emp_name 'Employee', Salary 'Monthly Salary', Depno FROM Emp WHERE Salary 64 | BETWEEN 5000 AND 30000 AND Depno IN (2, 4); 65 | #4 Display the name and hire date of every employee who was hired in 1994. 66 | select Emp_name, hiredate from Emp where hiredate like '1994%'; 67 | #5 Display the name, salary, and commission for all employees who earn commissions. Sort 68 | # data in descending order of salary and commissions. 69 | select Emp_name, Salary, Comm from Emp where comm >0 order by Salary desc, Comm desc; 70 | #6 Display the name and job title of all employees who do not have a manager. 71 | select Emp_name, Job from Emp, Dept where manager_id is null and Emp.Depno= 72 | Dept.department_id; 73 | #7 Display the names of all employees where the third letter of the name is an a. 74 | select Emp_name from Emp where Emp_name like '__a%'; 75 | #8 Display the name of all employees who have an a and an e in their name. 76 | select Emp_name from Emp where Emp_name like '%a%' and emp_name like '%e%'; 77 | #9 Display the name, job, and salary for all employees whose job is sales representative or 78 | # stock clerk and whose salary is not equal to 2,0000, 4000, or 7,000. 79 | select Emp_name, Job, Salary from Emp where Job in ('Sa_rep', 'CLERK') and Salary not in (2000, 80 | 4000, 7000); 81 | 82 | #10 Write a query that displays the employee’s names with the first letter capitalized and all 83 | # other letters lowercase and the length of the name for all employees whose name starts with 84 | # J, A, or M. Give each column an appropriate label. Sort the results by the employees’ names. 85 | select upper(Emp_name) "Name", length(emp_name) "Length" from Emp where Emp_name like 86 | 'J%' or Emp_name like 'M%' or Emp_name like 'A%' order by Emp_name; 87 | #11 Write a query to display the name, department number, and department name for al employees. 88 | select Emp.Emp_name, Emp.Depno, Dept.department_name from Emp , Dept where Emp.Depno = 89 | Dept.department_id order by Dept.department_name; 90 | #12 Create a query to display the name and hire date of any employee hired after employee Mathew 91 | select Emp_Name, Hiredate from Emp where ((Hiredate)>any(select Hiredate from Emp where 92 | Emp_Name='Mathew')); 93 | #13 Display the names and hire dates for all employees who were hired before their 94 | # managers, along with their manager’s names and hire dates. Label the columns Employee, 95 | # EmpHired, Manager, and Mgr Hired, respectively. 96 | select E.EMP_no, E.Emp_name 'Employee' , E.Hiredate 'EMP Hire Date' , E.Depno 'dept no', 97 | M.Emp_name 'Manager Name' , M.Hiredate 'Manager Hiredate' 98 | 99 | from Emp E, Dept, Emp M 100 | where Dept.manager_id = M.EMP_no 101 | and E.Depno=Dept.department_id 102 | and E.Hiredate < M.Hiredate; 103 | 104 | #14 Write a query to display the number of people with the same job. 105 | select Job, COUNT(*) 'No: of Jobs' from Emp group by Job; 106 | #15 Display the manager number and the salary of the lowest paid employee for that 107 | # manager.Exclude anyone whose manager is not known. Exclude any groups where the 108 | # minimum salary is less than 6,000. Sort the output in descending order of salary. 109 | select min(Salary) 'MINIMUM SALARY', manager_id, department_name 110 | 111 | from Emp,Dept 112 | 113 | where Emp.depno=Dept.department_id and manager_id is not null group by manager_id, 114 | department_name having MIN(Salary) > 6000 order by 'MINIMUM SALARY' desc; 115 | #16 Write a query to display each department’s name, location, number of employees, and the 116 | # average salary for all employees in that department. Label the columns Name, Location, Number 117 | of People, 118 | #and Salary, respectively. Round the average salary to two decimal places 119 | select D.department_name 'Name', D.loc 'Location ', COUNT(*) 'Number of People', 120 | ROUND(AVG(salary),2) 'Salary' 121 | 122 | from Emp E, Dept D where E.depno = D.department_id group by 123 | 124 | D.department_name, D.loc; 125 | #17 Write a query to display the name and hire date of any employee in the same department as 126 | amit. Exclude JOHN. 127 | select Emp_name, Hiredate from Emp where Depno = (select Depno from Emp where Emp_name = 128 | 'John') and emp_name<>'John'; 129 | 130 | #18 Write a query that displays the employee numbers names of all employees who work in a 131 | # department with any employee whose name contains a u. 132 | select EMP_no, Emp_name, department_name from Emp, Dept where Depno in (select Depno from 133 | Emp where Emp_name like '%u%') and Emp.Depno=Dept.department_id; 134 | #20 display employee name and department name of all employees that work in a department 135 | # that has at least 2 employees. Order the list in alphabetical order first by department name, then 136 | by employee name. 137 | select Emp_name, department_name from Emp, Dept where Emp.depno = Dept.department_id and 138 | Emp.depno in (select Depno from Emp group by Depno having count(*) >3) order by 139 | department_name, Emp_name; 140 | -------------------------------------------------------------------------------- /dbms_out/expt7: -------------------------------------------------------------------------------- 1 | mysql> select * from Faculty; 2 | +--------+-----------+ 3 | | F_Code | F_Name | 4 | +--------+-----------+ 5 | | 101 | Silgy | 6 | | 102 | Bindu | 7 | | 103 | Vidhya | 8 | | 104 | Sangeetha | 9 | | 105 | Jayakumar | 10 | +--------+-----------+ 11 | 5 rows in set (0.00 sec) 12 | 13 | mysql> select * from M_marks; 14 | ERROR 1146 (42S02): Table 'db.M_marks' doesn't exist 15 | mysql> select * from M_mark; 16 | +-------------+-------------+------+ 17 | | studentcode | subjectcode | mark | 18 | +-------------+-------------+------+ 19 | | 1 | 501 | 40 | 20 | | 1 | 502 | 70 | 21 | | 1 | 503 | 50 | 22 | | 1 | 504 | 80 | 23 | | 1 | 505 | 40 | 24 | | 1 | 508 | 70 | 25 | | 10 | 501 | 50 | 26 | | 10 | 504 | 1 | 27 | | 10 | 505 | 74 | 28 | | 11 | 501 | 60 | 29 | | 11 | 508 | 98 | 30 | | 12 | 501 | 40 | 31 | | 12 | 503 | 67 | 32 | | 2 | 501 | 90 | 33 | | 2 | 502 | 89 | 34 | | 2 | 503 | 77 | 35 | | 2 | 504 | 95 | 36 | | 2 | 505 | 74 | 37 | | 2 | 508 | 98 | 38 | | 3 | 501 | 40 | 39 | | 3 | 502 | 43 | 40 | | 3 | 503 | 40 | 41 | | 3 | 504 | 40 | 42 | | 3 | 505 | 40 | 43 | | 3 | 508 | 35 | 44 | | 4 | 501 | 50 | 45 | | 5 | 501 | 60 | 46 | | 5 | 502 | 43 | 47 | | 5 | 504 | 23 | 48 | | 6 | 501 | 67 | 49 | | 6 | 502 | 43 | 50 | | 6 | 503 | 40 | 51 | | 6 | 504 | 23 | 52 | | 7 | 501 | 23 | 53 | | 7 | 504 | 40 | 54 | | 7 | 505 | 42 | 55 | | 8 | 501 | 43 | 56 | | 8 | 505 | 40 | 57 | | 9 | 501 | 42 | 58 | | 9 | 504 | 1 | 59 | | 9 | 508 | 35 | 60 | +-------------+-------------+------+ 61 | 41 rows in set (0.00 sec) 62 | 63 | mysql> select * from Student; 64 | +-------------+-------------+------------+---------------+------------+ 65 | | studentcode | studentname | dob | studentbranch | adate | 66 | +-------------+-------------+------------+---------------+------------+ 67 | | 1 | Amitha | 1987-01-12 | cs | 2000-06-01 | 68 | | 10 | fahan | 1988-03-20 | cs | 2000-06-05 | 69 | | 11 | ginu | 1988-04-13 | cs | 2000-06-10 | 70 | | 12 | hamna | 1985-05-14 | cs | 2000-06-09 | 71 | | 2 | vaidehi | 1988-12-25 | cs | 2000-06-01 | 72 | | 3 | varun | 1988-10-02 | cs | 2000-06-02 | 73 | | 4 | turner | 1988-09-05 | cs | 2000-06-02 | 74 | | 5 | vani | 1988-07-20 | cs | 2000-06-05 | 75 | | 6 | binu | 1988-08-13 | cs | 2000-06-10 | 76 | | 7 | chitra | 1986-11-14 | cs | 2000-06-09 | 77 | | 8 | dona | 1991-12-02 | cs | 2000-06-02 | 78 | | 9 | elana | 1990-02-05 | cs | 2000-06-02 | 79 | +-------------+-------------+------------+---------------+------------+ 80 | 12 rows in set (0.00 sec) 81 | 82 | mysql> select * from Subject; 83 | +-------------+-------------+---------+--------------+ 84 | | subjectcode | subjectname | maxmark | faculty_code | 85 | +-------------+-------------+---------+--------------+ 86 | | 501 | Maths | 150 | 101 | 87 | | 502 | FCA | 100 | 102 | 88 | | 503 | DBMS | 100 | 105 | 89 | | 504 | OS | 75 | 103 | 90 | | 505 | DC | 200 | 104 | 91 | | 508 | DBMSLab | 1001 | 103 | 92 | +-------------+-------------+---------+--------------+ 93 | 6 rows in set (0.00 sec) 94 | 95 | 96 | #Display the number of faculties. 97 | +------------------+ 98 | | No of Faculty = | 99 | +------------------+ 100 | | 5 | 101 | +------------------+ 102 | 103 | #2) Display the total mark for each student. 104 | 105 | +-------------+------------+ 106 | | studentname | Total Mark | 107 | +-------------+------------+ 108 | | Amitha | 350 | 109 | | binu | 173 | 110 | | chitra | 105 | 111 | | dona | 83 | 112 | | elana | 78 | 113 | | fahan | 125 | 114 | | ginu | 158 | 115 | | hamna | 107 | 116 | | turner | 50 | 117 | | vaidehi | 523 | 118 | | vani | 126 | 119 | | varun | 238 | 120 | +-------------+------------+ 121 | 122 | #3) Display the subject,average mark for each subject. 123 | 124 | +-------------+--------------+ 125 | | subjectname | Average mark | 126 | +-------------+--------------+ 127 | | DBMS | 54.80 | 128 | | DBMSLab | 67.20 | 129 | | DC | 51.67 | 130 | | FCA | 57.60 | 131 | | Maths | 50.42 | 132 | | OS | 37.88 | 133 | +-------------+--------------+ 134 | 135 | 4) Display the name of subjects for which atleast one student got 136 | below 40%. 137 | 138 | +-------------+-----------------+ 139 | | subjectname | NO: OF STUDENTS | 140 | +-------------+-----------------+ 141 | | DBMSLab | 5 | 142 | | DC | 6 | 143 | | Maths | 8 | 144 | | OS | 4 | 145 | +-------------+-----------------+ 146 | 147 | Display the name,subject and percentage of mark who got below 148 | 40 %. 149 | 150 | +-------------+-------------+------+---------+------------+ 151 | | studentname | subjectname | mark | maxmark | Percentage | 152 | +-------------+-------------+------+---------+------------+ 153 | | Amitha | Maths | 40 | 150 | 26.67 | 154 | | Amitha | DC | 40 | 200 | 20.00 | 155 | | Amitha | DBMSLab | 70 | 1001 | 6.99 | 156 | | fahan | Maths | 50 | 150 | 33.33 | 157 | | fahan | OS | 1 | 75 | 1.33 | 158 | | fahan | DC | 74 | 200 | 37.00 | 159 | | ginu | DBMSLab | 98 | 1001 | 9.79 | 160 | | hamna | Maths | 40 | 150 | 26.67 | 161 | | vaidehi | DC | 74 | 200 | 37.00 | 162 | | vaidehi | DBMSLab | 98 | 1001 | 9.79 | 163 | | varun | Maths | 40 | 150 | 26.67 | 164 | | varun | DC | 40 | 200 | 20.00 | 165 | | varun | DBMSLab | 35 | 1001 | 3.50 | 166 | | turner | Maths | 50 | 150 | 33.33 | 167 | | vani | OS | 23 | 75 | 30.67 | 168 | | binu | OS | 23 | 75 | 30.67 | 169 | | chitra | Maths | 23 | 150 | 15.33 | 170 | | chitra | DC | 42 | 200 | 21.00 | 171 | | dona | Maths | 43 | 150 | 28.67 | 172 | | dona | DC | 40 | 200 | 20.00 | 173 | | elana | Maths | 42 | 150 | 28.00 | 174 | | elana | OS | 1 | 75 | 1.33 | 175 | | elana | DBMSLab | 35 | 1001 | 3.50 | 176 | +-------------+-------------+------+---------+------------+ 177 | 178 | 6) Display the faculties and alloted subjects for each faculty 179 | 180 | +-----------+-------------+ 181 | | F_name | subjectname | 182 | +-----------+-------------+ 183 | | Silgy | Maths | 184 | | Bindu | FCA | 185 | | Vidhya | OS | 186 | | Vidhya | DBMSLab | 187 | | Sangeetha | DC | 188 | | Jayakumar | DBMS | 189 | +-----------+-------------+ 190 | 7) Display the name of faculties who take more than one subject. 191 | 192 | +--------+ 193 | | F_Name | 194 | +--------+ 195 | | Vidhya | 196 | +--------+ 197 | 198 | Display name,subject,mark, % of mark in ascending order of mark 199 | 200 | +-------------+-------------+------+ 201 | | studentname | subjectname | mark | 202 | +-------------+-------------+------+ 203 | | fahan | OS | 1 | 204 | | elana | OS | 1 | 205 | | binu | OS | 23 | 206 | | chitra | Maths | 23 | 207 | | vani | OS | 23 | 208 | | varun | DBMSLab | 35 | 209 | | elana | DBMSLab | 35 | 210 | | dona | DC | 40 | 211 | | binu | DBMS | 40 | 212 | | varun | Maths | 40 | 213 | | Amitha | Maths | 40 | 214 | | varun | DBMS | 40 | 215 | | chitra | OS | 40 | 216 | | varun | OS | 40 | 217 | | varun | DC | 40 | 218 | | Amitha | DC | 40 | 219 | | hamna | Maths | 40 | 220 | | elana | Maths | 42 | 221 | | chitra | DC | 42 | 222 | | varun | FCA | 43 | 223 | | vani | FCA | 43 | 224 | | dona | Maths | 43 | 225 | | binu | FCA | 43 | 226 | | turner | Maths | 50 | 227 | | fahan | Maths | 50 | 228 | | Amitha | DBMS | 50 | 229 | | vani | Maths | 60 | 230 | | ginu | Maths | 60 | 231 | | hamna | DBMS | 67 | 232 | | binu | Maths | 67 | 233 | | Amitha | DBMSLab | 70 | 234 | | Amitha | FCA | 70 | 235 | | fahan | DC | 74 | 236 | | vaidehi | DC | 74 | 237 | | vaidehi | DBMS | 77 | 238 | | Amitha | OS | 80 | 239 | | vaidehi | FCA | 89 | 240 | | vaidehi | Maths | 90 | 241 | | vaidehi | OS | 95 | 242 | | vaidehi | DBMSLab | 98 | 243 | | ginu | DBMSLab | 98 | 244 | +-------------+-------------+------+ 245 | 246 | 247 | -------------------------------------------------------------------------------- /dbms_out/expt5: -------------------------------------------------------------------------------- 1 | +------------+-------------+------+-----+---------+----------------+ 2 | | Field | Type | Null | Key | Default | Extra | 3 | +------------+-------------+------+-----+---------+----------------+ 4 | | stud_id | int(11) | NO | PRI | NULL | auto_increment | 5 | | stud_fname | varchar(20) | YES | | NULL | | 6 | | stud_lname | varchar(20) | YES | | NULL | | 7 | | stud_email | varchar(20) | YES | | NULL | | 8 | | stud_ph | varchar(10) | YES | | NULL | | 9 | +------------+-------------+------+-----+---------+----------------+ 10 | 11 | 12 | select * from Student; 13 | +---------+------------+------------+--------------------+------------+ 14 | | stud_id | stud_fname | stud_lname | stud_email | stud_ph | 15 | +---------+------------+------------+--------------------+------------+ 16 | | 100 | shanti | vasan | shantiv@gmail.com | 9677483824 | 17 | | 101 | anjitha | k | anjithak@gmail.com | 9574884993 | 18 | | 102 | riya | khan | riyakhan@gmail.com | 9637833993 | 19 | +---------+------------+------------+--------------------+------------+ 20 | 21 | desc Subject; 22 | +----------+-------------+------+-----+---------+----------------+ 23 | | Field | Type | Null | Key | Default | Extra | 24 | +----------+-------------+------+-----+---------+----------------+ 25 | | sub_id | int(11) | NO | PRI | NULL | auto_increment | 26 | | sub_name | varchar(20) | YES | | NULL | | 27 | +----------+-------------+------+-----+---------+----------------+ 28 | select * from Subject; 29 | +--------+-----------+ 30 | | sub_id | sub_name | 31 | +--------+-----------+ 32 | | 200 | chemistry | 33 | | 201 | physics | 34 | | 202 | maths | 35 | +--------+-----------+ 36 | 37 | 38 | desc Marks; 39 | +---------+---------+------+-----+---------+-------+ 40 | | Field | Type | Null | Key | Default | Extra | 41 | +---------+---------+------+-----+---------+-------+ 42 | | sub_id | int(11) | NO | PRI | NULL | | 43 | | stud_id | int(11) | NO | PRI | NULL | | 44 | | marks | int(11) | YES | | NULL | | 45 | +---------+---------+------+-----+---------+-------+ 46 | 47 | select * from Marks; 48 | +--------+---------+-------+ 49 | | sub_id | stud_id | marks | 50 | +--------+---------+-------+ 51 | | 200 | 100 | 75 | 52 | | 200 | 101 | 94 | 53 | | 200 | 102 | 60 | 54 | | 201 | 100 | 85 | 55 | | 201 | 101 | 98 | 56 | | 201 | 102 | 70 | 57 | | 202 | 100 | 50 | 58 | | 202 | 101 | 96 | 59 | | 202 | 102 | 45 | 60 | +--------+---------+-------+ 61 | 62 | select * from Student where stud_id=100; 63 | +---------+------------+------------+-------------------+------------+ 64 | | stud_id | stud_fname | stud_lname | stud_email | stud_ph | 65 | +---------+------------+------------+-------------------+------------+ 66 | | 100 | shanti | rajan | shantiv@gmail.com | 9677483824 | 67 | +---------+------------+------------+-------------------+------------+ 68 | 69 | 70 | update Subject set sub_name='mathematics' where sub_id=202; 71 | Query OK, 1 row affected (0.04 sec) 72 | Rows matched: 1 Changed: 1 Warnings: 0 73 | 74 | mysql> select * from Subject where sub_id=202; 75 | +--------+-------------+ 76 | | sub_id | sub_name | 77 | +--------+-------------+ 78 | | 202 | mathematics | 79 | +--------+-------------+ 80 | 81 | 82 | update Marks set marks=83 where sub_id=200 AND stud_id=102; 83 | Query OK, 1 row affected (0.02 sec) 84 | Rows matched: 1 Changed: 1 Warnings: 0 85 | 86 | mysql> select * from Marks where sub_id=200 and stud_id=102; 87 | +--------+---------+-------+ 88 | | sub_id | stud_id | marks | 89 | +--------+---------+-------+ 90 | | 200 | 102 | 83 | 91 | +--------+---------+-------+ 92 | 93 | 94 | select * from Student; 95 | +---------+------------+------------+--------------------+------------+ 96 | | stud_id | stud_fname | stud_lname | stud_email | stud_ph | 97 | +---------+------------+------------+--------------------+------------+ 98 | | 100 | shanti | rajan | shantiv@gmail.com | 9677483824 | 99 | | 101 | anjitha | k | anjithak@gmail.com | 9574884993 | 100 | | 102 | riya | khan | riyakhan@gmail.com | 9637833993 | 101 | +---------+------------+------------+--------------------+------------+ 102 | 103 | 104 | select * from Subject; 105 | +--------+-------------+ 106 | | sub_id | sub_name | 107 | +--------+-------------+ 108 | | 200 | chemistry | 109 | | 201 | physics | 110 | | 202 | mathematics | 111 | +--------+-------------+ 112 | 113 | select * from Marks; 114 | +--------+---------+-------+ 115 | | sub_id | stud_id | marks | 116 | +--------+---------+-------+ 117 | | 200 | 100 | 75 | 118 | | 200 | 101 | 94 | 119 | | 200 | 102 | 83 | 120 | | 201 | 100 | 85 | 121 | | 201 | 101 | 98 | 122 | | 201 | 102 | 70 | 123 | | 202 | 100 | 50 | 124 | | 202 | 101 | 96 | 125 | | 202 | 102 | 45 | 126 | +--------+---------+-------+ 127 | 128 | select * from Student where stud_id=102; 129 | +---------+------------+------------+--------------------+------------+ 130 | | stud_id | stud_fname | stud_lname | stud_email | stud_ph | 131 | +---------+------------+------------+--------------------+------------+ 132 | | 102 | riya | khan | riyakhan@gmail.com | 9637833993 | 133 | +---------+------------+------------+--------------------+------------+ 134 | 135 | # Write a query to display student name, subject and marks of student ordered by marks. 136 | 137 | +------------+-------------+-------+ 138 | | stud_fname | sub_name | marks | 139 | +------------+-------------+-------+ 140 | | riya | mathematics | 45 | 141 | | shanti | mathematics | 50 | 142 | | riya | physics | 70 | 143 | | shanti | chemistry | 75 | 144 | | riya | chemistry | 83 | 145 | | shanti | physics | 85 | 146 | | anjitha | chemistry | 94 | 147 | | anjitha | mathematics | 96 | 148 | | anjitha | physics | 98 | 149 | +------------+-------------+-------+ 150 | 9 rows in set (0.00 sec) 151 | 152 | # Write a query to display student name, subject and marks of students who have marks greater than 70 in physics. 153 | 154 | +------------+----------+-------+ 155 | | stud_fname | sub_name | marks | 156 | +------------+----------+-------+ 157 | | shanti | physics | 85 | 158 | | anjitha | physics | 98 | 159 | +------------+----------+-------+ 160 | 2 rows in set (0.00 sec) 161 | 162 | # Write a query to display average marks in each subject. 163 | 164 | +-------------+------------+ 165 | | sub_name | avg(marks) | 166 | +-------------+------------+ 167 | | chemistry | 84.0000 | 168 | | mathematics | 63.6667 | 169 | | physics | 84.3333 | 170 | +-------------+------------+ 171 | 3 rows in set (0.02 sec) 172 | 173 | # Write a query to display the number of students. 174 | 175 | +----------------+ 176 | | count(stud_id) | 177 | +----------------+ 178 | | 3 | 179 | +----------------+ 180 | 1 row in set (0.00 sec) 181 | 182 | # Write a query to display the maximum and minimum marks obtained by students in each subject. 183 | 184 | +-------------+------------+------------+ 185 | | sub_name | max(marks) | min(marks) | 186 | +-------------+------------+------------+ 187 | | chemistry | 94 | 75 | 188 | | mathematics | 96 | 45 | 189 | | physics | 98 | 70 | 190 | +-------------+------------+------------+ 191 | 3 rows in set (0.00 sec) 192 | 193 | # Write a query to display the details of a student whose name begins with S. 194 | 195 | +---------+------------+------------+-------------------+------------+ 196 | | stud_id | stud_fname | stud_lname | stud_email | stud_ph | 197 | +---------+------------+------------+-------------------+------------+ 198 | | 100 | shanti | rajan | shantiv@gmail.com | 9677483824 | 199 | +---------+------------+------------+-------------------+------------+ 200 | 1 row in set (0.00 sec) 201 | 202 | # Write a query to display the details of a student whose first name contains a in the fourth place. 203 | 204 | +---------+------------+------------+--------------------+------------+ 205 | | stud_id | stud_fname | stud_lname | stud_email | stud_ph | 206 | +---------+------------+------------+--------------------+------------+ 207 | | 102 | riya | khan | riyakhan@gmail.com | 9637833993 | 208 | +---------+------------+------------+--------------------+------------+ 209 | 210 | # Write a query to display the name,subject and marks of students having marks between 50 and 75. 211 | 212 | +------------+-------------+-------+ 213 | | stud_fname | sub_name | marks | 214 | +------------+-------------+-------+ 215 | | shanti | chemistry | 75 | 216 | | riya | physics | 70 | 217 | | shanti | mathematics | 50 | 218 | +------------+-------------+-------+ 219 | 3 rows in set (0.00 sec) 220 | 221 | #Create a view to display student name and marks 222 | 223 | mysql> create view student_view as select stud_fname, stud_lname, sub_name,marks from Student, Subject, Marks where Student.stud_id=Marks.stud_id and Subject.sub_id=Marks.sub_id; 224 | Query OK, 0 rows affected (0.04 sec) 225 | 226 | mysql> create view student_view as select stud_fname, stud_lname, sub_name,marks from Student, Subject, Marks where Student.stud_id=Marks.stud_id and Subject.sub_id=Marks.sub_id; 227 | ERROR 1050 (42S01): Table 'student_view' already exists 228 | mysql> 229 | mysql> select * from student_view; 230 | +------------+------------+-------------+-------+ 231 | | stud_fname | stud_lname | sub_name | marks | 232 | +------------+------------+-------------+-------+ 233 | | shanti | rajan | chemistry | 75 | 234 | | shanti | rajan | physics | 85 | 235 | | shanti | rajan | mathematics | 50 | 236 | | anjitha | k | chemistry | 94 | 237 | | anjitha | k | physics | 98 | 238 | | anjitha | k | mathematics | 96 | 239 | | riya | khan | chemistry | 83 | 240 | | riya | khan | physics | 70 | 241 | | riya | khan | mathematics | 45 | 242 | +------------+------------+-------------+-------+ 243 | 9 rows in set (0.00 sec) 244 | 245 | -------------------------------------------------------------------------------- /dbms_out/expt8 grpby havin: -------------------------------------------------------------------------------- 1 | Expt 8 2 | 3 | Create two tables 4 | 1. Dept(Department_Id, Department_Name , Manager_id, Loc) 5 | Primary Key: Department_Id 6 | 2. Emp(Emp_no , Emp_name,Job , Salary , Hiredate,Comm , Depno ) 7 | Primary key : EMP_no 8 | 9 | mysql> select * from Dept; 10 | +---------------+-----------------+------------+------------+ 11 | | department_id | department_name | manager_id | loc | 12 | +---------------+-----------------+------------+------------+ 13 | | 1 | Administration | 2 | Boston | 14 | | 2 | Marketing | 1 | Boston | 15 | | 3 | Purchase | 8 | perryridge | 16 | | 4 | Programming | 7 | Hudson | 17 | | 5 | HR | NULL | Hudson | 18 | +---------------+-----------------+------------+------------+ 19 | 20 | 21 | emp 22 | +--------+-----------+------------+------------+--------+------+-------+ 23 | | EMP_no | Emp_name | Job | Hiredata | Salary | Comm | Depno | 24 | +--------+-----------+------------+------------+--------+------+-------+ 25 | | 1 | Steven | Marketing | 1995-01-06 | 24000 | NULL | 2 | 26 | | 2 | Neena | FI_ACCOUNT | 1987-02-06 | 34000 | NULL | 1 | 27 | | 3 | Lex | FI_MGR | 1980-01-06 | 240000 | NULL | 1 | 28 | | 4 | Alexander | Sa_Rep | 1987-06-06 | 20000 | NULL | 4 | 29 | | 5 | Bruce | IT_PROG | 1990-07-06 | 24000 | NULL | 4 | 30 | | 6 | David | IT_PROG | 1991-09-06 | 22000 | NULL | 4 | 31 | | 7 | vipin | IT_PROG | 1987-11-16 | 28000 | NULL | 4 | 32 | | 8 | Diana | Pur_Man | 1987-01-26 | 24000 | NULL | 3 | 33 | | 9 | John | FI_ACCOUNT | 1992-12-01 | 24000 | NULL | 1 | 34 | | 10 | Ismael | CLERK | 1994-03-29 | 4000 | NULL | 3 | 35 | | 11 | Mathew | CLERK | 1992-10-12 | 46000 | 200 | 3 | 36 | | 12 | Hayes | Marketing | 1998-04-21 | 14000 | 1000 | 3 | 37 | | 13 | sarun | Marketing | 1993-05-18 | 18000 | NULL | 2 | 38 | | 14 | Henin | FI_MGR | 1980-08-06 | 240000 | NULL | 1 | 39 | | 15 | Greesh | Clerk | 1980-08-06 | 240000 | NULL | 5 | 40 | +--------+-----------+------------+------------+--------+------+-------+ 41 | 42 | 43 | 44 | #1 Display the name and salary for all employees whose salary is not in the range of 45 | 5000 and 35000 46 | 47 | +----------+--------+ 48 | | Emp_name | Salary | 49 | +----------+--------+ 50 | | Lex | 240000 | 51 | | Ismael | 4000 | 52 | | Mathew | 46000 | 53 | | Henin | 240000 | 54 | | Greesh | 240000 | 55 | +----------+--------+ 56 | 57 | #2 Display the employee name, job ID, and start date of employees hired between 58 | February 20, 1990, and May 1, 1998. Order the query in ascending order by start 59 | date. 60 | 61 | +----------+--------+------------+------------+ 62 | | Emp_name | Salary | Job | Hiredata | 63 | +----------+--------+------------+------------+ 64 | | Bruce | 24000 | IT_PROG | 1990-07-06 | 65 | | David | 22000 | IT_PROG | 1991-09-06 | 66 | | Mathew | 46000 | CLERK | 1992-10-12 | 67 | | John | 24000 | FI_ACCOUNT | 1992-12-01 | 68 | | sarun | 18000 | Marketing | 1993-05-18 | 69 | | Ismael | 4000 | CLERK | 1994-03-29 | 70 | | Steven | 24000 | Marketing | 1995-01-06 | 71 | | Hayes | 14000 | Marketing | 1998-04-21 | 72 | +----------+--------+------------+------------+ 73 | 74 | #3 list the name and salary of employees who earn between 5,000 and12,000, and are 75 | in department 2 or 4. Label the columns Employee and Monthly Salary,respectively. 76 | 77 | +-----------+----------------+-------+ 78 | | Employee | Monthly Salary | Depno | 79 | +-----------+----------------+-------+ 80 | | Steven | 24000 | 2 | 81 | | Alexander | 20000 | 4 | 82 | | Bruce | 24000 | 4 | 83 | | David | 22000 | 4 | 84 | | vipin | 28000 | 4 | 85 | | sarun | 18000 | 2 | 86 | +-----------+----------------+-------+ 87 | 88 | #4 Display the name and hire date of every employee who was hired in 1994. 89 | 90 | select Emp_name, hiredate from Emp where hiredate like '1994%'; 91 | +----------+------------+ 92 | | Emp_name | hiredata | 93 | +----------+------------+ 94 | | Ismael | 1994-03-29 | 95 | +----------+------------+ 96 | 97 | #5 Display the name, salary, and commission for all employees who earn 98 | commissions. Sort data in descending order of salary and commissions. 99 | +----------+--------+------+ 100 | | Emp_name | Salary | Comm | 101 | +----------+--------+------+ 102 | | Mathew | 46000 | 200 | 103 | | Hayes | 14000 | 1000 | 104 | +----------+--------+------+ 105 | 106 | #6 Display the name and job title of all employees who do not have a manager. 107 | +----------+-------+ 108 | | Emp_name | Job | 109 | +----------+-------+ 110 | | Greesh | Clerk | 111 | +----------+-------+ 112 | 1 row in set (0.00 sec 113 | 114 | #7 Display the names of all employees where the third letter of the name is an a. 115 | +----------+ 116 | | Emp_name | 117 | +----------+ 118 | | Diana | 119 | +----------+ 120 | 121 | #8 Display the name of all employees who have an a and an e in their name. 122 | 123 | +-----------+ 124 | | Emp_name | 125 | +-----------+ 126 | | Neena | 127 | | Alexander | 128 | | Ismael | 129 | | Mathew | 130 | | Hayes | 131 | +-----------+ 132 | 133 | #9 Display the name, job, and salary for all employees whose job is sales 134 | representative or stock clerk and whose salary is not equal to 2,0000, 4000, or 7,000 135 | 136 | +-----------+--------+--------+ 137 | | Emp_name | Job | Salary | 138 | +-----------+--------+--------+ 139 | | Alexander | Sa_Rep | 20000 | 140 | | Mathew | CLERK | 46000 | 141 | | Greesh | Clerk | 240000 | 142 | +-----------+--------+--------+ 143 | 144 | #10 Write a query that displays the employee’s names with the first letter capitalized 145 | and all other letters lowercase and the length of the name for all employees whose 146 | name starts with J, A, or M. Give each column an appropriate label. Sort the results 147 | by the employees’ names. 148 | 149 | +-----------+--------+ 150 | | Name | Length | 151 | +-----------+--------+ 152 | | ALEXANDER | 9 | 153 | | JOHN | 4 | 154 | | MATHEW | 6 | 155 | +-----------+--------+ 156 | 3 rows in set (0.01 sec) 157 | 158 | #11 Write a query to display the name, department number, and department name for 159 | al employees. 160 | 161 | +-----------+-------+-----------------+ 162 | | Emp_name | Depno | department_name | 163 | +-----------+-------+-----------------+ 164 | | Henin | 1 | Administration | 165 | | Neena | 1 | Administration | 166 | | John | 1 | Administration | 167 | | Lex | 1 | Administration | 168 | | Greesh | 5 | HR | 169 | | sarun | 2 | Marketing | 170 | | Steven | 2 | Marketing | 171 | | Alexander | 4 | Programming | 172 | | Bruce | 4 | Programming | 173 | | David | 4 | Programming | 174 | | vipin | 4 | Programming | 175 | | Ismael | 3 | Purchase | 176 | | Mathew | 3 | Purchase | 177 | | Hayes | 3 | Purchase | 178 | | Diana | 3 | Purchase | 179 | +-----------+-------+-----------------+ 180 | 15 rows in set (0.00 sec) 181 | 182 | #12 Create a query to display the name and hire date of any employee hired after 183 | employee Mathew 184 | 185 | +----------+------------+ 186 | | Emp_Name | Hiredata | 187 | +----------+------------+ 188 | | Steven | 1995-01-06 | 189 | | John | 1992-12-01 | 190 | | Ismael | 1994-03-29 | 191 | | Hayes | 1998-04-21 | 192 | | sarun | 1993-05-18 | 193 | +----------+------------+ 194 | 195 | #13 Display the names and hire dates for all employees who were hired before their 196 | managers, along with their manager’s names and hire dates. Label the columns 197 | Employee, EmpHired, Manager, and Mgr Hired, respectively. 198 | 199 | +--------+-----------+---------------+---------+--------------+------------------+ 200 | | EMP_no | Employee | EMP Hire Data | dept no | Manager Name | Manager Hiredata | 201 | +--------+-----------+---------------+---------+--------------+------------------+ 202 | | 3 | Lex | 1980-01-06 | 1 | Neena | 1987-02-06 | 203 | | 4 | Alexander | 1987-06-06 | 4 | vipin | 1987-11-16 | 204 | | 13 | sarun | 1993-05-18 | 2 | Steven | 1995-01-06 | 205 | | 14 | Henin | 1980-08-06 | 1 | Neena | 1987-02-06 | 206 | +--------+-----------+---------------+---------+--------------+------------------+ 207 | 208 | #14 Write a query to display the number of people with the same job. 209 | 210 | +------------+-------------+ 211 | | Job | No: of Jobs | 212 | +------------+-------------+ 213 | | CLERK | 3 | 214 | | FI_ACCOUNT | 2 | 215 | | FI_MGR | 2 | 216 | | IT_PROG | 3 | 217 | | Marketing | 3 | 218 | | Pur_Man | 1 | 219 | | Sa_Rep | 1 | 220 | +------------+-------------+ 221 | #15 Display the manager number and the salary of the lowest paid employee for that 222 | manager.Exclude anyone whose manager is not known. Exclude any groups where 223 | the minimum salary is less than 6,000. Sort the output in descending order of salary 224 | 225 | +----------------+------------+-----------------+ 226 | | MINIMUM SALARY | manager_id | department_name | 227 | +----------------+------------+-----------------+ 228 | | 18000 | 1 | Marketing | 229 | | 24000 | 2 | Administration | 230 | | 20000 | 7 | Programming | 231 | +----------------+------------+-----------------+ 232 | 233 | #16 Write a query to display each department’s name, location, number of 234 | employees, and the average salary for all employees in that department. Label the 235 | columns Name, Location, Number of People, and Salary, respectively. Round the 236 | average salary to two decimal places 237 | 238 | +----------------+------------+------------------+-----------+ 239 | | Name | Location | Number of People | Salary | 240 | +----------------+------------+------------------+-----------+ 241 | | Administration | Boston | 4 | 134500.00 | 242 | | HR | Hudson | 1 | 240000.00 | 243 | | Marketing | Boston | 2 | 21000.00 | 244 | | Programming | Hudson | 4 | 23500.00 | 245 | | Purchase | perryridge | 4 | 22000.00 | 246 | +----------------+------------+------------------+-----------+ 247 | 248 | #17 Write a query to display the name and hire date of any employee in the same 249 | department as amit. Exclude JOHN. 250 | 251 | +----------+------------+ 252 | | Emp_name | Hiredata | 253 | +----------+------------+ 254 | | Neena | 1987-02-06 | 255 | | Lex | 1980-01-06 | 256 | | Henin | 1980-08-06 | 257 | +----------+------------+ 258 | 259 | #18 Write a query that displays the employee numbers names of all employees who work in a 260 | # department with any employee whose name contains a u. 261 | 262 | +--------+-----------+-----------------+ 263 | | EMP_no | Emp_name | department_name | 264 | +--------+-----------+-----------------+ 265 | | 1 | Steven | Marketing | 266 | | 4 | Alexander | Programming | 267 | | 5 | Bruce | Programming | 268 | | 6 | David | Programming | 269 | | 7 | vipin | Programming | 270 | | 13 | sarun | Marketing | 271 | +--------+-----------+-----------------+ 272 | 273 | 19 display employee name and department name of all employees that work in a 274 | department that has at least 2 employees. Order the list in alphabetical order first by 275 | department name, then by employee name 276 | 277 | +-----------+-----------------+ 278 | | Emp_name | department_name | 279 | +-----------+-----------------+ 280 | | Henin | Administration | 281 | | John | Administration | 282 | | Lex | Administration | 283 | | Neena | Administration | 284 | | Alexander | Programming | 285 | | Bruce | Programming | 286 | | David | Programming | 287 | | vipin | Programming | 288 | | Diana | Purchase | 289 | | Hayes | Purchase | 290 | | Ismael | Purchase | 291 | | Mathew | Purchase | 292 | +-----------+-----------------+ 293 | --------------------------------------------------------------------------------