Professional Documents
Culture Documents
Mysql Cheat Sheet A4
Mysql Cheat Sheet A4
CREATING AND DISPLAYING Use the ALTER TABLE statement to modify the table structure. To count the rows in the table: This deletes all rows satisfying the WHERE condition.
SELECT COUNT(*) To delete all data from a table, use the TRUNCATE TABLE
DATABASES To change a table name:
ALTER TABLE animal RENAME pet;
FROM animal; statement:
To create a database: TRUNCATE TABLE animal;
To count the non-NULL values in a column:
CREATE DATABASE zoo;
To add a column to the table: SELECT COUNT(name)
ALTER TABLE animal FROM animal;
To list all the databases on the server:
SHOW DATABASES;
ADD COLUMN name VARCHAR(64); CASTING
To count unique values in a column: From time to time, you need to change the type of a value.
To change a column name: SELECT COUNT(DISTINCT name) Use the CAST() function to do this.
To use a specified database: ALTER TABLE animal FROM animal; In MySQL, you can cast to these data types:
USE zoo; RENAME COLUMN id TO identifier; CHAR NCHAR BINARY DATE DATETIME
GROUP BY DECIMAL DOUBLE FLOAT REAL SIGNED
To delete a specified database: To change a column data type: To count the animals by species: UNSIGNED TIME YEAR JSON spatial_type
DROP DATABASE zoo; ALTER TABLE animal SELECT species, COUNT(id)
MODIFY COLUMN name VARCHAR(128); FROM animal
To list all tables in the database: GROUP BY species; To get a number as a signed integer:
SHOW TABLES; To delete a column: SELECT CAST(1234.567 AS signed);
ALTER TABLE animal To get the average, minimum, and maximum ages by habitat: -- result: 1235
To get information about a specified table: DROP COLUMN name; SELECT habitat_id, AVG(age),
DESCRIBE animal; MIN(age), MAX(age)
It outputs column names, data types, default values, and more To delete a table: FROM animal To change a column type to double:
about the table. DROP TABLE animal; GROUP BY habitat_id; SELECT CAST(column AS double);