[MySQL] database and table operations
Table of Contents
1. Database operations
Enter the database
mysql -u root -p
1.1 Display the current database
SHOW DATABASES;
1.2 Create a database
Grammar rules
CREATE DATABASE [IF NOT EXISTS] db_name [create_specification [,create_specification] ...]
描述信息
create_specification:
[DEFAULT] CHARACTER SET charset_name
[DEFAULT] COLLATE collation_name
Description: The
uppercase keyword
[] is optional.
CHARACTER SET: Specify the character set used by the database.
COLLATE: Specify the character set of the database.
Example:
Create a database named goods
CREATE DATABASE goods;
Note: When we create a database without specifying the character set and verification rules, the system uses the default character set: utf8, the verification rule is: utf8_ general_ ci
If the system does not have a database of goods, then create a database called goods, if If not
CREATE DATABASE IF NOT EXISTS goods;
If the system does not have a database of goods, create a goods database that uses the utf8mb4 character set, if not, do not create it (know it, generally generated by default)
CREATE DATABASE IF NOT EXISTS db_test CHARACTER SET utf8mb4;
1.3 Delete the database
grammar
DROP DATABASE [IF EXISTS] db_name;
Note: After the database is deleted, the corresponding database cannot be seen inside, and all the tables and data inside are deleted
1.4 Using the database
grammar:
use 数据库名;
2. Operation of the table
When you need to operate a table in the database, you need to use the
common data types of the database :
INT: integer
DECIMAL (M, D): floating point type
VARCHAR (SIZE): string type
TIMESTAMP: date type
text: long text data
2.1 Create a data table
grammar
CREATE TABLE table_name (
field1 datatype,
field2 datatype,
field3 datatype
);
You can use the comment
additional field description
Example:
Add a student information table in the students library
create table stu(
id int,
name varchar(20) comment '姓名',
sex verchar(8),
birthday timestamp
);
2.2 View form information
Statement:
desc + ‘表名’;
2.3 Delete Table
grammar
DROP TABLE[IF EXISTS] tbl_name [, tbl_name] ...
Example:
– Delete the stu_test table
drop table stu_test;
– If the stu_test table exists, delete the stu_test table
drop table if exists stu_test