相当于MySQL中的MSSQL IDENTITY列

时间:2022-01-29 07:38:36

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

MySQL中MSSQL IDENTITY列的等价物是什么?我如何在MySQL中创建此表?

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

2 个解决方案

#1


43  

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

#2


5  

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_INCREMENT syntax. 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%';

#1


43  

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

#2


5  

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_INCREMENT syntax. 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%';