Domanda

i created a flight class and here is the description of it.

  mysql> desc flight_class;

 +----------+--------------+------+-----+---------+-------+
 | Field    | Type         | Null | Key | Default | Extra |
 +----------+--------------+------+-----+---------+-------+
 | FID      | varchar(5)   | YES  | MUL | NULL    |       |
 | amount   | decimal(7,2) | YES  |     | NULL    |       |
 | no_seats | decimal(2,0) | YES  |     | NULL    |       |
 | class_id | int(11)      | NO   | PRI | 0       |       |
 +----------+--------------+------+-----+---------+-------+

then i want to make the class_id to a foreign key.

to drop the primary key i said

 mysql> alter table flight_class drop primary key;
ERROR 1025 (HY000): Error on rename of '.\flysafe\#sql-76c_1' to '.\flysafe\flight_class' (errno: 150)

i am a newbie to this. can any one tell me where i went wrong. or is it other tables that are affecting this table? please do give some resource for learning these right.

thanks anirudh.

È stato utile?

Soluzione

Without an index, maintaining an autoincrement column becomes too expensive, that's why MySQL requires an autoincrement column to be a leftmost part of an index.

You should remove the autoincrement property before dropping the key:

ALTER TABLE flight_class MODIFY id INT NOT NULL;
ALTER TABLE flight_class DROP PRIMARY KEY;

Note that you have a composite PRIMARY KEY which covers all three columns and id is not guaranteed to be unique.

If it happens to be unique, you can make it to be a PRIMARY KEY and AUTO_INCREMENT again:

ALTER TABLE flight_class MODIFY id INT NOT NULL PRIMARY KEY AUTO_INCREMENT;
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top