当前位置:网站首页>MySQL adds, modifies, and deletes table fields, field data types, and lengths (with various actual case statements)
MySQL adds, modifies, and deletes table fields, field data types, and lengths (with various actual case statements)
2022-06-25 12:47:00 【good_ good_ xiu】
1、 New fields
notes :add It is often used to add a field to an existing table .
grammar :
alter table Table name add Field name Field data type ( length ) [character set Field code collate Field sort code ] [NOT NULL] [DEFAULT 0] [COMMENT ‘ remarks ’] [(FIRST)|(AFTER The field... Already exists in the table )]
among , Has not been [] The parentheses include what is required , By [] The parentheses include settings that can be customized according to the field limits .
- CHARACTER SET Field code COLLATE Field sort code : Used to set the encoding value of a special field . for example : If you want to store the string containing the expression in the table field , But the database code is utf-8, Then the expression will be garbled . At this point, you need to change the field code to utf8mb4.(alter table Table name add Field name Field data type ( length ) character set utf8mb4 collate utf8mb4_general_ci)
- NOT NULL: Represents that the field cannot be empty .
- DEFAULT value : Set the default value . It can be a number or a string .
- COMMENT ‘ remarks ’: Note information of this field .
- (FIRST)|(AFTER Fields in the table ): Create the field in the first column or after the field in the table .
2、 Modify field name
notes :change It is often used to modify field names , But with modify equally . You can also change the field type and length . And the modified field ,alter table There are no restrictions such as default values set in the statement , The limit of the original field will be reset . Here's an example name1 Fields do not inherit name Comments for the field 、 Default value and other information .
grammar :
alter table [ Table name ] change [ Field name ] [ Field new name ] [ Type of field ]
for example : take a Tabular name Renamed as name1
alter table a change name name1 varchar(20);
3、 Modify field type 、 length
notes :modify It is often used to modify the type and length of fields in a table .
grammar :
alter table [ Table name ] modify column [ Field name ] [ New data types ( Modified length )]
for example : modify a In the table name The type of field is varchar, The length is 20. Then change the length to 30.
alter table a modify column name varchar(20);
alter table a modify column name varchar(30)
4、 Delete field
grammar :
alter table Table name drop column Field name ;
for example : Delete a In the table name Field
alter table a drop column name;
other
If the table field is modified , Not in force . Execute the following statement to refresh the table .
analyze table Table name
Actual case
1、 Set the field as the primary key ( Not commonly used )
In general, in practice , The primary key is specified during table creation and will not be modified , But if it needs to be modified later , You need to delete the existing primary key and set a new primary key .
Deleting directly will result in an error .Multiple primary key defined
Or if the deleted primary key is a self growing column, an error will be reported , First remove the self growth attribute , Delete the primary key .
-- remove a surface id The self growth property of the column
alter table a modify id int(10) unsigned;
-- Delete id Primary key
alter table a drop primary key;
-- Set up name Primary key
alter table a add primary key(name);
-- Or set up id、name It's a composite primary key , Also called double primary key
alter table a add primary key(id,name);
2、 Add a non empty field in the table
-- surface a newly added name Field cannot be empty
alter table a add name varchar(20) NOT NULL;
3、 Add a table default value field
-- surface a newly added createyear The default value of the field is 2021
alter table a add createyear varchar(20) DEFAULT '2021';
-- additional : Delete field defaults
alter table a alter createyear drop default;
-- additional : Add default values to existing fields
alter table a alter createyear set default '2021';
4、 Modify table self increment
alter table a auto_increment = 1;
边栏推荐
- 地理空间搜索 ->R树索引
- 利用cmd(命令提示符)安装mysql&&配置环境
- Penetration tool environment - installing sqli labs in centos7 environment
- 为何数据库也云原生了?
- Event triggered when El select Clear clears content
- Embedded software development written examination and interview notes (latest update: February 17, 2022)
- 3+1保障:高可用系统稳定性是如何炼成的?
- JS SMS countdown implementation (simple code)
- Initialize the project using the express framework
- Laravel echart statistical chart line chart
猜你喜欢

QT TCP UDP network communication < theory >

MySQL common interview questions

The drop-down box renders numbers instead of the corresponding text. How to deal with it

20220620 面试复盘

Zhangxiaobai's way of penetration (VIII) - detailed operation steps of SQL injection - Boolean blind injection of blind injection

Guess Tongyuan B

(6) Pyqt5--- > window jump (registration login function)

线上服务应急攻关方法论

Singleton mode in PHP to reduce memory consumption
![Select randomly by weight [prefix and + dichotomy + random target]](/img/84/7f930f55f8006a4bf6e23ef05676ac.png)
Select randomly by weight [prefix and + dichotomy + random target]
随机推荐
Micro engine generates QR code
Disconnected: No supported authentication methods available (server sent: )
Singleton mode in PHP to reduce memory consumption
Laravel is not logged in and cannot access the background by entering the URL
Slice() and slice() methods of arrays in JS
C program linking SQLSERVER database: instance failed
Fun pocket mall -- sharing the development source code of fun pocket app system
线上服务应急攻关方法论
Go novice exploration road 1
Go from 0 to 1. Obtain the installation package, get, post request, params, body and other parameters
Penetration tool environment -- use of cknife Chinese kitchen knife
为何数据库也云原生了?
Huikan source code -- Huikan app system secondary development source code sharing
Ramda rejects objects with null and empty object values in the data
Swagger document generated by node project API in vscode
Zhangxiaobai's way of penetration (VIII) - detailed operation steps of SQL injection - Boolean blind injection of blind injection
The amount is verified, and two zeros are spliced by integers during echo
515. Find Largest Value in Each Tree Row
torch.tensor拼接与list(tensors)
Slice and slice methods of arrays in JS