Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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_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.

like image 666
Adnan Avatar asked Dec 03 '25 21:12

Adnan


2 Answers

the foreign key must be a primary or unique key in the other table.

like image 64
Manoj Avatar answered Dec 06 '25 12:12

Manoj


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.

like image 20
CristiC Avatar answered Dec 06 '25 12:12

CristiC



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!