mysql modify field, character set, alter table optimization

Add one field at a time:
ALTER TABLE table name ADD COLUMN field name VARCHAR(20) NULL COMMENT 'newly added field' AFTER `previous field name`;

Comment means a comment;
After `previous field name` can be omitted, the default is to add a new field to the last column of the table, if you want to put a field after a field, you can add after `previous field name` to specify;

once Add multiple fields:
ALTER TABLE table name add(
Field 1 TINYINT NULL COMMENT 'xxxx ',
Field 2 TINYINT NULL COMMENT 'yyyy'
);




Delete one field at a time:
ALTER TABLE table name DROP COLUMN field name;



Delete multiple fields at once:
ALTER TABLE table name DROP COLUMN field 1, DROP COLUMN field 2;




Modify field name:
ALTER TABLE table name CHANGE old field name new field name varchar(10) NULL COMMENT 'comment text';



Modify the type of the field:
ALTER TABLE table name MODIFY COLUMN field name INT NULL COMMENT 'comment text';





Modify the character set of a single field:
ALTER TABLE table name MODIFY COLUMN field name VARCHAR(80) CHARACTER SET utf8mb4 NOT NULL COMMENT 'comment text';



Modify the character set of all fields (only valid for character type fields, will not modify non-character fields such as int):
ALTER TABLE table name CONVERT TO CHARACTER SET utf8mb4;



Modify the default character set:
ALTER TABLE table name EFAULT CHARACTER SET gbk;


The change is DEFAULT CHARSET=gbk; after ENGINE=InnoDB below;
CREATE TABLE `zzz` (
  `names` varchar(10) CHARACTER SET utf8 DEFAULT NULL,
  `kecheng` varchar(80) CHARACTER SET utf8mb4 NOT NULL COMMENT 'course',
  `score` int(11) DEFAULT NULL COMMENT 'score'
) ENGINE=InnoDB DEFAULT CHARSET=gbk;





Alter table optimization:
Alter table should be careful, mysql internally builds a new table according to the new table structure, then inserts the data in the old table into the new table, then deletes the old table, and then renames the new table to the old table name. If there is a lot of data in the table, the alter process will be very long. The
following three situations can be optimized (to avoid table reconstruction):
(1) Change the default value of the field
(2) Add/delete the AUTO_INCREMENT attribute of the field
(3) Add/delete /Modify the constant value of ENUM. For the delete operation, if there is a field that references this constant value, the structure of the query after deletion is an empty string.

For the above three cases, there are two ways to avoid table reconstruction:
assuming the current table name is users, the field The default value of gender is changed from 0 to 1. The two methods are as follows:
Method 1:
ALTER  TABLE s_user ALTER COLUMN gender SET DEFAULT 1;


instead of using
ALTER  TABLE s_user MODIFY COLUMN gender TINYINT NULL DEFAULT 1;



The first sql will not cause table reconstruction, while the second sql will cause table reconstruction.

Method 2:
Because these 3 parts of data are stored in the .frm of the table (there are three types of myisam engine structure files: .frm: table structure file.MYD : table data file. MYI: table index, innodb has two types: . frm: table structure file, .ibd: data and index files are the same. MyISAM index file and data file are separated, and the index file only saves data records Address. InnoDB is a clustered index, that is, the index file node contains complete data records) file, you can replace the .frm file of the old table with a new .frm file to achieve the purpose of modification, which has been tested and available.
Create  table  users_new  like users; -- 建新表


Alter table users_new alter column gender SET DEFAULT 1; -- change the default value of gender for the new table


Then go to the data directory of mysql, rename users.frm to users.frm.bak, copy users_new.frm and rename it to users.frm, and restart mysql to take effect.








Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326306430&siteId=291194637