[英]Are recursive foreign keys also marked as primary keys? (In Oracle SQL Developer)
Example: Is this recursive relationship written correctly?例子:这个递归关系写对了吗? Would Mgr be marked as a primary key or is this code correct?: Mgr 会被标记为主键还是此代码正确?:
create table Employee ( EMPNO number(10) not null, Mgr number(10), constraint pk_Employee primary key (EMPNO), constraint fk_emp_emp foreign key (Mgr) references Employee );创建表 Employee(EMPNO number(10) not null,Mgr number(10),约束 pk_Employee 主键(EMPNO),约束 fk_emp_emp 外键(Mgr)引用 Employee);
Your code is correct.你的代码是正确的。 I checked it with the following sql server and It worked fine.我用下面的 sql 服务器检查了它,它工作正常。
create table Employee (EMPNO int not null, Mgr int, constraint
pk_Employee primary key (EMPNO), constraint fk_emp_emp foreign key (Mgr) references Employee );
Would Mgr be marked as a primary key or is this code correct? Mgr 会被标记为主键还是此代码正确?
Code is correct (table gets created), but mgr
will not be part of a primary key constraint;代码是正确的(表被创建),但mgr
不会成为主键约束的一部分; a table can have only one primary key.一张表只能有一个主键。 If you meant to say that it'll be a composite primary key (ie it would have two columns), it will not either.如果您的意思是说它将是一个复合主键(即它将有两列),它也不会。 See the result of the 2nd query - only empno
is a primary key column.查看第二个查询的结果 - 只有empno
是主键列。
SQL> create table Employee ( EMPNO number(10) not null, Mgr number(10), constraint pk_Employee primary key (EMPNO), constraint fk_emp_emp foreign key (Mgr) references Employee );
Table created.
SQL> select a.constraint_type,
2 b.column_name
3 from user_cons_columns b join user_constraints a on a.constraint_name = b.constraint_name
4 where a.table_name = 'EMPLOYEE';
CONSTRAINT_TYPE COLUMN_NAME
-------------------- --------------------
C EMPNO
P EMPNO --> constraint type = P = primary key
R MGR
SQL>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.