在MySQL中修改数据库表结构

Alter Table - MySQL Command by Angela Bradley

Drop Column:
alter table icecream drop column flavor ;
Drop Column is used to remove an entire column and all its data from a table.
Add Column:
alter table icecream add column flavor varchar (20) ;
Add Column is used to add a column to your existing table.
Change:
alter table icecream change taste flavor varchar (10) ;
Change is used to change the column name. In our example it changes "taste" to be "flavor".
Add Unique:
alter table icecream add unique (quantity)
Add unique adds a new column to your table only if it does not already exist.
Modify:
alter table icecream modify flavor VARCHAR(35) ;
Modify is used to change a column's size. In our example we increase the size of the "flavor" column to 35 characters.

试验结果如下:

Enter password: ****
Welcome to the MySQL monitor.  Commands end with ; or /g.
Your MySQL connection id is 1
Server version: 5.0.45-community-nt MySQL Community Edition (GPL)

Type 'help;' or '/h' for help. Type '/c' to clear the buffer.

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| hibernate01        |
| mysql              |
| struts1            |
| test               |
+--------------------+
5 rows in set (0.08 sec)

mysql> use struts1;
Database changed
mysql> show tables;
+-------------------+
| Tables_in_struts1 |
+-------------------+
| person            |
| userdb            |
+-------------------+
2 rows in set (0.00 sec)

mysql> desc person;
+-------+-------------+------+-----+---------+----------------+
| Field | Type        | Null | Key | Default | Extra          |
+-------+-------------+------+-----+---------+----------------+
| id    | int(11)     | NO   | PRI | NULL    | auto_increment |
| name  | varchar(20) | NO   |     |         |                |
+-------+-------------+------+-----+---------+----------------+
2 rows in set (0.17 sec)

mysql> alter table person add column age int;
Query OK, 10 rows affected (0.75 sec)
Records: 10  Duplicates: 0  Warnings: 0

mysql> desc person;
+-------+-------------+------+-----+---------+----------------+
| Field | Type        | Null | Key | Default | Extra          |
+-------+-------------+------+-----+---------+----------------+
| id    | int(11)     | NO   | PRI | NULL    | auto_increment |
| name  | varchar(20) | NO   |     |         |                |
| age   | int(11)     | YES  |     | NULL    |                |
+-------+-------------+------+-----+---------+----------------+
3 rows in set (0.00 sec)

mysql> alter table person drop column age;
Query OK, 10 rows affected (0.63 sec)
Records: 10  Duplicates: 0  Warnings: 0

mysql> desc person;
+-------+-------------+------+-----+---------+----------------+
| Field | Type        | Null | Key | Default | Extra          |
+-------+-------------+------+-----+---------+----------------+
| id    | int(11)     | NO   | PRI | NULL    | auto_increment |
| name  | varchar(20) | NO   |     |         |                |
+-------+-------------+------+-----+---------+----------------+
2 rows in set (0.00 sec)

mysql> alter table person add column email varchar(10);
Query OK, 10 rows affected (0.23 sec)
Records: 10  Duplicates: 0  Warnings: 0

mysql> desc person;
+-------+-------------+------+-----+---------+----------------+
| Field | Type        | Null | Key | Default | Extra          |
+-------+-------------+------+-----+---------+----------------+
| id    | int(11)     | NO   | PRI | NULL    | auto_increment |
| name  | varchar(20) | NO   |     |         |                |
| email | varchar(10) | YES  |     | NULL    |                |
+-------+-------------+------+-----+---------+----------------+
3 rows in set (0.02 sec)

mysql> alter table person change email e-mail varchar(10);
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 '-mail
 varchar(10)' at line 1
mysql> alter table person change email varchar(20);
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 'varch
ar(20)' at line 1
mysql> alter table person change email address varchar(20);
Query OK, 10 rows affected (0.53 sec)
Records: 10  Duplicates: 0  Warnings: 0

mysql> desc person;
+---------+-------------+------+-----+---------+----------------+
| Field   | Type        | Null | Key | Default | Extra          |
+---------+-------------+------+-----+---------+----------------+
| id      | int(11)     | NO   | PRI | NULL    | auto_increment |
| name    | varchar(20) | NO   |     |         |                |
| address | varchar(20) | YES  |     | NULL    |                |
+---------+-------------+------+-----+---------+----------------+
3 rows in set (0.00 sec)

mysql> alter table person modify address varchar(40);
Query OK, 10 rows affected (0.31 sec)
Records: 10  Duplicates: 0  Warnings: 0

mysql> desc person;
+---------+-------------+------+-----+---------+----------------+
| Field   | Type        | Null | Key | Default | Extra          |
+---------+-------------+------+-----+---------+----------------+
| id      | int(11)     | NO   | PRI | NULL    | auto_increment |
| name    | varchar(20) | NO   |     |         |                |
| address | varchar(40) | YES  |     | NULL    |                |
+---------+-------------+------+-----+---------+----------------+
3 rows in set (0.00 sec)

mysql> alter table person add unique(name);
Query OK, 10 rows affected (0.52 sec)
Records: 10  Duplicates: 0  Warnings: 0

mysql> desc person;
+---------+-------------+------+-----+---------+----------------+
| Field   | Type        | Null | Key | Default | Extra          |
+---------+-------------+------+-----+---------+----------------+
| id      | int(11)     | NO   | PRI | NULL    | auto_increment |
| name    | varchar(20) | NO   | UNI |         |                |
| address | varchar(40) | YES  |     | NULL    |                |
+---------+-------------+------+-----+---------+----------------+
3 rows in set (0.00 sec)

mysql>




































































































  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值