mysql -u username -p
SHOW DATABASES;
USE database_name;
SHOW TABLES;
DESCRIBE table_name;
CREATE DATABASE database_name;
DROP DATABASE database_name;
CREATE TABLE employees (
id INT AUTO_INCREMENT PRIMARY KEY,
first_name VARCHAR(50),
last_name VARCHAR(50),
email VARCHAR(100) UNIQUE,
hire_date DATE,
salary DECIMAL(10, 2)
);
-- Add column
ALTER TABLE employees ADD COLUMN department VARCHAR(50);
-- Modify column
ALTER TABLE employees MODIFY COLUMN department VARCHAR(100);
-- Drop column
ALTER TABLE employees DROP COLUMN department;
DROP TABLE employees;
INSERT INTO employees (first_name, last_name, email, hire_date, salary)
VALUES ('John', 'Doe', 'john@example.com', '2023-01-15', 50000.00);
UPDATE employees
SET salary = 55000.00
WHERE id = 1;
DELETE FROM employees
WHERE id = 1;
SELECT * FROM employees;
SELECT first_name, last_name, salary
FROM employees
WHERE salary > 50000
ORDER BY last_name ASC;
SELECT * FROM employees
LIMIT 10 OFFSET 20;
SELECT * FROM employees
WHERE last_name LIKE 'S%';
SELECT * FROM employees
WHERE department_id IN (1, 2, 3);
SELECT * FROM employees
WHERE salary BETWEEN 40000 AND 60000;
SELECT e.first_name, e.last_name, d.department_name
FROM employees e
INNER JOIN departments d ON e.department_id = d.id;
SELECT e.first_name, e.last_name, d.department_name
FROM employees e
LEFT JOIN departments d ON e.department_id = d.id;
SELECT e.first_name, e.last_name, d.department_name
FROM employees e
RIGHT JOIN departments d ON e.department_id = d.id;
SELECT
COUNT(*) as total_employees,
AVG(salary) as average_salary,
MAX(salary) as highest_salary,
MIN(salary) as lowest_salary,
SUM(salary) as total_salary
FROM employees;
SELECT
CONCAT(first_name, ' ', last_name) as full_name,
UPPER(last_name) as upper_last_name,
LENGTH(first_name) as name_length
FROM employees;
SELECT
first_name,
hire_date,
YEAR(hire_date) as hire_year,
DATEDIFF(CURDATE(), hire_date) as days_employed
FROM employees;
CREATE INDEX idx_last_name ON employees (last_name);
CREATE UNIQUE INDEX idx_email ON employees (email);
DROP INDEX idx_last_name ON employees;
CREATE VIEW high_salary_employees AS
SELECT first_name, last_name, salary
FROM employees
WHERE salary > 60000;
SELECT * FROM high_salary_employees;
DELIMITER //
CREATE PROCEDURE GetEmployeesByDepartment(IN dept_id INT)
BEGIN
SELECT * FROM employees
WHERE department_id = dept_id;
END //
DELIMITER ;
CALL GetEmployeesByDepartment(1);
DELIMITER //
CREATE TRIGGER before_employee_update
BEFORE UPDATE ON employees
FOR EACH ROW
BEGIN
SET NEW.last_modified = NOW();
END //
DELIMITER ;
START TRANSACTION;
UPDATE accounts SET balance = balance - 100 WHERE id = 1;
UPDATE accounts SET balance = balance + 100 WHERE id = 2;
COMMIT;
-- Or ROLLBACK; if there's an error
CREATE USER 'newuser'@'localhost' IDENTIFIED BY 'password';
GRANT ALL PRIVILEGES ON database_name.* TO 'newuser'@'localhost';
FLUSH PRIVILEGES;
REVOKE ALL PRIVILEGES ON database_name.* FROM 'newuser'@'localhost';
mysqldump -u username -p database_name > backup.sql
mysql -u username -p database_name < backup.sql
EXPLAIN SELECT * FROM employees WHERE last_name = 'Smith';
OPTIMIZE TABLE employees;
SHOW PROCESSLIST;
CREATE FULLTEXT INDEX ft_idx ON articles (title, body);
SELECT * FROM articles
WHERE MATCH (title, body) AGAINST ('keyword' IN NATURAL LANGUAGE MODE);
SELECT department, job_title, SUM(salary)
FROM employees
GROUP BY department, job_title WITH ROLLUP;
INSERT INTO employees (id, first_name, last_name, salary)
VALUES (1, 'John', 'Doe', 50000)
ON DUPLICATE KEY UPDATE salary = salary + 1000;
2024 © All rights reserved - buraxta.com