Web Development Blog

MySQL Cheat Sheet

April 21st, 2011

ALTER TABLE

Updates an existing table.

ALTER TABLE table_name;
ADD COLUMN column-name data-type modifiers,
ADD PRIMARY KEY ( column name ),
DROP COLUMN column-name,
CHANGE old-column-name new-column-name data-type modifiers;

CREATE DATABASE

Creates a new database. Can be qualified with IF NOT EXISTS to ensure that the database is only created if there is not already a database with the same name.

CREATE DATABASE IF NOT EXISTS database-name;

CREATE TABLE

Creates a new database table. Can be qualified with IF NOT EXISTS to ensure that the table is only created if there is not already a table with the same name in the database.

CREATE TABLE IF NOT EXISTS table-name
( column-name data-type modifiers,
column-name data-type modifiers );

DELETE FROM

Deletes one or more rows from a table. All rows will be deleted if a WHERE clause is not specified. Requires a WHERE clause to identify the row/s to delete.

DELETE FROM table-name WHERE column-name = value;
DELETE FROM table-name;

DROP DATABASE

Deletes a database. Can be qualified with IF EXISTS to ensure a database with the same name already exists.

DROP DATABASE IF EXISTS database-name;

DROP TABLE

Deletes a table. Can be qualified with IF EXISTS to ensure a table with the same name already exists in the database.

DROP TABLE IF EXISTS table-name;

EXPLAIN

Displays column names, data types and optional modifers for the specified table.

EXPLAIN table-name;