mysql 修改表

需求 :

  1. 修改学生表名为其他名字
  2. 在学生信息表最后添加地址字段
  3. 将地址字段放到性别字段的前面
  4. 将地址字段的数据类型修改成其他数据类型

1 - 创建表

学生信息表,包含字段:学号、姓名、年龄、性别

create table if not exits student(
    s_no int,
    s_name varchar(10),
    s_age tinyint,
    s_sex char(2)
)

2 - 修改表名

use mdb; // 选择数据库

alter table student rename t_student; // 修改表名

// 使用以下语句检查修改是否成功
desc t_student;
desc student;

3 - 增加字段

  • 在表的最后增加
  • 在表的最前面增加
  • 在指定字段后增加
use mdb; // 选择数据库

alter table t_student add location varchar(40); //表的最后
alter table t_student add location varchar(40) first; //表的最前面
alter table t_student add location varchar(40) after s_sex; //指定字段后

desc t_student; // 使用以下语句检查增加是否成功

4 - 修改字段

  • 修改数据类型 modify
  • 修改字段名 change
  • 修改类型和名字
  • 修改字段顺序
use mdb; // 选择数据库

//location varchar(40) // 原来数据格式
alter table t_student modify location varchar(41); //修改数据类型
alter table t_student change location place varchar(41); //修改字段名location 为 place 
alter table t_student change location place varchar(42); //修改数据类型和字段名
alter table t_student modify s_age tinyint first; 
//修改 s_age 字段顺序为第一个位置
alter table t_student modify s_age tinyint after s_sex; 
//将s_age 字段调整到 s_sex 后面

desc t_student; // 使用以下语句检查修改是否成功

猜你喜欢

转载自blog.csdn.net/ai_shuyingzhixia/article/details/80109022