0% found this document useful (0 votes)
11 views6 pages

Kolsghir

The document contains SQL code for creating stored procedures, functions, triggers, and performing various database operations such as creating tables, inserting, updating, deleting data, and modifying table structures. It includes examples of using cursors within procedures to process data row by row. Additionally, it demonstrates how to log actions related to product insertions and manage relationships between tables using joins.

Uploaded by

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

Kolsghir

The document contains SQL code for creating stored procedures, functions, triggers, and performing various database operations such as creating tables, inserting, updating, deleting data, and modifying table structures. It includes examples of using cursors within procedures to process data row by row. Additionally, it demonstrates how to log actions related to product insertions and manage relationships between tables using joins.

Uploaded by

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

DELIMITER //

CREATE PROCEDURE NameOfProcedure (


IN FirstInput int,
IN SecondInput VARCHAR(255),
IN ThirthInput float,
IN ForthInput true,
IN FiveInput DECIMAL(10,2),
IN SixInput DATE,
OUT OutPut Date,
......
)
BEGIN
-- Logique Here
END //
DELIMITER;

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

DELIMITER //
CREATE FUNCTION NameOfFunction(
FirstInput int,
SecondInput VARCHAR(255),
ThirthInput float,
ForthInput true,
FiveInput DECIMAL(10,2),
SixInput DATE
)
RETURNS INT
BEGIN
-- Logique Here
RETURN ...;
END //
DELIMITER;
DELIMITER //
CREATE TRIGGER ProductInsertLog
AFTER INSERT ON Products
FOR EACH ROW
BEGIN
INSERT INTO ProductLogs (ProductID, Action, Timestamp)
VALUES (NEW.ProductID, 'INSERT', NOW());
END //
DELIMITER ;

-- Create Table
CREATE TABLE table_name (
column1 datatype,
column2 datatype,
...
);

-- Insert Data
INSERT INTO table_name (column1, column2, ...)
VALUES (value1, value2, ...);
-- Update Data
UPDATE table_name
SET column1 = value1, column2 = value2, ...
WHERE condition;

-- Delete Data
DELETE FROM table_name
WHERE condition;

-- Add a new column


ALTER TABLE table_name
ADD column_name datatype;

-- Rename a table
ALTER TABLE old_table_name
RENAME TO new_table_name;

-- Modify a column's datatype


ALTER TABLE table_name
MODIFY column_name new_datatype;

-- Rename a column
ALTER TABLE table_name
RENAME COLUMN old_column_name TO new_column_name;
-- Drop a column
ALTER TABLE table_name
DROP COLUMN column_name;

SELECT
loans.loan_id,
loans.book_id,
loans.borrower_id,
loans.loan_date,
loans.return_date,
borrowers.email
FROM Loans
INNER JOIN borrowers ON borrowers.borrower_id = loans.borrower_id
WHERE return_date IS NOT NULL;
SELECT
Enrollments.enrollment_id,
Enrollments.student_id,
Enrollments.course_id,
Enrollments.grade,
Students.enrollment_year,
Courses.course_name
FROM Enrollments
INNER JOIN Courses ON Enrollments.course_id = Courses.course_id
INNER JOIN Students ON Enrollments.student_id = Students.student_id;

DELIMITER //

CREATE PROCEDURE ProcedureName()


BEGIN
-- Declare variables to store data from the cursor
DECLARE variable1 DataType;
DECLARE variable2 DataType;
-- Add more variables as needed

-- Declare a flag to signal the end of the cursor


DECLARE done INT DEFAULT 0;

-- Declare the cursor


DECLARE cursor_name CURSOR FOR
SELECT column1, column2 -- Add more columns if needed
FROM table_name
WHERE conditions; -- Optional filtering conditions

-- Declare a handler for when the cursor reaches the end


DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = 1;

-- Open the cursor


OPEN cursor_name;

-- Loop through rows in the cursor


read_loop: LOOP
-- Fetch the next row into variables
FETCH cursor_name INTO variable1, variable2; -- Add more variables as needed
IF done THEN
-- Exit the loop if there are no more rows
LEAVE read_loop;
END IF;

-- Perform actions for each row (e.g., insert, update, log, etc.)
-- Example: INSERT INTO Audit_Log (operation_type, operation_time, details)
-- VALUES ("CURSOR_PROCESS", NOW(), CONCAT("Column1: ", variable1, ", Column2: ",
variable2));
END LOOP;

-- Close the cursor


CLOSE cursor_name;
END //

DELIMITER ;

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy