Integrity Constraints
CREATE TABLE DEPARTMENT (
DEPT_CODE CHAR(2) PRIMARY KEY,
DEPT_NAME VARCHAR(20) NOT NULL,
DEPT_MGR VARCHAR(30)
);
CREATE TABLE EMPLOYEE (
EMPLOYEE_NUM INT PRIMARY KEY,
EMPLOYEE_NAME VARCHAR(20) NOT NULL,
SALARY FLOAT(10,2) NOT NULL,
JOB_LEVEL CHAR(3),
DEPT_CODE CHAR(2),
CONSTRAINT FK_DEPT_CODE
FOREIGN KEY(DEPT_CODE)
REFERENCES DEPARTMENT(DEPT_CODE)
ON DELETE CASCADE
);
DELETE FROM DEPARTMENT
WHERE DEPT_CODE = "IT";
ALTER TABLE EMPLOYEE
DROP CONSTRAINT FK_DEPT_CODE;
ALTER TABLE EMPLOYEE
ADD CONSTRAINT FK_DEPT_CODE
FOREIGN KEY(DEPT_CODE)
REFERENCES DEPARTMENT(DEPT_CODE)
ON DELETE CASCADE ON UPDATE CASCADE;
UPDATE DEPARTMENT SET DEPT_CODE = "IE" WHERE DEPT_CODE = "IT";
ALTER TABLE EMPLOYEE
DROP CONSTRAINT FK_DEPT_CODE;
ALTER TABLE EMPLOYEE
ADD CONSTRAINT FK_DEPT_CODE
FOREIGN KEY(DEPT_CODE)
REFERENCES DEPARTMENT(DEPT_CODE)
ON DELETE CASCADE ON UPDATE SET NULL;
UPDATE DEPARTMENT SET DEPT_CODE = "IT" WHERE DEPT_CODE = "IE";
Working with Multiple Tables
Assessment 1
1. There are two rules which need to be satisfied in order to perform a union, intersect, and
accept in SQL. First, it must be made sure that the number as well as the order of the
columns must be the same in all queries. And lastly, the data types must also be
compatible.
2. An inner join must be used in order to display only all the matching records from 2 tables in a
database.
3. There will be a total of 30 records that will be produced in a cross join of one table with 5
records to a second table with 6 records.
4. Inner joins are advisable to use when a user wants to show only the matching records of
joined tables. Meanwhile, an outer join is supposed to be used when a user wants to display or
return rows from tables regardless if they match or not.
5. The main difference between these two joins is that the left join includes all the records from
the left side even if it does not exist in the right table. On the contrary, the right join returns all of
the rows from the right side regardless if it exists in the left table.
Activity
1. SELECT EMPLOYEE.EMPNO, ENAME, RATING, RATINGPERIOD
FROM EMPLOYEE INNER JOIN EMPEVALUATION
ON EMPLOYEE.EMPNO = EMPEVALUATION.EMPNO;
2. SELECT DISTINCT EMPLOYEE.EMPNO, ENAME, EMPADDRESS
FROM EMPLOYEE INNER JOIN EMPEVALUATION
ON EMPLOYEE.EMPNO = EMPEVALUATION.EMPNO
WHERE EMPEVALUATION.RATING>= 90.00;
3. SELECT EMPLOYEE.EMPNO, ENAME, EMPADDRESS
FROM EMPLOYEE INNER JOIN EMPEVALUATION
ON EMPLOYEE.EMPNO = EMPEVALUATION.EMPNO
WHERE EMPEVALUATION.RATING BETWEEN 70 AND 75 &&
EMPEVALUATION.RATINGPERIOD = "FY2018";
4. A. INTERSECT
SELECT DISTINCT EMPNO
FROM EMPLOYEE
WHERE DATEHIRED > '2018-12-31' &&
EMPNO IN ( SELECT EMPNO FROM EMPEVALUATION
WHERE RATING >= 90 && RATINGPERIOD = 'FY2019' );
B. JOIN
SELECT DISTINCT EMPLOYEE.EMPNO
FROM EMPLOYEE
INNER JOIN EMPEVALUATION
ON EMPLOYEE.EMPNO = EMPEVALUATION.EMPNO
WHERE EMPLOYEE.DATEHIRED > '2018-12-31' &&
EMPEVALUATION.RATING >= 90 &&
EMPEVALUATION.RATINGPERIOD = 'FY2019';
5. SELECT DISTINCT EMPLOYEE.EMPNO
FROM EMPLOYEE INNER JOIN EMPEVALUATION
ON EMPLOYEE.EMPNO = EMPEVALUATION.EMPNO
WHERE EMPEVALUATION.RATING != 90;