diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..17e71b8 --- /dev/null +++ b/.gitignore @@ -0,0 +1,447 @@ +## Ignore Visual Studio temporary files, build results, and +## files generated by popular Visual Studio add-ons. + +# User-specific files +*.suo +*.user +*.userosscache +*.sln.docstates + +# User-specific files (MonoDevelop/Xamarin Studio) +*.userprefs + +# Build results +[Dd]ebug/ +[Dd]ebugPublic/ +[Rr]elease/ +[Rr]eleases/ +x64/ +x86/ +build/ +bld/ +[Bb]in/ +[Oo]bj/ +out/ +target/ + +# Visual Studio 2015 cache/options directory +.vs/ +# Uncomment if you have tasks that create the project's static files in wwwroot +#wwwroot/ + +# MSTest test Results +[Tt]est[Rr]esult*/ +[Bb]uild[Ll]og.* + +# NUNIT +*.VisualState.xml +TestResult.xml + +# Build Results of an ATL Project +[Dd]ebugPS/ +[Rr]eleasePS/ +dlldata.c + +# Benchmark Results +BenchmarkDotNet.Artifacts/ + +# .NET Core +project.lock.json +project.fragment.lock.json +artifacts/ +**/Properties/launchSettings.json +# DNX +project.lock.json +artifacts/ + +*_i.c +*_p.c +*_i.h +*.ilk +*.meta +*.obj +*.pch +*.pdb +*.pgc +*.pgd +*.rsp +*.sbr +*.tlb +*.tli +*.tlh +*.tmp +*.tmp_proj +*.log +*.vspscc +*.vssscc +.builds +*.pidb +*.svclog +*.scc + +# Chutzpah Test files +_Chutzpah* + +# Visual C++ cache files +ipch/ +*.aps +*.ncb +*.opendb +*.opensdf +*.sdf +*.cachefile +*.VC.db +*.VC.VC.opendb +*.opensdf +*.sdf +*.cachefile + +# Visual Studio profiler +*.psess +*.vsp +*.vspx +*.sap + +# Visual Studio Trace Files +*.e2e + +# TFS 2012 Local Workspace +$tf/ + +# Guidance Automation Toolkit +*.gpState + +# ReSharper is a .NET coding add-in +_ReSharper*/ +*.[Rr]e[Ss]harper +*.DotSettings.user + +# JustCode is a .NET coding add-in +.JustCode + +# TeamCity is a build add-in +_TeamCity* + +# DotCover is a Code Coverage Tool +*.dotCover + +# AxoCover is a Code Coverage Tool +.axoCover/* +!.axoCover/settings.json + +# Visual Studio code coverage results +*.coverage +*.coveragexml + +# NCrunch +_NCrunch_* +.*crunch*.local.xml +nCrunchTemp_* + +# MightyMoose +*.mm.* +AutoTest.Net/ + +# Web workbench (sass) +.sass-cache/ + +# Installshield output folder +[Ee]xpress/ + +# DocProject is a documentation generator add-in +DocProject/buildhelp/ +DocProject/Help/*.HxT +DocProject/Help/*.HxC +DocProject/Help/*.hhc +DocProject/Help/*.hhk +DocProject/Help/*.hhp +DocProject/Help/Html2 +DocProject/Help/html + +# Click-Once directory +# publish/ + +# Publish Web Output +*.azurePubxml +# TODO: Comment the next line if you want to checkin your web deploy settings +# but database connection strings (with potential passwords) will be unencrypted +*.pubxml +*.publishproj + +# NuGet Packages +*.nupkg +# The packages folder can be ignored because of Package Restore +**/packages/* +# except build/, which is used as an MSBuild target. +!**/packages/build/ +# Uncomment if necessary however generally it will be regenerated when needed +#!**/packages/repositories.config + +# Windows Azure Build Output +csx/ +*.build.csdef + +# Windows Store app package directory +AppPackages/ + +# Visual Studio cache files +# files ending in .cache can be ignored +*.[Cc]ache +# but keep track of directories ending in .cache +!*.[Cc]ache/ + +# Others +ClientBin/ +[Ss]tyle[Cc]op.* +~$* +*~ +*.dbmdl +*.dbproj.schemaview +*.jfm +*.pfx +*.publishsettings +orleans.codegen.cs + +# Since there are multiple workflows, uncomment next line to ignore bower_components +# (https://github.com/github/gitignore/pull/1529#issuecomment-104372622) +#bower_components/ + +*.pfx +*.publishsettings +node_modules/ +orleans.codegen.cs + +# RIA/Silverlight projects +Generated_Code/ + +# Backup & report files from converting an old project file +# to a newer Visual Studio version. Backup files are not needed, +# because we have git ;-) +_UpgradeReport_Files/ +Backup*/ +UpgradeLog*.XML +UpgradeLog*.htm + +# SQL Server files +*.mdf +*.ldf +*.ndf + +# Business Intelligence projects +*.rdl.data +*.bim.layout +*.bim_*.settings + +# Microsoft Fakes +FakesAssemblies/ + +# GhostDoc plugin setting file +*.GhostDoc.xml + +# Node.js Tools for Visual Studio +.ntvs_analysis.dat +node_modules/ + +# Typescript v1 declaration files +typings/ +# Node.js Tools for Visual Studio +.ntvs_analysis.dat + +# Visual Studio 6 build log +*.plg + +# Visual Studio 6 workspace options file +*.opt + +# Visual Studio 6 auto-generated workspace file (contains which files were open etc.) +*.vbw + +# Visual Studio LightSwitch build output +**/*.HTMLClient/GeneratedArtifacts +**/*.DesktopClient/GeneratedArtifacts +**/*.DesktopClient/ModelManifest.xml +**/*.Server/GeneratedArtifacts +**/*.Server/ModelManifest.xml +_Pvt_Extensions + +# Paket dependency manager +.paket/paket.exe +paket-files/ + +# FAKE - F# Make +.fake/ + +# JetBrains Rider +.idea/ +*.sln.iml + +# CodeRush +.cr/ + +# Python Tools for Visual Studio (PTVS) +__pycache__/ +*.pyc + +# Cake - Uncomment if you are using it +# tools/** +# !tools/packages.config + +# Tabs Studio +*.tss + +# Telerik's JustMock configuration file +*.jmconfig + +# BizTalk build output +*.btp.cs +*.btm.cs +*.odx.cs +*.xsd.cs + +# OpenCover UI analysis results +OpenCover/ + +/build +/Build +/deploy +/package +bin/ +obj/ +sql/ +*/obj/debug +**/Debug +#ignore thumbnails created by windows +Thumbs.db +#Ignore files build by Visual Studio +*.obj +*.pdb +*.user +*.aps +*.pch +*.docstates +*.vspscc +*_i.c +*_p.c +*.ncb +*.suo +*.tlb +*.dbmdl +*.schemaview +*.tlh +*.cache +*.ilk +*.log +[Bb]in +[Dd]ebug*/ +*.lib +*.sbr +obj/ +[Rr]elease*/ +_ReSharper*/ +[Tt]est[Rr]esult* +*.docstates +*.swp +*.*~ +*.gpState +*.ReSharper* +*.preflight +*.nocommit +#Ignore Recovery Files made by Excel +~$*.xlsx +*.rdl.data + +*.jfm + +#====================================================================================== +# The below section could possibly be removed as these should be ignored by the above. +#====================================================================================== + +samples/in-memory/ticket-reservations/DemoWorkload/obj/Debug/TemporaryGeneratedFile_5937a670-0e60-4077-877b-f7221da3dda1.cs +*.nupkg +samples/in-memory/ticket-reservations/packages/CircularGauge.1.0.0/CreatePackageFile.bat +*.suo +samples/databases/wide-world-importers/sample-scripts/always-encrypted/PopulateAlwaysEncryptedData/bin/Debug/PopulateAlwaysEncryptedData.exe +*.pdb +samples/databases/wide-world-importers/sample-scripts/always-encrypted/PopulateAlwaysEncryptedData/bin/Debug/PopulateAlwaysEncryptedData.vshost.exe +*.Cache +samples/databases/wide-world-importers/sample-scripts/always-encrypted/PopulateAlwaysEncryptedData/bin/Debug/PopulateAlwaysEncryptedData.exe.config +samples/databases/wide-world-importers/sample-scripts/always-encrypted/PopulateAlwaysEncryptedData/obj/Debug/TemporaryGeneratedFile_036C0B5B-1481-4323-8D20-8F5ADCB23D92.cs +samples/databases/wide-world-importers/workload-drivers/vehicle-location-insert/MultithreadedInMemoryTableInsert/obj/Debug/TemporaryGeneratedFile_5937a670-0e60-4077-877b-f7221da3dda1.cs +*.exe +samples/features/in-memory/iot-smart-grid/ConsoleClient/bin/Release/DataGenerator.dll +samples/features/in-memory/iot-smart-grid/ConsoleClient/bin/Release/Reports/PowerDashboard.pbix +samples/databases/wide-world-importers/wwi-integration-etl/Daily ETL/bin/Development/Daily ETL.ispac +samples/databases/wide-world-importers/sample-scripts/always-encrypted/PopulateAlwaysEncryptedData/obj/Release/PopulateAlwaysEncryptedData.csproj.FileListAbsolute.txt +samples/databases/wide-world-importers/sample-scripts/always-encrypted/PopulateAlwaysEncryptedData/bin/Release/PopulateAlwaysEncryptedData.vshost.exe.config +samples/databases/wide-world-importers/sample-scripts/always-encrypted/PopulateAlwaysEncryptedData/obj/Release/TemporaryGeneratedFile_5937a670-0e60-4077-877b-f7221da3dda1.cs +*.dll +samples/features/in-memory/ticket-reservations/DemoWorkload/obj/Release/DemoWorkload.FrmConfig.resources +samples/features/in-memory/ticket-reservations/DemoWorkload/bin/Release/DemoWorkload.vshost.exe.config +samples/databases/wide-world-importers/sample-scripts/always-encrypted/PopulateAlwaysEncryptedData/obj/Debug/PopulateAlwaysEncryptedData.csproj.FileListAbsolute.txt +samples/databases/wide-world-importers/sample-scripts/always-encrypted/PopulateAlwaysEncryptedData/bin/Debug/PopulateAlwaysEncryptedData.vshost.exe.config +*.zip +samples/features/in-memory/ticket-reservations/packages/CircularGauge.1.0.0/CreatePackageFile.bat +samples/features/in-memory/ticket-reservations/TicketReservations/TicketReservations.dbmdl +samples/databases/wide-world-importers/workload-drivers/vehicle-location-insert/MultithreadedInMemoryTableInsert/obj/Release/TemporaryGeneratedFile_E7A71F73-0F8D-4B9B-B56E-8E70B10BC5D3.cs +samples/databases/wide-world-importers/workload-drivers/vehicle-location-insert/MultithreadedInMemoryTableInsert/obj/Debug/MultithreadedInMemoryTableInsert.csproj.FileListAbsolute.txt +samples/databases/wide-world-importers/sample-scripts/always-encrypted/PopulateAlwaysEncryptedData/obj/Debug/PopulateAlwaysEncryptedData.Properties.Resources.resources +samples/databases/wide-world-importers/sample-scripts/always-encrypted/PopulateAlwaysEncryptedData/obj/Release/PopulateAlwaysEncryptedData.PopulateAlwaysEncryptedDataMain.resources +samples/databases/wide-world-importers/sample-scripts/always-encrypted/PopulateAlwaysEncryptedData/bin/Debug/PopulateAlwaysEncryptedData.vshost.exe.manifest +samples/databases/wide-world-importers/sample-scripts/always-encrypted/PopulateAlwaysEncryptedData/obj/Debug/PopulateAlwaysEncryptedData.PopulateAlwaysEncryptedDataMain.resources +samples/databases/wide-world-importers/workload-drivers/vehicle-location-insert/MultithreadedInMemoryTableInsert/obj/Release/MultithreadedInMemoryTableInsert.Properties.Resources.resources +samples/databases/wide-world-importers/sample-scripts/always-encrypted/PopulateAlwaysEncryptedData/obj/Release/PopulateAlwaysEncryptedData.Properties.Resources.resources +samples/features/in-memory/ticket-reservations/packages/CircularGauge.1.0.0/ReadMe.txt +*.dacpac +samples/features/in-memory/ticket-reservations/TicketReservations/obj/Release/Model.xml +samples/features/in-memory/ticket-reservations/DemoWorkload/obj/Debug/TemporaryGeneratedFile_5937a670-0e60-4077-877b-f7221da3dda1.cs +*.dat + +*.user +samples/features/in-memory/ticket-reservations/DemoWorkload/obj/Release/TemporaryGeneratedFile_5937a670-0e60-4077-877b-f7221da3dda1.cs +samples/databases/wide-world-importers/workload-drivers/vehicle-location-insert/MultithreadedInMemoryTableInsert/obj/Debug/TemporaryGeneratedFile_E7A71F73-0F8D-4B9B-B56E-8E70B10BC5D3.cs +samples/features/in-memory/ticket-reservations/DemoWorkload/bin/Debug/DemoWorkload.vshost.exe.config +samples/features/in-memory/ticket-reservations/DemoWorkload/obj/Release/DemoWorkload.Properties.Resources.resources +samples/databases/wide-world-importers/wwi-integration-etl/Daily ETL/obj/Development/Project.params +samples/features/in-memory/ticket-reservations/DemoWorkload/obj/Debug/DemoWorkload.csproj.FileListAbsolute.txt +samples/features/in-memory/iot-smart-grid/Db/obj/Release/Model.xml +samples/applications/iot-smart-grid/DataGenerator/obj/Debug/TemporaryGeneratedFile_036C0B5B-1481-4323-8D20-8F5ADCB23D92.cs +samples/databases/wide-world-importers/workload-drivers/order-insert/MultithreadedOrderInsert/bin/Debug/MultithreadedOrderInsert.exe.config +samples/features/in-memory/iot-smart-grid/ConsoleClient/bin/Release/ConsoleClient.exe.config +*.jfm +samples/features/in-memory/ticket-reservations/TicketReservations/bin/Release/TicketReservations.publish.sql +samples/applications/iot-smart-grid/ConsoleClient/bin/Release/ConsoleClient.exe.config +samples/applications/iot-smart-grid/Db/Db.dbmdl +samples/databases/wide-world-importers/workload-drivers/order-insert/MultithreadedOrderInsert/obj/Release/TemporaryGeneratedFile_036C0B5B-1481-4323-8D20-8F5ADCB23D92.cs +samples/databases/wide-world-importers/wwi-dw-ssdt/wwi-dw-ssdt/obj/Debug/Model.xml +samples/databases/wide-world-importers/wwi-dw-ssdt/wwi-dw-ssdt/WideWorldImportersDW.dbmdl +samples/features/in-memory/iot-smart-grid/WinFormsClient/bin/Release/Reports/PowerDashboard.pbix +samples/databases/wide-world-importers/wwi-ssdt/wwi-ssdt/WideWorldImporters.dbmdl +samples/databases/wide-world-importers/workload-drivers/order-insert/MultithreadedOrderInsert/obj/Debug/TemporaryGeneratedFile_E7A71F73-0F8D-4B9B-B56E-8E70B10BC5D3.cs +samples/databases/wide-world-importers/wwi-ssasmd/wwi-ssasmd/bin/WWI-SSASMD.asdatabase +samples/applications/iot-smart-grid/Db/obj/Release/Db.sqlproj.FileListAbsolute.txt +*.manifest +samples/applications/iot-smart-grid/WinFormsClient/obj/Debug/TemporaryGeneratedFile_036C0B5B-1481-4323-8D20-8F5ADCB23D92.cs +samples/databases/wide-world-importers/wwi-ssasmd/wwi-ssasmd/obj/Development/IncrementalShapshot.xml +samples/applications/iot-smart-grid/ConsoleClient/bin/Release/Reports/PowerDashboard.pbix +samples/applications/iot-smart-grid/Db/obj/Release/Model.xml +samples/databases/wide-world-importers/workload-drivers/order-insert/MultithreadedOrderInsert/obj/Release/MultithreadedOrderInsert.csproj.FileListAbsolute.txt +samples/applications/iot-smart-grid/ConsoleClient/obj/Debug/TemporaryGeneratedFile_5937a670-0e60-4077-877b-f7221da3dda1.cs +samples/applications/iot-smart-grid/WinFormsClient/bin/Release/Client.exe.config +samples/databases/wide-world-importers/workload-drivers/order-insert/MultithreadedOrderInsert/bin/Release/MultithreadedOrderInsert.exe.config +samples/databases/wide-world-importers/workload-drivers/order-insert/MultithreadedOrderInsert/obj/Debug/MultithreadedInMemoryTableInsert.MultithreadedOrderInsertMain.resources +/samples/features/epm-framework/5.0/2Reporting/PolicyReports/PolicyEvaluationErrors.rdl.data +/samples/features/epm-framework/5.0/2Reporting/PolicyReports/PolicyEvaluationErrorDetails.rdl.data +/samples/features/epm-framework/5.0/2Reporting/PolicyReports/PolicyEvaluationDetails.rdl.data +/samples/features/epm-framework/5.0/2Reporting/PolicyReports/PolicyDetails.rdl.data +/samples/features/epm-framework/5.0/2Reporting/PolicyReports/PolicyDashboard.rdl.data +/samples/features/epm-framework/5.0/2Reporting/PolicyReports/bin/Debug +/samples/features/epm-framework/5.0/2Reporting/PolicyReports/PolicyDashboard - Backup.rdl +/samples/features/epm-framework/5.0/2Reporting/PolicyReports/PolicyDashboardFiltered.rdl.data +samples/features/sql-management-objects/src/out/CodeCoverage/CodeCoverage.config + +# Certificates +*.pem +*.p12 + +# Composer +/vendor/ \ No newline at end of file diff --git a/README.md b/README.md index 681d633..77424ef 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,3 @@ # Databases + Variant 11 diff --git a/db_script.sql b/db_script.sql new file mode 100644 index 0000000..ef7f6f6 --- /dev/null +++ b/db_script.sql @@ -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) +); diff --git a/eer_diagram.mwb b/eer_diagram.mwb new file mode 100644 index 0000000..d9e059e Binary files /dev/null and b/eer_diagram.mwb differ diff --git a/functions.sql b/functions.sql new file mode 100644 index 0000000..0ecab2b --- /dev/null +++ b/functions.sql @@ -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; diff --git a/inserts.sql b/inserts.sql new file mode 100644 index 0000000..0e51869 --- /dev/null +++ b/inserts.sql @@ -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); diff --git a/stored_procedures.sql b/stored_procedures.sql new file mode 100644 index 0000000..d9c7680 --- /dev/null +++ b/stored_procedures.sql @@ -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(); diff --git a/triggers.sql b/triggers.sql new file mode 100644 index 0000000..fa30f59 --- /dev/null +++ b/triggers.sql @@ -0,0 +1,363 @@ +USE pharmacy_db; +-- №1: забезпечити цілісність даних. + + +-- POST +DROP TRIGGER IF EXISTS post_update; + +DELIMITER // +CREATE TRIGGER post_update + BEFORE UPDATE + ON post + FOR EACH ROW +BEGIN + IF (old.id IN (SELECT post_id FROM employee)) THEN + SIGNAL SQLSTATE '45000' + SET MESSAGE_TEXT = 'cannot change linked entities'; + END IF; + IF (new.id != old.id) THEN + SIGNAL SQLSTATE '45000' + SET MESSAGE_TEXT = 'id is not same'; + END IF; +END // +DELIMITER ; + +DROP TRIGGER IF EXISTS post_delete; +DELIMITER // +CREATE TRIGGER post_delete + BEFORE DELETE + ON post + FOR EACH ROW +BEGIN + IF (old.id IN (SELECT post_id FROM employee)) THEN + SIGNAL SQLSTATE '45000' + SET MESSAGE_TEXT = 'there is at least one person with such data, you cannot delete it'; + END IF; +END // +DELIMITER ; + + +-- EFFECT ZONE + + +DROP TRIGGER IF EXISTS effect_zone_update; +DELIMITER // +CREATE TRIGGER effect_zone_update + BEFORE UPDATE + ON effect_zone + FOR EACH ROW +BEGIN + IF (old.id IN (SELECT zone_id FROM medicine_zone)) THEN + SIGNAL SQLSTATE '45000' + SET MESSAGE_TEXT = 'cannot change linked entities'; + END IF; + IF (new.id != old.id) THEN + SIGNAL SQLSTATE '45000' + SET MESSAGE_TEXT = 'id is not same'; + END IF; +END // +DELIMITER ; +# UPDATE effect_zone SET id = 16,name = 'Jojo' WHERE id = 3; + +DROP TRIGGER IF EXISTS effect_zone_delete; +DELIMITER // +CREATE TRIGGER effect_zone_delete + BEFORE DELETE + ON effect_zone + FOR EACH ROW +BEGIN + IF (old.id IN (SELECT zone_id FROM medicine_zone)) THEN + SIGNAL SQLSTATE '45000' + SET MESSAGE_TEXT = 'there is at least one person with such data, you cannot delete it'; + END IF; +END // +DELIMITER ; + +-- STREET +DROP TRIGGER IF EXISTS street_update; +DELIMITER // +CREATE TRIGGER street_update + BEFORE UPDATE + ON street + FOR EACH ROW +BEGIN + IF (old.id IN (SELECT street_id FROM pharmacy)) THEN + SIGNAL SQLSTATE '45000' + SET MESSAGE_TEXT = 'cannot change linked entities'; + END IF; + IF (new.id != old.id) THEN + SIGNAL SQLSTATE '45000' + SET MESSAGE_TEXT = 'id is not same'; + END IF; +END // +DELIMITER ; + +DROP TRIGGER IF EXISTS street_delete; +DELIMITER // +CREATE TRIGGER street_delete + BEFORE DELETE + ON street + FOR EACH ROW +BEGIN + IF (old.id IN (SELECT street_id FROM pharmacy)) THEN + SIGNAL SQLSTATE '45000' + SET MESSAGE_TEXT = 'there is at least one person with such data, you cannot delete it'; + END IF; +END // +DELIMITER ; + + +-- MEDICINE LIST +DROP TRIGGER IF EXISTS medicine_update; +DELIMITER // +CREATE TRIGGER medicine_update + BEFORE UPDATE + ON medicine_list + FOR EACH ROW +BEGIN + IF (old.id IN (SELECT medicine_id FROM medicine_zone) + OR old.id IN (SELECT medicine_id FROM pharmaсy_has_medicine)) THEN + SIGNAL SQLSTATE '45000' + SET MESSAGE_TEXT = 'cannot change linked entities'; + END IF; + IF (new.id != old.id) THEN + SIGNAL SQLSTATE '45000' + SET MESSAGE_TEXT = 'id is not same'; + END IF; + + +END // +DELIMITER ; + + +DROP TRIGGER IF EXISTS medicine_delete; +DELIMITER // +CREATE TRIGGER medicine_delete + BEFORE DELETE + ON medicine_list + FOR EACH ROW +BEGIN + IF (old.id IN (SELECT medicine_id FROM medicine_zone)) THEN + SIGNAL SQLSTATE '45000' + SET MESSAGE_TEXT = 'there is at least one person with such data, you cannot delete it'; + END IF; + IF (old.id IN (SELECT medicine_id FROM pharmaсy_has_medicine)) THEN + SIGNAL SQLSTATE '45000' + SET MESSAGE_TEXT = 'there is at least one person with such data, you cannot delete it'; + END IF; +END // +DELIMITER ; + + +-- Employee +DROP TRIGGER IF EXISTS employee_insert; + +DELIMITER // +CREATE TRIGGER employee_insert + BEFORE INSERT + ON employee + FOR EACH ROW +BEGIN + IF (new.post_id NOT IN (SELECT id FROM post)) THEN + SIGNAL SQLSTATE '45000' + SET MESSAGE_TEXT = 'FK error. no such data found'; + END IF; + IF (new.pharmacy_id NOT IN (SELECT id FROM pharmacy)) THEN + SIGNAL SQLSTATE '45000' + SET MESSAGE_TEXT = 'FK error. no such data found'; + END IF; +END// +DELIMITER ; + +DROP TRIGGER IF EXISTS employee_update; + +DELIMITER // +CREATE TRIGGER employee_update + BEFORE UPDATE + ON employee + FOR EACH ROW +BEGIN + IF (new.id != old.id) THEN + SIGNAL SQLSTATE '45000' + SET MESSAGE_TEXT = 'id is not same'; + END IF; +END// +DELIMITER ; + + +-- Pharmacy + +DROP TRIGGER IF EXISTS pharmacy_insert; + +DELIMITER // +CREATE TRIGGER pharmacy_insert + BEFORE INSERT + ON pharmacy + FOR EACH ROW +BEGIN + IF (new.street_id NOT IN (SELECT id FROM street)) THEN + SIGNAL SQLSTATE '45000' + SET MESSAGE_TEXT = 'FK error. no such data found'; + END IF; + +END// +DELIMITER ; + +DROP TRIGGER IF EXISTS pharmacy_update; +DELIMITER // +CREATE TRIGGER pharmacy_update + BEFORE UPDATE + ON pharmacy + FOR EACH ROW +BEGIN + IF (old.id IN (SELECT pharmacy_id FROM employee) + OR old.id IN (SELECT pharmacy_id FROM pharmaсy_has_medicine)) THEN + SIGNAL SQLSTATE '45000' + SET MESSAGE_TEXT = 'cannot change linked entities'; + END IF; + IF (new.id != old.id) THEN + SIGNAL SQLSTATE '45000' + SET MESSAGE_TEXT = 'id is not same'; + END IF; +END// +DELIMITER ; + +DROP TRIGGER IF EXISTS pharmacy_delete; +DELIMITER // +CREATE TRIGGER pharmacy_delete + BEFORE DELETE + ON pharmacy + FOR EACH ROW +BEGIN + IF (old.id IN (SELECT pharmacy_id FROM pharmaсy_has_medicine)) THEN + SIGNAL SQLSTATE '45000' + SET MESSAGE_TEXT = 'there is at least one person with such data, you cannot delete it'; + END IF; +END // +DELIMITER ; + +-- Medicine ZONE + +DROP TRIGGER IF EXISTS medicine_zone_insert; +DELIMITER // +CREATE TRIGGER medicine_zone_insert + BEFORE INSERT + ON medicine_zone + FOR EACH ROW +BEGIN + IF (new.medicine_id NOT IN (SELECT id FROM medicine_list)) THEN + SIGNAL SQLSTATE '45000' + SET MESSAGE_TEXT = 'FK error. no such data found'; + END IF; + IF (new.zone_id NOT IN (SELECT id FROM effect_zone)) THEN + SIGNAL SQLSTATE '45000' + SET MESSAGE_TEXT = 'FK error. no such data found'; + END IF; +END +// DELIMITER ; + +DROP TRIGGER IF EXISTS medicine_zone_update; +DELIMITER // +CREATE TRIGGER medicine_zone_update + BEFORE UPDATE + ON medicine_zone + FOR EACH ROW +BEGIN + IF (new.medicine_id NOT IN (SELECT id FROM medicine_list)) THEN + SIGNAL SQLSTATE '45000' + SET MESSAGE_TEXT = 'no such medicine_list found'; + END IF; + IF (new.zone_id NOT IN (SELECT id FROM effect_zone)) THEN + SIGNAL SQLSTATE '45000' + SET MESSAGE_TEXT = 'no such new effect_zone found'; + END IF; + +END +// DELIMITER ; +# UPDATE medicine_zone SET medicine_id = 110, zone_id = 6 WHERE medicine_id = 7 AND zone_id = 2; + +-- pharmacy has medicine + +DROP TRIGGER IF EXISTS pharmacy_has_medicine_insert; +DELIMITER // +CREATE TRIGGER pharmacy_has_medicine_insert + BEFORE INSERT + ON pharmaсy_has_medicine + FOR EACH ROW +BEGIN + IF (new.medicine_id NOT IN (SELECT id FROM medicine_list)) THEN + SIGNAL SQLSTATE '45000' + SET MESSAGE_TEXT = 'FK error. no such data found'; + END IF; + IF (new.pharmacy_id NOT IN (SELECT id FROM pharmacy)) THEN + SIGNAL SQLSTATE '45000' + SET MESSAGE_TEXT = 'FK error. no such data found'; + END IF; +END +// DELIMITER ; + +DROP TRIGGER IF EXISTS pharmacy_has_medicine_update; +DELIMITER // +CREATE TRIGGER pharmacy_has_medicine_update + BEFORE UPDATE + ON pharmaсy_has_medicine + FOR EACH ROW +BEGIN + IF (new.pharmacy_id NOT IN (SELECT id FROM pharmacy)) THEN + SIGNAL SQLSTATE '45000' + SET MESSAGE_TEXT = 'no such link in the linking table'; + END IF; + IF (new.medicine_id NOT IN (SELECT id FROM medicine_list)) THEN + SIGNAL SQLSTATE '45000' + SET MESSAGE_TEXT = 'no such link in the linking table'; + END IF; +END +// DELIMITER ; + +-- 2. +DROP TRIGGER IF EXISTS passport_data_format; +DELIMITER // +CREATE TRIGGER passport_data_format + BEFORE INSERT + ON employee + FOR EACH ROW +BEGIN + IF (new.passport_data NOT RLIKE '[a-zA-Z]{2} [0-9]{6}') THEN + SIGNAL SQLSTATE '45000' + SET MESSAGE_TEXT = 'passport data does not match provided pattern!'; + END IF; +END +// +DELIMITER ; + +-- 3. +DROP TRIGGER IF EXISTS name_format; +DELIMITER // +CREATE TRIGGER name_format + BEFORE INSERT + ON employee + FOR EACH ROW +BEGIN + IF (new.name NOT RLIKE 'Василь|Іван|Галина|Олександра') THEN + SIGNAL SQLSTATE '45000' + SET MESSAGE_TEXT = 'name does not match provided pattern!'; + END IF; +END +// +DELIMITER ; + + +-- 4. +DROP TRIGGER IF EXISTS ban_delete_on_street; +DELIMITER // +CREATE TRIGGER ban_delete_on_street + BEFORE DELETE + ON street + FOR EACH ROW +BEGIN + SIGNAL SQLSTATE '45000' + SET MESSAGE_TEXT = 'you cannot delete data on this table!'; +END // +DELIMITER ; + +# DELETE street from street where id = 9;