mysql修改数据表

本文详细介绍了如何在MySQL中进行数据表修改,包括修改表名、字段数据类型、字段名,添加、删除和调整字段排列位置,以及更改表的存储引擎等关键操作。对于每个操作,都提供了具体的步骤和示例。
摘要由CSDN通过智能技术生成

1.修改表名

语法:

alter table 旧表名 rename 新表名;

mysql> alter table tb_emp2 rename tb_employer2;

2.修改字段的数据类型

语法:

alter table 表名 modify 字段名 数据类型;

  • 表名:要修改哪个表下的字段类型

  • 字段名:要修改的字段

  • 数据类型:是指修改后字段的新数据类型

    mysql> desc tb_employer2;
    +--------+-------------+------+-----+---------+-------+
    | Field  | Type        | Null | Key | Default | Extra |
    +--------+-------------+------+-----+---------+-------+
    | id     | int(11)     | NO   |     | NULL    |       |
    | name   | varchar(25) | YES  |     | NULL    |       |
    | deptid | int(11)     | YES  |     | NULL    |       |
    | salary | float       | YES  |     | NULL    |       |
    +--------+-------------+------+-----+---------+-------+
    4 rows in set (0.00 sec)
    
    mysql> alter table tb_employer2 modify name varchar(30);
    Query OK, 0 rows affected (0.00 sec)
    Records: 0  Duplicates: 0  Warnings: 0
    
    mysql> desc tb_employer2;
    +--------+-------------+------+-----+---------+-------+
    | Field  | Type        | Null | Key | Default | Extra |
    +--------+-------------+------+-----+---------+-------+
    | id     | int(11)     | NO   |     | NULL    |       |
    | name   | varchar(30) | YES  |     | NULL    |       |
    | deptid | int(11)     | YES  |     | NULL    |       |
    | salary | float       | YES  |     | NULL    |       |
    +--------+-------------+------+-----+---------+-------+
    4 rows in set (0.00 sec)
    
    
    

3.修改字段名

语法:

alter table 表名 change 旧字段名 新字段名 新数据类型

其中新数据类型指的是修改后的数据类型,如果不需要修改字段的数据类型,可以将新数据类型和原来设置的一样

但是,但是,但是,数据类型这里不能不写,不能为空,这个语法格式!!!

#将旧字段名name修改为new-name
mysql> alter table tb_employer2 change name new-name varchar(30);

#这里也可以修改数据类型,即新旧字段名一样即可
mysql> alter table tb_employer2 change name name varchar(40);

#这里数据类型这个位置没有写旧报错了
mysql> alter table tb_employer2 change name new_name ;
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 1
#加上数据类型后就正常了
mysql> alter table tb_employer2 change name new_name varchar(40);
Query OK, 0 rows affected (0.00 sec)
Records: 0  Duplicates: 0  Warnings: 0

注意:数据库中有数据的时候,不要轻易的去修改数据类型,修改数据类型可能会影响到数据表中已有的数据记录

4.添加字段

一个完整的字段包括字段名、数据类型、完整性约束

语法:

alter table 表名 add 新字段名 数据类型

4.1 添加无完整性约束条件的字段

#添加localtion字段
mysql> alter table tb_emp3 add location varchar(100);

4.2 添加有完整性约束条件的字段

#添加interest字段
mysql> alter table tb_emp3 add interest set('music','song','play');

4.3 在表的第一列添加字段

#在第一列添加column1字段
mysql> alter table tb_emp3 add column1 int(11) first;

4.4 在表的指定列之后添加一个字段

#在name列后添加column2字段
mysql> alter table tb_emp3 add column2 int(11) after name;

5. 删除字段

语法:

alter table 表名 drop 字段名

#删除column1字段
mysql> alter table tb_emp3 drop column1;

6. 修改字段的排列位置

对于一个数据表来说,在创建的时候,字段在表中的排列顺序就已经确定了。但表的结构并不是完全不可以改变的,可以通过alter table来改变表中字段的相应位置

语法:

alter table 表名 modify 字段1 数据类型 FIRST|AFTER 字段2

字段1指要修改位置的字段,数据类型值字段1的数据类型,first为可选参数,指将字段1修改为表的第一个字段,after字段2指将字段1插入到字段2后面

6.1 修改字段为表的第一个字段

mysql> desc tb_emp3;
+----------+----------------------------+------+-----+---------+-------+
| Field    | Type                       | Null | Key | Default | Extra |
+----------+----------------------------+------+-----+---------+-------+
| id       | int(11)                    | NO   | PRI | NULL    |       |
| name     | varchar(25)                | YES  |     | NULL    |       |
| column2  | int(11)                    | YES  |     | NULL    |       |
| deotid   | int(11)                    | YES  |     | NULL    |       |
| salary   | float                      | YES  |     | NULL    |       |
| location | varchar(100)               | YES  |     | NULL    |       |
| interest | set('music','song','play') | YES  |     | NULL    |       |
+----------+----------------------------+------+-----+---------+-------+

将interest字段修改为第一个字段

mysql> alter table tb_emp3 modify interest set('music','song','play') first;

6.2 修改字段到表的指定列之后

mysql> desc tb_emp3;
+----------+----------------------------+------+-----+---------+-------+
| Field    | Type                       | Null | Key | Default | Extra |
+----------+----------------------------+------+-----+---------+-------+
| interest | set('music','song','play') | YES  |     | NULL    |       |
| id       | int(11)                    | NO   | PRI | NULL    |       |
| name     | varchar(25)                | YES  |     | NULL    |       |
| column2  | int(11)                    | YES  |     | NULL    |       |
| deotid   | int(11)                    | YES  |     | NULL    |       |
| salary   | float                      | YES  |     | NULL    |       |
| location | varchar(100)               | YES  |     | NULL    |       |
+----------+----------------------------+------+-----+---------+-------+

将salary字段插入到name字段后面

mysql> alter table tb_emp3 modify salary float after name;

7.更改表的存储引擎

查看当前数据库版本呢以及支持的存储引擎

在这里插入图片描述

更改表的存储引擎语法:

alter table 表名 engine=更改后的存储引擎名

  • 查看当前表所使用的存储引擎

    mysql> show create table tb_emp3\G;
    *************************** 1. row ***************************
           Table: tb_emp3
    Create Table: CREATE TABLE `tb_emp3` (
      `interest` set('music','song','play') DEFAULT NULL,
      `id` int(11) NOT NULL,
      `name` varchar(25) DEFAULT NULL,
      `salary` float DEFAULT NULL,
      `column2` int(11) DEFAULT NULL,
      `deotid` int(11) DEFAULT NULL,
      `location` varchar(100) DEFAULT NULL,
      PRIMARY KEY (`id`)
    ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4
    

    修改存储引擎为myisam

    mysql> alter table tb_emp3 engine=myisam;
    
  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

河 静

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值