简体   繁体   English

添加外键约束

[英]Adding a foreign key constraint

Is it possible to add a constraint just like foreign key after inserting some values in the table? 在表中插入一些值后,是否可以像外键一样添加约束?

If it's possible how can I do it, because I'm trying to add it with 如果有可能我该怎么做,因为我正在尝试添加它

alter table ...add constraint ..foreign key .." 

But I get an error: 但是我收到一个错误:

impossible de valider (SYS.COMPAGNIEID1) - clés parents introuvables. 不可能的de Valider(SYS.COMPAGNIEID1) - clés父母introuvables。

In order to have a define a foreign key, you have to have defined a primary key in the "parent" table. 要定义外键,必须在“父”表中定义主键。 For example, using the standard EMP and DEPT tables, you would first define the column DEPTNO as a primary key in the DEPT table. 例如,使用标准EMP和DEPT表,首先将DEPTNO列定义为DEPT表中的主键。 You would then define DEPTNO in EMP table as a foreign key, that references DEPTNO in DEPT. 然后,您将在EMP表中将DEPTNO定义为外键,它在DEPT中引用DEPTNO。 If you have data in you table, (by default) the data will be validated to make sure the data conforms to the constraints. 如果您在表中有数据(默认情况下),将验证数据以确保数据符合约束。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM