diff --git a/lesson09/task1.sql b/lesson09/task1.sql new file mode 100644 index 0000000..129797c --- /dev/null +++ b/lesson09/task1.sql @@ -0,0 +1,54 @@ +#Задание№1 +START TRANSACTION; +INSERT INTO sample.users (name, birthday_at, created_at, updated_at) + select u.name, u.birthday_at, u.created_at, u.updated_at + from shop.users as u + where u.id =1; +COMMIT; + +#Задание№2 +use shop; +drop view if exists prod_view; +CREATE VIEW prod_view AS + select p.name as prod_name, c.name as cat_name + from products as p, catalogs as c + where p.catalog_id = c.id; + +#Задание№3 +use shop; +drop table if exists tb_create_at; +CREATE TABLE tb_create_at ( + id SERIAL PRIMARY KEY, + created_at DATETIME +); +INSERT INTO tb_create_at (created_at) VALUES + ('2018-08-01'), + ('2018-08-04'), + ('2018-08-16'), + ('2018-08-17'), + ('2018-08-19'), + ('2018-08-21'), + ('2018-08-22'); + +SELECT `x`.sequence_date, IF(`x`.sequence_date in (select created_at from tb_create_at),1,0) as `flag` + FROM (SELECT DATE_ADD('2018-08-01', INTERVAL `n`.`id` - 1 day) as sequence_date + FROM (SELECT @N := @N +1 AS id + FROM mysql.help_relation , (SELECT @N:=0) dum LIMIT 31) `n` + WHERE DATE_ADD('2018-01-01', INTERVAL `n`.`id` -1 DAY) <= '2018-12-30' ) x; + +#Задание№4 +use shop; +select created_at +into @search_date +from (select * + from tb_create_at as t1 + order by t1.created_at desc + limit 5) as t2 +order by t2.created_at +limit 1; +delete from tb_create_at as tb + where tb.created_at < @search_date; + + + + \ No newline at end of file diff --git a/lesson09/task2.sql b/lesson09/task2.sql new file mode 100644 index 0000000..8ab45c8 --- /dev/null +++ b/lesson09/task2.sql @@ -0,0 +1,42 @@ +#Задание №1 +drop user if exists 'shop'@'localhost'; +drop user if exists 'shop_read'@'localhost'; +CREATE USER 'shop'@'localhost' IDENTIFIED WITH sha256_password BY 'pass'; +CREATE USER 'shop_read'@'localhost' IDENTIFIED WITH sha256_password BY 'pass'; + +GRANT ALL ON shop.* TO 'shop'@'localhost'; +GRANT SELECT ON shop.* TO 'shop_read'@'localhost'; + + + + +#Задание №2 +use shop; +drop table if exists accounts; +CREATE TABLE accounts ( + id SERIAL PRIMARY KEY, + name VARCHAR(255), + `password` VARCHAR(255) +) ; + +INSERT INTO accounts + (name, `password`) +VALUES + ('alex', 'pass1'), + ('john', 'pass2'), + ('ivan', 'pass3'); + + +drop view if exists accounts_view; +CREATE VIEW accounts_view AS + select * + from accounts; + +drop user if exists 'user_read'@'localhost'; +CREATE USER 'user_read'@'localhost' IDENTIFIED WITH sha256_password BY 'pass'; + +GRANT select ON shop.accounts_view TO 'user_read'@'localhost'; + + + + \ No newline at end of file diff --git a/lesson09/task3.sql b/lesson09/task3.sql new file mode 100644 index 0000000..6527983 --- /dev/null +++ b/lesson09/task3.sql @@ -0,0 +1,81 @@ +#Задание №1 +use shop; +drop procedure if exists hello; +delimiter // +CREATE PROCEDURE hello () +begin + set @a = HOUR(NOW()); + SELECT case + when (0 <= @a) && (@a < 6) then 'Доброй ночи' + when (6 <= @a) && (@a < 12) then 'Доброе утро' + when (12 <= @a) && (@a < 18) then 'Добрый день' + when (18 <= @a) && (@a < 24) then 'Добрый вечер' + end as hello; +end// + +call hello(); + + + + + + +#Задание №2 +use shop; +drop trigger if exists check_update_products; +delimiter // +CREATE TRIGGER check_update_products BEFORE UPDATE ON products +FOR EACH ROW BEGIN + IF (NEW.name is null and NEW.description is null or + old.name is null and NEW.description is null or + NEW.name is null and old.description is null) THEN + SIGNAL SQLSTATE '45000' SET MESSAGE_TEXT = 'Update canceled'; + END IF; +END// +delimiter ; + +UPDATE products as p SET p.description="wd", p.name="wqd" +WHERE p.id=1; + +drop trigger if exists check_insert_products; +delimiter // +CREATE TRIGGER check_insert_products BEFORE insert ON products +FOR EACH ROW BEGIN + IF (NEW.name is null and NEW.description is null) THEN + SIGNAL SQLSTATE '45000' SET MESSAGE_TEXT = 'Insert canceled'; + END IF; +END// + +INSERT INTO products + (price, catalog_id) +VALUES + (7890.00, 1); + + + + +#Задание №2 +use shop; +drop procedure if exists fib; +delimiter // +CREATE PROCEDURE fib(n INT) +begin + DECLARE m INT default 0; + DECLARE k INT DEFAULT 1; + DECLARE i INT; + DECLARE tmp INT; + + SET m=0; + SET k=1; + SET i=1; + + WHILE (i<=n) DO + SET tmp=m+k; + SET m=k; + SET k=tmp; + SET i=i+1; + END WHILE; + select m; + end// + + call fib(10)