[MySQL] Basic Practical Chapter (1)—Database and Data Table Operations

Create management database

Create database

grammar:

CREATE DATABASE 数据库名称;

Note:It must be unique on the same database server. Databases with the same name are not allowed to exist.
In addition, if you do not customize the encoding method when creating the database, the system default encoding method will be used, so we can customize the encoding method while creating the database.
grammar:

CREATE DATABASE 数据库名称
DEFAULT CHARACTER SET 字符集名
DEFAULT COLLATE 校对规则名;

character setDefines the way MySQL stores strings
==Collation rules==Defines the way to compare strings

View and select databases

View basic information about all databases existing on the server
grammar:

SHOW DATABASES;

After the database is created, to manage the data tables of the database, you must first select the database. The syntax for selecting a database is as follows:
grammar:

USE 数据库名称;

After execution, when Database changed appears, it indicates that the database has been assigned to the specified location.

Modify database

After the database is created, the encoding method is determined. SQL statements can be used to modify the encoding of the database.
grammar:
method one:

ALTER DATABASE 数据库名称
DEFAULT CHARACTER SET 字符集名
DEFAULT COLLATE 校对规则;

Method two:

ALTER DATABASE 数据库名称 
CHARACTER SET‘字符集’;

Delete database

grammar:
method one:

DROP DATABASE 数据库名称;

Method two:

DROP DATABASE IF EXISTS 数据库名;

If the database exists, the deletion will be successful.
If the database does not exist, it will end silently and no error will be reported.

Create management data table

MySQL data is stored in the database in the structure of a relational table. A data table is an entity that stores data in a relational database. After the database is created, tables must be created in the database to store data.

Create data table

grammar:

CREATE TABLE 数据库名称
(
字段名1 数据类型 [约束条件1],
字段名2 数据类型 [约束条件2],
.......
字段名n 数据类型 [约束条件n]
)

Data types refer to all allowed data types in the database system.
MySQL data types can be divided into three major categories: numerical types, character types and binary types, date and time types.

Numeric type

type size
TINYINT 1Bytes
SMALLINT 2Bytes
MEDIUMINT 3Bytes
INT/INTEGER 4Bytes
BIGINT 8 Bytes
FLOAT 4Bytes
DOUBLE 8Bytes
DECIMAL For DECIMAL (M, D) if M>D, it is M+2, otherwise it is D+2

Integer types and floating point types can be collectively referred to as numeric data types.
DECIMAL(M,D) where M represents the number of digits in the entire value, and D represents the number of digits after the decimal point.
Example: The value that DECIMAL(4,3) can save is 3.123

String and binary typesIncluding CHAR, VARCHAR, TINYBLOB, TINYTEXT, BLOB, TEXT, etc.
The VARCHAR(N) type is the most commonly used, and N must be set when used. N represents the number of characters, not the number of bytes.

Date and time typesIncluding DATE, TIME, YEAR, DATETIME, TIMESTAMP, etc.

type size Format
DATE 3 YYYY-MM-DD
TIME 3 HH:MM:SS
YEAR 1 YYYY
DATETIME 8 YYYY-MM-DD HH:MM:SS
TIMESTAMP 4 YYYY-MM-DD HH:MM:SS

DATETIME can only reflect the local time zone.
TIMESTAMP is related to time zone, sorting is fast and convenient
Notice:
When there is an illegal value or a value that mysql cannot represent, time types other than TIMESTAMP will be filled with 0 value by default. The TIMESTAMP will be filled in with the system default current time and date.

View data

View the data table structure
Method 1:
grammar

SHOW CREATE TABLE 数据表名称;

Method Two:
grammar
DESCRIBE table name;
abbreviation

desc 表名;

Modify data table

grammar:
ALTER TABLE table name [Modify·Option]

Modify table name

ALTER TABLE 旧表名 RENAMETO】新表名;

Modify table character set

ALTER TABLE [DEFAULT]
CHARACTER SET 字符集名[DEFAULT] COLLATE 校队规则名;

Modify table fields

ALTER TABLE 表名 CHANGE 旧字段名 新字段名 新数据类型;

Delete field

ALTER TABLE 表名 DROP 字段名;

Add new field

ALTER TABLE 表名 ADD 新字段名 新数据类型[约束条件][FIRET 或 AFTER 字段名];

Modify field data type

ALTER TABLE 表名 MODIFY 字段名 数据类型[约束条件];

Set auto increment

ALTER TABLE 表名 MODIFY 字段名 INT AUTO_INCREMENT;

Remove autoincrement

ALTER TABLE 表名 MODIFY 字段名 INT;

Delete data table

Deleting a data table means deleting a table that already exists in the database, and the data in the data table will also be deleted.
grammar

DROP TABLE 表名;

The article ends here. If you have any questions, please add the WeChat message below and make progress together! ! !

Guess you like

Origin blog.csdn.net/2202_75623950/article/details/132418031