How can I get around this foreign key constraint having to have a unique name?

It is not possible because you would have a conflict in the filename for the file that is used for the index IIRC. I probably would name the key _ or something like that.

I don't remember exactly! I faced this problem, it requires unique constraint name even if constraint belong to different schema/db on same machine/instalation – Ratnesh Maurya Jul 11 '09 at 21:40 Sure, you could make up unique names; however, this is more error-prone than simply allowing MySQL to find a unique name. See this URL: dev.mysql.Com/doc/refman/5.1/en/… "If the CONSTRAINT symbol clause is given, the symbol value must be unique in the database.

If the clause is not given, InnoDB creates the name automatically. " – wilmoore Oct 7 '10 at 7:56.

You are creating an index (constraint) by the name of product_id via: INDEX product_id Then you are going and creating another constraint (for the foreign key) with the same name: CONSTRAINT product_id What you need to do is allow the server to provide a default, unique constraint name by removing the CONSTRAINT product_id See this URL: dev.mysql.com/doc/refman/5.1/en/innodb-f... "If the CONSTRAINT symbol clause is given, the symbol value must be unique in the database. If the clause is not given, InnoDB creates the name automatically.

In PostgreSQL, the default for naming indexes is to append "_pkey" and "_fkey" to the name of the primary and foreign key, respectively. So your case would look like: INDEX `product_id_fkey` (`product_id` ASC) , UPDATE: I just tried this and it worked. See if that's what you had in mind.

Use test; create table if not exists test. Product ( product_id int not null auto_increment, name varchar(80) not null, primary key(product_id) ); create table if not exists test. Category ( category_id int not null auto_increment, name varchar(80) not null, primary key(category_id) ); create table if not exists test.

Product_category ( product_id int, category_id int, primary key(product_id, category_id), constraint product_id_fkey foreign key(product_id) references product(product_id) on delete cascade on update no action, constraint category_id_fkey foreign key(category_id) references category(category_id) on delete cascade on update no action ); insert into test. Product(name) values('teddy bear'); insert into test. Category(name) values('toy'); insert into test.

Product_category select p. Product_id, c. Category_id from product as p, category as c where p.Name = 'teddy bear' and c.

Name = 'toy.

– Brian Reindel Jul 11 '09 at 21:40 Try it and see would be my advice. – duffymo Jul 11 '09 at 21:41 AFAIK it does not matters in mysql – Ratnesh Maurya Jul 11 '09 at 21:51 "It will create the first table with the foreign key constraint, but when it tries to create the second table it throws an error" - then what's this about? – duffymo Jul 11 '09 at 22:21.

I'm not sure why these have to be unique, but from reading the MySQL forums it appears that they do. However, I think it has something more to do with the INDEX name. I have two tables that have foreign key constraints referencing the same primary key on a third table.

If it helps, I'm using MySQL workbench to design the schema. I usually name my foreign key on each table the same name as the primary key it references. I guess this isn't possible.

It will create the first table with the foreign key constraint, but when it tries to create the second table it throws an error. I want the foreign key names to be the same as the primary key in both of the other tables. What should I remove here so that I can use these names.

What is the best practice here.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions