Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the sql statement that modifies the table structure

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly explains the "modify the table structure of the sql statement is", the article explains the content is simple and clear, easy to learn and understand, the following please follow the editor's ideas slowly in depth, together to study and learn "modify the table structure of the sql statement is" it!

In mysql, you can use the "ALTER TABLE" statement to modify the table structure, simply by using the "ALTER TABLE table name add/drop/alter field" statement. The "ALTER TABLE" statement is used to add, modify, or delete columns from an existing table.

The operating environment of this tutorial: windows7 system, mysql8 version, Dell G3 computer.

ALTER TABLE statement

The ALTER TABLE statement is used to add, modify, or delete columns from an existing table.

SQL ALTER TABLE syntax

To add columns to the table, use the following syntax:

ALTER TABLE table_nameADD column_name datatype

To delete a column from a table, use the following syntax:

ALTER TABLE table_name DROP COLUMN column_name

Note: some database systems do not allow this way of deleting columns from database tables (DROP COLUMN column_name).

To change the data type of a column in a table, use the following syntax:

ALTER TABLE table_nameALTER COLUMN column_name datatype

Example:

CREATE TABLE `login_ user` (`id` int (32) NOT NULL AUTO_INCREMENT, `name` varchar (225) CHARACTER SET utf8 COLLATE utf8_general_ci DEFAULT NULL COMMENT 'name', `password` varchar (26) DEFAULT NULL COMMENT 'password 3x, `type` varchar (32) DEFAULT NULL, `state`varchar (32) DEFAULT NULL, `create_ time` datetime DEFAULT NULL, `update_ time` datetime DEFAULT NULL, `password5` DEFAULT NULL COMMENT' password 5mm, PRIMARY KEY (`id`) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8

1. Modify fields: generally modify properties and data types

Alter table login_user modify password varchar (25) DEFAULT NULL COMMENT 'password 2'

two。 Rename field: alter table table name change old field new field data type [attribute] [location]

Alter table login_user change password2 password varchar (26) DEFAULT NULL COMMENT 'password 3'

3. New field: alter table table name add [column] field name data type [column attribute] [location]

Location: fields can be stored anywhere in the table

First: first position

After: after which field; by default, after the last field.

-- add to the end

Alter table login_user add password3 varchar (26) DEFAULT NULL COMMENT 'password 4'

-- add to the specified field after alter table + table name + add + field type to be added + after + field name to follow

Alter table login_user add password6 varchar (26) DEFAULT NULL COMMENT 'password 6' after password

4. Delete field: alter table table name drop field name

Alter table login_user drop password5 thank you for your reading, the above is "modify the table structure of the sql statement is" the content, after the study of this article, I believe that you modify the table structure of the sql statement is this problem has a deeper understanding, the specific use of the situation also need to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report