Strange problems while setting foreign key - sql

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.

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

Related

Oracle returns ORA-00904: invalid identifier error when adding foreign keys

I am unsure as to why I can't insert org_id and vol_id as foreign keys. It returns the error in the title ORA-00904: invalid identifier
CREATE TABLE VOLUNTEERS (
vol_id varchar2(3) not null,
vol_fname varchar2(10) not null,
vol_lname varchar2(10) not null,
vol_address varchar2(20) not null,
vol_telephone varchar2(10) not null,
primary key (vol_id));
CREATE TABLE ORGANIZATIONS (
org_id varchar2(3) not null,
org_name varchar2(30) not null,
org_contact varchar2(20) not null,
org_address varchar2(20) not null,
org_telephone varchar2(10) not null,
primary key (org_id));
alter table volunteers
add foreign key (org_id) references ORGANIZATIONS;
alter table organizations
add foreign key (vol_id) references VOLUNTEERS;
ALTER TABLE child_table
ADD CONSTRAINT fk_name
FOREIGN KEY (col_name) REFERENCES table_name(col_name);
A foreign key constraint is a constraint on an existing column. You have not defined the columns in each of the tables:
CREATE TABLE VOLUNTEERS (
vol_id varchar2(3) not null,
vol_fname varchar2(10) not null,
vol_lname varchar2(10) not null,
vol_address varchar2(20) not null,
vol_telephone varchar2(10) not null,
org_id varchar2(3),
------^
primary key (vol_id)
);
CREATE TABLE ORGANIZATIONS (
org_id varchar2(3) not null,
org_name varchar2(30) not null,
org_contact varchar2(20) not null,
org_address varchar2(20) not null,
org_telephone varchar2(10) not null,
vol_id varchar2(3),
------^
primary key (org_id)
);
Once the columns are defined, the foreign key constraints will work.

How to fix invalid data type size in this sql query?

The query i wrote as
CREATE TABLE Shopper (
Shopperid NUMBER(4) CONSTRAINT shpr_shprid_pk PRIMARY KEY,
ShopperName VARCHAR2(20) CONSTRAINT shpr_shprname_nn NOT NULL,
Gender CHAR(6) CONSTRAINT shpr_gdr_chk CHECK(Gender IN ('Male','Female')),
MobileNo NUMBER(10) CONSTRAINT shpr_mobno_nn NOT NULL,
Address VARCHAR2(50)
);
The issue is with NUMBER type definitions, try:
CREATE TABLE Shopper (
Shopperid NUMBER CONSTRAINT shpr_shprid_pk PRIMARY KEY,
ShopperName VARCHAR2(20) CONSTRAINT shpr_shprname_nn NOT NULL,
Gender CHAR(6) CONSTRAINT shpr_gdr_chk CHECK(Gender IN ('Male','Female')),
MobileNo NUMBER CONSTRAINT shpr_mobno_nn NOT NULL,
Address VARCHAR2(50)
);

ORA-00957 duplicate column name error, when trying to reference the same primary key with 3 foreign keys

I'm having problems with creating tables:
CREATE TABLE EMPLOYEE
(
employee_id NUMBER(5) NOT NULL UNIQUE,
position VARCHAR2(100) NOT NULL,
name VARCHAR2(255) NOT NULL,
salary NUMBER(6) NOT NULL
CONSTRAINT employee_pk PRIMARY KEY (employee_id)
);
CREATE TABLE PROJECT
(
project_id NUMBER(5) NOT NULL UNIQUE,
name VARCHAR(100) NOT NULL,
budget NUMBER(6) NOT NULL,
consultant_leader NUMBER(5) NOT NULL,
developer_leader NUMBER(5) NOT NULL,
project_leader NUMBER(5) NOT NULL,
CONSTRAINT project_pk PRIMARY KEY (PROJECT_ID),
CONSTRAINT fk_leader
FOREIGN KEY (consultant_leader, developer_leader, project_leader)
REFERENCES EMPLOYEE (employee_id, employee_id, employee_id)
);
In the last section, when I try to reference the employee's table employee_id, I'm getting ORA-00957. I think it's because the 3 different leader type foreign key references the same employee_id, but as far as I know, it should not be a problem. Is the syntax wrong?
Your immediate problem is that you need three foreign key relationships, not one with three columns.
But, there is no need to declare a primary key as being unique. So, I would recommend:
CREATE TABLE EMPLOYEE (
employee_id NUMBER(5) NOT NULL PRIMARY KEY,
position VARCHAR2(100) NOT NULL,
name VARCHAR2(255) NOT NULL,
salary NUMBER(6) NOT NULL
);
CREATE TABLE PROJECT (
project_id NUMBER(5) NOT NULL PRIMARY KEY,
name VARCHAR(100) NOT NULL,
budget NUMBER(6) NOT NULL,
consultant_leader NUMBER(5) NOT NULL,
developer_leader NUMBER(5) NOT NULL,
project_leader NUMBER(5) NOT NULL,
CONSTRAINT fk_leader FOREIGN KEY (consultant_leader)
REFERENCES EMPLOYEE (employee_id),
CONSTRAINT fk_leader FOREIGN KEY (developer_leader)
REFERENCES EMPLOYEE (employee_id),
CONSTRAINT fk_leader FOREIGN KEY (project_leader)
REFERENCES EMPLOYEE (employee_id)
);
You don't need to put the PRIMARY KEY constraint in-line, of course. The advantage of declaring it separately is that you can give the constraint a name to your liking.
I think you should create three distinct FK: FK_Consultant, FK_developer, FK_projleader

creating table in in cmd [duplicate]

This question already has answers here:
SQL - Missing right parenthesis
(4 answers)
Closed 7 years ago.
create table reservation
(
reservationid varchar2(6) primary key,
userid varchar2(6) foreign key references userprofile(userid),
vehicleid varchar2(6) foreign key references vehicle(vehicleid),
routeid varchar2(8) foreign key references route(routeid),
bookingdate date not null,
journeydate date not null,
driverid varchar2(6) foreign key references driver(driverid),
bookingstatus varchar2(20) not null,
totalfare number(10) not null,
boardingpoint varchar2(30) not null,
droppoint varchar2(30) not null,
vname varchar2(20) not null
);
I am getting an error:
ERROR at line 1:
ORA-00907: missing right parenthesis
You don't need the foreign key for an inline reference. This code works in SQL Fiddle:
create table vehicle (vehicleid varchar2(6) primary key);
create table userprofile (userid varchar2(6) primary key);
create table route (routeid varchar2(8) primary key);
create table driver (driverid varchar2(6) primary key);
create table reservation
(
reservationid varchar2(6) primary key,
userid varchar2(6) references userprofile(userid) ,
vehicleid varchar2(6) references vehicle(vehicleid),
routeid varchar2(8) references route(routeid),
bookingdate date not null,
journeydate date not null,
driverid varchar2(6) references driver(driverid),
bookingstatus varchar2(20) not null,
totalfare number(10) not null,
boardingpoint varchar2(30) not null,
droppoint varchar2(30) not null,
vname varchar2(20) not null
);

SQL ORA-02256: number of referencing columns must match referenced columns

these are my tables :
CREATE TABLE EMPLOYEE(
Emp_id number(4),
Emp_name varchar2(30),
Emp_gender varchar2(1),
Status varchar2(30),
Years_service number(4),
Primary Key (emp_id)
);
CREATE TABLE ACTIVITY(
Act_id number(4),
Description varchar2(30),
Category_code varchar2(1),
Primary Key(Act_id)
);
CREATE TABLE ALLOCATION(
Emp_id number(4) NOT NULL,
Act_id number(4) NOT NULL,
Hourly_rate number(5,2) NOT NULL,
Primary Key (Act_id, Emp_id),
Foreign Key (Act_id) REFERENCES ACTIVITY,
Foreign Key (Emp_id) REFERENCES EMPLOYEE,
CONSTRAINT CK_ALLOCATION_RATE CHECK(Hourly_rate > 0 and Hourly_rate<300)
);
CREATE TABLE ACTION(
Week_no number(2) NOT NULL,
Hrs_worked number(4,1) NOT NULL,
Act_id number(4) NOT NULL,
emp_id number(4) NOT NULL,
Primary Key (Week_no, Act_id, emp_id),
Foreign Key (Act_id) References Allocation,
Foreign Key (emp_id) References Allocation
);
Table employee, activity and allocation are created perfectly. but when i try to create table action and referencing the foreign key to table allocation, it says: ORA-02256: number of referencing columns must match referenced columns.
When using references I think it is a good idea to include the columns in both tables, even if the syntax makes this unnecessary (the default is to the primary key).
You have a composite primary key, so you need a composite key for the foreign reference:
CREATE TABLE ACTION (
Week_no number(2) NOT NULL,
Hrs_worked number(4,1) NOT NULL,
Act_id number(4) NOT NULL,
emp_id number(4) NOT NULL,
Primary Key (Week_no, Act_id, emp_id),
Foreign Key (Act_id, emp_id) References Allocation(Act_id, emp_id)
);