0% found this document useful (0 votes)
106 views3 pages

MySQL Cheat Sheet

This MySQL cheat sheet provides essential commands for database management, including creating and dropping databases and tables, performing CRUD operations, and using constraints. It also covers joins, aggregate functions, subqueries, indexes, views, and user permissions. The document serves as a quick reference for common MySQL tasks and syntax.

Uploaded by

rha7727
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
106 views3 pages

MySQL Cheat Sheet

This MySQL cheat sheet provides essential commands for database management, including creating and dropping databases and tables, performing CRUD operations, and using constraints. It also covers joins, aggregate functions, subqueries, indexes, views, and user permissions. The document serves as a quick reference for common MySQL tasks and syntax.

Uploaded by

rha7727
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

MySQL Cheat Sheet

1. Database Basics
SHOW DATABASES;
CREATE DATABASE db_name;
USE db_name;
DROP DATABASE db_name;

2. Table Operations
SHOW TABLES;
CREATE TABLE table_name (id INT, name VARCHAR(50));
DESCRIBE table_name;
DROP TABLE table_name;

3. CRUD Operations
-- INSERT
INSERT INTO table_name (column1, column2) VALUES (value1, value2);

-- SELECT
SELECT * FROM table_name;
SELECT column1 FROM table_name WHERE condition;

-- UPDATE
UPDATE table_name SET column1 = value1 WHERE condition;

-- DELETE
DELETE FROM table_name WHERE condition;

4. Constraints
PRIMARY KEY, FOREIGN KEY, NOT NULL, UNIQUE, DEFAULT, CHECK

CREATE TABLE students (


id INT PRIMARY KEY,
name VARCHAR(50) NOT NULL,
age INT CHECK (age >= 18)
);

5. Joins
-- INNER JOIN
SELECT * FROM A INNER JOIN B ON A.id = B.a_id;

-- LEFT JOIN
SELECT * FROM A LEFT JOIN B ON A.id = B.a_id;

-- RIGHT JOIN
SELECT * FROM A RIGHT JOIN B ON A.id = B.a_id;

-- FULL OUTER JOIN (via UNION)


(SELECT * FROM A LEFT JOIN B ON A.id = B.a_id)
UNION
(SELECT * FROM A RIGHT JOIN B ON A.id = B.a_id);

6. Aggregate Functions
SELECT COUNT(*), MAX(age), MIN(age), AVG(age), SUM(salary) FROM table_name;
SELECT department, COUNT(*) FROM employees GROUP BY department;

7. Subqueries & Aliases


SELECT name FROM employees WHERE salary > (SELECT AVG(salary) FROM employees);
SELECT e.name AS employee_name FROM employees e;

8. Indexes & Views


CREATE INDEX idx_name ON table_name (column);
CREATE VIEW view_name AS SELECT column1 FROM table_name;

9. Users & Permissions


CREATE USER 'user'@'localhost' IDENTIFIED BY 'password';
GRANT ALL PRIVILEGES ON db_name.* TO 'user'@'localhost';
FLUSH PRIVILEGES;

You might also like