How to add, delete and modify columns in MySQL database

How to add, delete and modify columns in MySQL database

This article uses an example to describe how to add, delete, and modify columns in a MySQL database. Share with you for your reference, the details are as follows:

Create a new table user_info:

CREATE TABLE user_info(
id INT NOT NULL PRIMARY KEY AUTO_INCREMENT,
username CHAR(20) NOT NULL DEFAULT '',
gender TINYINT UNSIGNED NOT NULL DEFAULT 0,
weight TINYINT UNSIGNED NOT NULL DEFAULT 0
)ENGINE=MyISAM DEFAULT CHARSET=utf8;

The new column is the last column of the table by default.

Syntax: alter table table name add column name column type column attribute

alter table user_info add height tinyint unsigned not null default 0;

Deleting a column

Syntax: alter table table name drop column name

alter table user_info drop height;

Add a column and put it after the specified column

Syntax: alter table table name add column name type attribute [default value] after specify column name

alter table user_info add height tinyint not null default 0 after username;

Modify the specified column name

Syntax: alter table table name change old column name new column name type attribute default value

alter table user_info change height shengao smallint not null default 0;

modify Modify the column, but cannot modify the column name

Syntax: alter table table name modify column name type attribute default value

alter table user_info modify shengao tinyint not null default 0;

Readers who are interested in more MySQL-related content can check out the following topics: "Summary of MySQL Common Functions", "Summary of MySQL Log Operation Skills", "Summary of MySQL Transaction Operation Skills", "Summary of MySQL Stored Procedure Skills" and "Summary of MySQL Database Lock-Related Skills".

I hope this article will be helpful to everyone's MySQL database design.

You may also be interested in:
  • The difference between MySQL database stored procedures and transactions
  • The meaning and calculation method of QPS and TPS of MySQL database
  • How to backup and restore the mysql database if it is too large
  • PHP backend source code example for backing up MySQL database
  • How to use shell scripts to automatically back up multiple MySQL databases every day
  • Introduction to using mysqli's prepare to operate the database in PHP5
  • MySQL database migration quickly exports and imports large amounts of data
  • Shell script to operate MySQL database to delete duplicate data
  • Combining insert and select to implement the method of "inserting the maximum value of a field in the database + 1"
  • Talk about some experience in database field design

<<:  Implementation process of nginx high availability cluster

>>:  Real-time refresh of long connection on Vue+WebSocket page

Recommend

Nofollow makes the links in comments and messages really work

Comments and messages were originally a great way...

In-depth understanding of Vue's plug-in mechanism and installation details

Preface: When we use Vue, we often use and write ...

Echarts Basic Introduction: General Configuration of Bar Chart and Line Chart

1Basic steps of echarts Four Steps 1 Find the DOM...

PNG Alpha Transparency in IE6 (Complete Collection)

Many people say that IE6 does not support PNG tra...

MySQL data duplicate checking and deduplication implementation statements

There is a table user, and the fields are id, nic...

js dynamically adds example code for a list of circled numbers

1. Add the ul tag in the body first <!-- Unord...

MySQL cursor principle and usage example analysis

This article uses examples to explain the princip...

Docker installation and configuration steps for MySQL

Table of contents Preface environment Install Cre...

Detailed tutorial on installing Docker and docker-compose suite on Windows

Table of contents Introduction Download and insta...

How to solve the problem of character set when logging in to Linux

Character set error always exists locale: Cannot ...

Example of how to optimize MySQL insert performance

MySQL Performance Optimization MySQL performance ...