Skip to content

Instantly share code, notes, and snippets.

@sotlucas
Last active July 22, 2019 01:20
Show Gist options
  • Save sotlucas/fcef197167dbcea0c82bab9d61566b07 to your computer and use it in GitHub Desktop.
Save sotlucas/fcef197167dbcea0c82bab9d61566b07 to your computer and use it in GitHub Desktop.
MySQL Cheatsheet

Create table

CREATE TABLE table_name(
  column_name TYPE CONSTRAINT1 CONSTRAINT2,
  another_column TYPE CONSTRAINT1
);

Delete table

DROP TABLE table_name;

Show all tables

SHOW TABLES;

Types

INT

VARCHAR(length)

Constraints

PRIMARY KEY

NOT NULL

UNIQUE

DEFAULT 'default'

AUTO_INCREMENT

SELECT

SELECT column_name,another_column FROM table_name; : select specific columns

SELECT * FROM table_name; : select all columns

SELECT column_name,another_column FROM table_name WHERE column_name='something'; : select specific columns matching the condition

SELECT * FROM table_name WHERE column_name='something'; : select all columns matching the condition

UPDATE

UPDATE table_name SET column_name='new_data'; : update all rows from column

UPDATE table_name SET column_name='new_data' WHERE column_name='something'; : update specific column matching the condition

UPDATE table_name SET column_name='new_data', another_column='new_data' WHERE column_name='something'; : update specific columns matching the condition

DELETE

DELETE FROM table_name; : deletes all rows from the table

DELETE FROM table_name WHERE column_name='something'; : deletes all rows from the table matching the condition

Other commands

ORDER BY column_name : order the result by the given columnin ascending order

ORDER BY column_name DESC : order the result by the given columnin descending order

LIMIT number : limit the number of rows gotten from the result

IN (value1, value2, value3) : if like an OR for every value

DISTINCT : don't get duplicates from the result

Comparison operators

= : equals

<> : not equals

> : greater than

< : less than

>= : greater than or equal

<= : less than or equal

Logic operators

OR

AND

Comments

-- This is an inline comment

/*
This is a multiline comment
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment