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

Implementation example of scan code payment in vue project (with demo)

Table of contents Demand background Thought Analy...

Implementation of two basic images for Docker deployment of Go

1. golang:latest base image mkdir gotest touch ma...

How to install and configure the supervisor daemon under centos7

Newbie, record it yourself 1. Install supervisor....

Web interview Vue custom components and calling methods

Import: Due to project requirements, we will enca...

Web front-end skills summary (personal practical experience)

1. Today, when I was making a page, I encountered ...

Solution to the failure of entering the container due to full docker space

Since the problem occurred rather suddenly and th...

React+Typescript implements countdown hook method

First, setInterval is encapsulated as a Hook 👇 im...

Talk about the 8 user instincts behind user experience in design

Editor's note: This article is contributed by...

Summary of MySQL injection bypass filtering techniques

First, let’s look at the GIF operation: Case 1: S...

WeChat applet date and time component (year, month, day, hour, and minute)

This article example shares the specific code of ...

Vue Getting Started with Weather Forecast

This article example shares the specific code of ...

React's component collaborative use implementation

Table of contents Nesting Parent-child component ...

Introduction to the use and advantages and disadvantages of MySQL triggers

Table of contents Preface 1. Trigger Overview 2. ...