学习ALTER TABLE删除、添加和修改字段和类型
    CREATE TABLE alter_tab01(
    id int,
    col01 char(20))
    engin=InnoDB default charset=utf8;
 
 
 
    删除字段
        ALTER TABLE <tab_name> DROP <col_name>;
  
mysql> alter table alter_tab01 drop col01;
Query OK, 0 rows affected (0.01 sec)
Records: 0 Duplicates: 0 Warnings: 0
 
 
    添加字段
        ALTER TABLE <tab_name> ADD <col_name> TYPE;
        ALTER TABLE <tab_name> ADD <col_name> TYPE [ FIRST| AFTER <col_name>];
        ALTER TABLE <tab_name> ADD <col_name> TYPE NOT NULL;
 

mysql> alter table alter_tab01 add col01 char(20);
Query OK, 0 rows affected (0.01 sec)
Records: 0 Duplicates: 0 Warnings: 0 mysql> alter table alter_tab01 add col02 char(20) first;
Query OK, 0 rows affected (0.01 sec)
Records: 0 Duplicates: 0 Warnings: 0 mysql> alter table alter_tab01 add col03 char(20) after id;
Query OK, 0 rows affected (0.01 sec)
Records: 0 Duplicates: 0 Warnings: 0 mysql> alter table alter_tab01 add col04 char(20) not null;
Query OK, 0 rows affected (0.00 sec)
Records: 0 Duplicates: 0 Warnings: 0 mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec) mysql> show columns from alter_tab01;
+-------+----------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+----------+------+-----+---------+-------+
| col02 | char(20) | YES | | NULL | |
| id | int(11) | YES | | NULL | |
| col03 | char(20) | YES | | NULL | |
| col01 | char(20) | YES | | NULL | |
| col04 | char(20) | NO | | NULL | |
+-------+----------+------+-----+---------+-------+
5 rows in set (0.00 sec)
    修改字段类型及名称
 
        ALTER TABLE <tab_name> MODIFY <col_name> TYPE;
        ALTER TABLE <tab_name> CHANGE <old_name> <new_name> TYPE;

mysql> alter table alter_tab01 modify col02 varchar(10);
Query OK, 0 rows affected (0.01 sec)
Records: 0 Duplicates: 0 Warnings: 0 mysql> alter table alter_tab01 change col02 new_col02 char(2);
Query OK, 0 rows affected (0.01 sec)
Records: 0 Duplicates: 0 Warnings: 0 mysql> show columns from alter_tab01;
+-----------+----------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-----------+----------+------+-----+---------+-------+
| new_col02 | char(2) | YES | | NULL | |
| id | int(11) | YES | | NULL | |
| col03 | char(20) | YES | | NULL | |
| col01 | char(20) | YES | | NULL | |
| col04 | char(20) | NO | | NULL | |
+-----------+----------+------+-----+---------+-------+
5 rows in set (0.00 sec)
 
    
    修改字段NOT NULL约束与默认值
 
        ALTER TABLE <tab_name> MODIFY <col_name> TYPE NOT NULL DEFAULT <默认值>;

mysql> alter table alter_tab01 modify id bigint not null default 1;
Query OK, 0 rows affected (0.01 sec)
Records: 0 Duplicates: 0 Warnings: 0 mysql> show columns from alter_tab01;
+-----------+------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-----------+------------+------+-----+---------+-------+
| new_col02 | char(2) | YES | | NULL | |
| id | bigint(20) | NO | | 1 | |
| col03 | char(20) | YES | | NULL | |
| col01 | char(20) | YES | | NULL | |
| col04 | char(20) | NO | | NULL | |
+-----------+------------+------+-----+---------+-------+
5 rows in set (0.00 sec)
 
    修改字段的默认值
 
        ALTER TABLE <tab_name> ALTER <col_name> SET DEFAULT <默认值>;
        ALTER TABLE <tab_name> ALTER <col_name> DROP DEFAULT;

mysql> alter table alter_tab01 alter new_col02 set default '01';
Query OK, 0 rows affected (0.01 sec)
Records: 0 Duplicates: 0 Warnings: 0 mysql> show columns from alter_tab01;
+-----------+------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-----------+------------+------+-----+---------+-------+
| new_col02 | char(2) | YES | | 01 | |
| id | bigint(20) | NO | | 1 | |
| col03 | char(20) | YES | | NULL | |
| col01 | char(20) | YES | | NULL | |
| col04 | char(20) | NO | | NULL | |
+-----------+------------+------+-----+---------+-------+
5 rows in set (0.00 sec) mysql> alter table alter_tab01 alter new_col02 drop default;
Query OK, 0 rows affected (0.00 sec)
Records: 0 Duplicates: 0 Warnings: 0 mysql> show columns from alter_tab01;
+-----------+------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-----------+------------+------+-----+---------+-------+
| new_col02 | char(2) | YES | | NULL | |
| id | bigint(20) | NO | | 1 | |
| col03 | char(20) | YES | | NULL | |
| col01 | char(20) | YES | | NULL | |
| col04 | char(20) | NO | | NULL | |
+-----------+------------+------+-----+---------+-------+
5 rows in set (0.00 sec)
 
修改表的存储引擎
 
    ALTER TABLE <tab_name> ENGINE=<引擎名>【MyISAM | InnoDB | BDB | Memory | Merge | Archive | Federated | Cluster/NDB | Other】

mysql> show table status like 'alter_tab01'\G
*************************** 1. row ***************************
Name: alter_tab01
Engine: InnoDB
Version: 10
Row_format: Compact
Rows: 0
Avg_row_length: 0
Data_length: 16384
Max_data_length: 0
Index_length: 0
Data_free: 4194304
Auto_increment: NULL
Create_time: 2018-05-03 16:11:39
Update_time: NULL
Check_time: NULL
Collation: utf8_general_ci
Checksum: NULL
Create_options:
Comment:
1 row in set (0.00 sec) mysql> alter table alter_tab01 engine=MyISAM;
Query OK, 0 rows affected (0.01 sec)
Records: 0 Duplicates: 0 Warnings: 0 mysql> show table status like 'alter_tab01'\G
*************************** 1. row ***************************
Name: alter_tab01
Engine: MyISAM
Version: 10
Row_format: Fixed
Rows: 0
Avg_row_length: 0
Data_length: 0
Max_data_length: 54887620458577919
Index_length: 1024
Data_free: 0
Auto_increment: NULL
Create_time: 2018-05-03 16:12:35
Update_time: 2018-05-03 16:12:35
Check_time: NULL
Collation: utf8_general_ci
Checksum: NULL
Create_options:
Comment:
1 row in set (0.00 sec)
    修改表的名称
        ALTER TABLE <tab_name> RENAME TO <new_name>;
 
mysql> alter table alter_tab01 rename to alter_tab02;
Query OK, 0 rows affected (0.00 sec) mysql> show table status like 'alter_tab02'\G
*************************** 1. row ***************************
Name: alter_tab02
Engine: InnoDB
Version: 10
Row_format: Compact
Rows: 0
Avg_row_length: 0
Data_length: 16384
Max_data_length: 0
Index_length: 0
Data_free: 4194304
Auto_increment: NULL
Create_time: 2018-05-03 16:14:02
Update_time: NULL
Check_time: NULL
Collation: utf8_general_ci
Checksum: NULL
Create_options:
Comment:
1 row in set (0.00 sec)

MySQL-ALTER TABLE命令学习[20180503]的更多相关文章

  1. SQL ALTER TABLE 命令

    SQL ALTER TABLE 命令 SQL ALTER TABLE 命令用于添加.删除或者更改现有数据表中的列. 你还可以用 ALTER TABLE 命令来添加或者删除现有数据表上的约束. 语法: ...

  2. MySQL ALTER TABLE: ALTER vs CHANGE vs MODIFY COLUMN

    ALTER COLUMN 语法: ALTER [COLUMN] col_name {SET DEFAULT literal | DROP DEFAULT} 作用: 设置或删除列的默认值.该操作会直接修 ...

  3. mysql ALTER TABLE语句 语法

    mysql ALTER TABLE语句 语法 作用:用于在已有的表中添加.修改或删除列.无铁芯直线电机 语法:添加列:ALTER TABLE table_name ADD column_name da ...

  4. 【待整理】MySQL alter table modify vs alter table add产生state不一样

    MySQL:5.6.35 OS:redhat5.8 今天更新数据库某些表字段,有如下两SQL: ①alter table xx modify xxxx;(表大概是77w) ②alter table s ...

  5. mysql Alter table设置default的问题,是bug么?

    不用不知道,用了没用? 昨天在线上创建了一个表,其中有两个列是timestamp类型的,创建语句假设是这样的: create table timetest(id int, createtime tim ...

  6. mysql truncate table命令使用总结

    truncate使用注意 由于上过truncate table a_table命令一次当,将教训记录下来,以示警戒!     mysql truncate table a_table命令受影响结果说明 ...

  7. MySQL ALTER TABLE语法

    先看一下定义(密密麻麻) ALTER TABLE tbl_name [alter_specification [, alter_specification] ...] [partition_optio ...

  8. MySQL alter table时执行innobackupex全备再看Seconds_Behind_Master

    1.场景描述 早上7:25 接到Report中心同学告警,昨天业务报表数据没有完整跑出来,缺少500位业务员的数据,并且很快定位到,缺少的是huabei_order库上的数据.Report中心的数据是 ...

  9. mysql alter table

    准备: create table t(x int,y int); 用法 1: 修改列的数据类 alter table t modify column y nvarchar(32); 用法2: 给表加一 ...

随机推荐

  1. 【node】用koa搭建一个增删改服务(一)

    前文,vue分类里有一个日志demo的练习,这篇文章就是介绍针对日志demo的服务是怎么写的 一.koa搭建项目 1. npm init 2. npm install koa 二.建数据库 下面是项目 ...

  2. ArcGIS10.3+Oracle12C+ArcGIS Server10.3安装布署(之一)

    1.安装Oracle12C 2.配置Oracle12C的PDB监听(1)原始listener.ora文件 改为: (2)原始tnsnames.ora文件 改为: 3.连接Oracle12C的PDB数据 ...

  3. MS SQL Server数据库查询优化技巧

    [摘 要]本文主要是对MS SQL Server数据库查询优化技巧进行了说明和分析,对索引使用.查询条件以及数据表的设计等进行了阐述.中国论文网 http://www.xzbu.com/2/view- ...

  4. linux 用户管理 groupadd、groupmod、groupdel、gpasswd

    添加用户组groupadd [选项] 组名 /usr/sbin/groupadd执行权限:root一个用户可以属于多个所属组,但有一个缺省组,和用户名同名-g GID:指定组ID 修改用户组 grou ...

  5. Ionic step by step (1)

    刚接触 ionic,一步一步学习,有错误的,望大家指出. 公式 Ionic = Cordova + Angular2 + ionic CSS Cordova: 提供了使用 JavaScript 调用 ...

  6. 【日常记录】【unity3d】 2D跳跃过快导致角色某帧陷入地面

    如果角色运动过快会导致嵌入地面再反弹出来 : 可以使用更高质量的检测方式 "Continuous" :就可以解决这个问题

  7. spring boot(10)-tomcat jdbc连接池

    默认连接池 tomcat jdbc是从tomcat7开始推出的一个连接池,相比老的dbcp连接池要优秀很多.spring boot将tomcat jdbc作为默认的连接池,只要在pom.xml中引入了 ...

  8. C#中的三种timer

    转 https://blog.csdn.net/hoiven/article/details/51362582 如果你需要使用规律的时间间隔重复执行一些方法,最简单的方式是使用定时器(timer). ...

  9. SQL Server 索引知识-应用,维护

    创建聚集索引 a索引键最好唯一(如果不唯一会隐形建立uniquier列(4字节)确保唯一,也就是这列都会复制到所有非聚集索引中) b聚集索引列所占空间应尽量小(否则也会使非聚集索引的空间变大) c聚集 ...

  10. 在 Azure VM 上安装 LEMP Web 服务器

    本文逐步讲解如何在 Azure 中的 Ubuntu VM 上部署 NGINX Web 服务器.MySQL 和 PHP(LEMP 堆栈). LEMP 堆栈可以替代常用的 LAMP 堆栈,可安装在 Azu ...