MySQL中的表中增加刪除字段


 

1增加兩個字段:

復制代碼
mysql> create table id_name(id int,name varchar(20)); Query OK, 0 rows affected (0.13 sec) mysql> alter table id_name add age int,add address varchar(11); Query OK, 0 rows affected (0.13 sec) Records: 0  Duplicates: 0  Warnings: 0 mysql> desc id_name; +---------+-------------+------+-----+---------+-------+ | Field   | Type        | Null | Key | Default | Extra | +---------+-------------+------+-----+---------+-------+ | id      | int(11)     | YES  |     | NULL    |       | | name    | varchar(20) | YES  |     | NULL    |       | | age     | int(11)     | YES  |     | NULL    |       | | address | varchar(11) | YES  |     | NULL    |       | +---------+-------------+------+-----+---------+-------+ 4 rows in set (0.00 sec) 2.刪除兩個字段 mysql> alter table id_name drop column age,drop column address; Query OK, 0 rows affected (0.14 sec) Records: 0  Duplicates: 0  Warnings: 0 mysql> desc id_name; +-------+-------------+------+-----+---------+-------+ | Field | Type        | Null | Key | Default | Extra | +-------+-------------+------+-----+---------+-------+ | id    | int(11)     | YES  |     | NULL    |       | | name  | varchar(20) | YES  |     | NULL    |       | +-------+-------------+------+-----+---------+-------+ 2 rows in set (0.00 sec) 3.插入 mysql> insert into id_name values (1,'qustdjx'); Query OK, 1 row affected (0.00 sec) 4.查詢看一下 mysql> alter table id_name add age int,add address varchar(11); Query OK, 1 row affected (0.07 sec) Records: 1  Duplicates: 0  Warnings: 0 mysql> select * from id_name; +------+---------+------+---------+ | id   | name    | age  | address | +------+---------+------+---------+ |    1 | qustdjx | NULL | NULL    | +------+---------+------+---------+ 1 row in set (0.00 sec) 5.新增字段並插入 mysql> insert into id_name values(2,'qust',14,'山東'); Query OK, 1 row affected (0.00 sec) mysql> select * from id_name; +------+---------+------+---------+ | id   | name    | age  | address | +------+---------+------+---------+ |    1 | qustdjx | NULL | NULL    | |    2 | qust    |   14 | 山東    | +------+---------+------+---------+ 2 rows in set (0.00 sec)  
復制代碼

1.增加一個字段
alter table user add COLUMN new1 VARCHAR(20) DEFAULT NULL; //增加一個字段,默認為空
alter table user add COLUMN new2 VARCHAR(20) NOT NULL;  //增加一個字段,默認不能為空  www.2cto.com  
 
2.刪除一個字段
alter table user DROP COLUMN new2;   //刪除一個字段
 
3.修改一個字段
alter table user MODIFY new1 VARCHAR(10);  //修改一個字段的類型
 
alter table user CHANGE new1 new4 int;  //修改一個字段的名稱,此時一定要重新指定該字段的類型

http://blog.csdn.net/qustdjx/article/details/8875827


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM