diff --git a/project/DDL.sql b/project/DDL.sql new file mode 100644 index 0000000..9ef804f --- /dev/null +++ b/project/DDL.sql @@ -0,0 +1,75 @@ +--drop tables & views +drop view admission; +drop table eligible; +drop table student; +drop table examinee; +drop table profile; +drop table applicant; +drop table department; + + +--profile +create table profile( + reg_no varchar(30) primary key, + hsc_roll varchar(30) not null, + name varchar(30) not null, + father_name varchar(30) not null, + mother_name varchar(30) not null, + birth_date date not null, + address varchar(30) not null, + email varchar(30) unique not null, + religion varchar(30) not null check (religion in ('Islam','Hindu','Budhha','Christ')) +); + +--applicant +create table applicant( + applicant_id varchar(30) primary key, + exam_venue varchar(30) not null, + apply_time timestamp not null +); + +--eligible +create table eligible( + reg_no varchar(30), + applicant_id varchar(30), + hsc_mark numeric(3) not null check (hsc_mark<=600), + hsc_gpa numeric(3,2) not null check(hsc_gpa<=5.00), + ssc_gpa numeric(3,2) not null check(ssc_gpa<=5.00), + eligibility varchar(5) not null check(eligibility in ('yes','no')), + foreign key (reg_no) references profile (reg_no), + foreign key (applicant_id) references applicant (applicant_id) + on delete cascade +); + +--examinee +create table examinee( + applicant_id varchar(30), + phy_mark numeric(3) not null check (phy_mark<=150), + chem_mark numeric(3) not null check (chem_mark<=150), + math_mark numeric(3) not null check (math_mark<=150), + eng_mark numeric(3) not null check (eng_mark<=50), + gpa_mark numeric(3) not null check (gpa_mark<=100), + total_mark numeric(3) check (total_mark<=600), + merit_place numeric(5) primary key, + foreign key (applicant_id) references applicant(applicant_id) + on delete cascade +); + +--department +create table department( + roll_no varchar(30) primary key, + dept_name varchar(30) not null, + dept_id numeric(2) not null, + faculty varchar(30) not null +); + +--student +create table student( + merit_place numeric(5) not null, + roll_no varchar(30) not null, + dept_choice varchar(30) not null, + priority numeric(2) not null, + foreign key (merit_place) references examinee(merit_place), + foreign key (roll_no) references department(roll_no) + on delete cascade +); diff --git a/project/DML.sql b/project/DML.sql new file mode 100644 index 0000000..2b480d3 --- /dev/null +++ b/project/DML.sql @@ -0,0 +1,59 @@ +--pagesize & linesize +set pagesize 1500; +set linesize 300; + +-- select +select * from profile; +select name from profile; + +--update & delete +INSERT INTO applicant VALUES ('A016', 'Venue1', timestamp '2023-05-20 09:00:00'); +update applicant set exam_venue = 'Venue5' where applicant_id = 'A016'; +delete from applicant where applicant_id = 'A016'; + +--union, intersect, except +select applicant_id,apply_time from applicant where extract (hour from apply_time)>10 +union +select applicant_id,apply_time from applicant where extract (hour from apply_time)<16; + +--with clause +with avg_number(hsc_mark) as (select avg(hsc_mark) from eligible) +select * from eligible,avg_number where eligible.hsc_mark > avg_number.hsc_mark; + +--aggregate +select count (distinct hsc_gpa) as gpa_hsc from eligible; +select max (total_mark) as exam_marks from examinee; + +--group by & having +select faculty,count(distinct dept_name) as number_of_dept from department group by faculty; +select faculty,count(distinct dept_name) as number_of_dept from department group by faculty having count(dept_name) > 2; + +--nested subquery +select name from profile where reg_no = +(select reg_no from eligible where applicant_id = +(select applicant_id from examinee where merit_place = 1 )); + +--set membership +select name from profile where reg_no in +(select reg_no from eligible where applicant_id in +(select applicant_id from examinee where merit_place >=1 and merit_place <=5 )); + +--string +select dept_name,faculty from department where dept_name like '%Engineer%'; + +--join operations +select applicant_id,total_mark,merit_place,roll_no from examinee natural join student order by merit_place asc; +select name,merit_place from profile join eligible on profile.reg_no = eligible.reg_no join examinee on eligible.applicant_id = examinee.applicant_id where merit_place<4 order by merit_place asc; + +--views +create view admission as select name,p.reg_no,a.applicant_id,hsc_gpa,ssc_gpa,gpa_mark,phy_mark,math_mark,chem_mark,eng_mark,total_mark, +e.merit_place,s.roll_no,dept_name,dept_id,faculty from profile p join eligible eg on p.reg_no = eg.reg_no +join applicant a on a.applicant_id = eg.applicant_id +join examinee e on e.applicant_id = a.applicant_id +join student s on s.merit_place = e.merit_place +join department d on d.roll_no = s.roll_no; +<<<<<<< HEAD +select * from admission; +======= +select * from admission; +>>>>>>> d3c61a4cb8141bb22c264f82bf7fb81d38d7b22f diff --git a/project/INSERT_DATA.sql b/project/INSERT_DATA.sql new file mode 100644 index 0000000..6a9af9c --- /dev/null +++ b/project/INSERT_DATA.sql @@ -0,0 +1,89 @@ +--INSERT INTO profile (reg_no, hsc_roll, name, father_name, mother_name, birth_date, address, email, religion) VALUES +INSERT INTO profile VALUES ('R001', 'HSC001', 'John Doe', 'Michael Doe', 'Jane Doe', date '2000-01-01', '123 Main St', 'johndoe@example.com', 'Islam'); +INSERT INTO profile VALUES ('R002', 'HSC002', 'Jane Smith', 'David Smith', 'Mary Smith', date '1998-05-15', '456 Elm St', 'janesmith@example.com', 'Hindu'); +INSERT INTO profile VALUES ('R003', 'HSC003', 'Robert Johnson', 'Thomas Johnson', 'Sarah Johnson', date '1999-08-22', '789 Oak St', 'robertjohnson@example.com', 'Budhha'); +INSERT INTO profile VALUES ('R004', 'HSC004', 'Emily Davis', 'Daniel Davis', 'Laura Davis', date '2001-03-10', '321 Pine St', 'emilydavis@example.com', 'Christ'); +INSERT INTO profile VALUES ('R005', 'HSC005', 'Michael Wilson', 'John Wilson', 'Emily Wilson', date '2002-07-17', '567 Maple St', 'michaelwilson@example.com', 'Islam'); +INSERT INTO profile VALUES ('R006', 'HSC006', 'Sarah Thompson', 'James Thompson', 'Jennifer Thompson', date '1999-11-05', '890 Cedar St', 'sarahthompson@example.com', 'Hindu'); +INSERT INTO profile VALUES ('R007', 'HSC007', 'Matthew Lee', 'Brian Lee', 'Karen Lee', date '1999-02-28', '234 Birch St', 'matthewlee@example.com', 'Budhha'); +INSERT INTO profile VALUES ('R008', 'HSC008', 'Olivia Martinez', 'Joseph Martinez', 'Maria Martinez', date '2000-06-12', '678 Oak St', 'oliviamartinez@example.com', 'Christ'); +INSERT INTO profile VALUES ('R009', 'HSC009', 'Christopher Harris', 'William Harris', 'Jessica Harris', date '2000-09-20', '912 Pine St', 'christopherharris@example.com', 'Islam'); +INSERT INTO profile VALUES ('R010', 'HSC010', 'Ava Clark', 'Christopher Clark', 'Samantha Clark', date '2001-12-08', '345 Maple St', 'avaclark@example.com', 'Hindu'); +INSERT INTO profile VALUES ('R011', 'HSC011', 'David Rodriguez', 'Andrew Rodriguez', 'Elizabeth Rodriguez', date '1998-04-18', '567 Cedar St', 'davidrodriguez@example.com', 'Budhha'); +INSERT INTO profile VALUES ('R012', 'HSC012', 'Sophia Walker', 'Jason Walker', 'Kimberly Walker', date '1999-07-25', '890 Birch St', 'sophiawalker@example.com', 'Christ'); +INSERT INTO profile VALUES ('R013', 'HSC013', 'Daniel Green', 'Mark Green', 'Patricia Green', date '2002-10-14', '123 Oak St', 'danielgreen@example.com', 'Islam'); +INSERT INTO profile VALUES ('R014', 'HSC014', 'Mia Lewis', 'Robert Lewis', 'Susan Lewis', date '2001-01-31', '456 Pine St', 'mialewis@example.com', 'Hindu'); +INSERT INTO profile VALUES ('R015', 'HSC015', 'Andrew Hill', 'David Hill', 'Nancy Hill', date '2000-06-27', '789 Maple St', 'andrewhill@example.com','Islam'); + +--INSERT INTO applicant (applicant_id, exam_venue, apply_time) VALUES +INSERT INTO applicant VALUES ('A001', 'Venue1', timestamp '2023-05-18 09:00:00'); +INSERT INTO applicant VALUES ('A002', 'Venue2', timestamp '2023-05-18 10:15:00'); +INSERT INTO applicant VALUES ('A003', 'Venue1', timestamp '2023-05-18 11:30:00'); +INSERT INTO applicant VALUES ('A004', 'Venue3', timestamp '2023-05-18 13:00:00'); +INSERT INTO applicant VALUES ('A005', 'Venue2', timestamp '2023-05-18 14:30:00'); +INSERT INTO applicant VALUES ('A006', 'Venue4', timestamp '2023-05-18 15:45:00'); +INSERT INTO applicant VALUES ('A007', 'Venue3', timestamp '2023-05-18 17:00:00'); +INSERT INTO applicant VALUES ('A008', 'Venue5', timestamp '2023-05-19 09:30:00'); +INSERT INTO applicant VALUES ('A009', 'Venue2', timestamp '2023-05-19 10:45:00'); +INSERT INTO applicant VALUES ('A010', 'Venue1', timestamp '2023-05-19 12:00:00'); +INSERT INTO applicant VALUES ('A011', 'Venue4', timestamp '2023-05-19 13:15:00'); +INSERT INTO applicant VALUES ('A012', 'Venue5', timestamp '2023-05-19 14:30:00'); +INSERT INTO applicant VALUES ('A013', 'Venue3', timestamp '2023-05-19 15:45:00'); +INSERT INTO applicant VALUES ('A014', 'Venue2', timestamp '2023-05-19 17:00:00'); +INSERT INTO applicant VALUES ('A015', 'Venue1', timestamp '2023-05-20 09:00:00'); + +--INSERT INTO eligible INSERT INTO eligible VALUES (reg_no, applicant_id, hsc_mark, hsc_gpa, ssc_gpa, eligibility) VALUES +INSERT INTO eligible VALUES ('R001', 'A001', 580, 4.50, 4.00, 'yes'); +INSERT INTO eligible VALUES ('R002', 'A002', 550, 4.00, 3.50, 'yes'); +INSERT INTO eligible VALUES ('R003', 'A003', 600, 5.00, 4.50, 'yes'); +INSERT INTO eligible VALUES ('R004', 'A004', 540, 3.75, 3.25, 'no'); +INSERT INTO eligible VALUES ('R005', 'A005', 590, 4.80, 4.20, 'yes'); +INSERT INTO eligible VALUES ('R006', 'A006', 510, 3.50, 3.00, 'yes'); +INSERT INTO eligible VALUES ('R007', 'A007', 570, 4.30, 3.75, 'yes'); +INSERT INTO eligible VALUES ('R008', 'A008', 480, 3.00, 2.75, 'no'); +INSERT INTO eligible VALUES ('R009', 'A009', 560, 4.20, 3.50, 'yes'); +INSERT INTO eligible VALUES ('R010', 'A010', 520, 3.80, 3.25, 'yes'); +INSERT INTO eligible VALUES ('R011', 'A011', 590, 4.80, 4.20, 'yes'); +INSERT INTO eligible VALUES ('R012', 'A012', 500, 3.25, 2.75, 'no'); +INSERT INTO eligible VALUES ('R013', 'A013', 550, 4.00, 3.50, 'yes'); +INSERT INTO eligible VALUES ('R014', 'A014', 580, 4.50, 4.00, 'yes'); +INSERT INTO eligible VALUES ('R015', 'A015', 530, 3.60, 3.00, 'yes'); + +--INSERT INTO examinee (applicant_id, phy_mark, chem_mark, math_mark, eng_mark, gpa_mark, total_mark, merit_place) VALUES +INSERT INTO examinee VALUES ('A001', 120, 140, 145, 40, 85, 530, 2); +INSERT INTO examinee VALUES ('A002', 110, 130, 135, 38, 75, 488, 6); +INSERT INTO examinee VALUES ('A003', 145, 150, 150, 48, 95, 588, 1); +INSERT INTO examinee VALUES ('A005', 100, 120, 125, 35, 90, 470, 8); +INSERT INTO examinee VALUES ('A006', 130, 145, 140, 42, 65, 522, 4); +INSERT INTO examinee VALUES ('A007', 90, 110, 115, 32, 80, 427, 10); +INSERT INTO examinee VALUES ('A009', 115, 135, 130, 37, 77, 494, 5); +INSERT INTO examinee VALUES ('A010', 80, 100, 105, 30, 71, 386, 11); +INSERT INTO examinee VALUES ('A011', 105, 125, 120, 36, 90, 476, 7); +INSERT INTO examinee VALUES ('A013', 95, 115, 110, 33, 75, 428, 9); +INSERT INTO examinee VALUES ('A014', 125, 140, 135, 41, 85, 526, 3); +INSERT INTO examinee VALUES ('A015', 85, 105, 100, 29, 66, 385, 12); + +--INSERT INTO department (roll_no, dept_name, dept_id, faculty) VALUES +INSERT INTO department VALUES ('R001', 'Computer Science', 07, 'Faculty of Science'); +INSERT INTO department VALUES ('R002', 'Electrical Engineering', 01, 'Faculty of Engineering'); +INSERT INTO department VALUES ('R003', 'Mechanical Engineering', 05, 'Faculty of Engineering'); +INSERT INTO department VALUES ('R004', 'Chemistry', 15, 'Faculty of Science'); +INSERT INTO department VALUES ('R005', 'Physics', 13, 'Faculty of Science'); +INSERT INTO department VALUES ('R006', 'English Literature', 19, 'Faculty of Arts'); +INSERT INTO department VALUES ('R007', 'Business Administration', 17, 'Faculty of Business'); +INSERT INTO department VALUES ('R008', 'Mathematics', 09, 'Faculty of Science'); +INSERT INTO department VALUES ('R009', 'Civil Engineering', 03, 'Faculty of Engineering'); +INSERT INTO department VALUES ('R010', 'Biology', 11, 'Faculty of Science'); + +--INSERT INTO student (merit_place, roll_no, dept_choice, priority) VALUES +INSERT INTO student VALUES (1, 'R001', 'Computer Science', 1); +INSERT INTO student VALUES (2, 'R002', 'Electrical Engineering', 1); +INSERT INTO student VALUES (3, 'R003', 'Mechanical Engineering', 1); +INSERT INTO student VALUES (4, 'R004', 'Chemistry', 2); +INSERT INTO student VALUES (5, 'R005', 'Physics', 2); +INSERT INTO student VALUES (6, 'R006', 'English Literature', 3); +INSERT INTO student VALUES (7, 'R007', 'Business Administration', 5); +INSERT INTO student VALUES (8, 'R008', 'Mathematics', 4); +INSERT INTO student VALUES (9, 'R009', 'Civil Engineering', 1); +INSERT INTO student VALUES (10, 'R010', 'Biology', 6); + diff --git a/project/PLSQL.sql b/project/PLSQL.sql new file mode 100644 index 0000000..7983458 --- /dev/null +++ b/project/PLSQL.sql @@ -0,0 +1,137 @@ +--variable declare and print value +set serveroutput on +declare +roll department.roll_no%type; +dept department.dept_name%type; +faculty department.faculty%type; +begin +select dept_name,roll_no,faculty into dept,roll,faculty from department where dept_id=07; +dbms_output.put_line('Roll: '||roll||' Dept: '||dept||' Faculty: '||faculty); +end; +/ + +--insert and set default value +set serveroutput on +declare +roll department.roll_no%type:='R016'; +dept department.dept_name%type:='CSE'; +faculty department.faculty%type:='EEE'; +id department.dept_id%type:='07'; +begin +insert into department VALUES(roll,dept,id,faculty); +end; +/ +select * from department where roll_no='R016'; + +--row type +set serveroutput on +declare +dept_row department%rowtype; +begin +select roll_no, dept_name, dept_id, faculty into dept_row +from department where roll_no='R010'; +dbms_output.put_line('Roll: '||dept_row.roll_no||' Dept: '||dept_row.dept_name||' Faculty: '||dept_row.faculty); +end; +/ + +--cursor and row count with while loop +set serveroutput on +declare +cursor c is select roll_no,dept_name from department; +dept_row department%rowtype; +begin +open c; +fetch c into dept_row.roll_no,dept_row.dept_name; +while c%found loop +dbms_output.put_line('Roll: '||dept_row.roll_no||' Dept: '||dept_row.dept_name); +dbms_output.put_line('Count: '||c%rowcount); +fetch c into dept_row.roll_no,dept_row.dept_name; +end loop; +close c; +end; +/ + +--if/elseif/else +set serveroutput on +declare +cursor c is select reg_no,hsc_mark from eligible; +e_row eligible%rowtype; +begin +open c; +fetch c into e_row.reg_no, e_row.hsc_mark; +while c%found loop +if e_row.hsc_mark>540 +then dbms_output.put_line('Reg_no: '||e_row.reg_no||' HSC_Mark: '||e_row.hsc_mark||'Student rating: Outstanding'); +elsif e_row.hsc_mark>500 +then dbms_output.put_line('Reg_no: '||e_row.reg_no||' HSC_Mark: '||e_row.hsc_mark||'Student rating: Good'); +else +dbms_output.put_line('Reg_no: '||e_row.reg_no||' HSC_Mark: '||e_row.hsc_mark||'Student rating: Medium'); +end if; +dbms_output.put_line('Count: '||c%rowcount); +fetch c into e_row.reg_no, e_row.hsc_mark; +end loop; +close c; +end; +/ + +--procedure +create or replace procedure proc( + merit in examinee.merit_place%type, + id out examinee.applicant_id%type, + marks out examinee.total_mark%type +) is var varchar(30); +begin +select applicant_id,total_mark into id,marks from examinee where merit_place=merit; +end; +/ + +set serveroutput on +declare +merit examinee.merit_place%type:=1; +id examinee.applicant_id%type; +mark examinee.total_mark%type; +begin +proc(merit,id,mark); +dbms_output.put_line('Applicant_id: '||id||' Marks: '||mark||' Merit_place: '||merit); +end; +/ + +--function +create or replace function fun( + roll in department.roll_no%type +) return varchar as faculty department.faculty%type; +begin +select faculty into faculty from department where roll_no=roll; +return faculty; +end; +/ + +set serveroutput on +declare +roll department.roll_no%type:='R001'; +faculty department.faculty%type; +dept department.dept_name%type; +cursor c is select dept_name,faculty from department where faculty= fun(roll); +begin +open c; +fetch c into dept,faculty; +while c%found loop +dbms_output.put_line('Dept: '||dept||' Faculty: '||faculty); +fetch c into dept,faculty; +end loop; +close c; +end; +/ + +--drop procedur / function +drop procedure proc; +drop function fun; + +--trigger +CREATE or REPLACE TRIGGER update_total_mark +BEFORE INSERT OR UPDATE ON examinee +REFERENCING OLD AS O NEW AS n +FOR EACH ROW +BEGIN + n.total_mark := n.phy_mark + n.chem_mark + n.math_mark + n.eng_mark + n.gpa_mark; +END; diff --git a/project/marks.sql b/project/marks.sql new file mode 100644 index 0000000..52639f0 --- /dev/null +++ b/project/marks.sql @@ -0,0 +1,26 @@ +create or replace procedure printMark(elg in eligible.eligibility%type) as +cursor mark is select applicant_id,phy_mark,chem_mark,math_mark from examinee where applicant_id in (select applicant_id from eligible where eligibility=elg); +id examinee.applicant_id%type; +pM examinee.phy_mark%type; +cM examinee.chem_mark%type; +mM examinee.math_mark%type; +begin +open mark; +fetch mark into id,pM,cM,mM; +while mark%found loop +dbms_output.put_line('Applicant id: '||id||' Phy_Mark: '||pM||' Chem_Mark: '||cM||' Math_Mark: '||mM); +fetch mark into id,pM,cM,mM; +end loop; +close mark; +end; +/ + +set serveroutput on +declare +elg eligible.eligibility%type:='yes'; +begin +printMark(elg); +end; +/ + +select applicant_id,eligibility from eligible; \ No newline at end of file