&引用;参考资料;SQL中的语句

&引用;参考资料;SQL中的语句,sql,reference,Sql,Reference,我有一个类似SQL的表 table Court CourtID numeric(4) primary key Name varchar(40) not null Place varchar(40) not null Type varchar(3) not null TypeID numeric(4) references Court(CourtID) default null 我找不到关于引用语句代表什么以及它如何将TypeI

我有一个类似SQL的表

table Court
    CourtID  numeric(4) primary key
    Name     varchar(40) not null
    Place    varchar(40) not null
    Type     varchar(3) not null
    TypeID   numeric(4) references Court(CourtID) default null

我找不到关于引用语句代表什么以及它如何将TypeID与CourtID联系起来的信息?

它只是外键的简写语法

各种各样的谷歌搜索结果都带有“sql引用关键字”

或者简单地尝试一下,它通常比谷歌搜索(老式方式)更有帮助

您的示例显示了一个自引用外键。这是一种常见的模式,用于对父母或配偶等关系进行建模,其中所有记录都属于同一个基表,但可能相互引用

Connected to:
Oracle Database 11g Enterprise Edition Release 11.2.0.1.0 - 64bit Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options

SQL> create table court(
  2  courtid integer primary key,
  3  typeid integer references court(courtid)
  4  );

Table created.

SQL> insert into court values(1,0);
insert into court values(1,0)
*
ERROR at line 1:
ORA-02291: integrity constraint (MSMITH.SYS_C0016710) violated - parent key not
found

SQL> insert into court values(1,1);

1 row created.
上述语法将为约束生成一个“随机”名称。更好的语法是显式命名约束。请注意,如果使用其他外键语法重新创建它,会发生什么

SQL> create table court(
  2  courtid integer primary key,
  3  typeid integer,
  4  constraint fk_typeid foreign key (typeid) references court(courtid)
  5  );

Table created.

SQL> insert into court values(1,0);
insert into court values(1,0)
*
ERROR at line 1:
ORA-02291: integrity constraint (MSMITH.FK_TYPEID) violated - parent key not
found

该键现在命名为FK_TYPEID,而不是SYS_C0016710

TYPEID,它没有在任何其他表中使用,因此我认为它不是外键,它只是引用了非常相同的表主键。但是为什么会这样呢?外键可以是自引用的。在数据库管理系统的手册中,关于
createtable
语句,都有解释