MySQL-ALTER TABLE命令学习[20180503]
2018-06-18 01:25:00来源:未知 阅读 ()
学习ALTER TABLE删除、添加和修改字段和类型
mysql> alter table alter_tab01 drop col01;
Query OK, 0 rows affected (0.01 sec)
Records: 0 Duplicates: 0 Warnings: 0
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)
修改字段类型及名称
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)
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)
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)
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)
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)
标签:
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有
- Linux简介及最常用命令(简单易学能解决95%以上的问题) 2019-10-08
- 一份非常完整的MySQL规范 2019-09-17
- PHP的灵魂HashTable结构解读 2019-09-08
- 29 条实用 Linux 命令 2019-08-29
- mysql cmd的常用命令 2019-08-09
IDC资讯: 主机资讯 注册资讯 托管资讯 vps资讯 网站建设
网站运营: 建站经验 策划盈利 搜索优化 网站推广 免费资源
网络编程: Asp.Net编程 Asp编程 Php编程 Xml编程 Access Mssql Mysql 其它
服务器技术: Web服务器 Ftp服务器 Mail服务器 Dns服务器 安全防护
软件技巧: 其它软件 Word Excel Powerpoint Ghost Vista QQ空间 QQ FlashGet 迅雷
网页制作: FrontPages Dreamweaver Javascript css photoshop fireworks Flash