228 Show New! Save questions or answers and organize your favorite content. I’m trying to modify a table to make its primary key column
Am I doing something wrong or is this not possible? +--------------------+ | VERSION() | +--------------------+ | 5.0.75-0ubuntu10.2 | +--------------------+
TRiG 9,8346 gold badges55 silver badges106 bronze badges asked Jan 30, 2010 at 19:16
answered Jan 30, 2010 at 19:19
romanroman 10.9k1 gold badge28 silver badges42 bronze badges 5 Roman is right, but note that the auto_increment column must be part of the PRIMARY KEY or a UNIQUE KEY (and in almost 100% of the cases, it should be the only column that makes up the PRIMARY KEY):
answered Jan 30, 2010 at 19:22
Roland BoumanRoland Bouman 30.4k6 gold badges65 silver badges67 bronze badges 2 In my case it only worked when I put
TRiG 9,8346 gold badges55 silver badges106 bronze badges answered Aug 11, 2015 at 4:02
The SQL to do this would be:
There are a couple of reasons that your SQL might not work. First, you must re-specify the data type (
You can find more information in the MySQL documentation: http://dev.mysql.com/doc/refman/5.1/en/alter-table.html for the modify column syntax and http://dev.mysql.com/doc/refman/5.1/en/create-table.html for more information about specifying columns. answered Jan 30, 2010 at 19:32
Steven OxleySteven Oxley 6,3876 gold badges41 silver badges54 bronze badges You must specify the type of the column before the
Unmitigated 49.9k7 gold badges49 silver badges65 bronze badges answered Jan 30, 2010 at 19:22
Håvard SHåvard S 22.5k6 gold badges59 silver badges71 bronze badges 1
( answered Jan 30, 2010 at 19:21
Dan SoapDan Soap 9,9841 gold badge40 silver badges49 bronze badges You can apply the atuto_increment constraint to the data column by the following query:
But, if the columns are part of a foreign key constraint you, will most probably receive an error. Therefore, it is advised to turn off foreign_key_checks by using the following query:
Therefore, use the following query instead:
answered Nov 24, 2020 at 8:29
You can do it like this:
Unmitigated 49.9k7 gold badges49 silver badges65 bronze badges answered Apr 12, 2018 at 6:50
1 You can use the following query to make document_id to increment automatically
It is preferred to make document_id primary key as well
answered Apr 22, 2018 at 5:22
Azhar ZafarAzhar Zafar 1,2849 silver badges13 bronze badges 1 Below statement works. Note that you need to mention the data type again for the column name (redeclare the data type the column was before).
Unmitigated 49.9k7 gold badges49 silver badges65 bronze badges answered Jul 5, 2015 at 17:30
(int taken as an example, you should set it to the type the column had before)
fedorqui 260k99 gold badges527 silver badges578 bronze badges answered Dec 7, 2014 at 17:39
If none of the above works try this. This is what I did in MYSQL and yes, you need to write the column name (document_id) twice.
Unmitigated 49.9k7 gold badges49 silver badges65 bronze badges answered Dec 19, 2014 at 21:06
SusieSusie 5,00010 gold badges51 silver badges74 bronze badges Setting column as primary key and auto_increment at the same time:
answered Aug 21, 2018 at 12:25
To modify the column in mysql we use alter and modify keywords. Suppose we have created a table like:
Now we want to modify type of the column id to integer with auto increment. You could do this with a command like:
solarissmoke 28.6k13 gold badges65 silver badges70 bronze badges answered Jul 16, 2016 at 6:07 Previous Table syntax:
For changing the TransactionId to auto increment use this query
Rob♦ 26.7k15 gold badges81 silver badges94 bronze badges answered Aug 18, 2017 at 13:47
Pang 9,222146 gold badges85 silver badges118 bronze badges answered Feb 24, 2017 at 19:12
Aminur RahmanAminur Rahman 3801 gold badge4 silver badges13 bronze badges Just like this:
answered Jul 9, 2018 at 18:54
As you are redefining the column again, you have to specify the datatype again and add auto_increment to it as it's a part of datatype.
answered Dec 8, 2017 at 12:52
OptimizerOptimizer 6477 silver badges7 bronze badges Try the following:
xKobalt 1,4982 gold badges12 silver badges19 bronze badges answered Mar 13, 2019 at 6:16
Since
In this case you can either add new column as
ORFill the existing null index with incremental numbers using this method,
answered Jan 7, 2021 at 7:55
Use the following queries:
answered Jun 19, 2017 at 17:44
How do I make a column auto increment in MySQL?In MySQL, the syntax to change the starting value for an AUTO_INCREMENT column using the ALTER TABLE statement is: ALTER TABLE table_name AUTO_INCREMENT = start_value; table_name.
Where is auto increment in SQL?The MS SQL Server uses the IDENTITY keyword to perform an auto-increment feature. In the example above, the starting value for IDENTITY is 1, and it will increment by 1 for each new record. Tip: To specify that the "Personid" column should start at value 10 and increment by 5, change it to IDENTITY(10,5) .
How do I add auto increment to a column?Here's the SQL statement to add AUTO INCREMENT constraint to id column. ALTER TABLE sales MODIFY id INT NOT NULL AUTO_INCREMENT PRIMARY KEY; Next we will add a couple of rows in sales table. As you can see, the MySQL has automatically increased and populated id column with values 7 and 8.
What is auto increment in MySQL?Auto-increment allows a unique number to be generated automatically when a new record is inserted into a table. Often this is the primary key field that we would like to be created automatically every time a new record is inserted.
|