0% found this document useful (0 votes)
13 views

Untitled Document 1

The document defines SQL functions and procedures to interact with a student database table. It creates the Students table with columns for id, name, email, department, and year. It then populates the table with sample data. Functions and procedures are created to count, update, delete and retrieve records as well as insert new records.

Uploaded by

SHANAY
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views

Untitled Document 1

The document defines SQL functions and procedures to interact with a student database table. It creates the Students table with columns for id, name, email, department, and year. It then populates the table with sample data. Functions and procedures are created to count, update, delete and retrieve records as well as insert new records.

Uploaded by

SHANAY
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

LAB

SHANAY KAPASI
19BCE1796
PERFORM FUNCTION/PROCEDURE

CREATE TABLE Students (


id INT PRIMARY KEY,
name VARCHAR(50),
email VARCHAR(50),
department VARCHAR(50),
year INT
);
INSERT INTO Students (id, name, email, department, year) VALUES
(1, 'John Smith', '[email protected]', 'Computer Science', 3),
(2, 'Jane Doe', '[email protected]', 'Electrical Engineering', 2),
(3, 'Bob Johnson', '[email protected]', 'Mechanical Engineering', 4),
(4, 'Sarah Lee', '[email protected]', 'Civil Engineering', 1),
(5, 'Mike Brown', '[email protected]', 'Computer Science', 2),
(6, 'Emily Davis', '[email protected]', 'Electrical Engineering', 3),
(7, 'Tom Jones', '[email protected]', 'Mechanical Engineering', 2),
(8, 'Amy Chen', '[email protected]', 'Civil Engineering', 4),
(9, 'David Kim', '[email protected]', 'Computer Science', 1),
(10, 'Lisa Wang', '[email protected]', 'Electrical Engineering', 4),
(11, 'Mike Johnson', '[email protected]', 'Mechanical Engineering', 3),
(12, 'Julia Lee', '[email protected]', 'Civil Engineering', 2),
(13, 'Chris Park', '[email protected]', 'Computer Science', 4),
(14, 'Mary Kim', '[email protected]', 'Electrical Engineering', 1),
(15, 'Eric Lee', '[email protected]', 'Mechanical Engineering', 2),
(16, 'Jessica Lee', '[email protected]', 'Civil Engineering', 3),
(17, 'Brian Chen', '[email protected]', 'Computer Science', 2),
(18, 'Alice Wang', '[email protected]', 'Electrical Engineering', 3),
(19, 'Matt Johnson', '[email protected]', 'Mechanical Engineering', 1),
(20, 'Linda Kim', '[email protected]', 'Civil Engineering', 4);
select * from Students;
CREATE FUNCTION CountStudentsByDepartment(dept_name VARCHAR(50)) RETURNS INT
BEGIN
DECLARE COUNT INT;
SELECT COUNT(*) INTO count FROM Students WHERE department = dept_name;
RETURN COUNT;
END;
CREATE PROCEDURE UpdateStudentYear(student_id INT, new_year INT)
BEGIN
UPDATE Students SET year = new_year WHERE id = student_id;
END;

-- Usage example
CALL UpdateStudentYear(1, 4); -- Update John Smith's year to 4
CREATE PROCEDURE DeleteStudent(student_id INT)
BEGIN
DELETE FROM Students WHERE id = student_id;
END;

-- Usage example
CALL DeleteStudent(5); -- Delete Mike Brown's record
CREATE FUNCTION GetStudentEmail(student_name VARCHAR(50)) RETURNS
VARCHAR(50)
BEGIN
DECLARE email VARCHAR(50);
SELECT email INTO email FROM Students WHERE name = student_name;
RETURN email;
END;

-- Usage example
SELECT name, GetStudentEmail(name) AS email
FROM Students
WHERE department = 'Computer Science';
CREATE FUNCTION CountStudentsByYear(year_num INT) RETURNS INT
BEGIN
DECLARE count INT;
SELECT COUNT(*) INTO count FROM Students WHERE year = year_num;
RETURN count;
END;

-- Usage example
SELECT year, CountStudentsByYear(year) AS student_count
FROM Students
GROUP BY year;
CREATE PROCEDURE UpdateStudentDepartment(student_id INT, new_department
VARCHAR(50))
BEGIN
UPDATE Students SET department = new_department WHERE id = student_id;
END;

-- Usage example
CALL UpdateStudentDepartment(3, 'Computer Science'); -- Update Bob Johnson's department
to Computer Science
CREATE PROCEDURE InsertNewStudent(student_id INT, student_name VARCHAR(50),
student_email VARCHAR(50), student_department VARCHAR(50), student_year INT)
BEGIN
INSERT INTO Students (id, name, email, department, year)
VALUES (student_id, student_name, student_email, student_department, student_year);
END;
-- Usage example
CALL InsertNewStudent(21, 'Jessica Smith', '[email protected]', 'Civil Engineering', 2); --
Insert a new student record
CREATE FUNCTION GetStudentDepartmentByEmail(student_email VARCHAR(50)) RETURNS
VARCHAR(50)
BEGIN
DECLARE dept VARCHAR(50);
SELECT department INTO dept FROM Students WHERE email = student_email;
RETURN dept;
END;

-- Usage example
SELECT name, GetStudentDepartmentByEmail(email) AS department
FROM Students
WHERE year = 3;
OUTPUT

department student_count
------------------- -------------
Civil Engineering 4
Computer Science 5
Electrical Engineering 5
Mechanical Engineering 6

name email
----------------- ---------------------
John Smith [email protected]
Mike Brown [email protected]
David Kim [email protected]
Chris Park [email protected]
Brian Chen [email protected]
year student_count
---- -------------
3 6
2 6
4 8
1 4
--------------- -------------------
John Smith Computer Science
Jane Doe Electrical Engineering
Bob Johnson Mechanical Engineering
Sarah Lee Civil Engineering
Mike Brown Computer Science
Emily Davis Electrical Engineering
Tom Jones Mechanical Engineering
Amy Chen Civil Engineering
David Kim Computer Science
Lisa Wang Electrical Engineering
Mike Johnson Mechanical Engineering
Julia Lee Civil Engineering
Chris Park Computer Science
Mary Kim Electrical Engineering
Eric Lee Mechanical Engineering
Jessica Lee Civil Engineering
Brian Chen Computer Science
Alice Wang Electrical Engineering
Matt Johnson Mechanical Engineering
Linda Kim Civil Engineering

You might also like