0% found this document useful (0 votes)
5 views2 pages

db note function

The document contains SQL functions and procedures for managing tournament data in a database. It includes functions to retrieve tournament information, calculate total tournament duration, validate user credentials, and retrieve organizers for a specific tournament. Each function is accompanied by example SQL queries for execution.

Uploaded by

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

db note function

The document contains SQL functions and procedures for managing tournament data in a database. It includes functions to retrieve tournament information, calculate total tournament duration, validate user credentials, and retrieve organizers for a specific tournament. Each function is accompanied by example SQL queries for execution.

Uploaded by

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

--------------------------------------------------

function
--------------------------------------------------
1. Retrieves information about a tournament based on the provided tournament ID.

CREATE OR REPLACE FUNCTION get_tournament_info(


p_tournament_id IN NUMBER
) RETURN VARCHAR2 AS
v_tournament_info VARCHAR2(500);
BEGIN
SELECT TOURNAMENT_NAME || ' - ' || TOURNAMENT_DATE || ' - ' || TOURNAMENT_TYPE
INTO v_tournament_info
FROM TOURNAMENT
WHERE TOURNAMENT_ID = p_tournament_id;

RETURN v_tournament_info;
END get_tournament_info;

SELECT get_tournament_info(123) AS tournament_info


FROM dual;

---------------------------------------------------------
2. Calculates the total duration of all tournaments.

CREATE OR REPLACE FUNCTION calculate_total_duration RETURN NUMBER AS


v_total_duration NUMBER := 0;
BEGIN
SELECT SUM(DURATION) INTO v_total_duration FROM INFO;
RETURN v_total_duration;
END calculate_total_duration;

SELECT calculate_total_duration() AS total_duration FROM dual;

------------------------------------------------------------
3. Validates user credentials by checking if the provided username and password
match any user in the database.

CREATE OR REPLACE FUNCTION validate_user_credentials(


p_username IN VARCHAR2,
p_password IN VARCHAR2
) RETURN BOOLEAN AS
v_count NUMBER;
BEGIN
SELECT COUNT(*) INTO v_count
FROM USERS
WHERE USERNAME = p_username
AND PASSWORD = p_password;
IF v_count > 0 THEN
RETURN TRUE;
ELSE
RETURN FALSE;
END IF;
END validate_user_credentials;

DECLARE
v_is_valid BOOLEAN;
BEGIN
v_is_valid := validate_user_credentials('rakib', '123123');
IF v_is_valid THEN
DBMS_OUTPUT.PUT_LINE('User credentials are valid');
ELSE
DBMS_OUTPUT.PUT_LINE('Invalid username or password');
END IF;
END;

-------------------------------------------------------
4. Retrieves organizers associated with a specific tournament and outputs their
names.

CREATE OR REPLACE PROCEDURE get_organizers_for_tournament(


p_tournament_id IN NUMBER
) AS
BEGIN
FOR organizer_rec IN (SELECT ORGANIZER_NAME FROM ORGANIZER WHERE TOURNAMENT_ID
= p_tournament_id) LOOP
DBMS_OUTPUT.PUT_LINE('Organizer: ' || organizer_rec.ORGANIZER_NAME);
END LOOP;
END get_organizers_for_tournament;

DECLARE
v_tournament_id NUMBER := 1;
BEGIN
get_organizers_for_tournament(v_tournament_id);
END;

---------------------------------------------------------------

You might also like