Get the App
SLTechnology News&Howtos  ›  Database  › 

Mysql self-increment and uniqueness constraint

Shulou Source: shulou.com Published: 2022-06-01 03:20:26 10月04日 Update

When a data table has been deleted many times, its self-increment column will be very chaotic and unattractive, so we need to reorder it so that it starts from 1 again.

How to do this: delete the original id field in the data table, and then re-establish the id field

Delete the id field:

Alter table bumen drop id

Rebuild Field:

ALTER TABLE bumen ADD id INT UNSIGNED AUTO_INCREMENT NOT NULL PRIMARY KEY FIRST

-- INT UNSIGNED unsigned integers (pure numbers). FIRST is inserted at the beginning of the table and at the end of the table by default.

-

Custom value-added

The value-added setting starts from 50:

Create table bumen (id int not null primary key auto_increment) auto_increment=50,charset=gbk;-specifies that the language name should be placed outside the parentheses of the field

The modification starts with the increment of 100:

ALTER TABLE bumen AUTO_INCREMENT = 100;-- change the table after it is created

-

Create a department table with three fields: id serial number, department name, and manager:

CREATE TABLE bu5 (

Id INT (5) UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY

NAME CHAR (10) NOT NULL UNIQUE

Manger VARCHAR (20) NOT NULL) charset=gbk

-- id pure digit 5-digit, non-null, self-growing, primary key; name 10-bit, non-null, uniqueness constraint, manger adaptive 20-bit, non-null

Add a unique harness to the field:

ALTER TABLE bu5 CHANGENAME NAME CHAR (10) UNIQUE;-redefines the name field

-

Tags: Fields value-added uniqueness numbers data datasheets departments chaos beauty three names attributes serial numbers parentheses integers methods symbols managers harnesses growth Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno macOS NVidia Huawei MySQL Shulou Tech Info