1 Replies - 863 Views - Last Post: 18 April 2015 - 08:47 AM

#1 Uushie  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 27
  • Joined: 23-October 10

Reference Previous Tables when creating tables

Posted 26 February 2015 - 01:08 PM

I'm not sure if I'm doing this correct. Currently new to oracle so I'm trying to design new tables that reference old keys in older tables

This isn't my code but this is how I'm programming it so it is just an example of where I'm having trouble:

create table Primary
(PrimaryID varchar2(9) constraint Primary_PrimaryID_pk primary key
constraint Primary_PrimaryID_nn not null,
PrimaryName varchar2(30) constraint Primary_PrimaryName_nn not null);

create table RPrimary
(RPrimaryID varchar2(9) constraint RPrimary_RPrimaryID_pk primary key
constraint Primary_PrimaryID_nn not null,
[b]PrimaryID varchar2(9) references Primary_PrimaryID,[/b]
RPrimaryName varchar2(30) constraint RPrimary_EPrimaryName_nn not null);



I'm positive I'm doing something wrong but I'm inexperienced so I'm not sure how to exactly reference previous table data. Thanks a bunch in advance.

Is This A Good Question/Topic? 0
  • +

Replies To: Reference Previous Tables when creating tables

#2 ndc85430  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 563
  • Posts: 2,304
  • Joined: 13-June 14

Re: Reference Previous Tables when creating tables

Posted 18 April 2015 - 08:47 AM

So, what you're asking is how to make sure that values in the column of one table exist in another table in which they're the primary key? This is known as a foreign key. Your syntax on line 9 isn't correct. The form should be

column_name type references table(other_column_name)


where "table" is the name of the other table and "other_column_name" is the primary key column in that table.

You can find some info on foreign keys here.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1