Home  >  Article  >  Database  >  How to add fields in mysql

How to add fields in mysql

(*-*)浩
(*-*)浩Original
2019-05-08 14:02:5220087browse

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

How to add fields in mysql

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,

How to add fields in mysql

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn