Many of my friends in mysql think that fields with an AUTO_INCREMENT type ID cannot be modified, but that’s actually a mistake. Here’s how to change and set the starting value of an autoincrement ID in mysql.
general method of setting autoincrement field:
general method of setting autoincrement field:
add:
when creating table
create table table1(id int auto_increment primary key,...)
after creating the table add:
alter table table1 add id int auto_increment primary key 自增字段,一定要设置为primary key.
many times we hope that the id of the data in the table does not start from 1, like qq, id starts from 10000
code is as follows:
add:
when creating table
CREATE TABLE `orders` (
`order_num` int(11) NOT NULL auto_increment,
`order_date` datetime NOT NULL,
`cust_id` int(11) NOT NULL,
PRIMARY KEY (`order_num`),
KEY `fk_orders_customers` (`cust_id`),
CONSTRAINT `fk_orders_customers` FOREIGN KEY (`cust_id`) REFERENCES `customers` (`cust_id`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB AUTO_INCREMENT=10000 DEFAULT CHARSET=utf8;
create table add:
alter table users AUTO_INCREMENT=10000;
and this statement is also applicable to modify the id of the existing table, such as after a large number of data deletion, id from 654321 back to 123456
alter table users AUTO_INCREMENT=123456;
p>
div>
Read More:
- MySQL creates tables and sets auto increment of primary keys
- Failed to read auto-increment value from the storage engine in MySQL
- [reprint and save] MySQL does not set the primary key and uses the self growing ID method
- Optimization method of vs2017 starting speed
- There is a solution to the problem: severity = corrected, type = physical layer, id = 00e5 or id = 00e8 (receiver ID) under Ubuntu
- Export and import method of MySQL under Linux
- Android studio auto import auto import
- MySQL master-slave replication. An error is reported when starting slave. Slave failed to initialize relay log info structure from the repository
- The compilation error appears with lvalue required as Increment operand
- Resolve the problem of “event ID 4107” or “event ID 11” errors recorded in the application logs of windows and windows server
- MySQL driver compiling method of QT under windows and solutions to abnormal errors
- New fields in hive table and modification of field comments
- A convenient method to count the total number of query results in MySQL
- Common errors and modification methods of findbug
- phpstudy Error while setting value ‘STRICT_TRANS_TABLES, NO_AUTO_CREATE_USER, NO_ENGINE_SUBSTITUTION
- Android EditText cursor control, color modification, show and hide
- Mysql error 1452 – Cannot add or update a child row: a foreign key constraint fails
- R language – path setting and working directory modification
- Idle line number setting and linenumbers modification
- Modification scheme of binary files in dot matrix font library