1

I am trying to create one-to-one relationships between the a User and Archive table. I successfully achieved that using entity relationship diagram, but not in actual SQL code.

enter image description here

I wan to create these tables using SQL code like shown below, but at the same time defining its relationship with Table 2.

CREATE TABLE Table1 ( Some code

);

user1036645
  • 63
  • 2
  • 8
  • Are you thinking of a foreign key constraint? What DBMS are you using? – crthompson Apr 02 '15 at 20:45
  • I am using Derby. Basically, I have two tables called user and archive. I know their relationship is one to one, but I don't know how to translate that in SQL. Please help – user1036645 Apr 02 '15 at 20:48
  • 1
    I was not familiar w/ derby until this moment. I'm still not quite sure of what you're asking however. If you are trying to select records via sql you would use an `inner join` if you are trying to maintain database integrity by enforcing that there is always a 1 to 1 relationship you would use a foreign key constraint. – crthompson Apr 02 '15 at 20:51
  • [This link](https://db.apache.org/derby/docs/10.2/ref/rrefsqlj24513.html) was my first google hit. It shows pretty standard ansi sql create table code. It has a great page on [constraints](https://db.apache.org/derby/docs/10.2/ref/rrefsqlj13590.html#rrefsqlj13590) You are looking at adding something like this: `REFERENCES table2(some_id_column)` as shown in the 6th example. – crthompson Apr 02 '15 at 21:04

0 Answers0