Strange problems while setting foreign key
Database: Oracle 10.1 on os:xp2002
I am setting foreign key in employee table but it I get an error
no matching unique or primary key for this column-list
I am using following queries for creating the tables.
Department table:
create table department(
d_name varchar2(10) not null,
d_no_of_employees number(4));
Employee table:
create table employee(
e_id number(4) ,
e_name varchar2(30) not null,
e_f_name varchar2(30) not null,
e_desg varchar2(20) not null,
e_address varchar2(50) not null,
e_phone_no number(12) not null,
e_salary number(10) not null,
e_house_开发者_C百科rent number(6) not null,
e_conv_allow number(6) not null,
e_email varchar2(50) not null unique,
d2_name varchar2(10) not null,
e_hire_month number(2) not null,
e_hire_year number(4) not null,
constraint e_id_pk primary key(e_id),
constraint d2_name_fk foreign key(d2_name) references department(d_name))
;
Any solution please.
the foreign key must be a primary or unique key in the other table.
Make d_name PRIMARY KEY of Department table.
create table department(
d_name varchar2(10) not null,
d_no_of_employees number(4),
constraint d_name_pk primary key(d_name));
And then create the Employee table.
From what you've posted you don't seem to have a primary key on the department
table. Something like:
create table department(
d_name varchar2(10) not null,
d_no_of_employees number(4)
constraint department_pk primary key(d_name));
Or after creation:
alter table department
add constraint department_pk primary key(d_name)
...
精彩评论