Assignment 2 DATABASE (6)

.docx

School

Lambton College *

*We aren’t endorsed by this school

Course

4203

Subject

Computer Science

Date

Apr 3, 2024

Type

docx

Pages

10

Uploaded by hihjk

Report
CSD-4203 Database Programming Practice Exercises_2 [4%] Instruction: Include FULL Screenshot that display your code and output on Apex Exercise 1 (1%) A. Create an anonymous block that returns the number of students in a section. Prompt for section id. Name the file P2.1.sql. Set SERVEROUTPUT ON; create table students (STUDENTNO NUMBER(4), STUDENTNAME VARCHAR2(30), MAJOR VARCHAR2(50), SECTION NUMBER(5), COURSE VARCHAR2(30), PROFFESSOR VARCHAR2(20), GRADE NUMBER(3) ); INSERT INTO students(STUDENTNAME,MAJOR,SECTION,COURSE,PROFFESSOR,GRADE) values ('Richa','Computer programmer',10001,'Front End','Sam salvatore',93); INSERT INTO students(STUDENTNAME,MAJOR,SECTION,COURSE,PROFFESSOR,GRADE) values ('Laura','Computer programmer',10001,'Front End','Sam salvatore',91); INSERT INTO students(STUDENTNAME,MAJOR,SECTION,COURSE,PROFFESSOR,GRADE) values ('Katherine','Computer programmer',10001,'Front End','Sam salvatore',96); INSERT INTO students(STUDENTNAME,MAJOR,SECTION,COURSE,PROFFESSOR,GRADE) values ('Elena','Computer programmer',10001,'Front End','Sam salvatore',89); INSERT INTO students(STUDENTNAME,MAJOR,SECTION,COURSE,PROFFESSOR,GRADE) values ('Damon','Computer programmer',10001,'Front End','Sam salvatore',96); DECLARE v_section_id NUMBER; v_student_no NUMBER;
BEGIN v_section_id := &section; SELECT COUNT(*) INTO v_student_no FROM students WHERE section = v_section_id; DBMS_OUTPUT.PUT_LINE('There are '||v_student_no||' students in the section '|| v_section_id); END; B. Create an anonymous block that return the average numeric grade for a section. Prompt for section id and return the average grade. Name the file P2.2.sql. set SERVEROUTPUT ON; create table students1 (STUDENTNO NUMBER(4),
STUDENTNAME VARCHAR2(30), MAJOR VARCHAR2(50), SECTION NUMBER(5), COURSE VARCHAR2(30), PROFFESSOR VARCHAR2(20), GRADE NUMBER(3) ); INSERT INTO students1(STUDENTNAME,MAJOR,SECTION,COURSE,PROFFESSOR,GRADE) values ('Richa','Computer programmer',10001,'Front End','Sam salvatore',93); INSERT INTO students1(STUDENTNAME,MAJOR,SECTION,COURSE,PROFFESSOR,GRADE) values ('Laura','Computer programmer',10001,'Front End','Sam salvatore',91); INSERT INTO students1(STUDENTNAME,MAJOR,SECTION,COURSE,PROFFESSOR,GRADE) values ('Katherine','Computer programmer',10001,'Front End','Sam salvatore',96); INSERT INTO students1(STUDENTNAME,MAJOR,SECTION,COURSE,PROFFESSOR,GRADE) values ('Elena','Computer programmer',10001,'Front End','Sam salvatore',89); INSERT INTO students1(STUDENTNAME,MAJOR,SECTION,COURSE,PROFFESSOR,GRADE) values ('Damon','Computer programmer',10001,'Front End','Sam salvatore',96); DECLARE v_section_id NUMBER; v_average_grade NUMBER; BEGIN v_section_id := &section_id; SELECT AVG(GRADE) INTO v_average_grade FROM students1 WHERE section = v_section_id; DBMS_OUTPUT.PUT_LINE('The Average Grade in section ' || v_section_id || ' is ' || TO_CHAR(v_average_grade, '999.99')); END; Exercise 2 (1%) Complete the following: Create a view called gl_stdV1 that returns the required data Create an anonymous block that: o Accesses gl_stdV1
o Uses the %ROWTYPE attribute to define a record structure for the view data o Prompts for student number and section id o Output the required information o SET SERVEROUTPUT ON SET VERIFY OFF DROP TABLE std; DROP VIEW gl_stdV1; -- Creating the students table CREATE TABLE std ( student_id NUMBER PRIMARY KEY, student_name VARCHAR2(50), student_mark INTEGER, section_id NUMBER ); -- Inserting sample data into the students table INSERT INTO std VALUES (1, 'Richa', 90, 101); INSERT INTO std VALUES (2, 'Katherine', 87, 102); INSERT INTO std VALUES (3, 'Madison', 91, 101); ---creating a view for student table--- CREATE VIEW gl_stdV1 AS SELECT * FROM std; DECLARE -- Define a record structure using %ROWTYPE v_student_data gl_stdV1%ROWTYPE; -- Variables for user input v_student_id NUMBER := &get_student_id; v_section_id NUMBER := &get_section_id; BEGIN -- Access the view gl_stdV1 using the defined record structure SELECT * INTO v_student_data FROM gl_stdV1 WHERE student_id = v_student_id AND section_id = v_section_id; -- Output the required information DBMS_OUTPUT.PUT_LINE('Student ID: ' || v_student_data.student_id); DBMS_OUTPUT.PUT_LINE('Student Name: ' || v_student_data.student_name); DBMS_OUTPUT.PUT_LINE('Section ID: ' || v_student_data.section_id);
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help