[MySQL] Add, delete, check and modify database and table structures


Students who need cloud servers and other cloud products to learn Linux can move to / --> Tencent Cloud <-- / --> Alibaba Cloud <-- / --> Huawei Cloud <-- / official website, lightweight cloud servers are low-cost to 112 yuan/year, and new users can enjoy ultra-low discounts on their first order.


 Table of contents

1. Library operation

1. Create a database

2. The encoding used by the database

2.1 Query the encoding set and verification set

2.2 View the character set and check set of the database

2.3 Create a database to specify the character set and check set

2.4 The data filtered by different verification sets have different results.

3. View the database

4. Modify the database

5. Delete the database

6. Database backup and recovery

6.1 Back up the entire database

6.2 Restore the entire database

6.3 Back up only one table or multiple databases

7. Check the database connection status

2. Table operations

1. Creation of table

2. View the table

2.1 View table

2.2 View detailed information when creating a table

3. Modification of table

3.1 Insert data into the table

3.2 Add new fields to the table

3.3 Modify table name

3.4 Modify the column name of a column in the table

3.5 Modify the data type of a column in the table

4. Deletion of table

4.1 Delete a column in the table

4.2 Delete table


1. Library operation

1. Create a database

#创建数据库————本质是在/var/lib/mysql中创建一个目录
mysql> create database database1;
Query OK, 1 row affected (0.01 sec)
#进入数据库
mysql> use test1;
Database changed
#创建表
mysql> create table if not exists person(name varchar(20));
Query OK, 0 rows affected (0.34 sec)

2. The encoding used by the database

When creating a database, there are two encoding sets:

1. Database encoding set: The encoding set for database storage data.

2. Database check set: supports the encoding used by the database for field comparison. It is essentially an encoding format used to read data in the database.

2.1 Query the encoding set and verification set

#查询当前MySQL的编码集
mysql> show variables like 'character_set_database';
+------------------------+-------+
| Variable_name          | Value |
+------------------------+-------+
| character_set_database | utf8  |
+------------------------+-------+
1 row in set (0.00 sec)
#查询当前MySQL的校验集
mysql> show variables like 'collation_database';
+--------------------+-----------------+
| Variable_name      | Value           |
+--------------------+-----------------+
| collation_database | utf8_general_ci |
+--------------------+-----------------+
1 row in set (0.01 sec)

2.2 View the character set and check set of the database

#查看数据库支持的所有字符集
show charset;
#查看数据库支持的所有校验集
show collation;
#在数据库目录下查看d1数据库的字符集和校验集
[root@VM-4-11-centos mysql]# cat d2/db.opt
default-character-set=utf8
default-collation=utf8_general_ci

2.3 Create a database to specify the character set and check set

#创建数据库时指定字符集
mysql> create database d2 charset=utf8;
Query OK, 1 row affected (0.00 sec)

mysql> create database d3 character set utf8;
Query OK, 1 row affected (0.00 sec)

#创建数据库时指定字符集和校验集
mysql> create database d4 charset=utf8 collate utf8_general_ci;
Query OK, 1 row affected (0.00 sec)

When we create a database without specifying a character set and verification rules, the system uses the default character set: utf8, and the verification rules are: utf8_ general_ ci

2.4 The data filtered by different verification sets have different results.

#创建一个数据库,校验规则使用utf8_ general_ ci[不区分大小写]
create database test1 collate utf8_general_ci;
use test1;
create table person(name varchar(10));
insert into person values('a');
insert into person values('A');
insert into person values('b');
insert into person values('B');
#进行查询
mysql> use test1;
mysql> select * from person where name='a';
+------+
| name |
+------+
| a    |
| A    |
+------+
2 rows in set (0.01 sec)
#创建一个数据库,校验规则使用utf8_ bin[区分大小写]
create database test2 collate utf8_bin;
use test2
create table person(name varchar(20));
insert into person values('a');
insert into person values('A');
insert into person values('b');
insert into person values('B');
#进行查询
mysql> use test2;
mysql> select * from person where name='a';
+------+
| name |
+------+
| a    |
+------+
2 rows in set (0.01 sec)

Select different verification sets, and the results filtered out by the database are different.

3. View the database

#查看数据库
mysql> show databases;
#进入数据库
mysql> use test1;
Database changed
#查看当前数据库中的所有表
mysql> show tables;
+-----------------+
| Tables_in_test1 |
+-----------------+
| person          |
+-----------------+
1 row in set (0.00 sec)
#查看自己当前处于哪个表
mysql> select database();
+------------+
| database() |
+------------+
| test1      |
+------------+
1 row in set (0.01 sec)
#查看数据库当初的创建指令
mysql> show create database test2;
+----------+---------------------------------------------------------------------------------+
| Database | Create Database                                                                 |
+----------+---------------------------------------------------------------------------------+
| test2    | CREATE DATABASE `test2` /*!40100 DEFAULT CHARACTER SET utf8 COLLATE utf8_bin */ |
+----------+---------------------------------------------------------------------------------+
1 row in set (0.00 sec)

1. MySQL recommends that our keywords be capitalized, but it is not required.

2. The backtick `` in the database name is to prevent the database name being used from being a keyword.

3. /*!40100 default.... */ This is not a comment. It means that the current mysql version is greater than version 4.01, so execute this sentence.

4. Modify the database

#修改数据库的字符集和校验集
mysql> alter database test1 charset=gbk collate gbk_chinese_ci;
Query OK, 1 row affected (0.01 sec)

5. Delete the database

#删除数据库————本质是在/var/lib/mysql中删除一个目录
#不建议直接删除数据库,建议备份后删除
mysql> drop database database1;
Query OK, 0 rows affected (0.01 sec)

6. Database backup and recovery

6.1 Back up the entire database

#未启动MySQL客户端使用,备份的是对该数据库的有效操作
mysqldump -P3306 -uroot -p -B test1 > test.sql

This is a MySQL command used to export data from a database to a SQL file. Specifically, this command will export all data in the database named test1 to a file named test.sql.

The following is the meaning of each parameter in this command:

  • -P3306: Specify the port number of the MySQL server as 3306.
  • -uroot: Use the root username to connect to the MySQL server.
  • -p: Prompts the user for a password to connect to the MySQL server.
  • -B test1: Specify the name of the database to be exported as test1.
  • > test.sql: Output the exported data to a file named test.sql.

Please note that before executing this command, you need to ensure that you have sufficient permissions to export the data in the database. In addition, if you want to export multiple databases, you can specify multiple database names after the -B parameter, separated by spaces.

6.2 Restore the entire database

mysql> source /root/MySQL/test.sql
Query OK, 0 rows affected (0.00 sec)
Query OK, 0 rows affected (0.00 sec)

The recovery process is to re-execute all the backup commands.

6.3 Back up only one table or multiple databases

#仅备份几张张表
# mysqldump -u root -p 数据库名 表名1 表名2 > D:/mytest.sql
#同时备份多个数据库
# mysqldump -u root -p -B 数据库名1 数据库名2 ... > 数据库存放路径

Note: The backup is performed from the shell command line, not the MySQL client. If you back up a database without the -B parameter, when restoring the database, you need to create an empty database first, then use the database, and then use source to restore.

7. Check the database connection status

mysql> show processlist;
+----+------+-----------+-------+---------+------+----------+------------------+
| Id | User | Host      | db    | Command | Time | State    | Info             |
+----+------+-----------+-------+---------+------+----------+------------------+
| 40 | root | localhost | test1 | Query   |    0 | starting | show processlist |
+----+------+-----------+-------+---------+------+----------+------------------+
1 row in set (0.01 sec)

2. Table operations

1. Creation of table

mysql> create table if not exists use1(
    -> id int,
    -> name varchar(20) comment '用户名',
    -> birthday date comment '用户生日'
    -> )character set utf8 collate utf8_general_ci engine MyIsam;
Query OK, 0 rows affected (0.02 sec)

mysql> create table if not exists use2(
    -> name varchar(20) comment '用户名',
    -> passwword char(32) comment '用户密码',
    -> birthday date comment '用户生日'
    -> )charset=utf8 collate=utf8_general_ci engine=InnoDB;
Query OK, 0 rows affected (0.15 sec)

Different storage engines use different files to create tables.

The users table storage engine is MyISAM, and there are three different files in the data directory, namely:

users.frm: table structure

users.MYD: table data

users.MYI: table index

2. View the table

2.1 View table

#查看当前处于哪个数据库
mysql> select database();
+------------+
| database() |
+------------+
| use_db     |
+------------+
1 row in set (0.01 sec)
#查看数据库中存在的表
mysql> show tables;
+------------------+
| Tables_in_use_db |
+------------------+
| use1             |
| use2             |
+------------------+
2 rows in set (0.00 sec)
#查看表的属性
mysql> desc use1;
+----------+-------------+------+-----+---------+-------+
| Field    | Type        | Null | Key | Default | Extra |
+----------+-------------+------+-----+---------+-------+
| id       | int(11)     | YES  |     | NULL    |       |
| name     | varchar(20) | YES  |     | NULL    |       |
| birthday | date        | YES  |     | NULL    |       |
+----------+-------------+------+-----+---------+-------+
3 rows in set (0.00 sec)

2.2 View detailed information when creating a table

#查看创建表时的详细信息
mysql> show create table use1;
+-------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table | Create Table                                                                                                                                                                                         |
+-------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| use1  | CREATE TABLE `use1` (
  `id` int(11) DEFAULT NULL,
  `name` varchar(20) DEFAULT NULL COMMENT '用户名',
  `birthday` date DEFAULT NULL COMMENT '用户生日'
) ENGINE=MyISAM DEFAULT CHARSET=utf8        |
+-------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)

#加上\G选项精简信息
mysql> show create table use1 \G
*************************** 1. row ***************************
       Table: use1
Create Table: CREATE TABLE `use1` (
  `id` int(11) DEFAULT NULL,
  `name` varchar(20) DEFAULT NULL COMMENT '用户名',
  `birthday` date DEFAULT NULL COMMENT '用户生日'
) ENGINE=MyISAM DEFAULT CHARSET=utf8
1 row in set (0.00 sec)

3. Modification of table

3.1 Insert data into the table

#在表中插入数据
mysql> insert into user values (1,'张三','2000-10-1');
Query OK, 1 row affected (0.00 sec)
mysql> insert into user values (2,'李四','1978-10-1');
Query OK, 1 row affected (0.01 sec)

#查看表内容
mysql> select* from user;
+------+--------+------------+
| id   | name   | birthday   |
+------+--------+------------+
|    1 | 张三   | 2000-10-01 |
|    2 | 李四   | 1978-10-01 |
+------+--------+------------+
2 rows in set (0.03 sec)
mysql> insert into t3 (id,online) values (123,1);
Query OK, 1 row affected (0.03 sec)

3.2 Add new fields to the table

#在表中插入字段,新增字段位于birthday之后
mysql> alter table user add image_path varchar(128) comment '用户头像路径' after birthday;
Query OK, 2 rows affected (0.03 sec)
Records: 2  Duplicates: 0  Warnings: 0
#打印表
mysql> select* from user;
+------+--------+------------+------------+
| id   | name   | birthday   | image_path |
+------+--------+------------+------------+
|    1 | 张三   | 2000-10-01 | NULL       |
|    2 | 李四   | 1978-10-01 | NULL       |
+------+--------+------------+------------+
2 rows in set (0.00 sec)

3.3 Modify table name

#修改表名为user
mysql> alter table use1 rename to user;
Query OK, 0 rows affected (0.01 sec)

3.4 Modify the column name of a column in the table

#查一下创建表的SQL
mysql> show create table user\G;
*************************** 1. row ***************************
       Table: user
Create Table: CREATE TABLE `user` (
  `id` int(11) DEFAULT NULL,
  `name` varchar(60) DEFAULT NULL,
  `birthday` date DEFAULT NULL COMMENT '用户生日'
) ENGINE=MyISAM DEFAULT CHARSET=utf8
1 row in set (0.00 sec)

#修改name列名为xingming。注意新字段需要完整定义
mysql> alter table user change name xingming varchar(60) DEFAULT NULL;
Query OK, 0 rows affected (0.04 sec)
Records: 0  Duplicates: 0  Warnings: 0

3.5 Modify the data type of a column in the table

#将name字段的属性由varchar(20)修改为varchar(60)
#但是这样修改将会丢失COMMENT '用户姓名'字段
mysql> alter table user modify name varchar(60);
Query OK, 2 rows affected (0.00 sec)
Records: 2  Duplicates: 0  Warnings: 0

#9列为原先创建表时的语句,10列为修改后的语句。
#COMMENT '用户姓名'将丢失,所以修改表中某一列的属性时,将创建时的SQL复制过来再进行修改
`name` varchar(20) DEFAULT NULL COMMENT '用户姓名',
`name` varchar(60) DEFAULT NULL,

Modifying the data type of a column in the table is an overwriting modification. In order to prevent the fields set when creating the table from being lost, first copy the creation information of the original table and then modify it.

4. Deletion of table

4.1 Delete a column in the table

#删除user
mysql> alter table user drop image_path;
Query OK, 2 rows affected (0.05 sec)
Records: 2  Duplicates: 0  Warnings: 0

Be careful when deleting. The data in the corresponding column will be deleted after deletion.

4.2 Delete table

mysql> drop table user;
Query OK, 0 rows affected (0.00 sec)

Guess you like

Origin blog.csdn.net/gfdxx/article/details/131160432