When I added a primary key to a Mysql table, I used the following SQL statement:
- Alter Table'Table name'Add Column'Column name'Int Not NullAuto_increment comment'Primary key'Before 'a column name ';
The following two errors are returned:
- Error Code: 1064
- You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'before column name' at line 1
- (0 MS taken)
This error indicates a syntax error near before.
- Error Code: 1075
- Incorrect table definition; there can be only one auto column and it must be defined as a key
- (0 MS taken)
This error indicates that only the primary key can specify auto-increment columns.
I found the reason on the internet, saying that I want to add a column before modifying the column. I think this is quite troublesome. I tried it myself and I can do it in one sentence:
- Alter Table'Table name'Add Column'Column name'Int Not NullAuto_incrementPrimary KeyComment'Primary key' First;
The preceding statement adds an Automatically increasing primary key field to a table and places the field in the first column.
If you want to put it behind other columns, you canFirstChangeAfter 'existing column name'