Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Lesson 9 #6

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added 6/EER model.mwb
Binary file not shown.
89 changes: 89 additions & 0 deletions 6/Выполняемые комманды урок 6.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
Задание 1
--Внешние ключи таблицы профилей на таблицу users и media
ALTER TABLE profiles
ADD CONSTRAINT profiles_user_id_fk
FOREIGN KEY (user_id) REFERENCES users(id)
ON DELETE CASCADE,
ADD CONSTRAINT profiles_photo_id_fk
FOREIGN KEY (photo_id) REFERENCES media(id)
ON DELETE SET NULL;

--Внешние ключи таблицы friendship на таблицы users, profiles и friendship_statuses:
ALTER TABLE friendship
ADD CONSTRAINT friendship_user_id_fk
FOREIGN KEY (user_id) REFERENCES users(id),
ADD CONSTRAINT friendship_friend_id_fk
FOREIGN KEY (friend_id) REFERENCES profiles(user_id),
ADD CONSTRAINT friendship_status_id_fk
FOREIGN KEY (status_id) REFERENCES friendship_statuses(id);

--Внешние ключи таблицы likes:
ALTER TABLE likes
ADD CONSTRAINT likes_user_id_fk
FOREIGN KEY (user_id) REFERENCES users(id),
ADD CONSTRAINT likes_target_type_id_fk
FOREIGN KEY (target_type_id) REFERENCES target_types(id);

-- Внешние ключи таблицы messages:
ALTER TABLE messages
ADD CONSTRAINT messages_from_user_id_fk
FOREIGN KEY (from_user_id) REFERENCES users(id),
ADD CONSTRAINT messages_to_user_id_fk
FOREIGN KEY (to_user_id) REFERENCES users(id);


-- Внешний ключ таблицы media:
ALTER TABLE media
ADD CONSTRAINT media_user_id_fk
FOREIGN KEY (user_id) REFERENCES users(id),
ADD CONSTRAINT media_media_tupe_id_fk
FOREIGN KEY (media_type_id) REFERENCES media_types(id);

-- Внешние ключи таблицы posts:
ALTER TABLE posts
ADD CONSTRAINT posts_user_id_fk
FOREIGN KEY (user_id) REFERENCES communities_users(user_id);
ALTER TABLE posts
ADD CONSTRAINT posts_community_id_fk
FOREIGN KEY (community_id) REFERENCES communities_users(community_id);


--Внешний ключ таблицы communities_users на users:
ALTER TABLE communities_users
ADD CONSTRAINT communities_users_user_id_fk
FOREIGN KEY (user_id) REFERENCES users(id);

-- Внешний ключ communities_users на communities:
ALTER TABLE communities_users
ADD CONSTRAINT communities_users_community_id_fk
FOREIGN KEY (community_id) REFERENCES communities(id);

Задание 3

SELECT COUNT(target_id),
(SELECT gender FROM profiles) AS gender
FROM likes
GROUP BY gender;

Задание 4

SELECT first_name AS name,
(SELECT birthday FROM profiles) AS data,
(SELECT target_id FROM likes) AS like
FROM users
ORDER BY data DESC
LIMIT 10;

Задание 5


--Активность пользователей выбрал как отправка сообщений другим пользователям, по последней дате отправления.
SELECT CONCAT(first_name, ' ', last_name) AS 'Имя Фамилия',
(SELECT created_at FROM messages ORDER BY created_at DESC) AS 'Последнее сообщение'
FROM users;






Binary file added 6/диаграмма отношений 2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added 6/диаграмма отношений.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
33 changes: 33 additions & 0 deletions 7/Выполняемые комманды урок 7.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
--Задание 1
SELECT u.name,
o.id
FROM users u
JOIN orders o
ON o.id = u.user_id
ORDER BY u.name;

--Задание 2
SELECT p.name AS 'Имя товара',
-> c.name AS 'Имя каталога'
-> FROM products p
-> JOIN catalogs c
-> ON c.id = p.catalog_id;
+-------------------------+-------------------+
| Имя товара | Имя каталога |
+-------------------------+-------------------+
| Intel Core i3-8100 | Процессоры |
| Intel Core i5-7400 | Процессоры |
| AMD FX-8320E | Процессоры |
| AMD FX-8320 | Процессоры |
| ASUS ROG MAXIMUS X HERO | Материнские платы |
| Gigabyte H310M S2H | Материнские платы |
| MSI B250M GAMING PRO | Материнские платы |
+-------------------------+-------------------+
7 rows in set (0.00 sec)

--Задание 3
SELECT id AS 'Номер рейса',
(SELECT name FROM cities WHERE lable = 'from') AS 'Откуда',
(SELECT name FROM cities WHERE lable = 'to') AS 'Куда'
FROM flights;

43 changes: 43 additions & 0 deletions 8/Выполняемые комманды урок 8.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
--1. Подсчитать колличество лайков:
SELECT p.gender,
COUNT(*) AS total
FROM profiles p
JOIN likes l
ON p.user_id = l.user_id
GROUP BY p.gender
ORDER BY total DESC;

--2.Подсчитать общее количество лайков десяти самым молодым пользователям.
a)
SELECT p.birthday
FROM profiles p
JOIN likes l
ON l.user_id = p.user_id
WHERE l.target_id = p.user_id AND l.target_type_id = 2
ORDER BY birthday DESC;

б)
SELECT p.birthday,
COUNT(l.target_id)
FROM profiles p
JOIN likes l
ON l.target_id = p.user_id
WHERE target_type_id = 2;


--3.
SELECT CONCAT(first_name, ' ', last_name) AS u.user,
SUM(COUNT(l.user_id) + COUNT(m.user_id) + COUNT(me.user_id) AS overall_activity
FROM users u
JOIN likes l
ON u.id = l.user_id
JOIN media m
ON u.id = m.user_id
JOIN messages me
u.id = me.user_id
ORDER BY overall_activity
LIMIT 10;




59 changes: 59 additions & 0 deletions 9/Выполняемые комманды урок 9.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@

Транзакции, переменные, представления
--1 Переместите запись id = 1 из таблицы shop.users в таблицу sample.users
START TRANSACTION;
SELECT users FROM shop WHERE id = 1;
UPDATE sample SET INSERT INTO users.shop(id);
COMMIT;

--2 Создание представления:
CREATE VIEW nema_cat AS
SELECT p.name AS 'Название товара', c.name AS 'Название каталога' FROM products p
JOIN catalogs c
ON p.id = c.id;
mysql> SELECT * FROM nema_cat;
+-------------------------+--------------------+
| Название товара | Название каталога |
+-------------------------+--------------------+
| Intel Core i3-8100 | Процессоры |
| Intel Core i5-7400 | Материнские платы |
| AMD FX-8320E | Видеокарты |
| AMD FX-8320 | Жесткие диски |
| ASUS ROG MAXIMUS X HERO | Оперативная память |
| MSI B250M GAMING PRO | Мониторы |
+-------------------------+--------------------+
6 rows in set (0.00 sec)


Хранимые процедуры, функции, треггеры.

Создайте хранимую функцию hello()
--1
DELIMITER //
CREATE FUNCTION hello ()
RETURNS TEXT DETERMINISTIC
BEGIN
CASE
WHEN SELECT DATE_FORMAT BETWEEN 06:00:00 AND 11:59:59 THEN
RETURN 'Доброе утро' AS good_morning;
WHEN SELECT DATE_FORMAT BETWEEN 12:00:00 AND 17:59:59 THEN
RETURN 'Добрый день' AS good_afternoon;
WHEN SELECT DATE_FORMAT BETWEEN 18:00:00 AND 23:59:59 THEN
RETURN 'Добрый вечер' AS good_evning;
WHEN SELECT DATE_FORMAT BETWEEN 00:00:00 AND 05:59:59 THEN
RETURN 'Доброй ночи' AS good_night;
END CASE;
END//

SELECT hello()//

--2 Создание триггера:
CREATE TRIGGER name_description BEFORE INSERT ON products
FOR EACH ROW
BEGIN
IF NEW.name IS NULL AND NEW.description IS NULL THEN SIGNAL SQLSTATE '45000' SET MESSAGE = 'ERROR' END IF
END//