Unix Technical Forum

how to add a primary key to a table

This is a discussion on how to add a primary key to a table within the MySQL forums, part of the Database Server Software category; --> show columns from score, where student_id is a foreign key to one table, and event_id a foreign key to ...


Go Back   Unix Technical Forum > Database Server Software > MySQL

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 02-28-2008, 10:29 AM
anne001
 
Posts: n/a
Default how to add a primary key to a table


show columns from score, where student_id is a foreign key to one
table, and event_id a foreign key to another.

+------------+------------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default |
Extra |
+------------+------------------+------+-----+---------+-------+
| student_id | int(10) unsigned | NO | PRI | |
|
| event_id | int(10) unsigned | NO | PRI | |
|
| score | int(11) | NO | | |
|
+------------+------------------+------+-----+---------+-------+


this blog gives the recipee, but it does not work for me
http://www.jroller.com/dschneller/en...primary_key_to

mysql> ALTER TABLE child
-> ADD COLUMN child_id BIGINT(20) AUTO_INCREMENT NOT
NULL FIRST,
-> ADD PRIMARY KEY(child_id);

alter table score
-> add column id bigint(2) auto_increment not null
first,
-> add primary key(id);
ERROR 1068 (42000): Multiple primary key defined

what do I need to do to add a primary key to this table?

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 02-28-2008, 10:29 AM
Paul Lautman
 
Posts: n/a
Default Re: how to add a primary key to a table

anne001 wrote:
> show columns from score, where student_id is a foreign key to one
> table, and event_id a foreign key to another.
>
> +------------+------------------+------+-----+---------+-------+
>> Field | Type | Null | Key | Default |

> Extra |
> +------------+------------------+------+-----+---------+-------+
>> student_id | int(10) unsigned | NO | PRI | |
>>
>> event_id | int(10) unsigned | NO | PRI | |
>>
>> score | int(11) | NO | | |
>>

> +------------+------------------+------+-----+---------+-------+
>
>
> this blog gives the recipee, but it does not work for me
> http://www.jroller.com/dschneller/en...primary_key_to
>
> mysql> ALTER TABLE child
> -> ADD COLUMN child_id BIGINT(20) AUTO_INCREMENT NOT
> NULL FIRST,
> -> ADD PRIMARY KEY(child_id);
>
> alter table score
> -> add column id bigint(2) auto_increment not null
> first,
> -> add primary key(id);
> ERROR 1068 (42000): Multiple primary key defined
>
> what do I need to do to add a primary key to this table?


The table already has a primary key. A table can have only one primary key.


Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 02-28-2008, 10:29 AM
anne001
 
Posts: n/a
Default Re: how to add a primary key to a table

How do I remove the primary key label on the foreign keys, so I can
add a single autoincrement primary key column?

alter table score drop primary key;
ERROR 1025 (HY000): Error on rename of './sampdb/#sql-185_9' to './
sampdb/score' (errno: 150)

I think it is because the table has two primary keys for some reason.


Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 02-28-2008, 10:29 AM
Jerry Stuckle
 
Posts: n/a
Default Re: how to add a primary key to a table

anne001 wrote:
> How do I remove the primary key label on the foreign keys, so I can
> add a single autoincrement primary key column?
>
> alter table score drop primary key;
> ERROR 1025 (HY000): Error on rename of './sampdb/#sql-185_9' to './
> sampdb/score' (errno: 150)
>
> I think it is because the table has two primary keys for some reason.
>
>
>


Why do you need an autoincrement value? student_id/event_id already is
unique; an autoincrement would be redundant.

The structure given is correct for a table such as this; only in very
rare circumstances should you need an autoincrememt value.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5 (permalink)  
Old 02-28-2008, 10:29 AM
anne001
 
Posts: n/a
Default Re: how to add a primary key to a table

> Why do you need an autoincrement value? student_id/event_id already is
> unique; an autoincrement would be redundant.


because for a join table, ruby on rails expects a single
autoincremental key, it cannot work with multiple keys.

I have copied the old table into a new table without the key type, but
it would be nice to know how to change the original table directly

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #6 (permalink)  
Old 02-28-2008, 10:29 AM
Jerry Stuckle
 
Posts: n/a
Default Re: how to add a primary key to a table

anne001 wrote:
>> Why do you need an autoincrement value? student_id/event_id already is
>> unique; an autoincrement would be redundant.

>
> because for a join table, ruby on rails expects a single
> autoincremental key, it cannot work with multiple keys.
>
> I have copied the old table into a new table without the key type, but
> it would be nice to know how to change the original table directly
>
>


Read up on the ALTER TABLE command.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump


All times are GMT. The time now is 09:56 PM.


Powered by vBulletin® Version 3.6.5
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
SEO by vBSEO 3.2.0
www.UnixAdminTalk.com