================ TABLES AND COLUMNS ================
create table employees (
employee_id int,
first_name varchar(50),
last_name varchar(50),
hourly_pay decimal(5, 2),
hire_date date
);
alter table employees
rename column hourly_pay to phone_number;
alter table employees
modify phone_number varchar(15);
alter table employees
add email varchar(50);
alter table employees
modify email varchar(50)
first;
alter table employees
drop hire_date;
select * from employees;
================ TABLES AND ROWS ================
create table employees (
employee_id int,
first_name varchar(50),
last_name varchar(50),
hourly_pay decimal(5, 2),
hire_date date
);
insert into employees
values (1, "Eguene", "Krabs", 25.50, "2023-01-02"),
(2, "Squidward", "Tentacles", 15.00, "2023-01-03"),
(3, "Spongebob", "Squarepants", 12.50, "2023-01-04"),
(4, "Patrick", "Star", 12.50, "2023-01-05"),
(5, "Sandy", "Cheeks", 17.25, "2023-01-06");
# If We Want To Add Specifc data!
insert into employees(employee_id, first_name, last_name)
values (6, " "الصغير,";)"المربع
select * from employees;
================ TABLES AND SELECTING DATA ================
-- select first_name, last_name from employees where employee_id = 1;
-- select employee_id, first_name from employees where hourly_pay >= 15;
-- select * from employees where hire_date is null and hourly_pay is null;
-- select * from employees where hire_date is not null;
================ Update & Delete ================
UPDATE employees
SET hourly_pay = 12.50, hire_date = "2023-01-07"
WHERE employee_id = 6;
*******************************
DELETE FROM employees
WHERE employee_id = 6;
================ AUTOCOMMIT ================
SET AUTOCOMMIT = OFF; ==> TURN OFF THE AUTOSAVE AFTER EXCUTE
COMMIT ==> SAVE MANUALY
ROLLBACK ==> REDO MANULAY
================ DATE & TIME ================
CREATE TABLE time_test(
the_date DATE,
the_time TIME,
the_date_time DATETIME
);
================ UNIQUE CONSTRAINT ================
INSERT INTO time_test
VALUES(CURRENT_DATE(), CURRENT_TIME, NOW());
SELECT * FROM time_test;
CREATE TABLE test(
id INT -- NOT NULL,
test_name VARCHAR(25) -- UNIQUE()
);
ALTER TABLE test
ADD CONSTRAINT
UNIQUE(test_name);
ALTER TABLE test
MODIFY id INT NOT NULL;
INSERT INTO test
VALUES (1, "PIZZA"),
(2, "HAMBURGR");
SELECT * FROM test;
================ IMPORTANT OPERATIONS ================
-- SELECT * FROM `products` ORDER BY Price ASC LIMIT 2;
-- SELECT DISTINCT Category FROM `products`;
-- SELECT * FROM `products` WHERE Name like 'S____';
-- SELECT * FROM `products` WHERE Name like 'S____';
-- SELECT * FROM `products` WHERE Price BETWEEN 100 AND 1000;
-- SELECT Category, COUNT(*) FROM `products` GROUP BY Category;
-- SELECT AVG(Price), Category, COUNT(*) FROM `products` GROUP BY Category HAVING
AVG(Price) > 100;
-- SELECT AVG(Price) FROM `products` HAVING AVG(Price) > 100;
================ JOIN OPERATIONS ================
SELECT students.id, students.name, courses.course_name
FROM students
INNER JOIN courses ON students.id = courses.student_id;
=============
SELECT students.id, students.name, courses.course_name
FROM students
LEFT JOIN courses ON students.id = courses.student_id;
=============
SELECT students.id, students.name, courses.course_name
FROM students
RIGHT JOIN courses ON students.id = courses.student_id;
=============
SELECT students.id, students.name, courses.course_name
FROM students
LEFT JOIN courses ON students.id = courses.student_id
UNION
SELECT students.id, students.name, courses.course_name
FROM students
RIGHT JOIN courses ON students.id = courses.student_id;