can't insert the values into the tables
Hi Frederico!
Thanks for reaching out.
Please download the .zip file provided as a resource just under this video:
https://learn.365datascience.com/courses/sql/loading-the-employees-database/
After you do that, unzip it and there's nothing to adjust. You'll only have to start MySQL, load the *.sql code and run it! Then, run use employees;
to confirm the database has been properly loaded.
Hope this helps but please feel free to get back to us should you need further assistance. Thank you!
Kind regards,
Martin
The problem is i'm using the sql server instead of MySQL.
Thank you!
Best regards,
Frederico
Hi Frederico,
Thanks for your reply.
In principle, I suppose there's nothing different from what we show in class - you need to load the code and run it in order to set up the employees database.
Then, you are right that the code for SQL Server is different. Our course is based on MySQL, so we don't normally provide feedback on the other SQL versions.
Nevertheless, I tried to adapt the first, fundamental part of the *.sql code so that it runs on SQL Server (you should be able to successfully run the entire code with these adjustments at the beginning) [please see attached file].
IF DB_ID('employees') IS NOT NULL
DROP DATABASE employees;
GO
IF DB_ID('employees') IS NULL
CREATE DATABASE employees;
GO
USE employees;
GO
PRINT 'CREATING DATABASE STRUCTURE';
GO
IF OBJECT_ID('dept_emp', 'U') IS NOT NULL DROP TABLE dept_emp;
IF OBJECT_ID('dept_manager', 'U') IS NOT NULL DROP TABLE dept_manager;
IF OBJECT_ID('titles', 'U') IS NOT NULL DROP TABLE titles;
IF OBJECT_ID('salaries', 'U') IS NOT NULL DROP TABLE salaries;
IF OBJECT_ID('employees', 'U') IS NOT NULL DROP TABLE employees;
IF OBJECT_ID('departments', 'U') IS NOT NULL DROP TABLE departments;
GO
CREATE TABLE employees (
emp_no INT NOT NULL PRIMARY KEY,
birth_date DATE NOT NULL,
first_name VARCHAR(14) NOT NULL,
last_name VARCHAR(16) NOT NULL,
gender CHAR(1) NOT NULL CHECK (gender IN ('M','F')),
hire_date DATE NOT NULL
);
GO
CREATE TABLE departments (
dept_no CHAR(4) NOT NULL PRIMARY KEY,
dept_name VARCHAR(40) NOT NULL UNIQUE
);
GO
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,
PRIMARY KEY (emp_no, dept_no),
FOREIGN KEY (emp_no) REFERENCES employees (emp_no) ON DELETE CASCADE,
FOREIGN KEY (dept_no) REFERENCES departments (dept_no) ON DELETE CASCADE
);
GO
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,
PRIMARY KEY (emp_no, dept_no),
FOREIGN KEY (emp_no) REFERENCES employees (emp_no) ON DELETE CASCADE,
FOREIGN KEY (dept_no) REFERENCES departments (dept_no) ON DELETE CASCADE
);
GO
CREATE TABLE titles (
emp_no INT NOT NULL,
title VARCHAR(50) NOT NULL,
from_date DATE NOT NULL,
to_date DATE NULL,
PRIMARY KEY (emp_no, title, from_date),
FOREIGN KEY (emp_no) REFERENCES employees (emp_no) ON DELETE CASCADE
);
GO
CREATE TABLE salaries (
emp_no INT NOT NULL,
salary INT NOT NULL,
from_date DATE NOT NULL,
to_date DATE NOT NULL,
PRIMARY KEY (emp_no, from_date),
FOREIGN KEY (emp_no) REFERENCES employees (emp_no) ON DELETE CASCADE
);
GO
IF OBJECT_ID('dept_emp_latest_date', 'V') IS NOT NULL DROP VIEW dept_emp_latest_date;
GO
CREATE 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;
GO
IF OBJECT_ID('current_dept_emp', 'V') IS NOT NULL DROP VIEW current_dept_emp;
GO
CREATE VIEW current_dept_emp AS
SELECT l.emp_no, d.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 d.to_date = l.to_date;
GO
INSERT INTO employees (emp_no, birth_date, first_name, last_name, gender, hire_date)
VALUES (10001, '1953-09-02', 'Georgi', 'Facello', 'M', '1986-06-26');
GO
Then, please consider switching to MySQL Workbench. I am sure this would help you complete the course more quickly and learn more from it. Then, it will be relatively easier to see how to switch into other SQL versions.
Hope this helps.
Kind regards,
Martin