DEPARTMENT
EMPLOYEE
Contain #Dept_ID
#Emp_ID
*Dept_name
*Empl_name
Works
*DOB in
(#)email
DEPARTMENTS
Key type Optionality Column name
PK * DEPT_ID
* DEPT_name
EMPLOYEES
Key type Optionality Column name
PK * EMP_ID
* EMP_NAME
* DOB
UK * Email
FK o Dept_ID
1/ create the tables
Quistion1:
CREATE TABLE Rand.DEPARTMENTS
(DEPT_ID int PRIMARY KEY,
DEPT_name varchar(45) NOT NULL );
CREATE TABLE Rand.EMPLOYEES
( EMP_ID int PRIMARY KEY,
EMP_name varchar(100) NOT NULL,
DOB DATE NOT NULL ,
EMAIL varchar(100) UNIQUE NOT NULL,
DEPT_ID int,
CONSTRAINT FOREIGN KEY (DEPT_ID) REFERENCES Rand.DEPARTMENTS (DEPT_ID)
);
2/ add the column salary to table employees and add a check to make sure that the salary is not
a negative number
Question 2:
ALTER TABLE Rand.EMPLOYEES
ADD SALARY int CHECK (SALARY>0);
3/ change the max size for the email column to 50
Question 3:
ALTER TABLE Rand.EMPLOYEES
MODIFY EMAIL varchar(50);
4/ add a column address to table employee
Question 4:
ALTER TABLE Rand.EMPLOYEES
add Address varchar(45);
5/ delete the column address from table employee
ALTER TABLE Rand.EMPLOYEES
DROP Address;
6/ fill the tables
DEPT_ID DEPT_name
1 Human resources
2 Finance
3 Marketing
INSERT INTO Rand.DEPARTMENTS
VALUES (1, 'Human resources');
INSERT INTO Rand.DEPARTMENTS
VALUES (2, 'Finance');
INSERT INTO Rand.DEPARTMENTS
VALUES (3,'Marketing');
Emp_ID EMP_NAME DOB EMAIL DEPT_ID SALARY
123 Mohammad 1990-3-2 M@yu 2 100000
INSERT INTO Rand.EMPLOYEES
VALUES (123, 'Mohammad', '1990-3-2', 'M@yu', 2, 10000);
7/ query all the tables
SELECT * FROM Rand.DEPARTMENTS;
SELECT * FROM Rand.EMPLOYEES;
8/ query only the EMP_NAME and DOB Column
SELECT EMP_name, DOB FROM Rand.EMPLOYEES;
9/ query the SALARY * 12 and give an allias “ Annual Salary”
SELECT SALARY , SALARY *12 AS "Annual Salary"
from Rand.EMPLOYEES;
10/ retrieve only the record with DEPT_ID =2 from the table departments
SELECT *
FROM Rand.DEPARTMENTS
WHERE DEPT_ID=2;
11/ retrieve the DEPT_name in alphabetical order
SELECT *
FROM Rand.DEPARTMENTS
ORDER BY DEPT_name
12/ retrieve the EMP_NAME from table EMPLOYEE and DEPT_name from table DEPARTMENT
SELECT e.EMP_name , d.DEPT_Name
FROM Rand.DEPARTMENTS d JOIN Rand.EMPLOYEES e
on d.DEPT_ID= e.DEPT_ID;