Lab 6 Solutions
Lab 6 Solutions
RETURNS record AS
$$
DECLARE
student_record RECORD;
BEGIN
END LOOP;
END;
$$
LANGUAGE plpgsql;
select gsd3()
ii. Create a PL/PGSQL procedure that takes a student's registration
number as input and updates their age to 20. Test the procedure by
updating the age of a specific student
call usa1(107,20)
select avgage()
iv Create a PL/PGSQL procedure that inserts a new row into the "Course"
table, enrolling a student with a given registration number into a
specified course. Test the procedure by enrolling a student in a course
-- Create a PL/pgSQL procedure to enroll a student in a course
CREATE OR REPLACE PROCEDURE enroll2(
courseid1 INT,
regno1 INT
)
LANGUAGE plpgsql
AS $$
BEGIN
-- Check if the student with the given registration number exists
IF NOT EXISTS (SELECT 1 FROM Student WHERE regno = regno1) THEN
RAISE EXCEPTION 'Student with Registration Number % does not exist.', regno1;
END IF;
-- Check if the course with the given course ID exists
IF NOT EXISTS (SELECT 1 FROM Course WHERE courseid = courseid1) THEN
RAISE EXCEPTION 'Course with Course ID % does not exist.', courseid1;
END IF;
-- Insert a new row into the Course table to enroll the student in the course
INSERT INTO course (regno, courseid)
VALUES ((SELECT regno FROM Student WHERE regno = regno1), courseid1);
-- Commit the transaction
COMMIT;
END;
$$;
call enroll2(2,104)
SELECT get_student_count();
VI. Develop a PL/PGSQL procedure that deletes a student's record from
the "Student" table based on their registration number. Test the
procedure by deleting a specific student's record
CREATE OR REPLACE PROCEDURE delete_student1(
regno1 int
)
LANGUAGE plpgsql
AS $$
BEGIN
DELETE FROM student WHERE regno = regno1;
COMMIT;
END;
$$;
CALL delete_student1(105);
VII Develop a PL/SQL procedure that updates the address of a student
based on their registration number. Test the procedure by updating a
specific student's address.
p_reg_number INT,
p_new_address VARCHAR
LANGUAGE plpgsql
AS $$
BEGIN
UPDATE students
ELSE
END IF;
END;
$$;
INSERT INTO students (name, address) VALUES ('John Doe', '123 Main St');
DO $$
DECLARE
v_age INT;
student_record RECORD;
BEGIN
-- Accept age as input
v_age := 20; -- Replace with the desired age or use an input method if available