Oracle foreign key constraints set foreign key constraints when creating a table

a grammar
set at the column level
CREATE TABLE table1
(colume_name datetype REFERENCES
table2(column_name),...)
table1: from table
table2: main table
Foreign key constraints are also known as primary and secondary table relationships.
Notice:
When setting foreign key constraints, the fields of the primary table must be primary keys.
The corresponding fields in the master and slave tables must be of the same data type.
The value of the foreign key field in the slave table must come from the value of the corresponding field in the master table, or a null value, other values ​​are not allowed.
 
Two examples
This example demonstrates: the value of the foreign key field in the slave table must come from the value of the corresponding field in the master table, or be a null value, other values ​​are not allowed.
  1. SQL> create table typeinfo
  2. 2(typeid varchar2(10) primary key,
  3. 3typename varchar2(20));
  4. 表已创建。
  5. SQL> create table userinfo_f
  6. 2(id varchar2(10) primary key,
  7. 3 username varchar2(20),
  8. 4 typeid_new varchar2(10) references typeinfo(typeid));
  9. 表已创建。
  10. SQL> insert into typeinfo values(1,1);
  11. 已创建1行。
  12. SQL> insert into userinfo_f(id,typeid_new) values(1,2);
  13. insert into userinfo_f(id,typeid_new) values(1,2)
  14. *
  15. 1行出现错误:
  16. ORA-02291:违反完整约束条件(SYSTEM.SYS_C0011061)-未找到父项关键字
  17. SQL> insert into userinfo_f(id,typeid_new) values(1,1);
  18. 已创建1行。
  19. SQL> insert into userinfo_f(id,typeid_new) values(2,null);
  20. 已创建1行。
 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326152211&siteId=291194637