【MySql】mysql 表的常规管理

本文涉及的产品
云数据库 RDS MySQL Serverless,0.5-2RCU 50GB
简介: 记录一些简单的表的管理知识,方便使用!mysql> desc yql9; +-------+---------+------+-----+---------+-------+| Field | Type    | Null | Key | Default ...
记录一些简单的表的管理知识,方便使用!
mysql> desc yql9; 
+-------+---------+------+-----+---------+-------+
| Field | Type    | Null | Key | Default | Extra |
+-------+---------+------+-----+---------+-------+
| id    | int(11) | YES  |     | NULL    |       |
| val   | char(5) | YES  |     | NULL    |       |
+-------+---------+------+-----+---------+-------+
2 rows in set (0.00 sec)

1 添加字段 ALTER TABLE tabname ADD field_name field_type;
mysql> alter table yql9 add new_id int(5) unsigned  default 0 not null auto_increment ,add primary key (new_id);
ERROR 1067 (42000): Invalid default value for 'new_id'
mysql> alter table yql9 add new_id int(5) unsigned  not null auto_increment ,add primary key (new_id);
Query OK, 0 rows affected (0.00 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> alter table yql9 add gmt_created timestamp;
Query OK, 0 rows affected (0.00 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> desc yql9;
+-------------+-----------------+------+-----+-------------------+-----------------------------+
| Field       | Type            | Null | Key | Default           | Extra                       |
+-------------+-----------------+------+-----+-------------------+-----------------------------+
| id          | int(11)         | YES  |     | NULL              |                             |
| val         | char(5)         | YES  |     | NULL              |                             |
| new_id      | int(5) unsigned | NO   | PRI | NULL              | auto_increment              |
| gmt_created | timestamp       | NO   |     | CURRENT_TIMESTAMP | on update CURRENT_TIMESTAMP |
+-------------+-----------------+------+-----+-------------------+-----------------------------+
4 rows in set (0.01 sec)

2 删除字段  alter table tabname drop field_name;
mysql> alter table yql9 drop column new_id;
Query OK, 0 rows affected (0.01 sec)
Records: 0  Duplicates: 0  Warnings: 0
mysql> desc yql9;
+-------------+-----------+------+-----+-------------------+-----------------------------+
| Field       | Type      | Null | Key | Default           | Extra                       |
+-------------+-----------+------+-----+-------------------+-----------------------------+
| id          | int(11)   | YES  |     | NULL              |                             |
| val         | char(5)   | YES  |     | NULL              |                             |
| gmt_created | timestamp | NO   |     | CURRENT_TIMESTAMP | on update CURRENT_TIMESTAMP |
+-------------+-----------+------+-----+-------------------+-----------------------------+
3 rows in set (0.00 sec)
mysql> alter table yangql9 drop v;
Query OK, 0 rows affected (0.00 sec)
Records: 0  Duplicates: 0  Warnings: 0
mysql> desc yangql9;
+-------------+-----------+------+-----+-------------------+-----------------------------+
| Field       | Type      | Null | Key | Default           | Extra                       |
+-------------+-----------+------+-----+-------------------+-----------------------------+
| id          | int(11)   | NO   | PRI | 0                 |                             |
| gmt_created | timestamp | NO   |     | CURRENT_TIMESTAMP | on update CURRENT_TIMESTAMP |
+-------------+-----------+------+-----+-------------------+-----------------------------+
2 rows in set (0.00 sec)

3 修改字段的数据类型 alter table tabname change old_field_name new_field_name field_type;
mysql> alter table yql9 change val v integer;
Query OK, 0 rows affected (0.00 sec)
Records: 0  Duplicates: 0  Warnings: 0
mysql> desc yql9;
+-------------+-----------+------+-----+-------------------+-----------------------------+
| Field       | Type      | Null | Key | Default           | Extra                       |
+-------------+-----------+------+-----+-------------------+-----------------------------+
| id          | int(11)   | YES  |     | NULL              |                             |
| v           | int(11)   | YES  |     | NULL              |                             |
| gmt_created | timestamp | NO   |     | CURRENT_TIMESTAMP | on update CURRENT_TIMESTAMP |
+-------------+-----------+------+-----+-------------------+-----------------------------+
3 rows in set (0.00 sec)

mysql> alter table yql9 change v v tinyint not null default 0;
Query OK, 0 rows affected (0.00 sec)
Records: 0  Duplicates: 0  Warnings: 0
mysql> desc yql9;
+-------------+------------+------+-----+-------------------+-----------------------------+
| Field       | Type       | Null | Key | Default           | Extra                       |
+-------------+------------+------+-----+-------------------+-----------------------------+
| id          | int(11)    | YES  |     | NULL              |                             |
| v           | tinyint(4) | NO   |     | 0                 |                             |
| gmt_created | timestamp  | NO   |     | CURRENT_TIMESTAMP | on update CURRENT_TIMESTAMP |
+-------------+------------+------+-----+-------------------+-----------------------------+
3 rows in set (0.00 sec)

4 重命名表
mysql> alter  table yql9 rename yangql9;
Query OK, 0 rows affected (0.01 sec)

5 添加索引 alter table tabname add index /create index idxname on tabname(column_name);
mysql> alter table yangql9 add index id_idx (id);
Query OK, 0 rows affected (0.03 sec)
Records: 0  Duplicates: 0  Warnings: 0
mysql> desc yangql9;
+-------------+------------+------+-----+-------------------+-----------------------------+
| Field       | Type       | Null | Key | Default           | Extra                       |
+-------------+------------+------+-----+-------------------+-----------------------------+
| id          | int(11)    | YES  | MUL | NULL              |                             |
| v           | tinyint(4) | NO   |     | 0                 |                             |
| gmt_created | timestamp  | NO   |     | CURRENT_TIMESTAMP | on update CURRENT_TIMESTAMP |
+-------------+------------+------+-----+-------------------+-----------------------------+
3 rows in set (0.00 sec)
6 添加主键
mysql> alter table yangql9 add primary key(id);  
Query OK, 0 rows affected (0.00 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> desc yangql9;
+-------------+------------+------+-----+-------------------+-----------------------------+
| Field       | Type       | Null | Key | Default           | Extra                       |
+-------------+------------+------+-----+-------------------+-----------------------------+
| id          | int(11)    | NO   | PRI | 0                 |                             |
| v           | tinyint(4) | NO   |     | 0                 |                             |
| gmt_created | timestamp  | NO   |     | CURRENT_TIMESTAMP | on update CURRENT_TIMESTAMP |
+-------------+------------+------+-----+-------------------+-----------------------------+
3 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
目录
相关文章
|
Oracle 关系型数据库 MySQL
【mysql】—— 表的内连和外连
【mysql】—— 表的内连和外连
|
4月前
|
存储 关系型数据库 MySQL
【MySQL】 表的操作
【MySQL】 表的操作
|
7天前
|
存储 关系型数据库 MySQL
MySQL 表管理
MySQL 表管理
|
1月前
|
存储 SQL 关系型数据库
【mysql】—— 表的操作
【mysql】—— 表的操作
|
4月前
|
关系型数据库 MySQL
零基础带你学习MySQL—修改表(六)
零基础带你学习MySQL—修改表(六)
|
6月前
|
存储 索引
2.MySQL表的操作
2.MySQL表的操作
25 0
|
6月前
|
存储 关系型数据库 MySQL
【MYSQL高级】Mysql 索引基础介绍
【MYSQL高级】Mysql 索引基础介绍
33 0
|
8月前
|
关系型数据库 MySQL 数据库
MySQL 管理方法
MySQL 管理方法
70 1
|
9月前
|
SQL 存储 关系型数据库
MySQL表的操作
MySQL表的操作
69 0
|
SQL 关系型数据库 MySQL
MySQL表管理
| int | 整型 | | float| 单精度浮点 4字节32位| | double| 双精度浮点 8字节64位| | varchar| 可变长度的字符类型| | text| 文本| | decimal (5,2)| 5个有效长度数字,小数点后面有2位| | image| 图片| | char| 固定长度的字符类型|