相当于 MySQL 中的 MSSQL IDENTITY 列

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10283780/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 13:04:32  来源:igfitidea点击:

Equivalent of MSSQL IDENTITY Column in MySQL

mysqlsql-servertsqlidentity

提问by Allan Chua

What is the equivalent of MSSQL IDENTITYColumns in MySQL? How would I create this table in MySQL?

IDENTITYMySQL中 MSSQL列的等价物是什么?我将如何在 MySQL 中创建这个表?

CREATE TABLE Lookups.Gender
(
    GenderID   INT         IDENTITY(1,1) NOT NULL,
    GenderName VARCHAR(32) NOT NULL
);

回答by Joe Stefanelli

CREATE TABLE Lookups.Gender
(
    GenderID   INT         NOT NULL AUTO_INCREMENT,
    GenderName VARCHAR(32) NOT NULL
);

回答by MojganK

CREATE TABLE `Persons` (
  `ID` int(11) NOT NULL AUTO_INCREMENT,
  `LastName` varchar(255) NOT NULL,
  `FirstName` varchar(255) DEFAULT NULL,
  `Address` varchar(255) DEFAULT NULL,
  `City` varchar(255) DEFAULT NULL,
  PRIMARY KEY (`ID`)
) ENGINE=InnoDB AUTO_INCREMENT=100 DEFAULT CHARSET=latin1;

This above example uses the AUTO_INCREMENTsyntax. You can specify a starting offset specific to the table.

上面的示例使用了AUTO_INCREMENT语法。您可以指定特定于表的起始偏移量。

The Increment, however, has to be set globally.

递增,但是,必须在全球范围内设置。

SET @@auto_increment_increment=10;

SET @@auto_increment_increment=10;

You can also set a global default for the offset like follows:

您还可以为偏移量设置全局默认值,如下所示:

SET @@auto_increment_offset=5;

SET @@auto_increment_offset=5;

To view your current values, type SHOW VARIABLES LIKE 'auto_inc%';

要查看当前值,请键入 SHOW VARIABLES LIKE 'auto_inc%';