how to use foreign-keys with hsql create table?

假装没事ソ 提交于 2019-12-13 18:06:07

问题


could someone explain me how to use foreign keys in hsql? I would like it in an create table, but working alter table is also ok. I am working without tools, just in eclipse whats wrong with the hsql code?

CREATE TABLE user(
  t_id INTEGER GENERATED BY DEFAULT AS IDENTITY(START WITH 1, INCREMENT   BY   1) PRIMARY KEY,
  name VARCHAR(30),
  lastname VARCHAR(30),
  email  VARCHAR(30),  
  --FOREIGN KEY (b_id) REFERENCES bookingnumber(b_id)
);

CREATE TABLE bookingnumber (
  b_id INTEGER PRIMARY KEY
);

ALTER TABLE user
  ADD FOREIGN KEY (fk_b_id) REFERENCES bookingnumber(b_id);

回答1:


Perhaps you are trying to link each booking number to a user. In this case, multiple booking numbers may exist for each user. If you want to do this, add a column T_ID to the BOOKINGNUMBER table and created the foreign key on this table.

But your statement is linking each user to a booking number and doesn't have the correct syntax. It needs a column named B_ID in the USER table to work. And the syntax would be like this:

ALTER TABLE user ADD FOREIGN KEY (b_id) REFERENCES bookingnumber(b_id);



回答2:


I was facing a similar situation and this helped me

CREATE TABLE child(c1 INTEGER, c2 VARCHAR, FOREIGN KEY (c1, c2) REFERENCES parent(p1, p2));

A more detailed explanation can be found at http://www.d.umn.edu/~tcolburn/cs4531/hsqldb_docs/guide/guide.html#N102F8



来源:https://stackoverflow.com/questions/42156408/how-to-use-foreign-keys-with-hsql-create-table

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