473,395 Members | 1,442 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Alter a primary key auto increment column?

I need to change my primary key column type from smallint to int.
I have tried:
ALTER TABLE livegroup MODIFY id INT UNSIGNED NOT NULL AUTO_INCREMENT;
But get an error message certainly since my id-column is primary key
and references other tables as well.

How can I come around this problem?
Need help

/Martin

This is my table definition

livegroup (
id SMALLINT UNSIGNED NOT NULL AUTO_INCREMENT,
type VARCHAR(60) NOT NULL,
name CHAR(60) NOT NULL,
public TINYINT NOT NULL,
creator SMALLINT UNSIGNED NOT NULL,
lastmodified TIMESTAMP(8),
PRIMARY KEY (id),
INDEX (creator),
FOREIGN KEY (creator) REFERENCES user (id) ON DELETE CASCADE
) TYPE=INNODB;

livegroups (
data_id SMALLINT UNSIGNED NOT NULL,
livegroup_id SMALLINT UNSIGNED NOT NULL,
PRIMARY KEY (data_id, livegroup_id),
INDEX (data_id),
INDEX (livegroup_id),
FOREIGN KEY (data_id) REFERENCES livedata (id) ON DELETE CASCADE,
FOREIGN KEY (livegroup_id) REFERENCES livegroup (id) ON DELETE
CASCADE
) TYPE=INNODB;
Jul 20 '05 #1
1 28280
"Lannsjo" <la*****@home.se> wrote in message
I need to change my primary key column type from smallint to int.
I have tried:
ALTER TABLE livegroup MODIFY id INT UNSIGNED NOT NULL AUTO_INCREMENT;
But get an error message certainly since my id-column is primary key
and references other tables as well.
Just thinking out aloud, what if you alter the table that uses the id-column
as a foreign key? Here are the steps I'd try:

(1) alter table livegroups and make livegroups.livegroup_id an int.

If that doesn't work

(1a) drop the foreign key relationship on livegroups.livegroup_id =
livegroup.id.
(1b) alter table livegroups and make livegroups.livegroup_id an int.

If that doesn't work

(1a) drop the foreign key relationship on livegroups.livegroup_id =
livegroup.id.
(1b) drop the index on livegroup_id.
(1c) alter table livegroups and make livegroups.livegroup_id an int.

(2) Similarly, alter table livegroup to make livegroup.id an int. Try the
version where you just change the column, if that doesn't work drop the
primary key then alter the column.

(3) In the base table livegroup add back the index if you had to drop it.

(4) In the dependent tables add back the index and foreign key relatinoship
if you had to drop it.
ALTER TABLE livegroup MODIFY id INT UNSIGNED NOT NULL AUTO_INCREMENT; How can I come around this problem?
Need help

/Martin

This is my table definition

livegroup (
id SMALLINT UNSIGNED NOT NULL AUTO_INCREMENT,
type VARCHAR(60) NOT NULL,
name CHAR(60) NOT NULL,
public TINYINT NOT NULL,
creator SMALLINT UNSIGNED NOT NULL,
lastmodified TIMESTAMP(8),
PRIMARY KEY (id),
INDEX (creator),
FOREIGN KEY (creator) REFERENCES user (id) ON DELETE CASCADE
) TYPE=INNODB;

livegroups (
data_id SMALLINT UNSIGNED NOT NULL,
livegroup_id SMALLINT UNSIGNED NOT NULL,
PRIMARY KEY (data_id, livegroup_id),
INDEX (data_id),
INDEX (livegroup_id),
FOREIGN KEY (data_id) REFERENCES livedata (id) ON DELETE CASCADE,
FOREIGN KEY (livegroup_id) REFERENCES livegroup (id) ON DELETE
CASCADE
) TYPE=INNODB;

Jul 20 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Eric | last post by:
Let me start by saying that I'm still very new at PHP/MySql and will take any advice. :-) The issues is that every once in a while users get an error when they use this code. I believe the...
0
by: Nick | last post by:
Is it possible to have multiple auto increments in one column? Say I have two tables... 1. table 'messageboards' with fields ('id' , 'name') 2. table 'messagethreads' with fields ('id' ,...
1
by: Lannsjo | last post by:
I need to change my primary key column type from smallint to int. I have tried: ALTER TABLE livegroup MODIFY id INT UNSIGNED NOT NULL AUTO_INCREMENT; But get an error message certainly since my...
2
by: me | last post by:
I would like to add an Identity to an existing column in a table using a stored procedure then add records to the table and then remove the identity after the records have been added or something...
3
by: Metal Dave | last post by:
Hello, A script we run against the database as part of the upgrade of our product is failing with the following message: ALTER TABLE ALTER COLUMN EncodedID failed because STATISTICS hind_61_3...
2
by: RamaKrishna Narla | last post by:
In MS SQL Server, I have the following tables with some data in it. create table table1 ( column1 varchar(32), column2 int not null, column10 varchar(255), ..... primary key (column1,...
11
by: Greg P | last post by:
I'm using VB 2005, and have drug and dropped datagridviews onto my form. The default code that is generated for inserts tries to insert a value for the auto generated primary key. Below is the...
2
by: alexs | last post by:
Chaps, I'm starting to play with db2 V9.1 and am writing a stored procedure to manage accounting records from oiur RADIUS server. I've got an XML aware table with an auto increment primary...
2
by: musicbizguy | last post by:
Hello everyone. I'm a noob to MySql, so I'm hoping this is easy and I apologise in advance if I've got the terminology all wrong; I've searched the forum but couldn't find an answer. I have a...
5
by: giandeo | last post by:
Hello Dr. B Your solution is amazing. It works...........Wowh. Thank you so much. I still have one problem. I am using the autonumber of the access database as primary key. When I imported the...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.