Mysql Common Commands
Contents
1. MySQL statement specifications:
Keywords and function names should be in all capital letters;
Database names, table names, and field names must all be lowercase;
SQL statements must end with a semicolon.
2. Common MySQL commands:
- Display the current database version
SELECT VERSION();
- Display the current time:
SELECT NOW();
- Display the current user:
SELECT USER();
- Create a database:
The syntax is:
CREATE {DATABASE | SCHEMA} [IF NOT EXISTS] db_name [DEFAULT] CHARACTER SET [=] charset_name;
Enter the following statement to create database test2:
CREATE DATABASE test2;
If database test2 already exists, an error will be reported:
ERROR 1007 (HY000): Can’t create database ’test2’; database exists
If you add IF NOT EXISTS when creating the database, no error will be reported when encountering an existing database, but there will be a WARNING.
CREATE DATABASE IF NOT EXISTS test2; Query OK, 1 row affected, 1 warning (0.00 sec)
If you want to see WARNING information, just enter SHOW WARNING;.
- View the database:
The syntax is:
SHOW {DATABASES | SCHEMAS} [LIKE ‘pattern’ | WHERE expr];
Enter the following statement to view
SHOW DATABASES;
- Delete the database:
grammar:
DROP {DATABASE | SCHEMA} [IF EXISTS] db_name
If database test2 is entered, enter the following statement to delete the database:
DROP DATABASE test2;
If the database test2 that needs to be deleted does not exist, an error will be reported.
ERROR 1008 (HY000): Can’t drop database ’test2’; database doesn’t exist
If IF EXISTS is added when deleting, no error will be reported, but a WARNING:
DROP DATABASE IF EXISTS test2; Query OK, 1 row affected, 1 warning (0.00 sec)