MySQL数据库系统学习(从入门到精通)(中)

本文涉及的产品
RDS MySQL Serverless 基础系列,0.5-2RCU 50GB
云数据库 RDS MySQL,集群系列 2核4GB
推荐场景:
搭建个人博客
云数据库 RDS MySQL,高可用系列 2核4GB
简介: MySQL数据库系统学习(从入门到精通)

3.管理表

1.创建表

mysql> create table if not exists txt(id int,name varchar(10) not null,age int not null,primary key(id))engine=innodb default charset=utf8;
Query OK, 0 rows affected (0.00 sec)

2.查看表结构和类型

mysql> desc txt;
+-------+-------------+------+-----+---------+-------+
| Field | Type        | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| id    | int(11)     | NO   | PRI | NULL    |       |
| name  | varchar(10) | NO   |     | NULL    |       |
| age   | int(11)     | NO   |     | NULL    |       |
+-------+-------------+------+-----+---------+-------+
3 rows in set (0.00 sec)
mysql> show create table txt;
+-------+------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table | Create Table                                                                                                                                                     |
+-------+------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| txt   | CREATE TABLE `txt` (
  `id` int(11) NOT NULL,
  `name` varchar(10) NOT NULL,
  `age` int(11) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 |
+-------+------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)

3.插入数据

mysql> insert into txt (id,name,age) values(1,'zhangsan',12);
Query OK, 1 row affected (0.00 sec)
mysql> insert into txt values(2,'lisi',15),(3,'wangwu',18);
Query OK, 2 rows affected (0.00 sec)
Records: 2  Duplicates: 0  Warnings: 0
mysql> select * from txt;
+----+----------+-----+
| id | name     | age |
+----+----------+-----+
|  1 | zhangsan |  12 |
|  2 | lisi     |  15 |
|  3 | wangwu   |  18 |
+----+----------+-----+
3 rows in set (0.00 sec)

4.对表的字符集进行修改

mysql> alter table txt convert to character set gb18030;
Query OK, 3 rows affected (0.00 sec)
Records: 3  Duplicates: 0  Warnings: 0
mysql> show create table txt;
+-------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table | Create Table                                                                                                                                                        |
+-------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| txt   | CREATE TABLE `txt` (
  `id` int(11) NOT NULL,
  `name` varchar(10) NOT NULL,
  `age` int(11) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=gb18030 |
+-------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)

5.对表重命名

mysql> alter table a2 rename a3;
Query OK, 0 rows affected (0.01 sec)
mysql> show tables;
+----------------+
| Tables_in_test |
+----------------+
| a3             |
+----------------+

6.删除表

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

4.对表内数据进行修改

1.查看表内字符集
mysql> show full columns from txt;
+-------+-------------+-----------------+------+-----+---------+-------+---------------------------------+---------+
| Field | Type        | Collation       | Null | Key | Default | Extra | Privileges                      | Comment |
+-------+-------------+-----------------+------+-----+---------+-------+---------------------------------+---------+
| id    | int(11)     | NULL            | NO   | PRI | NULL    |       | select,insert,update,references |         |
| name  | varchar(10) | utf8_general_ci | NO   |     | NULL    |       | select,insert,update,references |         |
| age   | int(11)     | NULL            | NO   |     | NULL    |       | select,insert,update,references |         |
+-------+-------------+-----------------+------+-----+---------+-------+---------------------------------+---------+
2.对表内字段进行修改字符集
格式:alter table 表名 modify 字段 属性 character set 字符集 collate 校验集;
mysql> alter table txt modify age varchar(50) character set utf8 collate utf8_general_ci;
Query OK, 3 rows affected (0.00 sec)
Records: 3  Duplicates: 0  Warnings: 0
mysql> show full columns from txt;
+-------+-------------+-----------------+------+-----+---------+-------+---------------------------------+---------+
| Field | Type        | Collation       | Null | Key | Default | Extra | Privileges                      | Comment |
+-------+-------------+-----------------+------+-----+---------+-------+---------------------------------+---------+
| id    | int(11)     | NULL            | NO   | PRI | NULL    |       | select,insert,update,references |         |
| name  | varchar(50) | utf8_general_ci | YES  |     | NULL    |       | select,insert,update,references |         |
| age   | varchar(50) | utf8_general_ci | YES  |     | NULL    |       | select,insert,update,references |         |
+-------+-------------+-----------------+------+-----+---------+-------+---------------------------------+---------+
3 rows in set (0.00 sec)
3.对字段属性进行修改
mysql> alter table txt modify age int;
Query OK, 3 rows affected (0.02 sec)
Records: 3  Duplicates: 0  Warnings: 0
mysql> show full columns from txt;
| Field | Type        | Collation       | Null | Key | Default | Extra | Privileges                      | Comment |
+-------+-------------+-----------------+------+-----+---------+-------+---------------------------------+---------+
| id    | int(11)     | NULL            | NO   | PRI | NULL    |       | select,insert,update,references |         |
| name  | varchar(50) | utf8_general_ci | YES  |     | NULL    |       | select,insert,update,references |         |
| age   | int(11)     | NULL            | YES  |     | NULL    |       | select,insert,update,references |         |
3 rows in set (0.00 sec)
4.创建字段的属性
1.字段属性

image.png

2.其他选项

image.png

3.使用命令
mysql> create table a1 (
    -> id int primary key auto_increment,
    -> name varchar(10) not null unique,
    -> age int default 18,
    -> stu_id int );
Query OK, 0 rows affected (0.00 sec)
mysql> create table a2 (
    -> id int primary key auto_increment,
    -> name varchar(10),
    -> foreign key(id) references a1(id));
Query OK, 0 rows affected (0.00 sec)
5.修改字段内容
mysql> update txt set name='zhaoliu' where name='zhangsan';
Query OK, 1 row affected (0.00 sec)
Rows matched: 1  Changed: 1  Warnings: 0
mysql> select * from txt;
+----+---------+------+
| id | name    | age  |
+----+---------+------+
|  1 | zhaoliu |   12 |
|  2 | lisi    |   15 |
|  3 | wangwu  |   18 |
+----+---------+------+
3 rows in set (0.00 sec)
6.删除字段内容
mysql> delete from txt where name='zhaoliu';
Query OK, 1 row affected (0.00 sec)
mysql> select * from txt;
+----+--------+------+
| id | name   | age  |
+----+--------+------+
|  2 | lisi   |   15 |
|  3 | wangwu |   18 |
+----+--------+------+
2 rows in set (0.00 sec)
7.查看字段类型
mysql> show create table a1;
| a1    | CREATE TABLE `a1` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(10) NOT NULL,
  `age` int(11) DEFAULT '18',
  `stu_id` int(11) DEFAULT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `name` (`name`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 |
+-------+---------------------------------------------------------------------------------------------------
1 row in set (0.00 sec)
mysql> desc a2;
+-------+-------------+------+-----+---------+----------------+
| Field | Type        | Null | Key | Default | Extra          |
+-------+-------------+------+-----+---------+----------------+
| id    | int(11)     | NO   | PRI | NULL    | auto_increment |
| name  | varchar(10) | YES  |     | NULL    |                |
+-------+-------------+------+-----+---------+----------------+
2 rows in set (0.00 sec)
8.添加字段
mysql> alter table a1 add stu_number varchar(10) not null unique;
Query OK, 0 rows affected (0.01 sec)
Records: 0  Duplicates: 0  Warnings: 0
mysql> desc a1;
+------------+-------------+------+-----+---------+----------------+
| Field      | Type        | Null | Key | Default | Extra          |
+------------+-------------+------+-----+---------+----------------+
| id         | int(11)     | NO   | PRI | NULL    | auto_increment |
| name       | varchar(10) | NO   | UNI | NULL    |                |
| age        | int(11)     | YES  |     | 18      |                |
| stu_id     | int(11)     | YES  |     | NULL    |                |
| stu_number | varchar(10) | NO   | UNI | NULL    |                |
+------------+-------------+------+-----+---------+----------------+
5 rows in set (0.00 sec)
mysql> alter table a1 add stu_number_1 varchar(10) not null unique first;   #添加到首行
mysql> alter table a1 add stu_number_2 varchar(10) not null unique after id;  #添加到id行之后
Query OK, 0 rows affected (0.00 sec)
Records: 0  Duplicates: 0  Warnings: 0
9.删除字段
mysql> alter table a1 drop stu_number_1;
10.修改字段排序顺序
mysql> alter table a1 modify stu_number varchar(10) first;
Query OK, 0 rows affected (0.01 sec)
Records: 0  Duplicates: 0  Warnings: 0
mysql> desc a1;
+--------------+-------------+------+-----+---------+----------------+
| Field        | Type        | Null | Key | Default | Extra          |
+--------------+-------------+------+-----+---------+----------------+
| stu_number   | varchar(10) | YES  | UNI | NULL    |                |
| id           | int(11)     | NO   | PRI | NULL    | auto_increment |
| stu_number_2 | varchar(10) | NO   | UNI | NULL    |                |
| name         | varchar(10) | NO   | UNI | NULL    |                |
| age          | int(11)     | YES  |     | 18      |                |
| stu_id       | int(11)     | YES  |     | NULL    |                |
+--------------+-------------+------+-----+---------+----------------+
6 rows in set (0.00 sec)
mysql> alter table a1 modify age int after id ;
Query OK, 0 rows affected (0.01 sec)
Records: 0  Duplicates: 0  Warnings: 0
mysql> desc a1;
+--------------+-------------+------+-----+---------+----------------+
| Field        | Type        | Null | Key | Default | Extra          |
+--------------+-------------+------+-----+---------+----------------+
| stu_number   | varchar(10) | YES  | UNI | NULL    |                |
| id           | int(11)     | NO   | PRI | NULL    | auto_increment |
| age          | int(11)     | YES  |     | NULL    |                |
| stu_number_2 | varchar(10) | NO   | UNI | NULL    |                |
| name         | varchar(10) | NO   | UNI | NULL    |                |
| stu_id       | int(11)     | YES  |     | NULL    |                |
+--------------+-------------+------+-----+---------+----------------+
6 rows in set (0.00 sec)
11.删除外键约束
mysql> create table test4(id int primary key ,name varchar(10),constraint `test` foreign key(id) references test2(id));
Query OK, 0 rows affected (0.01 sec)
mysql> alter table test4 drop foreign key test;
Query OK, 0 rows affected (0.00 sec)
Records: 0  Duplicates: 0  Warnings: 0
mysql> desc test4;
+-------+-------------+------+-----+---------+-------+
| Field | Type        | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| id    | int(11)     | NO   | PRI | NULL    |       |
| name  | varchar(10) | YES  |     | NULL    |       |
+-------+-------------+------+-----+---------+-------+
2 rows in set (0.00 sec)
相关实践学习
如何在云端创建MySQL数据库
开始实验后,系统会自动创建一台自建MySQL的 源数据库 ECS 实例和一台 目标数据库 RDS。
全面了解阿里云能为你做什么
阿里云在全球各地部署高效节能的绿色数据中心,利用清洁计算为万物互联的新世界提供源源不断的能源动力,目前开服的区域包括中国(华北、华东、华南、香港)、新加坡、美国(美东、美西)、欧洲、中东、澳大利亚、日本。目前阿里云的产品涵盖弹性计算、数据库、存储与CDN、分析与搜索、云通信、网络、管理与监控、应用服务、互联网中间件、移动服务、视频服务等。通过本课程,来了解阿里云能够为你的业务带来哪些帮助     相关的阿里云产品:云服务器ECS 云服务器 ECS(Elastic Compute Service)是一种弹性可伸缩的计算服务,助您降低 IT 成本,提升运维效率,使您更专注于核心业务创新。产品详情: https://www.aliyun.com/product/ecs
目录
相关文章
|
23天前
|
安全 关系型数据库 MySQL
如何将数据从MySQL同步到其他系统
【10月更文挑战第17天】如何将数据从MySQL同步到其他系统
131 0
|
17天前
|
Java 数据库连接 测试技术
SpringBoot入门(4) - 添加内存数据库H2
SpringBoot入门(4) - 添加内存数据库H2
38 4
SpringBoot入门(4) - 添加内存数据库H2
|
20天前
|
Java 数据库连接 测试技术
SpringBoot入门(4) - 添加内存数据库H2
SpringBoot入门(4) - 添加内存数据库H2
27 2
SpringBoot入门(4) - 添加内存数据库H2
|
12天前
|
Java 数据库连接 测试技术
SpringBoot入门(4) - 添加内存数据库H2
SpringBoot入门(4) - 添加内存数据库H2
52 13
|
7天前
|
Java 数据库连接 测试技术
SpringBoot入门(4) - 添加内存数据库H2
SpringBoot入门(4) - 添加内存数据库H2
21 4
|
16天前
|
关系型数据库 MySQL Linux
Linux系统如何设置自启动服务在MySQL数据库启动后执行?
【10月更文挑战第25天】Linux系统如何设置自启动服务在MySQL数据库启动后执行?
62 3
|
20天前
|
存储 人工智能 Java
Neo4j从入门到精通:打造高效知识图谱数据库 | AI应用开发
在大数据和人工智能时代,知识图谱作为一种高效的数据表示和查询方式,逐渐受到广泛关注。本文从入门到精通,详细介绍知识图谱及其存储工具Neo4j,涵盖知识图谱的介绍、Neo4j的特点、安装步骤、使用方法(创建、查询)及Cypher查询语言的详细讲解。通过本文,读者将全面了解如何利用Neo4j处理复杂关系数据。【10月更文挑战第14天】
72 6
|
29天前
|
存储 关系型数据库 MySQL
PACS系统 中 dicom 文件在mysql 8.0 数据库中的 存储和读取(pydicom 库使用)
PACS系统 中 dicom 文件在mysql 8.0 数据库中的 存储和读取(pydicom 库使用)
25 2
|
29天前
|
Ubuntu 关系型数据库 MySQL
Linux系统MySQL安装
【10月更文挑战第19天】本文介绍了在 Linux 系统上安装 MySQL 的步骤,包括安装前准备、安装 MySQL、启动 MySQL 服务、配置 MySQL 以及验证安装。适用于 Ubuntu/Debian 和 CentOS/Fedora 系统,提供了详细的命令示例。
142 1
|
1月前
|
SQL NoSQL 关系型数据库
数据库学习
【10月更文挑战第8天】
20 1