Add a Delete field to a table in MySQL

Source: Internet
Author: User

1 Increase of two fields:

[SQL]View Plaincopy
  1. mysql> Create table id_name (id int,name varchar (20));
  2. Query OK, 0 rows affected (0.13 sec)
  3. mysql> alter table Id_name add age int,add address varchar (11);
  4. Query OK, 0 rows affected (0.13 sec)
  5. records:0 duplicates:0 warnings:0
  6. mysql> desc id_name;
  7. +---------+-------------+------+-----+---------+-------+  
  8. | Field | Type | Null | Key | Default |  Extra |
  9. +---------+-------------+------+-----+---------+-------+  
  10. | ID | Int (11) |     YES | |       NULL |  |
  11. | name | varchar (20) |     YES | |       NULL |  |
  12. | Age | Int (11) |     YES | |       NULL |  |
  13. | Address | varchar (11) |     YES | |       NULL |  |
  14. +---------+-------------+------+-----+---------+-------+  
  15. 4 rows in Set (0.00 sec)
  16. 2. Delete two fields
  17. mysql> alter table id_name drop column age,drop column address;
  18. Query OK, 0 rows affected (0.14 sec)
  19. records:0 duplicates:0 warnings:0
  20. mysql> desc id_name;
  21. +-------+-------------+------+-----+---------+-------+  
  22. | Field | Type | Null | Key | Default |  Extra |
  23. +-------+-------------+------+-----+---------+-------+  
  24. | ID | Int (11) |     YES | |       NULL |  |
  25. | name | varchar (20) |     YES | |       NULL |  |
  26. +-------+-------------+------+-----+---------+-------+  
  27. 2 rows in Set (0.00 sec)
  28. 3. Insert
  29. mysql> INSERT INTO id_name values (1,' QUSTDJX ');
  30. Query OK, 1 row Affected (0.00 sec)
  31. 4. See for yourself
  32. mysql> alter table Id_name add age int,add address varchar (11);
  33. Query OK, 1 row affected (0.07 sec)
  34. Records:1 duplicates:0 warnings:0
  35. Mysql> SELECT * from id_name;
  36. +------+---------+------+---------+  
  37. | ID |  Name | age | address |
  38. +------+---------+------+---------+  
  39. | 1 | QUSTDJX | NULL |  NULL |
  40. +------+---------+------+---------+  
  41. 1 row in Set (0.00 sec)
  42. 5. Add fields and insert
  43. mysql> INSERT INTO id_name values (2,' qust ', +,' Shandong ');
  44. Query OK, 1 row Affected (0.00 sec)
  45. Mysql> SELECT * from id_name;
  46. +------+---------+------+---------+  
  47. | ID |  Name | age | address |
  48. +------+---------+------+---------+  
  49. | 1 | QUSTDJX | NULL |  NULL |
  50. | 2 |   Qust | 14 | Shandong |
  51. +------+---------+------+---------+  
  52. 2 rows in Set (0.00 sec)


1. Add a field
ALTER TABLE user add COLUMN new1 VARCHAR () DEFAULT NULL; Add a field, default is empty
ALTER TABLE user add COLUMN new2 VARCHAR (a) not NULL; Add a field that cannot be empty by default www.2cto.com

2. Delete a field
ALTER TABLE user DROP COLUMN new2; Delete a field

3. Modify a Field
ALTER TABLE user MODIFY New1 VARCHAR (10); Modify the type of a field

ALTER TABLE user change new1 new4 int; To modify the name of a field, be sure to re-specify the field's type

Add a Delete field to a table in MySQL

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.