mysql add, modify, delete the field

First, add fields

1, a single field is added

format:

ALTER TABLE table name ADD COLUMN field name (name) field type length (VARCHAR (20)) encoding rules (CHARACTER SET utf8) character set rules (COLLATE utf8_general_ci) 
the default value (DEFAULT NULL or NOT NULL) Notes (the COMMENT 'name') Add to that column (aFTER id (fIRST: first column, bEFORE: before a field, aFTER: after a field))
ALTER TABLE user ADD COLUMN name VARCHAR(20) CHARACTER SET utf8 COLLATE utf8_general_ci DEFAULT NULL COMMENT '姓名' AFTER id;

2, a plurality of fields added

format:

  Add more fields similar format and a single field, just to add a field to write in brackets

ALTER TABLE user ADD(
  id VARCHAR ( 20 ) CHARACTER SET utf8 COLLATE utf8_general_ci DEFAULT NULL COMMENT 'id',
  name VARCHAR ( 20 ) CHARACTER SET utf8 COLLATE utf8_general_ci DEFAULT NULL COMMENT '姓名'
)

Note: When you add a field to add more fields can not be specified position, i.e. to the end of the table are the default field, because if you want to add more than one field, which specifies the name field is added to the front id, and performed at this time sql and did not submit the transaction, the database does not know where the id field, it will be error, can not specify the location.

Second, modify the field

1, modify the field Default / changed will be null is not empty

format:

MODIFY COLUMN ALTER TABLE table name field name VARCHAR (20) CHARACTER SET utf8 COLLATE utf8_general_ci the COMMENT Default 'Note';
ALTER TABLE user MODIFY COLUMN name VARCHAR(20) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL COMMENT '姓名';

2, modify the field name

format:

ALTER TABLE table name CHANGE COLUMN new original field name field name VARCHAR (20) CHARACTER SET utf8 COLLATE utf8_general_ci DEFAULT NULL COMMENT 'name';
ALTER TABLE user CHANGE COLUMN name login_name VARCHAR(20) CHARACTER SET utf8 COLLATE utf8_general_ci DEFAULT NULL COMMENT '名称';

Note: You can not use when you want to use to modify modify field names change, if not annotated and so the new fields would not comment, so try to write full time general modifications

3, modify the character set encoding rules

ALTER TABLE user MODIFY COLUMN name VARCHAR(20) CHARACTER SET utf8 COLLATE utf8_german2_ci DEFAULT NULL COMMENT '名称';

Third, delete the field

format:

ALTER TABLE table DROP the COLUMN field name
ALTER TABLE user DROP COLUMN name;

 

Guess you like

Origin www.cnblogs.com/fatTmonkey/p/11481697.html