From 0c9633f0fa63db78a1e7a00e46b8c51fb9803876 Mon Sep 17 00:00:00 2001 From: "github-classroom[bot]" <66690702+github-classroom[bot]@users.noreply.github.com> Date: Mon, 29 May 2023 07:43:41 +0000 Subject: [PATCH 1/5] Setting up GitHub Classroom Feedback From e2841b04d1cf7ccdc28e2f4ce633d89a24d14831 Mon Sep 17 00:00:00 2001 From: Irhamul Islam_1907093 <126981963+gg93a@users.noreply.github.com> Date: Mon, 29 May 2023 13:45:00 +0600 Subject: [PATCH 2/5] Add files via upload Database Project of 1907093 --- ddl-1907093.sql | 67 ++++++++++++ dml-1907093.sql | 202 +++++++++++++++++++++++++++++++++++ pl sql-1907093.sql | 254 +++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 523 insertions(+) create mode 100644 ddl-1907093.sql create mode 100644 dml-1907093.sql create mode 100644 pl sql-1907093.sql diff --git a/ddl-1907093.sql b/ddl-1907093.sql new file mode 100644 index 0000000..5b8090f --- /dev/null +++ b/ddl-1907093.sql @@ -0,0 +1,67 @@ +--dropping existing tables + + +drop table feedback; +drop table subscribers; +drop table news; +drop table topic; +drop table journalist; + +--creating table + +create table topic( + topic_id int, +title varchar(20), +primary key(topic_id) +); + +create table journalist( + j_id int, + j_name varchar(20), + primary key(j_id) +); + +create table news( + news_id int, + description varchar(50), + n_date date, + topic_id int, + j_id int, + primary key(news_id), + foreign key(topic_id) references topic(topic_id), + foreign key(j_id) references journalist(j_id) +); + +create table subscribers( + sub_id int, + sub_name varchar(20), + sub_age int, + sub_type varchar(20), + primary key(sub_id) +); + +create table feedback( + news_id int, + sub_id int, + reaction varchar(20), + comments varchar(20), + primary key(news_id,sub_id), + foreign key(news_id) references news(news_id) on delete cascade, + foreign key(sub_id) references subscribers(sub_id) on delete cascade +); + +--adding column in table + +alter table topic add category char(50); + +--modify column in table + +alter table topic modify category varchar(20); + +--rename column in table + +alter table topic rename column category to genre; + +--drop column from table + +alter table topic drop column genre; \ No newline at end of file diff --git a/dml-1907093.sql b/dml-1907093.sql new file mode 100644 index 0000000..1992202 --- /dev/null +++ b/dml-1907093.sql @@ -0,0 +1,202 @@ +set linesize 1000 +set pagesize 400 + +--inserting values in table + +insert into topic(topic_id,title) values(11,'politics'); +insert into topic(topic_id,title) values(12,'business'); +insert into topic(topic_id,title) values(13,'international'); +insert into topic(topic_id,title) values(14,'economics'); +insert into topic(topic_id,title) values(15,'sports'); +insert into topic(topic_id,title) values(16,'entertainment'); +insert into topic(topic_id,title) values(17,'environment'); +insert into topic(topic_id,title) values(18,'education'); +insert into topic(topic_id,title) values(19,'agriculture'); +insert into topic(topic_id,title) values(20,'economics'); + +insert into journalist(j_id,j_name) values(101,'raya bacchan'); +insert into journalist(j_id,j_name) values(102,'akbar hossain'); +insert into journalist(j_id,j_name) values(103,'angkita biswas'); +insert into journalist(j_id,j_name) values(104,'rahat mollick'); +insert into journalist(j_id,j_name) values(105,'ahnaf saber'); +insert into journalist(j_id,j_name) values(106,'nihal mojumder'); +insert into journalist(j_id,j_name) values(107,'james anderson'); +insert into journalist(j_id,j_name) values(108,'zamshed islam'); +insert into journalist(j_id,j_name) values(109,'dibbo dey'); +insert into journalist(j_id,j_name) values(110,'sakif zawad'); + +insert into news(news_id,description,n_date,topic_id,j_id) values (1,'a','21-dec-25',11,101); +insert into news(news_id,description,n_date,topic_id,j_id) values(2,'b','22-feb-17',12,102); +insert into news(news_id,description,n_date,topic_id,j_id) values(3,'c','24-mar-27',12,103); +insert into news(news_id,description,n_date,topic_id,j_id) values(4,'d','22-nov-23',16,104); +insert into news(news_id,description,n_date,topic_id,j_id) values(5,'e','11-feb-12',18,105); +insert into news(news_id,description,n_date,topic_id,j_id) values(6,'f','11-oct-29',19,106); +insert into news(news_id,description,n_date,topic_id,j_id) values(7,'g','27-jul-15',14,107); +insert into news(news_id,description,n_date,topic_id,j_id) values(8,'f','22-jan-12',15,108); +insert into news(news_id,description,n_date,topic_id,j_id) values(9,'i','14-jan-16',16,109); +insert into news(news_id,description,n_date,topic_id,j_id) values(10,'j','18-jan-27',16,110); + + +insert into subscribers (sub_id,sub_name,sub_age,sub_type) values(51,'aboni',17,'child'); +insert into subscribers (sub_id,sub_name,sub_age,sub_type) values(52,'sami',24,'adult'); +insert into subscribers (sub_id,sub_name,sub_age,sub_type) values(53,'zakaria',21,'adult'); +insert into subscribers (sub_id,sub_name,sub_age,sub_type) values(54,'rafi',25,'adult'); +insert into subscribers (sub_id,sub_name,sub_age,sub_type) values(55,'mahatab',34,'adult'); +insert into subscribers (sub_id,sub_name,sub_age,sub_type) values(56,'miraj',51,'adult'); +insert into subscribers (sub_id,sub_name,sub_age,sub_type) values(57,'nazmul',62,'senior citizen'); +insert into subscribers (sub_id,sub_name,sub_age,sub_type) values(58,'rokon',68,'senior citizen'); +insert into subscribers (sub_id,sub_name,sub_age,sub_type) values(59,'sara',27,'adult'); +insert into subscribers (sub_id,sub_name,sub_age,sub_type) values(60,'simi',22,'adult'); + +insert into feedback (news_id,sub_id,reaction,comments) values(1,51,'like','good'); +insert into feedback (news_id,sub_id,reaction,comments) values(2,52,'love','impressive'); +insert into feedback (news_id,sub_id,reaction,comments) values(3,53,'love','good'); +insert into feedback (news_id,sub_id,reaction,comments) values(4,54,'like','good'); +insert into feedback (news_id,sub_id,reaction,comments) values(5,55,'dislike','bad'); +insert into feedback (news_id,sub_id,reaction,comments) values(6,56,'dislike','bad'); +insert into feedback (news_id,sub_id,reaction,comments) values(7,57,'angry','very unpleasent'); +insert into feedback (news_id,sub_id,reaction,comments) values(8,58,'love','praise worthy'); +insert into feedback (news_id,sub_id,reaction,comments) values(9,59,'like','amazing'); +insert into feedback (news_id,sub_id,reaction,comments) values(10,60,'angry','good'); + +--displaying data of table using select + +select *from subscribers where sub_name='simi'; +select *from news where news_id=1; + +--updating data in table + +--changing subscribers name +update subscribers set sub_name='natasha' where sub_id=53; +--changing journalist name +update journalist set j_name='anshul chauhan' where j_id=102; + + +--deleting row from table + +delete from feedback where news_id=10; +--to check if it's working +select *from feedback; +--inserting this again +insert into feedback (news_id,sub_id,reaction,comments) values(10,60,'angry','good'); + + +--UNION + +--SHOWING THE NAME OF THE JOURNALISTS WHOSE NAME STARTS WITH a OR HAS M IN IT +select j_name from journalist where j_name like 'a%' union select j_name from journalist where j_name like '%m%'; + +--INTERSECT + +--SHOWING THE NAME OF THE JOURNALISTS WHOSE NAME ENDS WITH 'N' AND HAS 'A' IN THE NAME +select j_name from journalist where j_name like '%n' intersect select j_name from journalist where j_name like '%a%'; + +--EXCEPT/MINUS + +--SHOWING THE NAME OF THE JOURNALISTS WHOSE NAME ENDS WITH 'N' BUT NO 'R' IN THE NAME +select j_name from journalist where j_name like '%n' minus select j_name from journalist where j_name like '%r%'; + + +--WITH CLAUSE + +--SHOWING INFO OF SUBSCRIBERS HAVING HIGHEST AGE USING WITH CLAUSE +with maximum as (select max(sub_age) as max_age from subscribers) +select * from subscribers,maximum where subscribers.sub_age=maximum.max_age; + +--AGGREGATE FUNCTION + +--TOTAL ROW IN TABLE +select count(*) from topic; + +--TOTAL UNIQUE(NAME) JOURNALISTS IN NAME +select count(distinct title) as titles from topic; + +--AVERAGE AGE OF SUBSCRIBERS TABLE +select avg(sub_age) from subscribers; + +--SUM OF AGE IN SUBSCRIBERS TABLE + +select sum(sub_age) from subscribers; + +--MAXIMUM AGE OF SUBSCRIBERS TABLE +select max(sub_age) from subscribers; + +--MINIMUM AGE OF SUBSCRIBERS TABLE +select min(sub_age) from subscribers; + +--NESTED SUBQUERY + +--SHOWING TOPIC TITLE OF A JOURNALIST +select title from topic where topic_id=(select topic_id from news where j_id=(select j_id from journalist where j_name='raya bacchan')); + + +--SET MEMBERSHIP(IN,NOT IN) + +--SHOWING NAME OF TITLES HAVING 'S' IN THE END AND 'I' IN THE NAME +select title from topic where title like '%s' and title in (select title from topic where title like '%i%'); +select distinct(title) from topic where title like '%s' and title in (select title from topic where title like '%i%'); + + +--SHOWING NAME OF TITLES HAVING 'S' IN THE END BUT NO 'I' IN THE NAME +select title from topic where title like '%s' and title not in (select title from topic where title like '%i%'); +select distinct(title) from topic where title like '%s' and title not in (select title from topic where title like '%i%'); + + +--SOME,ALL + +--SHOWING SUBSCRIBERS HAVING AGE HIGHER THANT AT LEAST ONE MEMBER WITH LEAST AGE 25 +select sub_name from subscribers where sub_age>some(select sub_age from subscribers where sub_age>=25); + +--SHOWING SUBSCRIBERS HAVING AGE HIGHER THANT AT ALL MEMBERS WITH LEAST AGE 25 +select sub_name from subscribers where sub_age>all(select sub_age from subscribers where sub_age>=25); + +--SHOWING SUBSCRIBERS HAVING AGE HIGHER OR EQUAL THAN ALL MEMBERS WITH LEAST AGE 25 +select sub_name from subscribers where sub_age>=all(select sub_age from subscribers where sub_age>=25); + +--EXISTS,NOT EXISTS + +--FINDING A TITLE WITH LETTER 'N' IF THERE IS TITLE WITH LETTER 'B' +select title from topic where title like '%n%' and exists(select title from topic where title like '%b%'); + +--FINDING A TITLE WITH LETTER 'N' IF THERE IS NO TITLE WITH LETTER 'P' +select title from topic where title like '%n%' and not exists(select title from topic where title like '%p%'); + +--STRING OPERATION + +--FINDING INFO OF JOURNALISTS ENDING 'R' +select * from journalist where j_name like '%r'; + +--FINDING INFO OF COMMENTS IN FEEDBACK WITH LENGTH 4 +select * from feedback where comments like '____'; + +--JOIN OPERATION + +--NATURAL JOIN +select * from topic natural join news where title='politics'; + +--join +select * from topic join news on topic.title = 'politics' and topic.topic_id = news.topic_id; + +--left outer,right outer,full join + + +select title,n_date from topic left outer join news using(topic_id); +select title,n_date from topic right outer join news using(topic_id); +select title,n_date from topic full outer join news using(topic_id); + + +--CREATING A VIEW OF topic economics + +drop view econ; +create view econ as select * from topic natural join news where title='economics'; +select * from econ; + +--CREATING VIEW using econ + +drop view ONETWO; +create view ONETWO as select * from econ where title = 'economics'; +select * from ONETWO; + + + + diff --git a/pl sql-1907093.sql b/pl sql-1907093.sql new file mode 100644 index 0000000..6e8172f --- /dev/null +++ b/pl sql-1907093.sql @@ -0,0 +1,254 @@ +--pl/sql variable declaration + +--values from table to variables + +set serveroutput on +declare +id subscribers.sub_id%type; +name subscribers.sub_name%type; +age subscribers.sub_age%type; +begin +select sub_id,sub_name,sub_age into id,name,age from subscribers where sub_id=51; +dbms_output.put_line('ID: '|| id || ' Name: '|| name || ' AGE: '|| age); +end; +/ + +--Insert and set default value +delete from subscribers where sub_id = 51; +set serveroutput on +declare +id subscribers.sub_id%type:=51; +name subscribers.sub_name%type:='Aboni'; +age subscribers.sub_age%type:=17; +category subscribers.sub_type%type:='child'; + +begin +insert into subscribers values(id,name,age,category); +end; +/ + + +--Row type + +--taken values from table and stored using rowtype + +set serveroutput on +declare +subscribers_row subscribers%rowtype; +begin +select sub_id, sub_name, sub_age into subscribers_row.sub_id, subscribers_row.sub_name, subscribers_row.sub_age from subscribers where sub_id=51; +dbms_output.put_line('ID: '|| subscribers_row.sub_id || ' Name: '|| subscribers_row.sub_name || ' age: '|| subscribers_row.sub_age); +end; +/ + + +--cursor and row count + +--using cursor and finding the total number of rows + +set serveroutput on +declare +cursor c is select * from subscribers; +sub_row subscribers%rowtype; +begin +open c; +fetch c into sub_row.sub_id, sub_row.sub_name, sub_row.sub_age,sub_row.sub_type; + +while c%found loop +dbms_output.put_line('Id: '||sub_row.sub_id|| ' Name: '||sub_row.sub_name || ' Age: ' ||sub_row.sub_age); +dbms_output.put_line('Row count: '|| c%rowcount); +fetch c into sub_row.sub_id, sub_row.sub_name, sub_row.sub_age,sub_row.sub_type; +end loop; +close c; +end; +/ + + +--FOR LOOP/WHILE LOOP/ARRAY with extend() function + +--finding the title of the topics using loops and array with extend function + +set serveroutput on +declare + counter number; + t_title topic.title%type; + TYPE topicARRAY IS VARRAY(10) OF topic.title%type; + A_NAME topicARRAY:=topicARRAY(); +begin + counter:=1; + for x in 11..20 + loop + select title into t_title from topic where topic_id=x; + A_NAME.EXTEND(); + A_NAME(counter):=t_title; + counter:=counter+1; + end loop; + + counter:=1; + WHILE counter<=A_NAME.COUNT + LOOP + DBMS_OUTPUT.PUT_LINE(A_NAME(counter)); + counter:=counter+1; + END LOOP; +end; +/ + + +--ARRAY without extend() function + +--finding the title of the topics using loops and array without extend function + + +set serveroutput on +DECLARE + counter NUMBER := 1; + t_title topic.title%type; + TYPE topicARRAY IS VARRAY(10) OF topic.title%type; + A_NAME topicARRAY:=topicARRAY('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j'); + + -- VARRAY with a fixed size of 10 elements,initialized with some letters + +BEGIN + counter := 1; + FOR x IN 11..20 + LOOP + select title into t_title from topic where topic_id=x; + A_NAME(counter):=t_title; + counter := counter + 1; + END LOOP; + + counter := 1; + WHILE counter <= A_NAME.COUNT + LOOP + DBMS_OUTPUT.PUT_LINE(A_NAME(counter)); + counter := counter + 1; + END LOOP; +END; +/ + + +--if/else/ +--counting the number of age type of subscribers + +set serveroutput on +DECLARE + test1 number := 0; + test2 number := 0; + test3 number := 0; + + ageRange subscribers.sub_type%type; + +BEGIN + FOR x IN 51..60 + LOOP + select sub_type into ageRange from subscribers where sub_id=x; + if ageRange='adult' + then + test1 := test1 + 1; + elsif ageRange='child' + then + test2 := test2 + 1; + elsif ageRange= 'senior citizen' then + test3 := test3 + 1; + end if; + END LOOP; + + DBMS_OUTPUT.PUT_LINE('Adults are ' || test1); + DBMS_OUTPUT.PUT_LINE('Child are ' || test2); + DBMS_OUTPUT.PUT_LINE('Senior citizen are ' || test3); + + END; +/ + + +--procedure + +--subscribers having age greater than given age(only IN) + +CREATE OR REPLACE PROCEDURE GreaterAge( + given_age IN NUMBER +) +as +cursor c is select * from subscribers; +row subscribers%rowtype; + +begin +open c; +fetch c into row.sub_id, row.sub_name, row.sub_age, row.sub_type; + +while c%found loop +if row.sub_age >given_age then +dbms_output.put_line('Name: ' || row.sub_name || ' Age: ' || row.sub_age); + fetch c into row.sub_id, row.sub_name, row.sub_age, row.sub_type; + +else +fetch c into row.sub_id, row.sub_name, row.sub_age, row.sub_type; +end if; + +end loop; + +close c; +end; +/ + + +set serveroutput on +declare +given_age subscribers.sub_age%type:=20; +begin +GreaterAge(given_age); +end; +/ + + +--subscriber with the max age using procedure(OUT parameter only) + +CREATE OR REPLACE PROCEDURE maxAge( + op_age out subscribers.sub_age%type, + op_name out subscribers.sub_name%type +) +as +begin +select max(sub_age) into op_age from subscribers; + +select sub_name into op_name from subscribers where sub_age = op_age; +dbms_output.put_line('Name: ' || op_name || ' Age: ' || op_age); +end; +/ + + +set serveroutput on +declare +op_age subscribers.sub_age%type; + op_name subscribers.sub_name%type; +begin +maxAge(op_age, op_name); +end; +/ + + +--function + +--returning the number of users having higher age than given (in out parameter is used) + +set serveroutput on +create or replace function higher(var1 in out number) return number AS +begin + select count(*) into var1 from subscribers where subscribers.sub_age> var1; + return var1; +end; +/ + +set serveroutput on +declare +var1 number:= 20; +var2 number:=20; +begin +dbms_output.put_line('Subscribers older than ' || var2 ||' :'|| higher(var1)); +end; +/ + + + + + From 53eb7611df3fb3330e9f98c0eafb1800103cea9d Mon Sep 17 00:00:00 2001 From: Irhamul Islam_1907093 <126981963+Irham1907093@users.noreply.github.com> Date: Fri, 2 Jun 2023 10:41:38 +0600 Subject: [PATCH 3/5] Create README.md Showing details of the project --- README.md | 7 +++++++ 1 file changed, 7 insertions(+) create mode 100644 README.md diff --git a/README.md b/README.md new file mode 100644 index 0000000..0f67676 --- /dev/null +++ b/README.md @@ -0,0 +1,7 @@ +Project Name : Newspaper Management System +DDL,DML and PL-SQL files are uploaded separately + +Submitted By: +Irhamul Islam +1907093 +Year:3rd Semester:1st From 0d75f66fd874f70db971aeda4e9932022e4eccbe Mon Sep 17 00:00:00 2001 From: Irhamul Islam_1907093 <126981963+Irham1907093@users.noreply.github.com> Date: Fri, 2 Jun 2023 10:52:03 +0600 Subject: [PATCH 4/5] Update README.md --- README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/README.md b/README.md index 0f67676..41053fb 100644 --- a/README.md +++ b/README.md @@ -5,3 +5,5 @@ Submitted By: Irhamul Islam 1907093 Year:3rd Semester:1st + +Assalamualaikum Sir,Please note that my Username was 'gg93a' when I have uploaded the files.It was changed to 'irham1907093' later on 2 June,2023. From be619f9ff72a3bd8261d0804db6095e595831904 Mon Sep 17 00:00:00 2001 From: Irhamul Islam_1907093 <126981963+Irham1907093@users.noreply.github.com> Date: Sun, 9 Jul 2023 01:35:57 +0600 Subject: [PATCH 5/5] Add files via upload --- trigger.sql | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 trigger.sql diff --git a/trigger.sql b/trigger.sql new file mode 100644 index 0000000..9f63d91 --- /dev/null +++ b/trigger.sql @@ -0,0 +1,17 @@ +create trigger try +after update on topic +referencing old as o new as n +for each row +begin +update news set topic_id = :n.topic_id where topic_id = :o.topic_id; +end; +/ + +create trigger try2 +before delete on news +referencing old as o new as n +for each row +begin +update feedback set news_id = :n.news_id where news_id = :o.news_id; +end; +/ \ No newline at end of file