Skip to content
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
447 changes: 447 additions & 0 deletions .gitignore

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
# Databases

Variant 11
117 changes: 117 additions & 0 deletions db_script.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
CREATE DATABASE IF NOT EXISTS pharmacy_db;
USE pharmacy_db;

DROP TABLE IF EXISTS pharmaсy_has_medicine;
DROP TABLE IF EXISTS medicine_zone;
DROP TABLE IF EXISTS pharmacy;
DROP TABLE IF EXISTS medicine_list;
DROP TABLE IF EXISTS employee;
DROP TABLE IF EXISTS post;
DROP TABLE IF EXISTS effect_zone;
DROP TABLE IF EXISTS street;


-- ------------------------------------------------- --
-- STREET --
-- ------------------------------------------------- --
CREATE TABLE street
(
id INT NOT NULL AUTO_INCREMENT,
name VARCHAR(45) NOT NULL,
PRIMARY KEY (id)
);

-- ------------------------------------------------- --
-- EFFECT ZONE --
-- ------------------------------------------------- --

CREATE TABLE effect_zone
(
id INT NOT NULL AUTO_INCREMENT,
name VARCHAR(45) NOT NULL,
PRIMARY KEY (id)
);

-- ------------------------------------------------- --
-- POST --
-- ------------------------------------------------- --

CREATE TABLE post
(
id INT NOT NULL AUTO_INCREMENT,
name VARCHAR(25) NOT NULL,
PRIMARY KEY (id)
);

-- ------------------------------------------------- --
-- EMPLOYEE --
-- ------------------------------------------------- --
CREATE TABLE employee
(
id INT NOT NULL AUTO_INCREMENT,
name VARCHAR(45) NOT NULL,
surname VARCHAR(45) NOT NULL,
middle_name VARCHAR(45) NOT NULL,
identity_number VARCHAR(20) NOT NULL,
passport_data VARCHAR(20) NOT NULL,
experience DECIMAL(10, 1) NOT NULL,
birth_date DATE NOT NULL,
post_id INT NOT NULL,
pharmacy_id INT NOT NULL,
PRIMARY KEY (id)
);

-- ------------------------------------------------- --
-- MEDICINE LIST --
-- ------------------------------------------------- --

CREATE TABLE medicine_list
(
id INT NOT NULL AUTO_INCREMENT,
name VARCHAR(20) NOT NULL,
ministry_code VARCHAR(10) NOT NULL,
require_recipe TINYINT NOT NULL,
is_narcotic TINYINT NULL,
is_psychtropic TINYINT NULL,
PRIMARY KEY (id)
);

-- ------------------------------------------------- --
-- PHARMACY --
-- ------------------------------------------------- --

CREATE TABLE pharmacy
(
id INT NOT NULL AUTO_INCREMENT,
name VARCHAR(45) NOT NULL,
street_id VARCHAR(45) NOT NULL,
building_number INT NOT NULL,
web_adress VARCHAR(45) NULL,
work_time_open TIME NOT NULL,
work_time_close TIME NOT NULL,
is_open_saturday TINYINT NOT NULL,
is_open_sunday TINYINT NOT NULL,
PRIMARY KEY (id)
);

-- ------------------------------------------------- --
-- MEDICINE ZONE --
-- ------------------------------------------------- --

CREATE TABLE medicine_zone
(
medicine_id INT NOT NULL,
zone_id INT NOT NULL,
PRIMARY KEY (medicine_id, zone_id)
);

-- ------------------------------------------------- --
-- PHARMASY HAS MEDICINE --
-- ------------------------------------------------- --

CREATE TABLE pharmaсy_has_medicine
(
pharmacy_id INT NOT NULL,
medicine_id INT NOT NULL,
PRIMARY KEY (pharmacy_id, medicine_id)
);
Binary file added eer_diagram.mwb
Binary file not shown.
33 changes: 33 additions & 0 deletions functions.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
USE pharmacy_db;

SET GLOBAL log_bin_trust_function_creators = 1;
-- Task №1 для employee шукати AVG стовпця experience.

DROP FUNCTION IF EXISTS get_avg_working_experience;
DELIMITER //
CREATE FUNCTION get_avg_working_experience()
RETURNS DECIMAL(10, 1)
BEGIN
RETURN (SELECT AVG(experience)
FROM employee);
END //
DELIMITER ;

# SELECT name, experience
# FROM employee where experience > get_avg_working_experience();

-- Task №2 витягує за ключем між street та pharmacy значення поля street.name .
DROP FUNCTION IF EXISTS get_street_name;
DELIMITER //
CREATE FUNCTION get_street_name(
pharmacy_id INT
)
RETURNS VARCHAR(45)
BEGIN
RETURN (SELECT name
FROM street
WHERE id = (SELECT street_id FROM pharmacy WHERE id = pharmacy_id));
END //
DELIMITER ;

# select *, get_street_name(pharmacy.id) as street_name from pharmacy;
107 changes: 107 additions & 0 deletions inserts.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
USE pharmacy_db;


INSERT INTO street (name)
VALUES ('Kvitky-Osnovianenka'),
('Nyzynna'),
('Stepanivny'),
('Shevchenka'),
('Warszawska'),
('Armii Ludowej'),
('Wesela'),
('Veresneva'),
('Ochota'),
('Virmenska');

INSERT INTO post (name)
VALUES ('clerk'),
('dispencer'),
('assistant'),
('technician'),
('pharmacist'),
('director'),
('manager'),
('pharmalogist');

INSERT INTO pharmacy (id, name, street_id, building_number, web_adress, work_time_open,
work_time_close, is_open_saturday, is_open_sunday)
VALUES ('1', 'Apteka', '1', '23', 'www.adress.com', '9:00', '18:00', '1', '1'),
('2', 'biomed', '4', '12', 'www.biomed.ua', '00:00', '00:00', '1', '1'),
('3', 'aptechka', '8', '1', 'www.apt.com', '10:00', '21:00', '0', '0'),
('4', 'Znahar', '4', '4', 'www.znahar.org', '6:00', '22:00', '0', '1'),
('5', 'apt24', '1', '5', 'www.art24.ua', '9:00', '17:00', '0', '0'),
('6', 'StopAsylum', '7', '32', 'www.stpslm.com', '13:00', '18:00', '1', '0'),
('7', 'Fight your shyzophrenia', '1', '13', 'www.aptekaforyou.com', '10:00', '16:00', '0',
'1'),
('8', 'AptekaIot', '4', '21', 'www.iotapteka.com', '8:30', '00:00', '1', '1'),
('9', 'pharmacy', '3', '1', 'www.pharmacy.ua', '10:00', '17:00', '1', '0'),
('10', 'drugStore', '2', '42', 'www.drugstore.ua', '9:45', '21:45', '0', '0');


INSERT INTO effect_zone (name)
VALUES ('lungs'),
('heart'),
('bones'),
('neck'),
('back'),
('stomach'),
('liver'),
('tooth'),
('eyes'),
('ears');


INSERT INTO employee (name, surname, middle_name, identity_number, passport_data, experience,
birth_date, post_id, pharmacy_id)
VALUES ('Василь', 'Potter', 'Jamesovich', '1234342674', 'fn 212311', '2 ', '2002-12-12', '1', '1'),
('Галина', 'Veesley', 'Batkovysh', '1213423100', 'af 190193', '0 ', '2000-11-07', '3', '1'),
('Галина', 'Granger', 'Jean', '3221213400', 'at 300329', '4 ', '2003-09-15', '5', '4'),
('Іван', 'Malfoy', 'Lutsiusius', '4233909400', 'gr 302039', '15 ', '1980-07-28', '7', '2'),
('Василь', 'Snape', 'Forever', '1000000000', 'ui 203902', '12', '1991-09-09', '6', '9'),
('Іван', 'Andrew', 'Andriiovych', '2329310900', 're 403940', '6.5', '2000-01-01', '4', '3'),
('Василь', 'Mininkov', 'Maksymovych', '9999999999', 'ps 133722', '9', '2003-05-15', '6',
'6'),
('Іван', 'Shevchenko', 'Hryhorovych', '2930293092', 'rt 394939', '13', '1840-03-09', '8',
'8'),
('Василь', 'Franko', 'Yakovych', '3209302940', 'po 093094', '5', '1876-05-07', '2', '4'),
('Олександра', 'Ukrainka', 'Petrivna', '2930290392', 'uy 309403', '4', '1856-08-27', '3',
'1');


INSERT INTO medicine_list (name, ministry_code, require_recipe, is_narcotic, is_psychtropic)
VALUES ('Nimesyl', '324', '1', '0', '0'),
('Mefenaminka', '789', '0', '0', '0'),
('Aspiryn', '123', '0', '0', '0'),
('Viks', '324', '0', '0', '0'),
('Ibuprom', '789', '0', '0', '1'),
('Kanefron', '176', '1', '0', '0'),
('Vitamin C', '300', '0', '0', '0'),
('Hlicysed', '993', '0', '1', '0'),
('NervoNorm', '993', '1', '0', '1'),
('Tamipul', '346', '0', '0', '0');


INSERT INTO pharmaсy_has_medicine (pharmacy_id, medicine_id)
VALUES (1, 3),
(3, 2),
(6, 6),
(1, 7),
(2, 5),
(3, 8),
(4, 4),
(10, 10),
(8, 1),
(7, 6);


INSERT INTO medicine_zone (medicine_id, zone_id)
VALUES (1, 3),
(2, 5),
(3, 4),
(5, 1),
(7, 2),
(10, 4),
(4, 6),
(6, 7),
(9, 2),
(3, 8);
85 changes: 85 additions & 0 deletions stored_procedures.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
USE pharmacy_db;
-- параметризована вставка в співробітники.
DROP PROCEDURE IF EXISTS insert_into_employee;
DELIMITER //
CREATE PROCEDURE insert_into_employee(
name VARCHAR(45),
surname VARCHAR(45),
middle_name VARCHAR(45),
identity_number VARCHAR(20),
passport_data VARCHAR(20),
experience DECIMAL(10, 1),
birth_date DATE,
post_id INT,
pharmacy_id INT
)
BEGIN
INSERT INTO employee(name, surname, middle_name, identity_number, passport_data, experience,
birth_date, post_id, pharmacy_id)
VALUES (name, surname, middle_name, identity_number, passport_data, experience, birth_date,
post_id, pharmacy_id);
END //
DELIMITER ;

# call insert_into_employee('s','d','a','v','a','1','2022-01-01','8','9');
-- 2.
DROP PROCEDURE IF EXISTS insert_street;
DELIMITER //
CREATE PROCEDURE insert_street()
BEGIN
DECLARE completed_name VARCHAR(10);
DECLARE sample_name VARCHAR(10) DEFAULT ('Noname');
DECLARE num INT DEFAULT (0);
DECLARE i INT DEFAULT (0);
WHILE i < 10
DO
SET num = FLOOR(RAND() * 10);
WHILE (SELECT name FROM street WHERE name = CONCAT(sample_name, num)) =
CONCAT(sample_name, num)
DO
SET num = FLOOR(RAND() * 10);
END WHILE;
SET completed_name = CONCAT(sample_name, num);
INSERT INTO street(name) VALUES (completed_name);
SET i = i + 1;
END WHILE;
SELECT * FROM street;
END //
DELIMITER ;

# call insert_street();

-- 3 курсор.

DROP PROCEDURE IF EXISTS pharmacy_create_names;
DELIMITER //
CREATE PROCEDURE pharmacy_create_names()
BEGIN
DECLARE done BOOL DEFAULT FALSE;
DECLARE newer_name VARCHAR(45);
DECLARE name_cursor CURSOR
FOR SELECT name FROM pharmacy;
DECLARE CONTINUE HANDLER
FOR NOT FOUND SET done = TRUE;
OPEN name_cursor;
names_loop:
LOOP
FETCH name_cursor INTO newer_name;
IF done THEN
LEAVE names_loop;
END IF;

SET @new_table =
'CREATE TABLE IF NOT EXISTS pharmacy_names ( id INT NOT NULL AUTO_INCREMENT, name VARCHAR(45), PRIMARY KEY(id));';
SELECT @new_table;
PREPARE new_table_query FROM @new_table;
EXECUTE new_table_query;
INSERT INTO pharmacy_names (name) VALUES (newer_name);

END LOOP;
CLOSE name_cursor;
END //
DELIMITER ;

# DROP TABLE IF EXISTS pharmacy_names;
# CALL pharmacy_create_names();
Loading