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

MySQL CONTROL STATEMENTS

Here are the answers: 1. To display the odd-numbered film_id rows from the film table of the sakila database: SELECT * FROM film WHERE MOD(film_id, 2) != 0; 2. Use CASE to insert or update one row in the film table: DELIMITER // CREATE PROCEDURE p17 (IN choice INT) BEGIN CASE choice WHEN 1 THEN INSERT INTO film VALUES (500, 'New Film', '2022', 99, 5.99); WHEN 2 THEN UPDATE film SET title = 'Updated Film' WHERE film_id = 1; ELSE SELECT 'No action'; END CASE; END; //

Uploaded by

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

MySQL CONTROL STATEMENTS

Here are the answers: 1. To display the odd-numbered film_id rows from the film table of the sakila database: SELECT * FROM film WHERE MOD(film_id, 2) != 0; 2. Use CASE to insert or update one row in the film table: DELIMITER // CREATE PROCEDURE p17 (IN choice INT) BEGIN CASE choice WHEN 1 THEN INSERT INTO film VALUES (500, 'New Film', '2022', 99, 5.99); WHEN 2 THEN UPDATE film SET title = 'Updated Film' WHERE film_id = 1; ELSE SELECT 'No action'; END CASE; END; //

Uploaded by

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

MySQL CONTROL STATEMENTS

----------------------------------------------------------------
----------- To Print “Hello World!” using stored
procedure--------------------------------------------------------
--------------

delimiter //
CREATE PROCEDURE HelloWorld()
BEGIN
SELECT "Hello World!";
END; //

delimiter ;
CALL HelloWorld();

----------------------------------------------------------------
--- To Print “Hello (with given name)” using stored procedure---
----------------------------------------------------------------

CREATE FUNCTION HelloName(name VARCHAR(100))


RETURNS VARCHAR(120) DETERMINISTIC
RETURN CONCAT("Hello ", name);

SET @fn_res = HelloName("Hello!!!!!");


select @fn_res;

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

create table t(s1 int);

INSERT INTO t VALUES (17);

----------------------------------------------------------------
------------------ MySQL CONTROL STATEMENTS
-----------------------------------------------------------------
-----------------

------------------------Example for IF – ELSE


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

delimiter //

CREATE PROCEDURE p112 (IN parameter1 INT)


BEGIN
DECLARE variable1 INT;
SET variable1 = parameter1 + 1;
IF variable1 = 0 THEN
INSERT INTO t VALUES (17);
END IF;
IF parameter1 = 0 THEN
UPDATE t SET s1 = s1 + 1 where s1 = 17;
ELSE
UPDATE t SET s1 = s1 + 2 where s1 = 17;
END IF;
END; //

delimiter ;
CALL p12(1);
-----------------------------------------------------------------
-----

------------------------Example for CASE (switch case)


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

delimiter //
CREATE PROCEDURE p13 (IN parameter1 INT)
BEGIN
DECLARE variable1 INT;
SET variable1 = parameter1 + 1;
CASE variable1
WHEN 0 THEN
INSERT INTO t VALUES (17);
WHEN 1 THEN
INSERT INTO t VALUES (18);
ELSE
INSERT INTO t VALUES (19);
END CASE;
END; //

delimiter ;
CALL p13(0);
select * from t;

----------------------------------------------------------------
-----------------WHILE ... END WHILE
-----------------------------------------------------------------
-------------------------

delimiter //
CREATE PROCEDURE p14 ()
BEGIN
DECLARE v INT;
SET v = 0;
WHILE v < 5 DO
INSERT INTO t VALUES (v);
SET v = v + 1;
END WHILE;
END; //
delimiter ;
CALL p14();
select * from t;
-----------------------------------------------------------------
-----
// Here's another way of doing a loop, the REPEAT loop.
It's doing the same thing as the WHILE loop did, except that it
checks the condition after doing the statements,
instead of checking the condition before doing the statements, as
the WHILE loop did.

-----------------------------------------------------------------
----------------- REPEAT ... END REPEAT ------------------
----------------------------------------------------------------

delimiter //
CREATE PROCEDURE p15 ()
BEGIN
DECLARE v INT;
SET v = 5;
REPEAT
INSERT INTO t VALUES (v);
SET v = v + 1;
UNTIL v >= 10
END REPEAT;
END; //
delimiter ;
CALL p15();
select * from t;

-----------------------------------------------------------------
------------------ LOOP ... END LOOP ------------------------

delimiter //
CREATE PROCEDURE p16 ()
BEGIN
DECLARE v INT;
SET v = 10;
loop_label: LOOP
INSERT INTO t VALUES (v);
SET v = v + 1;
IF v >= 15 THEN
LEAVE loop_label;
END IF;
END LOOP;
END; //

delimiter ;
CALL p16();
select * from t;

-----------------------------------------------------------------
-----------------------------------------------------------------
---------
-- To display the even-numbered rows.
SELECT * FROM t WHERE MOD(s1, 2) = 0;

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

Questions:

1. Display the odd-numbered film_id rows from the film of the


sakila database.
2. Use “CASE” to insert or update one row in the film table(For
Ex. choice 1 for INSERT & Choice 2 for UPDATE).

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