100% found this document useful (1 vote)
255 views3 pages

HR Schema

The document creates and defines the structure of an HR database with tables for regions, countries, locations, departments, jobs, employees, and job history. Relationships between the tables are established using foreign keys. Views and triggers are also created to join and validate the data.
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
Download as txt, pdf, or txt
100% found this document useful (1 vote)
255 views3 pages

HR Schema

The document creates and defines the structure of an HR database with tables for regions, countries, locations, departments, jobs, employees, and job history. Relationships between the tables are established using foreign keys. Views and triggers are also created to join and validate the data.
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1/ 3

DROP DATABASE IF EXISTS hr; CREATE DATABASE hr; USE hr; CREATE TABLE regions ( region_id INT NOT

NULL , region_name varchar(25), primary key (region_id) ); CREATE TABLE countries ( country_id CHAR(2) NOT NULL , country_name varchar(40) , region_id INT , PRIMARY KEY (country_id) ) ; ALTER TABLE countries ADD (FOREIGN KEY (region_id) REFERENCES regions(region_id) ) ; CREATE TABLE locations ( location_id INT(4) , street_address varchar(40) , postal_code varchar(12) , city varchar(30) NOT NULL , state_province varchar(25) , country_id CHAR(2) , PRIMARY KEY (location_id) ) auto_increment=3300; /* CREATE SEQUENCE locations_seq\ START WITH 3300\ INCREMENT BY 100\ MAXVALUE 9900\ NOCACHE\ NOCYCLE;\ */ ALTER TABLE locations ADD ( FOREIGN KEY (country_id) REFERENCES countries(country_id) ) ; CREATE TABLE departments ( department_id INT(4) , department_name varchar(30) NOT NULL , manager_id INT(6) , location_id INT(4) , PRIMARY KEY (department_id) ) auto_increment=280; ALTER TABLE departments ADD ( FOREIGN KEY (location_id) REFERENCES locations (location_id) ) ;

/* CREATE SEQUENCE departments_seq\ START WITH 280\ INCREMENT BY 10\ MAXVALUE 9990\ NOCACHE\ NOCYCLE;\ */ CREATE TABLE jobs ( job_id VARCHAR(10) , job_title VARCHAR(35) NOT NULL , min_salary INT(6) , max_salary INT(6) , PRIMARY KEY (job_id) ) ; CREATE TABLE employees ( employee_id INT(6) , first_name varchar(20) , last_name varchar(25) NOT NULL , email varchar(25) NOT NULL , phone_INT varchar(20) , hire_date DATE NOT NULL , job_id varchar(10) NOT NULL , salary DECIMAL(8,2) , commission_pct DECIMAL(2,2) , manager_id INT(6) , department_id INT(4) , UNIQUE KEY (email) , PRIMARY KEY (employee_id) ) auto_increment=207; DELIMITER // CREATE TRIGGER emp_sal_min BEFORE INSERT ON employees FOR EACH ROW BEGIN IF (salary <= 0 ) THEN call unknown(); END IF; END// DELIMITER ; ALTER TABLE emp loyees ADD ( FOREIGN KEY (department_id) REFERENCES departments(department_id)); ALTER TABLE employees ADD ( FOREIGN KEY (job_id) REFERENCES jobs (job_id)); ALT ER TABLE employees ADD ( FOREIGN KEY (manager_id) REFERENCES employees(employee_ id)) ; ALTER TABLE departments ADD ( FOREIGN KEY (manager_id) REFERENCES employe es (employee_id)) ; /* CREATE SEQUENCE employees_seq\ START WITH 207\ INCREMENT BY 1\ NOCACHE\ NOCYCLE;\ */ CREATE TABLE job_history ( employee_id INT(6) NOT NU LL , start_date DATE NOT NULL , end_date DATE NOT NULL , job_id VARCHAR(10) NOT NULL , department_id INT(4) , PRIMARY KEY (employee_id, start_date) ) ; DELIMITE R // CREATE TRIGGER jobhist_dates BEFORE INSERT ON job_history FOR EACH ROW BEGI N IF (end_date > start_date ) THEN call unknown(); END IF; END// DELIMITER ; ALTER TABLE job_history ADD (FOREIGN KEY (job_id) REFERENCES jobs(job_id));

ALTER TABLE job_history ADD ( FOREIGN KEY (employee_id) REFERENCES employees(employee_id)); ALTER TABLE job_history ADD (FOREIGN KEY (department_id) REFERENCES departments(department_id)) ; CREATE OR REPLACE VIEW emp_details_view (employee_id, job_id, manager_id, department_id, location_id, country_id, first_name, last_name, salary, commission_pct, department_name, job_title, city, state_province, country_name, region_name) AS SELECT e.employee_id, e.job_id, e.manager_id, e.department_id, d.location_id, l.country_id, e.first_name, e.last_name, e.salary, e.commission_pct, d.department_name, j.job_title, l.city, l.state_province, c.country_name, r.region_name FROM employees e, departments d, jobs j,locations l, countries c, regions r WHERE e.department_id = d.department_id AND d.location_id = l.location_id AND l.country_id = c.country_id AND c.region_id = r.region_id AND j.job_id = e.job_id ;

You might also like