MySQL之修改表中的列
2015-06-07 14:21
645 查看
MySQL之修改表中的列
修改表中列的语法:
一张表,创建完毕,有了N列。之后还可以增加或删除或修改列
alter table 表名 add 列名称 列类型 列参数; [这样加的列在表的最后]
例:alter table m1 add username char(20) not null default '';
alter table 表名 add 列名称 列类型 列参数 after 某列; [新列加在某列后]
例:alter table m1 add username char(20) not null default '';
如果想增加一列,并位于表的最前面,用first
alter table 表名 add 列名称 列类型 列参数 first;
例:alter table m1 add pid int not null first;
-------------------------------------------------------------------------------------------------------------------
删除表中列的语法:
alter table 表名 drop 列名;
例:alter table m1 drop pid;
------------------------------------------------------------------------------------------------------------------
修改表中列类型的语法:
alter table 表名 modify 列名 新类型 新参数;
例:alter table m1 modify gender char(4) not null default '';
-------------------------------------------------------------------------------------------------------------------
修改表中列名及类型的语法:
alter table 表名 change 旧列名 新列名 新类型 新参数;
例:alter table m1 change id uid int unsigned;
---------------------------------------------------------------------------------------------------------------
下面是在mysql中的操作:
mysql> create table m1(
-> id int unsigned auto_increment primary key
-> );
Query OK, 0 rows affected (0.62 sec)
mysql> desc m1;
+-------+------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------+------------------+------+-----+---------+----------------+
| id | int(10) unsigned | NO | PRI | NULL | auto_increment |
+-------+------------------+------+-----+---------+----------------+
1 row in set (0.14 sec)
mysql> alter table m1 add username char(20) not null default '';
Query OK, 0 rows affected (0.49 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> desc m1;
+----------+------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+----------+------------------+------+-----+---------+----------------+
| id | int(10) unsigned | NO | PRI | NULL | auto_increment |
| username | char(20) | NO | | | |
+----------+------------------+------+-----+---------+----------------+
2 rows in set (0.01 sec)
mysql> alter table m1 add birth date not null default '0000-00-00';
Query OK, 0 rows affected (0.46 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> desc m1;
+----------+------------------+------+-----+------------+----------------+
| Field | Type | Null | Key | Default | Extra |
+----------+------------------+------+-----+------------+----------------+
| id | int(10) unsigned | NO | PRI | NULL | auto_increment |
| username | char(20) | NO | | | |
| birth | date | NO | | 0000-00-00 | |
+----------+------------------+------+-----+------------+----------------+
3 rows in set (0.01 sec)
mysql> alter table m1 add gender char(1) not null default '' after username;
Query OK, 0 rows affected (0.36 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> desc m1;
+----------+------------------+------+-----+------------+----------------+
| Field | Type | Null | Key | Default | Extra |
+----------+------------------+------+-----+------------+----------------+
| id | int(10) unsigned | NO | PRI | NULL | auto_increment |
| username | char(20) | NO | | | |
| gender | char(1) | NO | | | |
| birth | date | NO | | 0000-00-00 | |
+----------+------------------+------+-----+------------+----------------+
4 rows in set (0.01 sec)
mysql> #如果想增加一列,并位于表的最前面,用first
mysql> alter table m1 add pid int not null default '' first;
ERROR 1067 (42000): Invalid default value for 'pid'
mysql> alter table m1 add pid int not null first;
Query OK, 0 rows affected (0.40 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> desc m1;
+----------+------------------+------+-----+------------+----------------+
| Field | Type | Null | Key | Default | Extra |
+----------+------------------+------+-----+------------+----------------+
| pid | int(11) | NO | | NULL | |
| id | int(10) unsigned | NO | PRI | NULL | auto_increment |
| username | char(20) | NO | | | |
| gender | char(1) | NO | | | |
| birth | date | NO | | 0000-00-00 | |
+----------+------------------+------+-----+------------+----------------+
5 rows in set (0.01 sec)
mysql> #删除列
mysql> desc m1;
+----------+------------------+------+-----+------------+----------------+
| Field | Type | Null | Key | Default | Extra |
+----------+------------------+------+-----+------------+----------------+
| pid | int(11) | NO | | NULL | |
| id | int(10) unsigned | NO | PRI | NULL | auto_increment |
| username | char(20) | NO | | | |
| gender | char(1) | NO | | | |
| birth | date | NO | | 0000-00-00 | |
+----------+------------------+------+-----+------------+----------------+
5 rows in set (0.01 sec)
mysql> alter table m1 drop pid;
Query OK, 0 rows affected (0.37 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> desc m1;
+----------+------------------+------+-----+------------+----------------+
| Field | Type | Null | Key | Default | Extra |
+----------+------------------+------+-----+------------+----------------+
| id | int(10) unsigned | NO | PRI | NULL | auto_increment |
| username | char(20) | NO | | | |
| gender | char(1) | NO | | | |
| birth | date | NO | | 0000-00-00 | |
+----------+------------------+------+-----+------------+----------------+
4 rows in set (0.01 sec)
mysql> alter table m1 modify gender char(4) not null default '';
Query OK, 0 rows affected (0.47 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> desc m1;
+----------+------------------+------+-----+------------+----------------+
| Field | Type | Null | Key | Default | Extra |
+----------+------------------+------+-----+------------+----------------+
| id | int(10) unsigned | NO | PRI | NULL | auto_increment |
| username | char(20) | NO | | | |
| gender | char(4) | NO | | | |
| birth | date | NO | | 0000-00-00 | |
+----------+------------------+------+-----+------------+----------------+
4 rows in set (0.05 sec)
mysql> alter table m1 change id uid int unsigned;
Query OK, 0 rows affected (0.44 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> desc m1;
+----------+------------------+------+-----+------------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------+------------------+------+-----+------------+-------+
| uid | int(10) unsigned | NO | PRI | 0 | |
| username | char(20) | NO | | | |
| gender | char(4) | NO | | | |
| birth | date | NO | | 0000-00-00 | |
+----------+------------------+------+-----+------------+-------+
4 rows in set (0.01 sec)
mysql> exit;
----------------------------------------------------------------------------------
欢迎探讨与学习。。。。。。
修改表中列的语法:
一张表,创建完毕,有了N列。之后还可以增加或删除或修改列
alter table 表名 add 列名称 列类型 列参数; [这样加的列在表的最后]
例:alter table m1 add username char(20) not null default '';
alter table 表名 add 列名称 列类型 列参数 after 某列; [新列加在某列后]
例:alter table m1 add username char(20) not null default '';
如果想增加一列,并位于表的最前面,用first
alter table 表名 add 列名称 列类型 列参数 first;
例:alter table m1 add pid int not null first;
-------------------------------------------------------------------------------------------------------------------
删除表中列的语法:
alter table 表名 drop 列名;
例:alter table m1 drop pid;
------------------------------------------------------------------------------------------------------------------
修改表中列类型的语法:
alter table 表名 modify 列名 新类型 新参数;
例:alter table m1 modify gender char(4) not null default '';
-------------------------------------------------------------------------------------------------------------------
修改表中列名及类型的语法:
alter table 表名 change 旧列名 新列名 新类型 新参数;
例:alter table m1 change id uid int unsigned;
---------------------------------------------------------------------------------------------------------------
下面是在mysql中的操作:
mysql> create table m1(
-> id int unsigned auto_increment primary key
-> );
Query OK, 0 rows affected (0.62 sec)
mysql> desc m1;
+-------+------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------+------------------+------+-----+---------+----------------+
| id | int(10) unsigned | NO | PRI | NULL | auto_increment |
+-------+------------------+------+-----+---------+----------------+
1 row in set (0.14 sec)
mysql> alter table m1 add username char(20) not null default '';
Query OK, 0 rows affected (0.49 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> desc m1;
+----------+------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+----------+------------------+------+-----+---------+----------------+
| id | int(10) unsigned | NO | PRI | NULL | auto_increment |
| username | char(20) | NO | | | |
+----------+------------------+------+-----+---------+----------------+
2 rows in set (0.01 sec)
mysql> alter table m1 add birth date not null default '0000-00-00';
Query OK, 0 rows affected (0.46 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> desc m1;
+----------+------------------+------+-----+------------+----------------+
| Field | Type | Null | Key | Default | Extra |
+----------+------------------+------+-----+------------+----------------+
| id | int(10) unsigned | NO | PRI | NULL | auto_increment |
| username | char(20) | NO | | | |
| birth | date | NO | | 0000-00-00 | |
+----------+------------------+------+-----+------------+----------------+
3 rows in set (0.01 sec)
mysql> alter table m1 add gender char(1) not null default '' after username;
Query OK, 0 rows affected (0.36 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> desc m1;
+----------+------------------+------+-----+------------+----------------+
| Field | Type | Null | Key | Default | Extra |
+----------+------------------+------+-----+------------+----------------+
| id | int(10) unsigned | NO | PRI | NULL | auto_increment |
| username | char(20) | NO | | | |
| gender | char(1) | NO | | | |
| birth | date | NO | | 0000-00-00 | |
+----------+------------------+------+-----+------------+----------------+
4 rows in set (0.01 sec)
mysql> #如果想增加一列,并位于表的最前面,用first
mysql> alter table m1 add pid int not null default '' first;
ERROR 1067 (42000): Invalid default value for 'pid'
mysql> alter table m1 add pid int not null first;
Query OK, 0 rows affected (0.40 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> desc m1;
+----------+------------------+------+-----+------------+----------------+
| Field | Type | Null | Key | Default | Extra |
+----------+------------------+------+-----+------------+----------------+
| pid | int(11) | NO | | NULL | |
| id | int(10) unsigned | NO | PRI | NULL | auto_increment |
| username | char(20) | NO | | | |
| gender | char(1) | NO | | | |
| birth | date | NO | | 0000-00-00 | |
+----------+------------------+------+-----+------------+----------------+
5 rows in set (0.01 sec)
mysql> #删除列
mysql> desc m1;
+----------+------------------+------+-----+------------+----------------+
| Field | Type | Null | Key | Default | Extra |
+----------+------------------+------+-----+------------+----------------+
| pid | int(11) | NO | | NULL | |
| id | int(10) unsigned | NO | PRI | NULL | auto_increment |
| username | char(20) | NO | | | |
| gender | char(1) | NO | | | |
| birth | date | NO | | 0000-00-00 | |
+----------+------------------+------+-----+------------+----------------+
5 rows in set (0.01 sec)
mysql> alter table m1 drop pid;
Query OK, 0 rows affected (0.37 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> desc m1;
+----------+------------------+------+-----+------------+----------------+
| Field | Type | Null | Key | Default | Extra |
+----------+------------------+------+-----+------------+----------------+
| id | int(10) unsigned | NO | PRI | NULL | auto_increment |
| username | char(20) | NO | | | |
| gender | char(1) | NO | | | |
| birth | date | NO | | 0000-00-00 | |
+----------+------------------+------+-----+------------+----------------+
4 rows in set (0.01 sec)
mysql> alter table m1 modify gender char(4) not null default '';
Query OK, 0 rows affected (0.47 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> desc m1;
+----------+------------------+------+-----+------------+----------------+
| Field | Type | Null | Key | Default | Extra |
+----------+------------------+------+-----+------------+----------------+
| id | int(10) unsigned | NO | PRI | NULL | auto_increment |
| username | char(20) | NO | | | |
| gender | char(4) | NO | | | |
| birth | date | NO | | 0000-00-00 | |
+----------+------------------+------+-----+------------+----------------+
4 rows in set (0.05 sec)
mysql> alter table m1 change id uid int unsigned;
Query OK, 0 rows affected (0.44 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> desc m1;
+----------+------------------+------+-----+------------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------+------------------+------+-----+------------+-------+
| uid | int(10) unsigned | NO | PRI | 0 | |
| username | char(20) | NO | | | |
| gender | char(4) | NO | | | |
| birth | date | NO | | 0000-00-00 | |
+----------+------------------+------+-----+------------+-------+
4 rows in set (0.01 sec)
mysql> exit;
----------------------------------------------------------------------------------
欢迎探讨与学习。。。。。。
相关文章推荐
- MySQL之常用数据类型
- mysql在insert触发器里update当前数据
- mysql命令大全
- mysql配置文件my.cnf优化实例
- mysql
- mysql配置文件my.cnf参数优化和中文详解
- mysql 中的 my.ini 配置文件
- 1130-host '192.168.x.x' is not allowed to conneciton to this MySQL server
- lamp源码安装之二进制MySQL5.6.25安装
- mysql导入导出sql
- mysql查看、修改端口、指定端口登录
- mysql innodb插入意向锁
- mysql主从复制
- MySql处理数据库和表
- mysql数据库root密码忘记的修改方法
- MySQL 整体性能调优
- MySQL 中文显示乱码
- mysql数据库权限
- MySQL 中文乱码(全)
- mysql数据库按照同一字段不同值进行排序