MySQL中的表中增加刪除欄位

來源:互聯網
上載者:User

標籤:des   style   blog   http   io   ar   color   os   sp   

1增加兩個欄位:

 

[sql] view plaincopy
  1. mysql> create table id_name(id int,name varchar(20));  
  2. Query OK, 0 rows affected (0.13 sec)  
  3.   
  4.   
  5. mysql> alter table id_name add age int,add address varchar(11);  
  6. Query OK, 0 rows affected (0.13 sec)  
  7. Records: 0  Duplicates: 0  Warnings: 0  
  8.   
  9. mysql> desc id_name;  
  10.   
  11. +---------+-------------+------+-----+---------+-------+  
  12. | Field   | Type        | Null | Key | Default | Extra |  
  13. +---------+-------------+------+-----+---------+-------+  
  14. | id      | int(11)     | YES  |     | NULL    |       |  
  15. | name    | varchar(20) | YES  |     | NULL    |       |  
  16. | age     | int(11)     | YES  |     | NULL    |       |  
  17. | address | varchar(11) | YES  |     | NULL    |       |  
  18. +---------+-------------+------+-----+---------+-------+  
  19. 4 rows in set (0.00 sec)  
  20.   
  21. 2.刪除兩個欄位  
  22. mysql> alter table id_name drop column age,drop column address;  
  23. Query OK, 0 rows affected (0.14 sec)  
  24. Records: 0  Duplicates: 0  Warnings: 0  
  25.   
  26. mysql> desc id_name;  
  27. +-------+-------------+------+-----+---------+-------+  
  28. | Field | Type        | Null | Key | Default | Extra |  
  29. +-------+-------------+------+-----+---------+-------+  
  30. | id    | int(11)     | YES  |     | NULL    |       |  
  31. | name  | varchar(20) | YES  |     | NULL    |       |  
  32. +-------+-------------+------+-----+---------+-------+  
  33. 2 rows in set (0.00 sec)  
  34.   
  35. 3.插入  
  36. mysql> insert into id_name values (1,‘qustdjx‘);  
  37. Query OK, 1 row affected (0.00 sec)  
  38. 4.查詢看一下  
  39. mysql> alter table id_name add age int,add address varchar(11);  
  40. Query OK, 1 row affected (0.07 sec)  
  41. Records: 1  Duplicates: 0  Warnings: 0  
  42.   
  43. mysql> select * from id_name;  
  44. +------+---------+------+---------+  
  45. | id   | name    | age  | address |  
  46. +------+---------+------+---------+  
  47. |    1 | qustdjx | NULL | NULL    |  
  48. +------+---------+------+---------+  
  49. 1 row in set (0.00 sec)  
  50. 5.新增欄位並插入  
  51. mysql> insert into id_name values(2,‘qust‘,14,‘山東‘);  
  52. Query OK, 1 row affected (0.00 sec)  
  53.   
  54. mysql> select * from id_name;  
  55. +------+---------+------+---------+  
  56. | id   | name    | age  | address |  
  57. +------+---------+------+---------+  
  58. |    1 | qustdjx | NULL | NULL    |  
  59. |    2 | qust    |   14 | 山東    |  
  60. +------+---------+------+---------+  
  61. 2 rows in set (0.00 sec)  
  62.   
  63.    


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;  //修改一個欄位的名稱,此時一定要重新指定該欄位的類型

 

MySQL中的表中增加刪除欄位

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.