My SQL
My SQL
This is a cheat sheet of MySQL for easier references. MySQL is an open-source relational database management system. It is a central component of the LAMP open-source web application software stack. MySQL is used by many database-driven web applications, including Drupal, Joomla, phpBB, and WordPress.
Installation
To get started with MySQL, you can download it from here or, simply use xampp, which uses maria-db
, a drop in replacement of MySQL. To run MySQL, in linux environment or, docker/ podman container, I've a guide for you!
User
Login
Show Users
Create User
Grant All Privileges On All Databases
Show Grants
Remove Grants
Delete User
Exit
Database
General Commands
To run SQL files
Data Types
Integers
Float
Double
Decimal
Date
Date Time
Time
String
Comments
Data Definition Language (DDL)
Create Database
Show Databases
Use Database
Create Table
Show Tables
Describe Table
Rename Table
Renaming Column
Add Constraint to Column
Add Column
Drop Column
Modify the Datatype of column
Truncate Table
Trancute means to delete all the rows from the table but the table structure remains the same.
Drop Table
Drop Database
Data Manipulation Language (DML)
Insertion (Complete)
Insertion (Partial)
Updating all rows
Updating a specified row
Delete a specified row
Delete all rows
Data Query Language (DQL)
Display Table
Select only specified columns
Select only few rows
Select with condition
Where Clause
Greater than(>)
Greater than equal to(>=)
Less than(<)
Less than equal to(<=)
Range
BETWEEN and AND
OR
Null
Not null
ORDER BY Clause
Like Operator
Views
Create a view
Displaying view
Updating in view
Deleting record from view
Droping a view
Joins

Here are the different types of the JOINs in SQL:
(INNER) JOIN
: Returns records that have matching values in both tablesLEFT (OUTER) JOIN
: Returns all records from the left table, and the matched records from the right tableRIGHT (OUTER) JOIN
: Returns all records from the right table, and the matched records from the left tableFULL (OUTER) JOIN
: Returns all records when there is a match in either left or right table
Inner join
Full outer join
Left outer join
Right outer join
Left outer join - inner join
Right outer join - inner join
Aggregation
Sum function
Average function
Count function
Maximum function
Minimum function
Standard deviation function
Group concat function
Only COUNT function considers NULL values
Procedure
Creating procedure
Calling procedure
Drop procedure
Transaction
Begin transaction
Create savepoint
Rollback
Releasing savepoint
Commiting changes
Constraints
Not Null
Unique
Primary Key
Check
Default
Cloning
Duplicate a Table Schema
Duplicate a Table
Access Controls
Creating New User
the hostname part is set to localhost
, so the user will be able to connect to the MySQL server only from the localhost.
To grant access from another host, change the hostname part with the remote machine IP.
To create a user that can connect from any host, '%' is used in the hostname part:
Grant All Permissions
Asterisks(*) refers to the database and table names respectively. By using asterisks we can give access of all the databases or tables to the user.
Flush Privileges
All the changes won't be in effect unless this query is fired.
Specific User Permissions
type_of_permission
may have one of these value:
ALL PRIVILEGES - Allows user full access to a designated database (or if no database is selected, global access across the system).
CREATE - allows them to create new tables or databases.
DROP - allows them to them to delete tables or databases.
DELETE - allows them to delete rows from tables.
INSERT - allows them to insert rows into tables.
SELECT - allows them to use the
SELECT
command to read through databases.UPDATE - allow them to update table rows.
GRANT OPTION - allows them to grant or remove other usersâ privileges. Multiple permissions are given with commas.
Revoking permissions
Show User's Current Permissions
Delete a User
Set new password to a user
Reset Root Password
Stop MySQL service
Restart MySQL service without loading grant tables
The apersand (&) will cause the program to run in the background and --skip-grant-tables
enables everyone to to connect to the database server without a password and with all privileges granted. Login to shell
Set new password for root
Stop and start the server once again
Programming
Declare variables
Print them
For loop
Miscellaneous
Enabling foreign key checks
Disabling foreign key checks
Round
Repeated concatenation
Random float
Typecast to Int
Concatenation
Extract Month
Extract Year
Also thanks to
Last updated
Was this helpful?