employees
and mentorships
.employees
table includes fields like ID (integer), name, gender, contact number, age, and date created.mentorships
table includes status (ongoing), project, mentor ID, and mentee ID (both foreign keys).DROP
command before CREATE
to avoid errors when practicing.company HR
) and then use it.#
.
DROP SCHEMA IF EXISTS `company HR`;
CREATE SCHEMA `company HR`;
USE `company HR`;
# This is a comment
CREATE TABLE
with parentheses to bind code.INT
, VARCHAR
, TIMESTAMP
).NOT NULL
, PRIMARY KEY
, and AUTO_INCREMENT
.employees
table creation:
CREATE TABLE co_employees (
ID INT PRIMARY KEY AUTO_INCREMENT,
name VARCHAR(255) NOT NULL,
gender CHAR(1) NOT NULL,
contact_number VARCHAR(55) NULL,
age INT NOT NULL,
date_created TIMESTAMP NOT NULL DEFAULT NOW()
);
mentorships
table creation (including foreign keys):
CREATE TABLE mentorships (
mentor_id INT NOT NULL,
mentee_id INT NOT NULL,
status VARCHAR(255) NOT NULL,
project VARCHAR(255),
PRIMARY KEY (mentor_id, mentee_id, project),
CONSTRAINT FK1 FOREIGN KEY (mentor_id) REFERENCES co_employees(ID) ON DELETE CASCADE ON UPDATE RESTRICT,
CONSTRAINT FK2 FOREIGN KEY (mentee_id) REFERENCES co_employees(ID) ON DELETE CASCADE ON UPDATE RESTRICT,
CONSTRAINT UQ_mentor_mentee UNIQUE (mentor_id, mentee_id)
);
UNIQUE
for mentor/mentee).ALTER TABLE
and RENAME TO
.
ALTER TABLE co_employees RENAME TO employees;
ALTER TABLE
, DROP COLUMN
, ADD COLUMN
.
ALTER TABLE employees DROP COLUMN age;
ALTER TABLE employees ADD COLUMN salary FLOAT NOT NULL AFTER contact_number;
ALTER TABLE employees ADD COLUMN years_in_company INT NOT NULL AFTER salary;
ALTER TABLE
and DROP FOREIGN KEY
.INSERT INTO
.UPDATE
and SET
with a WHERE
clause.
UPDATE employees SET contact_number = 'new_number' WHERE ID = 1;
DELETE FROM
with a WHERE
clause.
DELETE FROM employees WHERE ID = 5;
SELECT
:
SELECT employee_name, salary FROM employees;
AS
:
SELECT employee_name AS "Employee Name", salary AS "Salary" FROM employees;
DISTINCT
to avoid duplicates.
SELECT DISTINCT years_in_company FROM employees;
WHERE
with operators like !=
(not equal) and LIKE
.LIKE
uses %
as a wildcard.
SELECT * FROM employees WHERE employee_name LIKE '%ER%';
IN
to filter based on multiple values.
SELECT * FROM employees WHERE ID NOT IN (7, 8);
AND
and OR
.ORDER BY
(default is ascending).DESC
for descending order.AVG()
.
SELECT AVG(salary) FROM employees;
ROUND()
with decimal places.
SELECT ROUND(AVG(salary), 2) FROM employees;
MAX()
and MIN()
.SUM()
.CONCAT()
.