Sql 如何避免外键必须具有唯一名称的限制?
我不知道为什么它们必须是独一无二的,但从MySQL论坛上看,它们似乎是独一无二的。然而,我认为它与索引名有更多的关系。我有两个表,它们的外键约束引用了第三个表上的相同主键。如果有帮助的话,我将使用MySQL workbench来设计模式Sql 如何避免外键必须具有唯一名称的限制?,sql,mysql,foreign-keys,Sql,Mysql,Foreign Keys,我不知道为什么它们必须是独一无二的,但从MySQL论坛上看,它们似乎是独一无二的。然而,我认为它与索引名有更多的关系。我有两个表,它们的外键约束引用了第三个表上的相同主键。如果有帮助的话,我将使用MySQL workbench来设计模式 我通常将每个表上的外键命名为与其引用的主键相同的名称。我想这是不可能的。它将使用外键约束创建第一个表,但当它尝试创建第二个表时,会抛出一个错误。下面是它抛出错误的第二个表: CREATE TABLE IF NOT EXISTS `joe`.`products_
我通常将每个表上的外键命名为与其引用的主键相同的名称。我想这是不可能的。它将使用外键约束创建第一个表,但当它尝试创建第二个表时,会抛出一个错误。下面是它抛出错误的第二个表:
CREATE TABLE IF NOT EXISTS `joe`.`products_to_categories` (
`product_to_category_id` INT NOT NULL AUTO_INCREMENT ,
`category_id` INT NOT NULL ,
`product_id` INT NOT NULL ,
PRIMARY KEY (`product_to_category_id`) ,
INDEX `category_id` (`category_id` ASC) ,
INDEX `product_id` (`product_id` ASC) ,
CONSTRAINT `category_id`
FOREIGN KEY (`category_id` )
REFERENCES `joe`.`categories` (`category_id` )
ON DELETE CASCADE
ON UPDATE NO ACTION,
CONSTRAINT `product_id`
FOREIGN KEY (`product_id` )
REFERENCES `joe`.`products` (`product_id` )
ON DELETE CASCADE
ON UPDATE NO ACTION)
ENGINE = InnoDB;
我希望外键名称与其他两个表中的主键相同。我应该在这里删除什么以便使用这些名称。这里的最佳做法是什么。这是不可能的,因为用于索引IIRC的文件的文件名可能存在冲突。我可能会将键命名为
INDEX `product_id_fkey` (`product_id` ASC) ,
更新:我刚试过这个,效果很好。看看这是不是你的想法
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';
在PostgreSQL中,命名索引的默认设置是在主键和外键的名称后面分别附加“\u pkey”和“\u fkey”。因此,您的案例如下所示:
INDEX `product_id_fkey` (`product_id` ASC) ,
更新:我刚试过这个,效果很好。看看这是不是你的想法
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';
您正在通过以下方式创建名为product\u id
的索引(约束):
索引产品id
然后,您将使用相同的名称创建另一个约束(用于外键):
约束<代码>产品id
您需要做的是允许服务器通过删除
约束<代码>产品id
请参阅此URL:
“如果给定了CONSTRAINT symbol子句,则符号值在数据库中必须是唯一的。如果未给定该子句,InnoDB将自动创建名称。”您正在通过以下方式创建一个名为product\u id
的索引(约束):
索引产品id
然后,您将使用相同的名称创建另一个约束(用于外键):
约束<代码>产品id
您需要做的是允许服务器通过删除
约束<代码>产品id
请参阅此URL:
“如果给定了CONSTRAINT symbol子句,则符号值在数据库中必须是唯一的。如果未给定该子句,InnoDB将自动创建名称。”我记不清了!!我遇到了这个问题,它需要唯一的约束名称,即使约束在同一台机器/安装上属于不同的schema/db,也可以组成唯一的名称;但是,这比简单地允许MySQL查找唯一名称更容易出错。查看此URL:“如果给定约束符号子句,则符号值在数据库中必须是唯一的。如果未给定该子句,InnoDB将自动创建名称。”我记不清了!!我遇到了这个问题,它需要唯一的约束名称,即使约束在同一台机器/安装上属于不同的schema/db,也可以组成唯一的名称;但是,这比简单地允许MySQL查找唯一名称更容易出错。请参阅此URL:“如果给定约束符号子句,则符号值在数据库中必须是唯一的。如果未给定该子句,InnoDB将自动创建名称。”那么,产品标识的索引是否真的导致了冲突,实际的外键名可以吗?试试看,这是我的建议。好吧,这在mysql中并不重要“它将创建具有外键约束的第一个表,但当它尝试创建第二个表时,它会抛出一个错误”-那么这是怎么回事呢?那么,product_id的索引真的会导致冲突吗,实际的外键名可以吗?试试看,这将是我的建议。AFAIK在mysql中它并不重要“它将使用外键约束创建第一个表,但是当它尝试创建第二个表时,它会抛出一个错误”-那么这是怎么回事?