在Apache Derby数据库中声明外键 [英] Declaring foreign key in Apache Derby database
问题描述
我正在使用 ij 10.10的Apache Derby数据库.
I am using Apache Derby database with ij 10.10.
我有两个桌子.第一个是 usertable
,第二个是 logintable
.在我的 usertable
中,有两列: userid
和 name
.我的 logintable
表有两列: userid
和 password
.我需要将 logintable
中的一列设置为外键,其中主键位于 usertable
中.
I have two tables. First is usertable
and the second is logintable
.
In my usertable
I have two columns: userid
and name
.
My logintable
table has two columns: userid
and password
.
I need to set one column in logintable
as foreign key where the primary key is in the usertable
.
我使用以下命令创建表:
I used the following command to create the table:
create table usertable (userid varchar(10) primary key, name varchar(20));
如何编写 logintable
以将 userid
设置为引用上述主键的外键?
How do I write the logintable
to set the userid
as a foreign key referring to the above primary key?
有人可以帮我吗?
推荐答案
I think you're looking for the FOREIGN KEY constraint syntax: http://db.apache.org/derby/docs/10.10/ref/rrefsqlj13590.html
,更具体地说,是REFERENCES语法: http://db.apache.org/derby/docs/10.10/ref/rrefsqlj16357.html#rrefsqlj16357
And, more specifically, the REFERENCES syntax: http://db.apache.org/derby/docs/10.10/ref/rrefsqlj16357.html#rrefsqlj16357
因此,当您创建"logintable"时,在CREATE TABLE语句中的某个点上,您将具有以下内容:
So when you are creating the "logintable", at some point in the CREATE TABLE statement you will have something like:
CONSTRAINT login_userid_ref FOREIGN KEY (userid) REFERENCES usertable(userid)
请注意,SQL语言具有各种备用语法样式,用于声明像这样的参照完整性约束.例如,您可以使用一种更简单的语法,最终得到如下结果:
Note that the SQL language has various alternate syntax styles for declaring referential integrity constraints like these; for example you can use a simpler syntax that ends up being something like:
create table logintable(
userid varchar(10) references usertable(userid),
password varchar(20));
这篇关于在Apache Derby数据库中声明外键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!