diff --git a/ddl/ddl_api.go b/ddl/ddl_api.go index 7ff671fa4f6ea..30cadd4a3a2d7 100644 --- a/ddl/ddl_api.go +++ b/ddl/ddl_api.go @@ -774,6 +774,7 @@ func columnDefToCol(ctx sessionctx.Context, offset int, colDef *ast.ColumnDef, o hasNullFlag = true case ast.ColumnOptionAutoIncrement: col.Flag |= mysql.AutoIncrementFlag + col.Flag |= mysql.NotNullFlag case ast.ColumnOptionPrimaryKey: // Check PriKeyFlag first to avoid extra duplicate constraints. if col.Flag&mysql.PriKeyFlag == 0 { diff --git a/ddl/serial_test.go b/ddl/serial_test.go index 26ca779dc3400..51dccd346b336 100644 --- a/ddl/serial_test.go +++ b/ddl/serial_test.go @@ -1652,6 +1652,7 @@ func (s *testIntegrationSuite7) TestInvisibleIndex(c *C) { // Implicit primary key cannot be invisible index // Create a implicit primary key tk.MustGetErrCode("create table t2(a int not null, unique (a) invisible)", errno.ErrPKIndexCantBeInvisible) + tk.MustGetErrCode("create table t2(a int auto_increment, unique key (a) invisible);", errno.ErrPKIndexCantBeInvisible) // Column `a` become implicit primary key after DDL statement on itself tk.MustExec("create table t2(a int not null)") tk.MustGetErrCode("alter table t2 add unique (a) invisible", errno.ErrPKIndexCantBeInvisible) diff --git a/executor/show.go b/executor/show.go index 7bb135d56230e..1922d3c067163 100644 --- a/executor/show.go +++ b/executor/show.go @@ -862,13 +862,13 @@ func ConstructResultOfShowCreateTable(ctx sessionctx.Context, tableInfo *model.T buf.WriteString(" VIRTUAL") } } + if mysql.HasNotNullFlag(col.Flag) { + buf.WriteString(" NOT NULL") + } if mysql.HasAutoIncrementFlag(col.Flag) { hasAutoIncID = true - buf.WriteString(" NOT NULL AUTO_INCREMENT") + buf.WriteString(" AUTO_INCREMENT") } else { - if mysql.HasNotNullFlag(col.Flag) { - buf.WriteString(" NOT NULL") - } // default values are not shown for generated columns in MySQL if !mysql.HasNoDefaultValueFlag(col.Flag) && !col.IsGenerated() { defaultValue := col.GetDefaultValue()