declaring foreign key in apache derby database

六月ゝ 毕业季﹏ 提交于 2021-02-04 14:50:04

问题


I am using Apache Derby database with ij 10.10.

I have 2 table 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 user table.

I used the command following command to create the table:

create table usertable (userid varchar(10) primary key,name varchar(20));

How do I write the logintable to set the userid as a foreign key referring to the above primary key.

Can anyone please help me out.


回答1:


I think you're looking for the FOREIGN KEY constraint syntax: http://db.apache.org/derby/docs/10.10/ref/rrefsqlj13590.html

And, more specifically, the REFERENCES syntax: http://db.apache.org/derby/docs/10.10/ref/rrefsqlj16357.html#rrefsqlj16357

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)

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));


来源:https://stackoverflow.com/questions/23571095/declaring-foreign-key-in-apache-derby-database

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!