Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Deleting a foreign key

Status
Not open for further replies.

OGHS

Programmer
Jun 21, 2001
6
CO
Hi. I was wondering if some of you know how to delete a foreign key on MySQL 4.0.5 (InnoDB), I tried the SQL92 syntax but it doesn`t works (I can not define a name to the foreign key when I create it, but I can give a constraint name to it, what it is useful for?)

MySQL syntax to create a FOREIGN KEY from an ALTER TABLE:
Code:
ALTER TABLE table_name ADD [CONSTRAINT name] FOREIGN KEY (col, ...) REFERENCES anotherTable(col1, ...);

STANDARD SQL92 syntax to delete a FOREIGN KEY:
Code:
ALTER TABLE table_name DROP FOREIGN KEY foreign_key_name;

Thank you in advance for any help,

OGHS
 
Hi OGHS,

if I am not totally wrong, mySQL doesn't support constraints. You will have to delete the the foreign-key in one table and make sure to delete the referring primary-key in the other table.

It's been a while that I worked with mySQL. Perhapse they included constraints in the meantime... but this would be a big (and nice!) suprise.

Is there anyone who can disabuse me?

cya

frag patrick.metz@epost.de
 
i think this eg. can describe the problem above, when we use one of the below:

alter table services
add foreign key (`Customer ID`) references `customer`.`Customer ID` on delete cascade
or
alter table services
add foreign key (`Customer ID`) references customer(`Customer ID`) on delete cascade

we get the error message:

Error Code : 1005
Can't create table '.\pc\#sql-15b0_1.frm' (errno: 150)
(0 ms taken)

any ideas ?

the two tables are:
create table `customer` (
`Customer ID` int(10) not null default `0`,
`Company` varchar(50) default null,
`Email` varchar(50) default null,
primary key (`Customer ID`)
)type=innodb

create table `services` (
`Services ID` int(10) not null default `0`,
`Customer ID` int(10) default null,
`Services Date` timestamp(14) not null,
`Problem` blob,
primary key (`Services ID`),
key `Customer` (`Customer ID`)
)type=innodb

consider the mysql-4.0.12-nt?
 
It looks like you can't drop a foreign key until 4.0.13
Starting from version 4.0.13, InnoDB supports

ALTER TABLE yourtablename
DROP FOREIGN KEY internally_generated_foreign_key_symbol

The you need to get that internal number using SHOW CREATE TABLE.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top