当前位置:网站首页>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;
边栏推荐
- PHP parsing QR code content
- Slice and slice methods of arrays in JS
- 初识CANOpen
- Talk about 11 key techniques of high availability
- PPT绘图之AI助力论文图
- Draw the satellite sky map according to the azimuth and elevation of the satellite (QT Implementation)
- 2021-09-28
- PHP replaces the key of a two-dimensional array with a specified element value
- 阿里稳定性之故障应急处理流程
- Go from 0 to 1. Obtain the installation package, get, post request, params, body and other parameters
猜你喜欢

线上服务应急攻关方法论

微信全文搜索技术优化

Jeecgboot startup popup configuration is still incorrect

Draw the satellite sky map according to the azimuth and elevation of the satellite (QT Implementation)

C program linking SQLSERVER database: instance failed

(7) Pyqt5 tutorial -- > > window properties and basic controls (continuous update)

3+1 guarantee: how is the stability of the highly available system refined?

2021-09-28

Idea2017 how to set not to automatically open a project at startup

The amount is verified, and two zeros are spliced by integers during echo
随机推荐
Some fields are ignored in tp6 query
Go novice exploration road 1
架构师需要具备的能力
英语口语 - 连读
地理空间搜索 ->R树索引
Elemtnui select control combined with tree control to realize user-defined search method
Zhangxiaobai's road of penetration (IV) -- detailed explanation of XSS cross site script vulnerabilities
架构师必备的七种能力
MySQL and excel tables importing database data (Excel for MySQL)
20220620 面试复盘
Match regular with fixed format beginning and fixed end
Navicat premium view password scheme
Mind mapping video
JS uses the for loop in the function to insert and delete the array at the specified position
JS enter three integers a, B and C, and sort them from large to small (two methods)
The drop-down box renders numbers instead of the corresponding text. How to deal with it
Array reorder based on a field
Concise H5 error page
JS array de duplication
C program linking SQLSERVER database: instance failed