Monday, January 3, 2011

Possible syntaxes to create parent & child relationship between tables

create table parent_table (
 p1 number primary key,
 p2 number
);

create table parent_table (
 p1 number constraint parent_table_pk primary key,
 p2 number
);

create table parent_table (
 p1 number,
 p2 number,
 primary key (p1)
);

create table parent_table (
 p1 number,
 p2 number,
 constraint parent_table_pk primary key (p1)
);

alter table parent_table add primary key(p1);

alter table parent_table add constraint parent_table_pk primary key(p1);

create table child_table (
 c1 number,
 c2 number references parent_table(p1)
);

create table child_table (
 c1 number,
 c2 number constraint child_table_fk references parent_table(p1)
);

create table child_table (
 c1 number,
 c2 number,
 foreign key (c2) references parent_table(p1)
);

create table child_table (
 c1 number,
 c2 number,
 constraint child_table_fk foreign key (c2) references parent_table(p1)
);

alter table child_table add foreign key (c2) references parent_table(p1);

alter table child_table add constraint child_table_fk foreign key (c2) references parent_table(p1);

No comments:

Post a Comment