diff --git a/README.md b/README.md index 3b24771..0759177 100644 --- a/README.md +++ b/README.md @@ -2,14 +2,21 @@ _MySQL, SQL language, setting environment variables._ -## 📔 Description +## Description -Intro pill which served as our first contact with the DBMS "MySQL", where we set an environment variable to be able to run the program from the command line, imported an example database from a ".sql" file provided by the official MySQL team, and executed some queries to play around with a DB and to getting familiar with the SQL language. +This pill will serve as our first contact with the DBMS "MySQL". The pill is divided into 2 separated parts. While the first part (employees.sql file) will display the schema of the database, the other - "queries" folder will include 4 sub-files which consists of queries according to different actions such as DELETE, INSERT, SELECT and UPDATE. -### 📂 Content +### Prerequisite -This repository contains only the following files: +- Install MySQL environment: MySQL(XAMPP) or MtSQL WorkBench -- **_README.md_**: this explanatory file. -- **_employees.sql_**: file containing an example database provided by the official MySQL team, to import and use in our exercise. -- **_queries.sql_**: file where we recorded all the necessary queries executed to fulfill this exercise. +### Technologies used + +- MySQL + +### Resources + +- [Official MySQL](https://www.mysql.com/) +- [W3Schools SQL](https://www.w3schools.com/sql/sql_syntax.asp) +- [Install MySQL](https://dev.mysql.com/downloads/installer/) +- [SQL Query Validator](https://www.eversql.com/sql-syntax-check-validator/) diff --git a/employees.sql b/employees.sql new file mode 100644 index 0000000..345964d --- /dev/null +++ b/employees.sql @@ -0,0 +1,108 @@ +-- Sample employee database +-- See changelog table for details +-- Copyright (C) 2007,2008, MySQL AB +-- +-- Original data created by Fusheng Wang and Carlo Zaniolo +-- http://www.cs.aau.dk/TimeCenter/software.htm +-- http://www.cs.aau.dk/TimeCenter/Data/employeeTemporalDataSet.zip +-- +-- Current schema by Giuseppe Maxia +-- Data conversion from XML to relational by Patrick Crews +-- +-- This work is licensed under the +-- Creative Commons Attribution-Share Alike 3.0 Unported License. +-- To view a copy of this license, visit +-- http://creativecommons.org/licenses/by-sa/3.0/ or send a letter to +-- Creative Commons, 171 Second Street, Suite 300, San Francisco, +-- California, 94105, USA. +-- +-- DISCLAIMER +-- To the best of our knowledge, this data is fabricated, and +-- it does not correspond to real people. +-- Any similarity to existing people is purely coincidental. +-- + +DROP DATABASE IF EXISTS employees; +CREATE DATABASE IF NOT EXISTS employees; +USE employees; + +SELECT 'CREATING DATABASE STRUCTURE' as 'INFO'; + +DROP TABLE IF EXISTS dept_emp, + dept_manager, + titles, + salaries, + employees, + departments; + +/*!50503 set default_storage_engine = InnoDB */; +/*!50503 select CONCAT('storage engine: ', @@default_storage_engine) as INFO */; + +CREATE TABLE employees ( + emp_no INT NOT NULL, + birth_date DATE NOT NULL, + first_name VARCHAR(14) NOT NULL, + last_name VARCHAR(16) NOT NULL, + gender ENUM ('M','F') NOT NULL, + hire_date DATE NOT NULL, + PRIMARY KEY (emp_no) +); + +CREATE TABLE departments ( + dept_no CHAR(4) NOT NULL, + dept_name VARCHAR(40) NOT NULL, + PRIMARY KEY (dept_no), + UNIQUE KEY (dept_name) +); + +CREATE TABLE dept_manager ( + emp_no INT NOT NULL, + dept_no CHAR(4) NOT NULL, + from_date DATE NOT NULL, + to_date DATE NOT NULL, + FOREIGN KEY (emp_no) REFERENCES employees (emp_no) ON DELETE CASCADE, + FOREIGN KEY (dept_no) REFERENCES departments (dept_no) ON DELETE CASCADE, + PRIMARY KEY (emp_no,dept_no) +); + +CREATE TABLE dept_emp ( + emp_no INT NOT NULL, + dept_no CHAR(4) NOT NULL, + from_date DATE NOT NULL, + to_date DATE NOT NULL, + FOREIGN KEY (emp_no) REFERENCES employees (emp_no) ON DELETE CASCADE, + FOREIGN KEY (dept_no) REFERENCES departments (dept_no) ON DELETE CASCADE, + PRIMARY KEY (emp_no,dept_no) +); + +CREATE TABLE titles ( + emp_no INT NOT NULL, + title VARCHAR(50) NOT NULL, + from_date DATE NOT NULL, + to_date DATE, + FOREIGN KEY (emp_no) REFERENCES employees (emp_no) ON DELETE CASCADE, + PRIMARY KEY (emp_no,title, from_date) +) +; + +CREATE TABLE salaries ( + emp_no INT NOT NULL, + salary INT NOT NULL, + from_date DATE NOT NULL, + to_date DATE NOT NULL, + FOREIGN KEY (emp_no) REFERENCES employees (emp_no) ON DELETE CASCADE, + PRIMARY KEY (emp_no, from_date) +) +; + +CREATE OR REPLACE VIEW dept_emp_latest_date AS + SELECT emp_no, MAX(from_date) AS from_date, MAX(to_date) AS to_date + FROM dept_emp + GROUP BY emp_no; + +# shows only the current department for each employee +CREATE OR REPLACE VIEW current_dept_emp AS + SELECT l.emp_no, dept_no, l.from_date, l.to_date + FROM dept_emp d + INNER JOIN dept_emp_latest_date l + ON d.emp_no=l.emp_no AND d.from_date=l.from_date AND l.to_date = d.to_date; \ No newline at end of file diff --git a/queries/delete.sql b/queries/delete.sql new file mode 100644 index 0000000..71f905a --- /dev/null +++ b/queries/delete.sql @@ -0,0 +1,10 @@ +-- Eliminate all employees with a salary greater than 20,000 +DELETE FROM employees +WHERE emp_no IN +(SELECT emp_no FROM salaries +WHERE salary > 20000); + +-- Remove the department that has more employees +DELETE FROM departments +WHERE dept_no = +(SELECT dept_no FROM dept_emp GROUP BY dept_no ORDER BY COUNT(emp_no) DESC LIMIT 1); diff --git a/queries/insert.sql b/queries/insert.sql new file mode 100644 index 0000000..23d9467 --- /dev/null +++ b/queries/insert.sql @@ -0,0 +1,116 @@ + +-- Insert value to table employees +INSERT INTO employees (emp_no, birth_date, first_name, last_name, gender, hire_date) +VALUES +(1, '1980-02-18', 'Carlos', 'Ochoa', 'M', '2005-06-04'), +(2, '1993-02-18', 'Teresa', 'Calvo', 'F', '2014-04-15'), +(3, '1982-02-18', 'Pilar', 'Paraiso', 'F', '2008-08-02'), +(4, '1995-02-18', 'Virginia', 'Gomez', 'F', '2020-07-30'), +(5, '1984-02-18', 'Ana', 'Navarra', 'F', '2014-05-30'), +(6, '1994-02-18', 'Carlos', 'Lopez', 'M', '2019-07-02'), +(7, '1986-02-18', 'Teresa', 'Martinez', 'F', '2017-04-18'), +(8, '1987-02-18', 'Ana', 'Ochoa', 'F', '2015-06-18'), +(9, '1996-02-18', 'Miguel', 'Ikene', 'M', '2020-01-24'), +(10, '1989-02-18', 'Fernando', 'Sanchez', 'M', '2011-09-30'), +(11, '1990-02-18', 'Paula', 'Perez', 'F', '2017-02-24'), +(12, '1975-02-18', 'Esperanza', 'Martin', 'F', '2012-03-20'), +(13, '1976-02-18', 'Belma', 'Gomez', 'F', '2005-11-28'), +(14, '1977-02-18', 'Paula', 'Lopez', 'F', '2009-04-18'), +(15, '1991-02-18', 'Juan', 'Caley', 'M', '2015-10-12'); + + +-- Insert value to table salaries +INSERT INTO salaries (emp_no, salary, from_date, to_date) +VALUES +(1, 9000, '2005-06-04', '2010-01-31'), +(1, 18000, '2010-02-01', '2012-12-31'), +(1, 26000, '2013-01-01', now()), +(2, 16000, '2014-04-15', now()), +(3, 18000, '2008-08-02', '2015-06-30'), +(3, 38000, '2015-07-01', now()), +(4, 22000, '2020-07-30', now()), +(5, 18000, '2014-05-30', '2020-04-30'), +(5, 36000, '2020-05-01', now()), +(6, 40000, '2019-07-02', now()), +(7, 42000, '2017-04-18', now()), +(8, 12000, '2015-06-18', now()), +(9, 24000, '2020-01-24', now()), +(10, 18000, '2011-09-30', '2019-09-30'), +(10, 35000, '2019-10-01', now()), +(11, 26000, '2017-02-24', now()), +(12, 12000, '2012-03-20', now()), +(13, 15000, '2005-11-28', '2012-08-31'), +(13, 42000, '2012-08-31', now()), +(14, 45000, '2009-04-18', now()), +(15, 28000, '2015-10-12', now()); + + +-- Insert value to table departments +INSERT INTO departments (dept_no, dept_name) +VALUES +('D01', 'Purchasing'), +('D02', 'Marketing'), +('D03', 'Accounting & Finance'), +('D04', 'Human Resource'), +('D05', 'Production'), +('D06', 'R&D'); + + +-- Insert value to table dept_emp +INSERT INTO dept_emp (emp_no, dept_no, from_date, to_date) +VALUES +(1, 'D02', '2005-06-04', '2012-12-31'), +(1, 'D05', '2010-02-01', now()), +(2, 'D04', '2014-04-15', '2018-04-15'), +(2, 'D02', '2018-04-16', now()), +(3, 'D04', '2008-08-02', now()), +(4, 'D01', '2020-07-30', now()), +(5, 'D03', '2014-05-30', '2017-07-15'), +(5, 'D06', '2017-07-16', now()), +(6, 'D01', '2019-07-02', now()), +(7, 'D02', '2017-04-18', '2020-10-15'), +(7, 'D05', '2020-10-16', now()), +(8, 'D03', '2015-06-18', '2019-02-16'), +(8, 'D04', '2019-02-17', now()), +(9, 'D01', '2020-01-24', now()), +(10, 'D02', '2011-09-30', '2013-01-31'), +(10, 'D05', '2013-02-01', now()), +(11, 'D03', '2017-02-24', '2018-04-15'), +(11, 'D01', '2018-04-16', now()), +(12, 'D06', '2012-03-20', '2016-06-30'), +(12, 'D02', '2016-07-01', now()), +(13, 'D03', '2005-11-28', '2009-10-31'), +(13, 'D05', '2009-11-01', now()), +(14, 'D01', '2009-04-18', '2020-01-31'), +(14, 'D06', '2020-02-01', now()), +(15, 'D02', '2015-10-12', '2017-03-31'), +(15, 'D05', '2017-04-01', now()); + +-- Insert value to table dept_manager +INSERT INTO dept_manager (emp_no, dept_no, from_date, to_date) +VALUES +(1, 'D05', '2010-02-01', now()), +(4, 'D01', '2020-07-30', now()), +(5, 'D06', '2017-07-16', now()), +(9, 'D01', '2020-01-24', now()), +(14, 'D06', '2020-02-01', now()); + + +-- Insert value to table titles +INSERT INTO titles (emp_no, title, from_date, to_date) +VALUES +(1, 'Bachelor of Commerce', '2002-08-15', NULL), +(2, 'Bachelor of Commerce', '2020-06-19', NULL), +(3, 'BA in Humanities & Social Sciences', '2005-10-01', NULL), +(4, 'Bachelor of Logistics Management', '2020-07-30', NULL), +(5, 'BA in Finance', '2020-05-01', NULL), +(6, 'Bachelor of Commerce', '2015-05-20', NULL), +(7, 'Bachelor of Economics Engineering', '2020-04-18', NULL), +(8, 'Bachelor of Human Resource Management', '2010-02-16', NULL), +(9, 'Bachelor of Logistics Management', '2019-05-25', NULL), +(10, 'Bachelor of Marketing', '2020-10-01', NULL), +(11, 'Bachelor of Civil Engineering', '2020-02-24', NULL), +(12, 'Bachelor of Economics Engineering', '2010-10-15', NULL), +(13, 'Bachelor of Finance and Accounting', '2000-05-24', NULL), +(14, 'Bachelor of Economics Engineering', '2006-12-03', NULL), +(15, 'Bachelor of Industrial Technologies Engineering', '2020-10-12', NULL); \ No newline at end of file diff --git a/queries/select.sql b/queries/select.sql new file mode 100644 index 0000000..19f583b --- /dev/null +++ b/queries/select.sql @@ -0,0 +1,47 @@ +-- Select all employees with a salary greater than 20,000 +SELECT * FROM employees INNER JOIN salaries +ON employees.emp_no = salaries.emp_no +WHERE salaries.salary > 20000; + + +-- Select all employees with a salary below 10,000 +SELECT * FROM employees INNER JOIN salaries +ON employees.emp_no = salaries.emp_no +WHERE salaries.salary < 10000; + +-- Select all employees who have a salary between 14.00 and 50,000 +SELECT * FROM employees INNER JOIN salaries +ON employees.emp_no = salaries.emp_no +WHERE salaries.salary BETWEEN 14000 AND 50000; + +-- Select the total number of employees +SELECT COUNT(emp_no) FROM employees; + +-- Select the name of employees without any being repeated +SELECT DISTINCT first_name FROM employees; + +-- Select the titles of the year 2019 +SELECT * FROM titles WHERE from_date LIKE '2019%'; +SELECT * FROM titles WHERE from_date BETWEEN '2019-01-01' AND '2019-12-31'; + + +-- Select only the name of the employees in capital letters +SELECT DISTINCT UCASE(first_name) FROM employees; + +-- Select the total number of employees who have worked in more than one department +SELECT COUNT(emp_no) +FROM (SELECT emp_no FROM dept_emp GROUP BY emp_no HAVING COUNT(emp_no) > 1) employees; + +-- Select the name, surname and number of times the employee has worked as a manager +SELECT employees.emp_no, employees.first_name, employees.last_name, Manager.times +FROM employees +LEFT JOIN (SELECT emp_no, COUNT(emp_no) AS times FROM dept_manager GROUP BY emp_no) AS Manager +ON Manager.emp_no = employees.emp_no +HAVING Manager.times >= 1; + +-- Select the name, surname and name of the current department of each employee +SELECT employees.emp_no, employees.first_name, employees.last_name, departments.dept_name +FROM employees +JOIN (SELECT emp_no, dept_no FROM dept_emp WHERE to_date = CURDATE()) AS T +ON employees.emp_no = T.emp_no +JOIN departments ON departments.dept_no = T.dept_no; \ No newline at end of file diff --git a/queries/update.sql b/queries/update.sql new file mode 100644 index 0000000..a2bba9e --- /dev/null +++ b/queries/update.sql @@ -0,0 +1,16 @@ +-- Update employees +SET SQL_SAFE_UPDATES = 0; +UPDATE employees +SET first_name = 'Maqueña' +WHERE first_name = 'Ana' + AND last_name = 'Navarra' + AND birth_date = '1984-02-18'; +SET SQL_SAFE_UPDATES = 1; + +-- Update departments +UPDATE departments SET dept_name = 'Supply Chain Management' WHERE dept_no = 'D01'; +UPDATE departments SET dept_name = 'Sales & Marketing' WHERE dept_no = 'D02'; +UPDATE departments SET dept_name = 'Finance & Accounting' WHERE dept_no = 'D03'; +UPDATE departments SET dept_name = 'Administration' WHERE dept_no = 'D04'; +UPDATE departments SET dept_name = 'Manufacturing' WHERE dept_no = 'D05'; +UPDATE departments SET dept_name = 'Research & Development' WHERE dept_no = 'D06'; \ No newline at end of file