0% found this document useful (0 votes)
75 views

MySQL Commands Cheat Sheet

The document provides a cheat sheet of MySQL commands for working with databases, tables, rows, and other operations. It includes commands for creating and dropping databases and tables, describing and showing table details, inserting, deleting, and updating rows, and more. Alter commands allow modifying tables after creation by adding, dropping, or changing columns and constraints. Additional documentation links are provided for more detailed explanations of alter table operations.

Uploaded by

Siddique Rana
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
75 views

MySQL Commands Cheat Sheet

The document provides a cheat sheet of MySQL commands for working with databases, tables, rows, and other operations. It includes commands for creating and dropping databases and tables, describing and showing table details, inserting, deleting, and updating rows, and more. Alter commands allow modifying tables after creation by adding, dropping, or changing columns and constraints. Additional documentation links are provided for more detailed explanations of alter table operations.

Uploaded by

Siddique Rana
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Introduction to Database Systems

MySQL Commands Cheat Sheet


-- Database-Level
DROP DATABASE databaseName -- Delete the database (irrecoverable!)
DROP DATABASE IF EXISTS databaseName -- Delete if it exists
CREATE DATABASE databaseName -- Create a new database
CREATE DATABASE IF NOT EXISTS databaseName -- Create only if it does not exists
SHOW DATABASES --Show all the databases in this server
USE databaseName -- Set the default (current) database
SELECT DATABASE() -- Show the default database
SHOW CREATE DATABASE databaseName -- Show the CREATE DATABASE
statement

-- Table-Level
DROP TABLE [IF EXISTS] tableName, ...
CREATE TABLE [IF NOT EXISTS] tableName
( columnName columnType columnAttribute, ...
PRIMARY KEY(columnName),
FOREIGN KEY (columnNmae) REFERENCES tableName (columnNmae)
)
SHOW TABLES -- Show all the tables in the default database
DESCRIBE|DESC tableName -- Describe the details for a table
ALTER TABLE tableName ... -- Modify a table, e.g., ADD COLUMN
and DROP COLUMN
ALTER TABLE tableName ADD columnDefinition
ALTER TABLE tableName DROP columnName
ALTER TABLE tableName ADD FOREIGN KEY (columnNmae) REFERENCES
tableName (columnNmae)
ALTER TABLE tableName DROP FOREIGN KEY constraintName
SHOW CREATE TABLE tableName -- Show the CREATE TABLE statement for this
tableName

-- Row-Level
INSERT INTO tableName VALUES (column1Value, column2Value,...)
-- Insert on all Columns
INSERT INTO tableName VALUES (column1Value, column2Value,...), ...
-- Insert multiple rows
INSERT INTO tableName (column1Name, ..., columnNName) VALUES
(column1Value, ..., columnNValue) -- Insert on
selected Columns
DELETE FROM tableName WHERE criteria
UPDATE tableName SET columnName = expr, ... WHERE criteria
SELECT * | column1Name AS alias1, ..., columnNName AS aliasN FROM tableName
WHERE criteria GROUP BY columnName ORDER BY columnName ASC|
DESC, ... HAVING groupConstraints LIMIT count | offset count

-- Others
SHOW WARNINGS; -- Show the warnings of the previous statement

1
Introduction to Database Systems

Alter Commands:
The basic syntax of ALTER TABLE to add a new column in an existing table is as follows:
ALTER TABLE table_name ADD column_name datatype;

The basic syntax of ALTER TABLE to DROP COLUMN in an existing table is as follows:
ALTER TABLE table_name DROP COLUMN column_name;

The basic syntax of ALT1ER TABLE to change the DATA TYPE of a column in a table is as
follows:
ALTER TABLE table_name MODIFY COLUMN column_name datatype;

The basic syntax of ALTER TABLE to add a NOT NULL constraint to a column in a table is as
follows:
ALTER TABLE table_name MODIFY column_name datatype NOT NULL;

The basic syntax of ALTER TABLE to ADD UNIQUE CONSTRAINT to a table is as follows:
ALTER TABLE table_name ADD CONSTRAINT MyUniqueConstraint UNIQUE(column1,
column2...);

The basic syntax of ALTER TABLE to ADD CHECK CONSTRAINT to a table is as follows:
ALTER TABLE table_name ADD CONSTRAINT MyUniqueConstraint CHECK (CONDITION);

The basic syntax of ALTER TABLE to ADD PRIMARY KEY constraint to a table is as follows:
ALTER TABLE table_name ADD CONSTRAINT MyPrimaryKey PRIMARY KEY (column1,
column2...);

The basic syntax of ALTER TABLE to DROP CONSTRAINT from a table is as follows:
ALTER TABLE table_name DROP CONSTRAINT MyUniqueConstraint;

The basic syntax of ALTER TABLE to DROP PRIMARY KEY constraint from a table is as
follows:
ALTER TABLE table_name DROP PRIMARY KEY;
Another useful link is:
http://www.techonthenet.com/mysql/tables/alter_table.php

More commands can be found on GOOGLE  … Keep exploring…!!

You might also like