-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
598 changed files
with
16,441 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
CREATE PROCEDURE example1() | ||
BEGIN | ||
DECLARE l_book_count INTEGER; | ||
|
||
SELECT COUNT(*) | ||
INTO l_book_count | ||
FROM books | ||
WHERE author LIKE '%HARRISON,GUY%'; | ||
|
||
SELECT CONCAT('Guy has written (or co-written) ', | ||
l_book_count , | ||
' books.'); | ||
|
||
-- Oh, and I changed my name, so... | ||
UPDATE books | ||
SET author = REPLACE (author, 'GUY', 'GUILLERMO') | ||
WHERE author LIKE '%HARRISON,GUY%'; | ||
|
||
END |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
CREATE FUNCTION f_age (in_dob datetime) returns int | ||
NO SQL | ||
BEGIN | ||
DECLARE l_age INT; | ||
IF DATE_FORMAT(NOW(),'00-%m-%d') >= DATE_FORMAT(in_dob,'00-%m-%d') THEN | ||
-- This person has had a birthday this year | ||
SET l_age=DATE_FORMAT(NOW(),'%Y')-DATE_FORMAT(in_dob,'%Y'); | ||
ELSE | ||
-- Yet to have a birthday this year | ||
SET l_age=DATE_FORMAT(NOW(),'%Y')-DATE_FORMAT(in_dob,'%Y')-1; | ||
END IF; | ||
RETURN(l_age); | ||
|
||
END; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
CREATE TRIGGER employees_trg_bu | ||
BEFORE UPDATE ON employees | ||
FOR EACH ROW | ||
BEGIN | ||
IF NEW.salary <50000 THEN | ||
SET NEW.contrib_401K=500; | ||
ELSE | ||
SET NEW.contrib_401K=500+(NEW.salary-50000)*.01; | ||
END IF; | ||
END |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
CREATE PROCEDURE pay_out_balance | ||
(account_id_in INT) | ||
|
||
BEGIN | ||
|
||
DECLARE l_balance_remaining NUMERIC(10,2); | ||
|
||
payout_loop:LOOP | ||
SET l_balance_remaining = account_balance(account_id_in); | ||
|
||
IF l_balance_remaining < 1000 THEN | ||
LEAVE payout_loop; | ||
|
||
ELSE | ||
CALL apply_balance(account_id_in, l_balance_remaining); | ||
END IF; | ||
|
||
END LOOP; | ||
|
||
END |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
CREATE PROCEDURE variable_demo() | ||
BEGIN | ||
DECLARE my_integer INT; /* 32-bit integer */ | ||
DECLARE my_big_integer BIGINT; /* 64-bit integer */ | ||
DECLARE my_currency NUMERIC(8,2); /* Number with 2 decimals*/ | ||
DECLARE my_pi FLOAT /* Floating point number*/ | ||
DEFAULT 3.1415926; /* initialized as PI */ | ||
DECLARE my_text TEXT; /* huge text */ | ||
DECLARE my_dob DATE | ||
DEFAULT '1960-06-21'; /* My Birthday */ | ||
DECLARE my_varchar VARCHAR(30) | ||
DEFAULT 'Hello World!'; /* 30 bytes of text*/ | ||
|
||
SET my_integer=20; | ||
SET my_big_integer=POWER(my_integer,3); | ||
|
||
END; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
CREATE PROCEDURE putting_it_all_together(in_department_id INT) | ||
DETERMINISTIC MODIFIES SQL DATA | ||
BEGIN | ||
DECLARE l_employee_id INT; | ||
DECLARE l_salary NUMERIC(8,2); | ||
DECLARE l_department_id INT; | ||
DECLARE l_new_salary NUMERIC(8,2); | ||
DECLARE done INT DEFAULT 0; | ||
|
||
DECLARE cur1 CURSOR FOR | ||
SELECT employee_id, salary, department_id | ||
FROM employees | ||
WHERE department_id=in_department_id | ||
FOR UPDATE ; | ||
|
||
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done=1; | ||
|
||
CREATE TEMPORARY TABLE IF NOT EXISTS emp_raises | ||
(employee_id INT, department_id INT, new_salary NUMERIC(8,2)); | ||
|
||
OPEN cur1; | ||
emp_loop: LOOP | ||
|
||
FETCH cur1 INTO l_employee_id, l_salary, l_department_id; | ||
|
||
IF done=1 THEN /* No more rows*/ | ||
LEAVE emp_loop; | ||
END IF; | ||
|
||
CALL new_salary(l_employee_id,l_new_salary); /*get new salary*/ | ||
|
||
IF (l_new_salary<>l_salary) THEN /*Salary changed*/ | ||
|
||
UPDATE employees | ||
SET salary=l_new_salary | ||
WHERE employee_id=l_employee_id; | ||
/* Keep track of changed salaries*/ | ||
INSERT INTO emp_raises (employee_id,department_id,new_salary) | ||
VALUES (l_employee_id,l_department_id,l_new_salary); | ||
END IF; | ||
|
||
END LOOP emp_loop; | ||
CLOSE cur1; | ||
/* Print out the changed salaries*/ | ||
SELECT employee_id,department_id,new_salary from emp_raises; | ||
COMMIT; | ||
END; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
drop procedure if exists discounted_price | ||
$$ | ||
CREATE PROCEDURE discounted_price | ||
(normal_price NUMERIC(8,2), | ||
OUT discount_price NUMERIC(8,2)) | ||
NO SQL | ||
BEGIN | ||
IF (normal_price>500) THEN | ||
SET discount_price=normal_price*.8; | ||
|
||
ELSEIF (normal_price>100) THEN | ||
SET discount_price=normal_price*.9; | ||
|
||
ELSE | ||
SET discount_price=normal_price; | ||
|
||
END IF; | ||
|
||
END; | ||
$$ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
DROP PROCEDURE IF EXISTS simple_loop | ||
$$ | ||
CREATE PROCEDURE simple_loop() | ||
DETERMINISTIC | ||
BEGIN | ||
|
||
DECLARE counter INT DEFAULT 0; | ||
|
||
simple_loop: LOOP | ||
SET counter=counter+1; | ||
IF counter=10 THEN | ||
LEAVE simple_loop; | ||
END IF; | ||
END LOOP simple_loop; | ||
SELECT 'I can count to 10'; | ||
END; | ||
$$ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
DROP PROCEDURE IF EXISTS customer_sales | ||
$$ | ||
CREATE PROCEDURE customer_sales | ||
(in_customer_id INT) | ||
READS SQL DATA | ||
BEGIN | ||
DECLARE total_sales NUMERIC(8,2); | ||
|
||
SELECT SUM(sale_value) | ||
INTO total_sales | ||
FROM sales | ||
WHERE customer_id=in_customer_id; | ||
|
||
SELECT CONCAT('Total sales for ',in_customer_id,' is ',total_sales); | ||
END; | ||
$$ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
DROP PROCEDURE IF EXISTS cursor_example2 | ||
// | ||
|
||
CREATE PROCEDURE cursor_example2(in_department_id INT) | ||
BEGIN | ||
DECLARE l_employee_id INT; | ||
DECLARE l_salary NUMERIC(8,2); | ||
DECLARE l_department_id INT; | ||
DECLARE l_new_salary NUMERIC(8,2); | ||
|
||
DECLARE done INT DEFAULT 0; | ||
|
||
DECLARE cur1 CURSOR FOR | ||
SELECT employee_id, salary, department_id | ||
FROM employees | ||
WHERE department_id=in_department_id | ||
FOR UPDATE ; | ||
|
||
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done=1; | ||
|
||
CREATE TEMPORARY TABLE IF NOT EXISTS emp_raises | ||
(employee_id INT, department_id INT, new_salary NUMERIC(8,2)); | ||
|
||
OPEN cur1; | ||
emp_loop: LOOP | ||
|
||
FETCH cur1 INTO l_employee_id, l_salary, l_department_id; | ||
|
||
IF done=1 THEN /* No more rows*/ | ||
LEAVE emp_loop; | ||
END IF; | ||
|
||
CALL new_salary(l_employee_id,l_new_salary); /*get new salary*/ | ||
|
||
IF (l_new_salary<>l_salary) THEN /*Salary changed*/ | ||
|
||
UPDATE employees | ||
SET salary=l_new_salary | ||
WHERE employee_id=l_employee_id; | ||
|
||
/* Keep track of changed salaries*/ | ||
INSERT INTO emp_raises (employee_id,department_id,new_salary) | ||
VALUES (l_employee_id,l_department_id,l_new_salary); | ||
|
||
END IF; | ||
|
||
END LOOP emp_loop; | ||
CLOSE cur1; | ||
|
||
/* Print out the changed salaries*/ | ||
SELECT employee_id,department_id,new_salary from emp_raises; | ||
|
||
COMMIT; | ||
|
||
END; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
CREATE PROCEDURE sp_update_salary | ||
(in_employee_id INT, in_new_salary NUMERIC(8,2)) | ||
DETERMINISTIC | ||
BEGIN | ||
IF in_new_salary <5000 OR in_new_salary > 500000 THEN | ||
SELECT 'Illegal salary; salary must be between $5,000 and $500,000'; | ||
ELSE | ||
UPDATE employees | ||
SET salary=in_new_salary | ||
WHERE employee_id=in_employee_id; | ||
END IF; | ||
END |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
CREATE PROCEDURE call_example | ||
(employee_id INT, employee_type VARCHAR(20)) | ||
NO SQL | ||
BEGIN | ||
DECLARE l_bonus_amount NUMERIC(8,2); | ||
|
||
IF employee_type='MANAGER' THEN | ||
CALL calc_manager_bonus(employee_id ,l_bonus_amount); | ||
ELSE | ||
CALL calc_minion_bonus(employee_id,l_bonus_amount); | ||
END IF; | ||
CALL grant_bonus(employee_id,l_bonus_amount); | ||
END; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
CREATE FUNCTION f_discount_price | ||
(normal_price NUMERIC(8,2)) | ||
RETURNS NUMERIC(8,2) | ||
BEGIN | ||
|
||
DECLARE discount_price NUMERIC(8,2); | ||
|
||
IF (normal_price>500) THEN | ||
SET discount_price=normal_price*.8; | ||
|
||
ELSEIF (normal_price>100) THEN | ||
SET discount_price=normal_price*.9; | ||
|
||
ELSE | ||
SET discount_price=normal_price; | ||
|
||
END IF; | ||
|
||
RETURN(discount_price); | ||
|
||
END; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
CREATE TRIGGER sales_bi_trg | ||
BEFORE INSERT ON sales | ||
FOR EACH ROW | ||
BEGIN | ||
IF NEW.sale_value > 500 THEN | ||
SET NEW.free_shipping='Y'; | ||
ELSE | ||
SET NEW.free_shipping='N'; | ||
END IF; | ||
IF NEW.sale_value > 1000 THEN | ||
SET NEW.discount=NEW.sale_value*.15; | ||
ELSE | ||
SET NEW.discount=0; | ||
END IF; | ||
END; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
<html><head><title>Employee listing</title><head><body> | ||
<h1>Employee listing</h1> | ||
<form method="post" > | ||
<p>Enter Department ID: | ||
<input type="text" name="dept_id" size="4"> | ||
<input type="submit" name="submit" value="submit"><p> | ||
</form> | ||
|
||
<?php | ||
$hostname = "localhost"; | ||
$username = "root"; | ||
$password = "secret"; | ||
$database = "prod"; | ||
|
||
if (IsSet ($_POST['submit'])) { | ||
|
||
$dbh = new mysqli($hostname, $username, $password, $database); | ||
|
||
/* check connection */ | ||
if (mysqli_connect_errno()) { | ||
printf("Connect failed: %s\n", mysqli_connect_error()); | ||
exit (); | ||
} | ||
$dept_id = $_POST['dept_id']; | ||
|
||
if ($result_set = $dbh->query("call employee_list( $dept_id )")) { | ||
print ('<table border="1" width="30%"> <tr> '. | ||
'<td>Employee_id</td><td>Surname</td><td>Firstname</td></tr>'); | ||
while ($row = $result_set->fetch_object()) { | ||
printf("<tr><td>%s</td><td>%s</td><td>%s</td></tr>\n", | ||
$row->employee_id, $row->surname, $row->firstname); | ||
} | ||
} else { | ||
printf("<p>Error:%d (%s) %s\n", mysqli_errno($dbh), | ||
mysqli_sqlstate($dbh), mysqli_error($dbh)); | ||
} | ||
print ("</table> "); | ||
$dbh->close(); | ||
} | ||
?> | ||
</body></html> | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
DECLARE l_int1 INT DEFAULT -2000000; | ||
DECLARE l_int2 INT UNSIGNED DEFAULT 4000000; | ||
DECLARE l_bigint1 BIGINT DEFAULT 4000000000000000; | ||
DECLARE l_float FLOAT DEFAULT 1.8e8; | ||
DECLARE l_double DOUBLE DEFAULT 2e45; | ||
DECLARE l_numeric NUMERIC(8,2) DEFAULT 9.95; | ||
|
||
DECLARE l_date DATE DEFAULT '1999-12-31'; | ||
DECLARE l_datetime DATETIME DEFAULT '1999-12-31 23:59:59'; | ||
|
||
DECLARE l_char CHAR(255) DEFAULT 'This will be padded to 255 chars'; | ||
DECLARE l_varchar VARCHAR(255) DEFAULT 'This will not be padded'; | ||
|
||
DECLARE l_text TEXT DEFAULT 'This is a really long string. In stored programs we can use text columns fairly freely, but in tables there are some limitations regarding indexing and use in various expressions.'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
CREATE PROCEDURE operators() | ||
BEGIN | ||
DECLARE a INT DEFAULT 2; | ||
DECLARE b INT DEFAULT 3; | ||
DECLARE c FLOAT; | ||
|
||
SET c=a+b; SELECT 'a+b=',c; | ||
SET c=a/b; SELECT 'a/b=',c; | ||
SET c=a*b; SELECT 'a*b=',c; | ||
|
||
|
||
IF (a<b) THEN | ||
SELECT 'a is less than b'; | ||
END IF; | ||
IF NOT (a=b) THEN | ||
SELECT 'a is not equal to b'; | ||
END IF; | ||
END; |
Oops, something went wrong.