Home >Database >Mysql Tutorial >How to add fields in mysql
When writing sql statements in MySQL, it is very important to add fields. For example: when the created table is not enough to meet the requirements, then you need to add fields and the requirements have been met.
Recommended courses: MySQL tutorial
Mysql method of adding fields:
First, take the table created below as an example:
CREATE TABLE IF NOT EXISTS `user`( `id` INT UNSIGNED AUTO_INCREMENT, `phone` CHAR(11) NOT NULL DEFAULT '', PRIMARY KEY ( `id` ) )ENGINE=InnoDB DEFAULT CHARSET=utf8;
Before, only the mobile phone number was stored, but now the information needs to be more complete, and the name and creation time are also required.
At this time, you need to use the method of adding fields.
ALTER TABLE `user` ADD `username` varchar(20) NULL DEFAULT '' COMMENT '用户名' AFTER `phone`, ADD `create_at` datetime NULL COMMENT '创建时间' AFTER `username`;
AFTER means to add it after a certain field, if not, add it at the end
When creating a table and referring to other people's tables, it is particularly time-saving to use a template, such as Use Navicat Premium tool to view other people's table information,
The above is the detailed content of How to add fields in mysql. For more information, please follow other related articles on the PHP Chinese website!