Compare commits
No commits in common. "fa91c4f08faa896f5b5db8141b9f3223b0f4f6cd" and "db10885c0e35873b6c930c7bb189a146d0122c28" have entirely different histories.
fa91c4f08f
...
db10885c0e
@ -1,5 +1,3 @@
|
||||
# Database Queries for Assignment-A3
|
||||
|
||||
## Creating database
|
||||
|
||||
```sql
|
||||
|
@ -1,5 +1,3 @@
|
||||
# Database Queries for Assignment-A4
|
||||
|
||||
## Creating tables
|
||||
|
||||
```sql
|
||||
|
76
Practical/Assignment-A4+A5/Queries-A5.md
Executable file
76
Practical/Assignment-A4+A5/Queries-A5.md
Executable file
@ -0,0 +1,76 @@
|
||||
## Creating tables & inserting data
|
||||
|
||||
Student marks:
|
||||
```sql
|
||||
CREATE TABLE Stud_Marks(
|
||||
Roll NUMBER(14),
|
||||
name VARCHAR(255),
|
||||
total_marks NUMBER(14),
|
||||
PRIMARY KEY(Roll)
|
||||
);
|
||||
|
||||
INSERT INTO Stud_Marks VALUES (1, 'Arora', 1350);
|
||||
INSERT INTO Stud_Marks VALUES (2, 'Shankar', 979);
|
||||
INSERT INTO Stud_Marks VALUES (3, 'Lara', 834);
|
||||
INSERT INTO Stud_Marks VALUES (4, 'Jambo', 667);
|
||||
INSERT INTO Stud_Marks VALUES (5, 'Tinde', 1400);
|
||||
|
||||
```
|
||||
|
||||
Result:
|
||||
```sql
|
||||
CREATE TABLE Result(
|
||||
Roll NUMBER(14),
|
||||
name VARCHAR(255),
|
||||
Class VARCHAR(255),
|
||||
FOREIGN KEY (Roll) REFERENCES Stud_Marks (Roll)
|
||||
);
|
||||
|
||||
```
|
||||
|
||||
## Procedure
|
||||
```sql
|
||||
CREATE OR REPLACE PROCEDURE proc_grade
|
||||
(rollno in number,
|
||||
p_roll_no out Stud_Marks.Roll%type,
|
||||
p_name out Stud_Marks.name%type,
|
||||
p_total out Stud_Marks.total_marks%type)
|
||||
AS
|
||||
BEGIN
|
||||
SELECT Roll, name, total_marks into p_roll_no, p_name, p_total from Stud_Marks WHERE Roll = rollno;
|
||||
IF p_total <= 1500 and p_total >= 990
|
||||
THEN
|
||||
INSERT INTO Result VALUES (p_roll_no, p_name, 'Distinction');
|
||||
ELSE IF p_total <= 989 and p_total >= 900
|
||||
THEN
|
||||
INSERT INTO Result VALUES (p_roll_no, p_name, 'First Class');
|
||||
ELSE IF p_total <= 899 and p_total >= 825
|
||||
THEN
|
||||
INSERT INTO Result VALUES (p_roll_no, p_name, 'Higher Second Class');
|
||||
ELSE
|
||||
INSERT INTO Result VALUES (p_roll_no, p_name, 'FAIL');
|
||||
END IF;
|
||||
END IF;
|
||||
END IF;
|
||||
EXCEPTION
|
||||
WHEN no_data_found THEN
|
||||
dbms_output.put_line('Rollno ' || rollno || ' is not present!!');
|
||||
END;
|
||||
/
|
||||
|
||||
```
|
||||
|
||||
## Calling Procedure:
|
||||
```sql
|
||||
DECLARE
|
||||
rollno number(14);
|
||||
p_roll_no Stud_Marks.Roll%type;
|
||||
p_name Stud_Marks.name%type;
|
||||
p_total_marks Stud_Marks.total_marks%type;
|
||||
BEGIN
|
||||
rollno := &rollno;
|
||||
proc_grade(rollno, p_roll_no, p_name, p_total_marks);
|
||||
END;
|
||||
/
|
||||
|
||||
```
|
147
Practical/Assignment-A6/Queries-A6.md
Executable file → Normal file
147
Practical/Assignment-A6/Queries-A6.md
Executable file → Normal file
@ -1,78 +1,107 @@
|
||||
# Database Queries for Assignment-A6
|
||||
## Creating tables
|
||||
|
||||
## Creating tables & inserting data
|
||||
|
||||
Student marks:
|
||||
`O_RollCall` table:
|
||||
```sql
|
||||
CREATE TABLE Stud_Marks(
|
||||
Roll NUMBER(14),
|
||||
name VARCHAR(255),
|
||||
total_marks NUMBER(14),
|
||||
PRIMARY KEY(Roll)
|
||||
|
||||
CREATE TABLE O_RollCall(
|
||||
name VARCHAR(255),
|
||||
roll NUMBER(14),
|
||||
class VARCHAR(255)
|
||||
);
|
||||
|
||||
INSERT INTO Stud_Marks VALUES (1, 'Arora', 1350);
|
||||
INSERT INTO Stud_Marks VALUES (2, 'Shankar', 979);
|
||||
INSERT INTO Stud_Marks VALUES (3, 'Lara', 834);
|
||||
INSERT INTO Stud_Marks VALUES (4, 'Jambo', 667);
|
||||
INSERT INTO Stud_Marks VALUES (5, 'Tinde', 1400);
|
||||
|
||||
```
|
||||
|
||||
Result:
|
||||
`N_RollCall` table:
|
||||
```sql
|
||||
CREATE TABLE Result(
|
||||
Roll NUMBER(14),
|
||||
name VARCHAR(255),
|
||||
Class VARCHAR(255),
|
||||
FOREIGN KEY (Roll) REFERENCES Stud_Marks (Roll)
|
||||
CREATE TABLE N_RollCall(
|
||||
name VARCHAR(255),
|
||||
roll NUMBER(14),
|
||||
class VARCHAR(255)
|
||||
);
|
||||
|
||||
```
|
||||
|
||||
## Inserting data
|
||||
|
||||
`O_RollCall` table:
|
||||
```sql
|
||||
INSERT INTO O_RollCall VALUES ('Stewie', 1, 'Comp 1');
|
||||
INSERT INTO O_RollCall VALUES ('Edie', 2, 'Comp 2');
|
||||
INSERT INTO O_RollCall VALUES ('Stomp', 3, 'Comp 3');
|
||||
INSERT INTO O_RollCall VALUES ('Lara', 4, 'Comp 1');
|
||||
INSERT INTO O_RollCall VALUES ('Foxy', 5, 'Comp 2');
|
||||
|
||||
```
|
||||
|
||||
`N_RollCall` table:
|
||||
```sql
|
||||
INSERT INTO N_RollCall VALUES ('Stewie', 1, 'Comp 1');
|
||||
INSERT INTO N_RollCall VALUES ('Edie', 2, 'Comp 2');
|
||||
INSERT INTO N_RollCall VALUES ('Stomp', 3, 'Comp 3');
|
||||
INSERT INTO N_RollCall VALUES ('Lara', 4, 'Comp 1');
|
||||
INSERT INTO N_RollCall VALUES ('Foxy', 5, 'Comp 2');
|
||||
INSERT INTO N_RollCall VALUES ('Gundeti', 6, 'Comp 3');
|
||||
INSERT INTO N_RollCall VALUES ('Kalas', 7, 'Comp 2');
|
||||
|
||||
```
|
||||
|
||||
## Procedure
|
||||
```sql
|
||||
CREATE OR REPLACE PROCEDURE proc_grade
|
||||
(rollno in number,
|
||||
p_roll_no out Stud_Marks.Roll%type,
|
||||
p_name out Stud_Marks.name%type,
|
||||
p_total out Stud_Marks.total_marks%type)
|
||||
AS
|
||||
BEGIN
|
||||
SELECT Roll, name, total_marks into p_roll_no, p_name, p_total from Stud_Marks WHERE Roll = rollno;
|
||||
IF p_total <= 1500 and p_total >= 990
|
||||
THEN
|
||||
INSERT INTO Result VALUES (p_roll_no, p_name, 'Distinction');
|
||||
ELSE IF p_total <= 989 and p_total >= 900
|
||||
THEN
|
||||
INSERT INTO Result VALUES (p_roll_no, p_name, 'First Class');
|
||||
ELSE IF p_total <= 899 and p_total >= 825
|
||||
THEN
|
||||
INSERT INTO Result VALUES (p_roll_no, p_name, 'Higher Second Class');
|
||||
ELSE
|
||||
INSERT INTO Result VALUES (p_roll_no, p_name, 'FAIL');
|
||||
END IF;
|
||||
END IF;
|
||||
END IF;
|
||||
EXCEPTION
|
||||
WHEN no_data_found THEN
|
||||
dbms_output.put_line('Rollno ' || rollno || ' is not present!!');
|
||||
END;
|
||||
/
|
||||
|
||||
```
|
||||
|
||||
## Calling Procedure:
|
||||
### Explicit cursor
|
||||
```sql
|
||||
DECLARE
|
||||
rollno number(14);
|
||||
p_roll_no Stud_Marks.Roll%type;
|
||||
p_name Stud_Marks.name%type;
|
||||
p_total_marks Stud_Marks.total_marks%type;
|
||||
p_name VARCHAR(255);
|
||||
p_rollno NUMBER(15);
|
||||
p_class VARCHAR(255);
|
||||
CURSOR cc1 IS SELECT * FROM N_RollCall WHERE roll NOT IN (SELECT roll FROM O_RollCall);
|
||||
BEGIN
|
||||
rollno := &rollno;
|
||||
proc_grade(rollno, p_roll_no, p_name, p_total_marks);
|
||||
OPEN cc1;
|
||||
LOOP
|
||||
FETCH cc1 INTO p_name, p_rollno, p_class;
|
||||
INSERT INTO O_RollCall VALUES (p_name, p_rollno, p_class);
|
||||
EXIT WHEN cc1%notfound;
|
||||
DBMS_OUTPUT.PUT_LINE(p_name || ' ' || p_rollno || ' ' || p_class);
|
||||
END LOOP;
|
||||
CLOSE cc1;
|
||||
END;
|
||||
/
|
||||
|
||||
```
|
||||
|
||||
### Parameterized cursor
|
||||
```sql
|
||||
DECLARE
|
||||
p_name VARCHAR(255);
|
||||
p_rollno NUMBER(15);
|
||||
p_class VARCHAR(255);
|
||||
CURSOR pp1(roll1 NUMBER) IS SELECT * FROM N_RollCall WHERE roll > roll1;
|
||||
|
||||
BEGIN
|
||||
OPEN pp1(3);
|
||||
LOOP
|
||||
FETCH pp1 INTO p_name, p_rollno, p_class;
|
||||
EXIT WHEN pp1%notfound;
|
||||
DBMS_OUTPUT.PUT_LINE(p_name || ' ' || p_rollno || ' ' || p_class);
|
||||
END LOOP;
|
||||
CLOSE pp1;
|
||||
|
||||
END;
|
||||
/
|
||||
|
||||
```
|
||||
|
||||
### Implicit Cursor
|
||||
```sql
|
||||
DECLARE
|
||||
total_rows NUMBER(2);
|
||||
|
||||
BEGIN
|
||||
UPDATE N_RollCall SET roll = roll + 1;
|
||||
IF sql%notfound THEN
|
||||
dbms_output.put_line('no roll was updated');
|
||||
ELSIF sql%found THEN
|
||||
total_rows := sql%rowcount;
|
||||
DBMS_OUTPUT.PUT_LINE( total_rows || ' roll calls were affected ');
|
||||
END IF;
|
||||
END;
|
||||
/
|
||||
|
||||
|
@ -1,110 +1,91 @@
|
||||
# Database Queries for Assignment-A7
|
||||
|
||||
## Creating tables
|
||||
|
||||
`O_RollCall` table:
|
||||
`Library` table:
|
||||
```sql
|
||||
CREATE TABLE O_RollCall(
|
||||
name VARCHAR(255),
|
||||
roll NUMBER(14),
|
||||
class VARCHAR(255)
|
||||
CREATE TABLE Library(
|
||||
id NUMBER(12),
|
||||
title VARCHAR(255),
|
||||
dateofissue DATE,
|
||||
author VARCHAR(255)
|
||||
);
|
||||
|
||||
```
|
||||
|
||||
`N_RollCall` table:
|
||||
`Library_Audit` table:
|
||||
```sql
|
||||
CREATE TABLE N_RollCall(
|
||||
name VARCHAR(255),
|
||||
roll NUMBER(14),
|
||||
class VARCHAR(255)
|
||||
CREATE TABLE Library_Audit(
|
||||
id NUMBER(12),
|
||||
title VARCHAR(255),
|
||||
dateofaction DATE,
|
||||
author VARCHAR(255),
|
||||
status VARCHAR(255)
|
||||
);
|
||||
|
||||
```
|
||||
|
||||
## Inserting data
|
||||
## Inserting values
|
||||
|
||||
`O_RollCall` table:
|
||||
`Library` table:
|
||||
```sql
|
||||
INSERT INTO O_RollCall VALUES ('Stewie', 1, 'Comp 1');
|
||||
INSERT INTO O_RollCall VALUES ('Edie', 2, 'Comp 2');
|
||||
INSERT INTO O_RollCall VALUES ('Stomp', 3, 'Comp 3');
|
||||
INSERT INTO O_RollCall VALUES ('Lara', 4, 'Comp 1');
|
||||
INSERT INTO O_RollCall VALUES ('Foxy', 5, 'Comp 2');
|
||||
INSERT INTO Library VALUES (1, 'Berserk', TO_DATE('2024-07-28','YYYY-MM-DD'), 'Prashant');
|
||||
INSERT INTO Library VALUES (2, 'Dark', TO_DATE('2024-07-15','YYYY-MM-DD'), 'Rajendra');
|
||||
INSERT INTO Library VALUES (3, 'Hannibal', TO_DATE('2024-07-20','YYYY-MM-DD'), 'Manoj');
|
||||
INSERT INTO Library VALUES (4, 'AOT', TO_DATE('2024-07-30','YYYY-MM-DD'), 'Rajesh');
|
||||
INSERT INTO Library VALUES (5, 'GOT', TO_DATE('2024-07-19','YYYY-MM-DD'), 'Anil');
|
||||
|
||||
```
|
||||
|
||||
`N_RollCall` table:
|
||||
## Trigger
|
||||
|
||||
```sql
|
||||
INSERT INTO N_RollCall VALUES ('Stewie', 1, 'Comp 1');
|
||||
INSERT INTO N_RollCall VALUES ('Edie', 2, 'Comp 2');
|
||||
INSERT INTO N_RollCall VALUES ('Stomp', 3, 'Comp 3');
|
||||
INSERT INTO N_RollCall VALUES ('Lara', 4, 'Comp 1');
|
||||
INSERT INTO N_RollCall VALUES ('Foxy', 5, 'Comp 2');
|
||||
INSERT INTO N_RollCall VALUES ('Gundeti', 6, 'Comp 3');
|
||||
INSERT INTO N_RollCall VALUES ('Kalas', 7, 'Comp 2');
|
||||
|
||||
```
|
||||
|
||||
## Procedure
|
||||
|
||||
### Explicit cursor
|
||||
```sql
|
||||
DECLARE
|
||||
p_name VARCHAR(255);
|
||||
p_rollno NUMBER(15);
|
||||
p_class VARCHAR(255);
|
||||
CURSOR cc1 IS SELECT * FROM N_RollCall WHERE roll NOT IN (SELECT roll FROM O_RollCall);
|
||||
CREATE OR REPLACE TRIGGER library_action
|
||||
AFTER INSERT OR UPDATE OR DELETE ON Library
|
||||
FOR EACH ROW
|
||||
BEGIN
|
||||
OPEN cc1;
|
||||
LOOP
|
||||
FETCH cc1 INTO p_name, p_rollno, p_class;
|
||||
INSERT INTO O_RollCall VALUES (p_name, p_rollno, p_class);
|
||||
EXIT WHEN cc1%notfound;
|
||||
DBMS_OUTPUT.PUT_LINE(p_name || ' ' || p_rollno || ' ' || p_class);
|
||||
END LOOP;
|
||||
CLOSE cc1;
|
||||
IF INSERTING THEN
|
||||
INSERT INTO Library_Audit (id, title, dateofaction, author, status) VALUES (:NEW.id, :NEW.title, current_timestamp, :NEW.author, 'Insert');
|
||||
ELSIF UPDATING THEN
|
||||
INSERT INTO Library_Audit (id, title, dateofaction, author, status) VALUES (:OLD.id, :OLD.title, current_timestamp, :OLD.author, 'Update');
|
||||
ELSIF DELETING THEN
|
||||
INSERT INTO Library_Audit (id, title, dateofaction, author, status) VALUES (:OLD.id, :OLD.title, current_timestamp, :OLD.author, 'Delete');
|
||||
END IF;
|
||||
END;
|
||||
/
|
||||
|
||||
```
|
||||
|
||||
### Parameterized cursor
|
||||
## Testing trigger
|
||||
|
||||
### Insert operation
|
||||
|
||||
```sql
|
||||
DECLARE
|
||||
p_name VARCHAR(255);
|
||||
p_rollno NUMBER(15);
|
||||
p_class VARCHAR(255);
|
||||
CURSOR pp1(roll1 NUMBER) IS SELECT * FROM N_RollCall WHERE roll > roll1;
|
||||
INSERT INTO Library VALUES (15, 'CREW', TO_DATE('2024-07-22','YYYY-MM-DD'), 'Ramesh');
|
||||
INSERT INTO Library VALUES (14, 'Ninteen Eighty Four', TO_DATE('2024-07-01','YYYY-MM-DD'), 'Omkar');
|
||||
|
||||
BEGIN
|
||||
OPEN pp1(3);
|
||||
LOOP
|
||||
FETCH pp1 INTO p_name, p_rollno, p_class;
|
||||
EXIT WHEN pp1%notfound;
|
||||
DBMS_OUTPUT.PUT_LINE(p_name || ' ' || p_rollno || ' ' || p_class);
|
||||
END LOOP;
|
||||
CLOSE pp1;
|
||||
SELECT * FROM Library;
|
||||
SELECT * FROM Library_Audit;
|
||||
|
||||
END;
|
||||
/
|
||||
```
|
||||
### Update operation
|
||||
|
||||
```sql
|
||||
UPDATE Library SET id = 6, title = 'Sherlock', author = 'Deepak' where id = 3;
|
||||
UPDATE Library SET id = 7, title = 'MR. ROBOT', author = 'Varad' where id = 4;
|
||||
|
||||
SELECT * FROM Library;
|
||||
SELECT * FROM Library_Audit;
|
||||
|
||||
```
|
||||
|
||||
### Implicit Cursor
|
||||
```sql
|
||||
DECLARE
|
||||
total_rows NUMBER(2);
|
||||
### Delete operation
|
||||
|
||||
BEGIN
|
||||
UPDATE N_RollCall SET roll = roll + 1;
|
||||
IF sql%notfound THEN
|
||||
dbms_output.put_line('no roll was updated');
|
||||
ELSIF sql%found THEN
|
||||
total_rows := sql%rowcount;
|
||||
DBMS_OUTPUT.PUT_LINE( total_rows || ' roll calls were affected ');
|
||||
END IF;
|
||||
END;
|
||||
/
|
||||
```sql
|
||||
DELETE FROM Library WHERE id = 1;
|
||||
DELETE FROM Library WHERE id = 5;
|
||||
|
||||
SELECT * FROM Library;
|
||||
SELECT * FROM Library_Audit;
|
||||
|
||||
```
|
||||
|
||||
---
|
||||
|
@ -1,93 +0,0 @@
|
||||
# Database Queries for Assignment-A8
|
||||
|
||||
## Creating tables
|
||||
|
||||
`Library` table:
|
||||
```sql
|
||||
CREATE TABLE Library(
|
||||
id NUMBER(12),
|
||||
title VARCHAR(255),
|
||||
dateofissue DATE,
|
||||
author VARCHAR(255)
|
||||
);
|
||||
|
||||
```
|
||||
|
||||
`Library_Audit` table:
|
||||
```sql
|
||||
CREATE TABLE Library_Audit(
|
||||
id NUMBER(12),
|
||||
title VARCHAR(255),
|
||||
dateofaction DATE,
|
||||
author VARCHAR(255),
|
||||
status VARCHAR(255)
|
||||
);
|
||||
|
||||
```
|
||||
|
||||
## Inserting values
|
||||
|
||||
`Library` table:
|
||||
```sql
|
||||
INSERT INTO Library VALUES (1, 'Berserk', TO_DATE('2024-07-28','YYYY-MM-DD'), 'Prashant');
|
||||
INSERT INTO Library VALUES (2, 'Dark', TO_DATE('2024-07-15','YYYY-MM-DD'), 'Rajendra');
|
||||
INSERT INTO Library VALUES (3, 'Hannibal', TO_DATE('2024-07-20','YYYY-MM-DD'), 'Manoj');
|
||||
INSERT INTO Library VALUES (4, 'AOT', TO_DATE('2024-07-30','YYYY-MM-DD'), 'Rajesh');
|
||||
INSERT INTO Library VALUES (5, 'GOT', TO_DATE('2024-07-19','YYYY-MM-DD'), 'Anil');
|
||||
|
||||
```
|
||||
|
||||
## Trigger
|
||||
|
||||
```sql
|
||||
CREATE OR REPLACE TRIGGER library_action
|
||||
AFTER INSERT OR UPDATE OR DELETE ON Library
|
||||
FOR EACH ROW
|
||||
BEGIN
|
||||
IF INSERTING THEN
|
||||
INSERT INTO Library_Audit (id, title, dateofaction, author, status) VALUES (:NEW.id, :NEW.title, current_timestamp, :NEW.author, 'Insert');
|
||||
ELSIF UPDATING THEN
|
||||
INSERT INTO Library_Audit (id, title, dateofaction, author, status) VALUES (:OLD.id, :OLD.title, current_timestamp, :OLD.author, 'Update');
|
||||
ELSIF DELETING THEN
|
||||
INSERT INTO Library_Audit (id, title, dateofaction, author, status) VALUES (:OLD.id, :OLD.title, current_timestamp, :OLD.author, 'Delete');
|
||||
END IF;
|
||||
END;
|
||||
/
|
||||
|
||||
```
|
||||
|
||||
## Testing trigger
|
||||
|
||||
### Insert operation
|
||||
|
||||
```sql
|
||||
INSERT INTO Library VALUES (15, 'CREW', TO_DATE('2024-07-22','YYYY-MM-DD'), 'Ramesh');
|
||||
INSERT INTO Library VALUES (14, 'Ninteen Eighty Four', TO_DATE('2024-07-01','YYYY-MM-DD'), 'Omkar');
|
||||
|
||||
SELECT * FROM Library;
|
||||
SELECT * FROM Library_Audit;
|
||||
|
||||
```
|
||||
### Update operation
|
||||
|
||||
```sql
|
||||
UPDATE Library SET id = 6, title = 'Sherlock', author = 'Deepak' where id = 3;
|
||||
UPDATE Library SET id = 7, title = 'MR. ROBOT', author = 'Varad' where id = 4;
|
||||
|
||||
SELECT * FROM Library;
|
||||
SELECT * FROM Library_Audit;
|
||||
|
||||
```
|
||||
|
||||
### Delete operation
|
||||
|
||||
```sql
|
||||
DELETE FROM Library WHERE id = 1;
|
||||
DELETE FROM Library WHERE id = 5;
|
||||
|
||||
SELECT * FROM Library;
|
||||
SELECT * FROM Library_Audit;
|
||||
|
||||
```
|
||||
|
||||
---
|
Loading…
Reference in New Issue
Block a user