MySQL_cheatsheet_withLinks_compressed
MySQL_cheatsheet_withLinks_compressed
2. Introduction of MySQL
• Developed by Oracle
• An open source relational database management system (RDBMS)
• Based on Structured Query Language (SQL)
02 MySQL CheatSheet
3. Properties of MySQL
4. Data Types
String Numeric
• DATE: YYYY-MM-DD
• DATETIME: YYYY-MM-DD HH:MM:SS
• TIMESTAMP: YYYY-MM-DD HH:MM:SS
• TIME: HH:MM:SS
03 MySQL CheatSheet
5. Types of commands
COMMANDS
04 MySQL CheatSheet
02 DDL Commands
1. CREATE
Syntax Example
2. ALTER
Used to add, delete, or modify columns and to add or delete constraints in an existing
table.
05 MySQL CheatSheet
New column added in the table
Syntax(Modify column)
Example(Modify column)
Syntax(Rename column)
Example(Rename column)
06 MySQL CheatSheet
Syntax(Drop column) Example(Drop column)
Renamed column
Syntax(Add constraint)
Example(Add constraint)
Syntax(Drop constraint)
07 MySQL CheatSheet
Example(Drop constraint)
3. TRUNCATE
Used to delete complete data of the table without deleting table structure.
Syntax Example
4. DROP
Syntax Example
08 MySQL CheatSheet
5. RENAME
Syntax Example
09 MySQL CheatSheet
03 Constraints
Prevents column from accepting Ensures that column does not accept
NULL values. NULL and duplicate values.
Example Example
CHECK UNIQUE
Limit the values for column. Ensures that column does not have
duplicate values, but allow NULL values.
Example Example
10 MySQL CheatSheet
FOREIGN KEY DEFAULT
A field in one table which refers to Used to provide default value to the column,
primary key of another table. which will be used if no value is provided
for that column.
Example Example
11 MySQL CheatSheet
04 DML Commands
1. INSERT Command
Syntax 1
Example 1
1 John 2002-09-09
12 MySQL CheatSheet
Syntax 2
Example 2
1 John 2002-09-09
2 Hannah 2000-08-07
Example 3
1 John 2002-09-09
2 Hannah 2000-08-07
3 Jim 2001-07-06
4 Jeffer 1999-09-02
13 MySQL CheatSheet
2. UPDATE
Syntax
Example 1
1 John 2002-09-09
2 Hannah 2000-08-07
3 Jimmy 2001-07-06
4 Jeffer 1999-09-02
Example 2
14 MySQL CheatSheet
empId empName empDOB
1 John 2002-09-09
2 Hannah 2000-08-07
3 Jimmy 2001-07-06
4 Jiya 2000-09-08
3. DELETE
Syntax 1 Example 1
4 Jiya 2000-09-08
15 MySQL CheatSheet
empId empName empDOB
1 John 2002-09-09
empId empName empDOB
2 Hannah 2000-08-07
3 Jimmy 2001-07-06
4 Jiya 2000-09-08
05 DQL Command(SELECT)
1 John 2002-09-09
2 Hannah 2000-08-07
3 Jimmy 2001-07-06
4 Jiya 2000-09-08
1. SELECT
Syntax
16 MySQL CheatSheet
2. FETCHING ALL Data
Syntax Example
1 John 2002-09-09
2 Hannah 2000-08-07
3 Jimmy 2001-07-06
4 Jiya 2000-09-08
Syntax
Example
17 MySQL CheatSheet
empId empName empDOB
1 John 2002-09-09
2 Hannah 2000-08-07
3 Jimmy 2001-07-06
4 Jiya 2000-09-08
Syntax
1 John
Example 2 Hannah
3 Jimmy
5. USING AS ALIAS
Example
18 MySQL CheatSheet
Employee_Id Employee_Name
1 John
2 Hannah
3 Jimmy
4 Jiya
6. USING WHERE
Syntax
Example
empId empName
1 John
19 MySQL CheatSheet
06 Other commands
Can used with insert, update and delete command only. Used to grant and takeback permissions from the user.
COMMIT GRANT
Save the transaction to the database Gives privileges to the user.
Syntax: COMMIT; Syntax: GRANT SELECT, UPDATE ON tableName
TO someUser, anotherUser;
ROLLBACK REVOKE
Used to undo the transactions that are not saved in
Takeback privileges from the user
the database
Syntax: REVOKE SELECT, UPDATE ON tableName
Syntax: ROLLBACK; FROM user1, user2;
SAVEPOINT
Used to rollback transactions to certain point instead
of rollback complete transaction
Syntax: SAVEPOINT savepointName;
20 MySQL CheatSheet
1. Subqueries
• Query inside query • Can be used with HAVING, WHERE,
• Enclosed in parenthesis. SELECT and FROM clause.
2. ALL Keyword
Syntax(With WHERE)
3 Herry 2500
Syntax(With HAVING)
21 MySQL CheatSheet
Example(With WHERE and HAVING Clause)
3 Herry 2500
empId
22 MySQL CheatSheet
3. ANY Keyword
Syntax
Example
SELECT empId, empName, empSal FROM Employee WHERE empSal > ANY
(SELECT mgrSal from Manager);
1 John 2300
3 Herry 2500
4. CORRELATED SUBQUERY
• A subquery in which inner query is dependent on outer query for its execution.
• The inner query executes for every selected record of the outer query.
23 MySQL CheatSheet
Example
1 John 2300
5. EXISTS OPERATOR
Syntax
Example
1 John 2300
3 Herry 2500
24 MySQL CheatSheet
6. SUBQUERY IN SELECT
Example
SELECT empId, empName, (SELECT avg(empSal) from Employee) as avgSal FROM Employee;
1 John 2300.0000
2 Hannah 2300.0000
3 Herry 2300.0000
7. SUBQUERY IN FROM
Example
1 John 2300
2 Hannah 2100
3 Herry 2500
25 MySQL CheatSheet
08 Functions
PRODUCT
Id Name Price
1 Bag 12.76
2 Gloves 30.98
3 Pen 10.87
AGGREGATE FUNCTIONS
Aggregate functions
AVG() SUM()
SUM()
Syntax Example
SUM()
SELECT SUM(Price) FROM Product;
26 MySQL CheatSheet
SUM(Price)
54.61
MAX()
Syntax Example
MAX()
SELECT MAX(Price) FROM Product;
MAX(Price)
30.98
MIN()
Syntax Example
MIN()
SELECT MIN(Price) FROM Product;
MIN(Price)
10.87
27 MySQL CheatSheet
AVG()
Syntax Example
AVG()
SELECT AVERAGE(Price) FROM Product;
AVERAGE(Price)
18.203333333333333
COUNT()
Syntax Example
COUNT()
SELECT COUNT(Id) FROM Product;
COUNT(Id)
28 MySQL CheatSheet
SOME OTHER FUNCTIONS
and value2.
CONVERSION FUNCTION
1 John I 7
2 Steve II 8
3 Jahanvi I 6
29 MySQL CheatSheet
1. ORDER BY Clause
Syntax
Example
Id Name Age
3 Jahanvi 6
1 John 7
2 Steve 8
2. GROUP BY Clause
Group rows on the basis of the value of the one or more specified columns.
Syntax
30 MySQL CheatSheet
Example
Count(Id) Class
2 I
1 II
3. HAVING Clause
Syntax
Example
Count(Id) Class
2 I
31 MySQL CheatSheet
4. DISTINCT Clause
Syntax
Example
Class
II
5. LIKE Clause
Used in WHERE clause for pattern matching( % -> zero or more characters, _ ->
single character)
Syntax
32 MySQL CheatSheet
Example
Id Name
1 John
3 Jahanvi
Example
Id Name
1 John
10 Combining tables
UNION JOIN
Used to combine data of two or more result sets of Join can combine the data of multiple tables.
SELECT Statement.
33 MySQL CheatSheet
Types of Join
SELF JOIN RIGHT JOIN FULL JOIN
Employee Manager
1. SELF JOIN
Example
3 Herry 1 John
1 John 2 Hannah
34 MySQL CheatSheet
2. RIGHT JOIN
Returns all the records from the right table and matching record from left table.
Example
3 Herry 1 Steve
1 John 2 Jim
3. LEFT JOIN
Returns all the records from the lefttable and matching record from right table.
Example
1 John 2 Jin
3 Herry 1 Steve
35 MySQL CheatSheet
4. FULL JOIN
Example
1 John 2 Jim
3 Herry 1 Steve
5. CROSS JOIN
Combines each row from first table with each row each row of second table.
Example
36 MySQL CheatSheet
empId empName mgrId mrgName
3 Herry 1 Steve
2 Hannah 1 Steve
1 John 1 Steve
3 Herry 2 Jim
2 Hannah 2 Jim
1 John 2 Jim
3 Herry 3 Alia
2 Hannah 3 Alia
1 John 3 Alia
6. INNER JOIN
Example
3 Herry 1 Steve
1 John 2 Jim
37 MySQL CheatSheet
11 Transactions
Statements.
A executes or none.
Rollback Transaction
failed Durability: Transaction is permanently saved
D after commit, changes not lost even in system
failure.
Commit Rollback
Save all the transaction operations to the database. Used to undo the transactions that are not saved in
the database
38 MySQL CheatSheet