This repository was archived by the owner on Aug 11, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathTopic 1.2.3.sql
More file actions
54 lines (43 loc) · 1.77 KB
/
Topic 1.2.3.sql
File metadata and controls
54 lines (43 loc) · 1.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
-- Вывод максимальной цены
CREATE DEFINER=`root`@`localhost` PROCEDURE `show_max_price`()
BEGIN
SELECT MAX(price) from testdb.phones;
END
-- Вывод телефонов в ценовом диапазоне
CREATE DEFINER=`root`@`localhost` PROCEDURE `find_the_phones_in_range`(IN first_price int, IN second_price int)
BEGIN
IF first_price >= second_price
THEN
SELECT * FROM testdb.phones WHERE price BETWEEN second_price AND first_price;
ELSE
SELECT * FROM testdb.phones WHERE price BETWEEN first_price AND second_price;
END IF;
END
-- Добавление нового телефона
CREATE DEFINER=`root`@`localhost` PROCEDURE `add_phone`(IN id int, IN model VARCHAR(50), IN manufacturer VARCHAR(50), IN quantity INT, IN price int)
BEGIN
INSERT INTO `testdb`.`phones` (`id`, `model`, `manufacturer`, `quantity`, `price`) VALUES (id, model, manufacturer, quantity, price);
END
-- Получение максимального значения id из таблицы
CREATE FUNCTION `get_max_id` ()
RETURNS INTEGER
DETERMINISTIC
BEGIN
DECLARE idx int;
SELECT MAX(id) INTO idx from testdb.phones;
RETURN idx;
END
-- Добавление нового телефона без id
CREATE DEFINER=`root`@`localhost` PROCEDURE `add_phone_without_id`(IN model VARCHAR(50), IN manufacturer VARCHAR(50), IN quantity INT, IN price int)
BEGIN
DECLARE id int;
SET id = testdb.get_max_id() + 1;
INSERT INTO `testdb`.`phones` (`id`, `model`, `manufacturer`, `quantity`, `price`) VALUES (id, model, manufacturer, quantity, price);
END
-- Изменение цен
CREATE DEFINER=`root`@`localhost` PROCEDURE `update_prices`()
BEGIN
UPDATE testdb.phones
SET price = price - 2000
WHERE quantity > 0 AND quantity <= 3;
END