[英]2 foreign key references for a single primary key
I have 3 tables: agreement, user, emails as follows and the id in user table is acting as a foreign key in email table(id(FK)) as well as agreement table id_two(FK). 我有3个表:协议,用户,电子邮件,如下所示,用户表中的id充当电子邮件表(id(FK))以及协议表id_two(FK)中的外键。
I have referenced id(Foriegn Key) in emails table to id(Primary key) in user table using the below code successfully: 我已成功使用以下代码将电子邮件表中的id(外键)引用到用户表中的id(主键):
ALTER TABLE emails MODIFY COLUMN id INT NOT NULL, ADD CONSTRAINT id FOREIGN KEY(id) REFERENCES user(id);
But i am getting an error #1022 - Can't write; 但是我收到一个错误#1022-无法编写; duplicate key in table '#sql-13f0_30e'
表'#sql-13f0_30e'中的重复键
when i execute the below query to refer id_two of agreement table to id(PK) of user table. 当我执行以下查询以将协议表的id_two引用到用户表的id(PK)时。
ALTER TABLE agreement MODIFY COLUMN id_two INT NOT NULL, ADD CONSTRAINT id_two FOREIGN KEY(id_two) REFERENCES user(id);
That's most probably because you already have a constraint defined in agreement
table named id_two
. 这很可能是因为您已经在名为
id_two
agreement
表中定义了约束。 Change the name of the constraint and see 更改约束的名称,请参阅
ALTER TABLE agreement MODIFY COLUMN id_two INT NOT NULL,
ADD CONSTRAINT id_FK2
FOREIGN KEY(id_two)
REFERENCES user(id);
Primary key is a column or group of columns that uniquely identify a row. 主键是唯一标识一行的一列或一组列。 Every table should have a primary key.
每个表都应有一个主键。 And a table cannot have more than one primary key.
一个表不能有多个主键。
Foreign key is a column or set of columns in one table whose values must have matching values in the primary key of another (or the same) table. 外键是一个表中的一列或一组列,其值在另一个(或相同)表的主键中必须具有匹配的值。 A foreign key is said to reference its primary key.
据说外键引用了它的主键。 Foreign keys are a mechanism for maintaining data integrity.
外键是用于维护数据完整性的机制。
For your problem, I have created the script for you. 对于您的问题,我已经为您创建了脚本。 I did it from scratch since I need to have table available before adding the constraints but I didn't added all columns, sorry!!:
我是从头开始的,因为在添加约束之前我需要有可用的表,但是我没有添加所有列,对不起!:
--Create user table and add id as primary key
CREATE TABLE user
(
Id Number (5) ,
Username Varchar2 (25),
Eamil Varchar2 (25),
CONSTRAINT user_pk PRIMARY KEY (id)
);
--Create "agreement" table and add "Agreement_Id" as primary key
CREATE TABLE agreement
(
Id_Two Number (5) ,
Agreement_Id Varchar2 (25),
type Varchar2 (25),
Constraint agreement_Pk Primary Key (agreement_id)
);
--Create "email" table and add "email_Id" as primary key
CREATE TABLE email
(
Id Number (5) ,
Agreement_Id Varchar2 (25),
Eamil_Id Varchar2 (25),
Constraint email_Pk Primary Key (Eamil_Id)
);
Now added constraints:
1. Foriegn key for "Agreement" table from "user" table:
Alter Table Agreement
ADD CONSTRAINT fk_agreement1
Foreign Key (Id_Two)
REFERENCES user(id)
2. Foreign key for "email" table from "Agreement" table:
Alter Table Email
ADD CONSTRAINT fk_email1
Foreign Key (Agreement_Id)
REFERENCES Agreement(Agreement_Id)
3. Foreign key for "email" table from "user" table:
Alter Table Email
ADD CONSTRAINT fk_email2
Foreign Key (id)
REFERENCES user(id)
Thus, you can add all constraints. 因此,您可以添加所有约束。
You have to give a different name to your foreign key (the name seems to be already taken). 您必须给外键起一个不同的名字(这个名字似乎已经被使用了)。 Change the name and it should work.
更改名称,它应该可以工作。
CONSTRAINT fk_email_user FOREIGN KEY (id) REFERENCES user(id);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.