PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > comp.databases.mysql > how to add a primary key to a table
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
how to add a primary key to a table

Réponse
 
LinkBack Outils de la discussion
Vieux 01/11/2007, 21h24   #1
anne001
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut 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?

  Réponse avec citation
Vieux 01/11/2007, 22h27   #2
Paul Lautman
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut 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.


  Réponse avec citation
Vieux 02/11/2007, 02h01   #3
anne001
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut 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.


  Réponse avec citation
Vieux 02/11/2007, 03h19   #4
Jerry Stuckle
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut 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
==================

  Réponse avec citation
Vieux 02/11/2007, 23h37   #5
anne001
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut 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

  Réponse avec citation
Vieux 03/11/2007, 02h39   #6
Jerry Stuckle
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut 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
==================

  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 11h20.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,15833 seconds with 14 queries