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

本文涉及的产品
RDS MySQL Serverless 基础系列,0.5-2RCU 50GB
云数据库 RDS MySQL,集群版 2核4GB 100GB
推荐场景:
搭建个人博客
云数据库 RDS MySQL,高可用版 2核4GB 50GB
简介: 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)
相关实践学习
基于CentOS快速搭建LAMP环境
本教程介绍如何搭建LAMP环境,其中LAMP分别代表Linux、Apache、MySQL和PHP。
全面了解阿里云能为你做什么
阿里云在全球各地部署高效节能的绿色数据中心,利用清洁计算为万物互联的新世界提供源源不断的能源动力,目前开服的区域包括中国(华北、华东、华南、香港)、新加坡、美国(美东、美西)、欧洲、中东、澳大利亚、日本。目前阿里云的产品涵盖弹性计算、数据库、存储与CDN、分析与搜索、云通信、网络、管理与监控、应用服务、互联网中间件、移动服务、视频服务等。通过本课程,来了解阿里云能够为你的业务带来哪些帮助     相关的阿里云产品:云服务器ECS 云服务器 ECS(Elastic Compute Service)是一种弹性可伸缩的计算服务,助您降低 IT 成本,提升运维效率,使您更专注于核心业务创新。产品详情: https://www.aliyun.com/product/ecs
目录
相关文章
|
1天前
|
关系型数据库 MySQL 数据库
『Django』模型入门教程-操作MySQL
一个后台如果没有数据库可以说废了一半。日常开发中大多数时候都在与数据库打交道。Django 为我们提供了一种更简单的操作数据库的方式。 在 Django 中,模型(Model)是用来定义数据库结构的类。每个模型类通常对应数据库中的一个表,类的属性对应表中的列。通过定义模型,Django 的 ORM(Object-Relational Mapping)可以将 Python 对象映射到数据库表,并提供一套 API 来进行数据库操作。 本文介绍模型的用法。
|
3天前
|
SQL 关系型数据库 MySQL
MySQL基础(二)----DML学习
MySQL基础(二)----DML学习
7 3
|
5天前
|
Java 关系型数据库 MySQL
Mybatis入门之在基于Springboot的框架下拿到MySQL中数据
Mybatis入门之在基于Springboot的框架下拿到MySQL中数据
15 4
|
8天前
|
SQL 数据库
零基础学习数据库SQL语句之操作表中数据的DML语句
零基础学习数据库SQL语句之操作表中数据的DML语句
11 0
零基础学习数据库SQL语句之操作表中数据的DML语句
|
1天前
|
移动开发 小程序 关系型数据库
java+ IDEA+ Uniapp+ mysql医院3D智能导诊系统源码
这是一个基于Java、IDEA、Uniapp和MySQL的医院3D智能导诊系统,采用Springboot后端框架和Redis、Mybatis Plus、RocketMQ等技术。系统通过对话式交互,精准推荐就诊科室,解决患者挂号困扰。它还具备智能预问诊功能,提升诊疗效率和准确性,确保医生能快速了解患者详情。此系统还支持小程序和H5,方便患者使用。
7 0
|
2天前
|
SQL 缓存 Java
必知的技术知识:hsql数据库使用详解(入门)及快速使用
必知的技术知识:hsql数据库使用详解(入门)及快速使用
|
3天前
|
关系型数据库 MySQL 数据库
轻松入门:使用Docker安装MySQL数据库的完全指南
轻松入门:使用Docker安装MySQL数据库的完全指南
|
5天前
|
SQL 数据库 数据库管理
逆向学习数据库篇:多表查询技术详解
逆向学习数据库篇:多表查询技术详解
8 0
|
5天前
|
存储 数据库连接 数据库
逆向学习数据库篇:表设计和数据库操作的核心概念与流程
逆向学习数据库篇:表设计和数据库操作的核心概念与流程
10 0
|
8天前
|
关系型数据库 MySQL Linux
Linux系统中Mysql5.7建立远程连接
Linux系统中Mysql5.7建立远程连接
6 0

热门文章

最新文章