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

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

Related

Getting ORA-00942 Error for "Table or View does not exist"

Not sure why, but referenced tabled not able to be identified. The following is the code:
CREATE TABLE Store (
Store_ID NUMBER NOT NULL,
Dept_ID NUMBER NOT NULL,
Manager_ID NUMBER NOT NULL,
Employee_ID NUMBER NOT NULL,
CONSTRAINT Store_PK PRIMARY KEY (Store_ID),
CONSTRAINT Manager_FK FOREIGN KEY (Manager_ID) REFERENCES Manager (Manager_ID),
CONSTRAINT Employee_FK FOREIGN KEY (Employee_ID) REFERENCES Employee (Employee_ID)
);
CREATE TABLE Manager
(
Manager_ID NUMBER NOT NULL,
Employee_ID NUMBER NOT NULL,
Manager_Lname VARCHAR(30) NOT NULL,
Manager_Fname VARCHAR(30) NOT NULL,
CONSTRAINT Manager_PK PRIMARY KEY (Manager_ID),
CONSTRAINT Employee_FK FOREIGN KEY (Employee_ID) REFERENCES Employee (Employee_ID)
);
CREATE TABLE OrderTable
(
Order_ID NUMBER NOT NULL,
Customer_ID NUMBER NOT NULL,
Vendor_ID NUMBER NOT NULL,
Product_ID NUMBER NOT NULL,
Tracking_Num NUMBER NOT NULL,
CONSTRAINT Order_PK PRIMARY KEY (Order_ID),
CONSTRAINT Customer_FK FOREIGN KEY (Customer_ID) REFERENCES Customer (Customer_ID),
CONSTRAINT Vendor_FK FOREIGN KEY (Vendor_ID) REFERENCES Vendor (Vendor_ID),
CONSTRAINT Product_FK FOREIGN KEY (Product_ID) REFERENCES Product (Product_ID),
CONSTRAINT Shipping_FK FOREIGN KEY (Tracking_Num) REFERENCES Shipping (Tracking_Num)
);
CREATE TABLE Customer
(
Customer_ID NUMBER NOT NULL,
Customer_Lname VARCHAR(30),
Customer_Fname VARCHAR(30),
Email VARCHAR(30),
Payment_Type VARCHAR(15),
CONSTRAINT Customer_PK PRIMARY KEY (Customer_ID)
);
CREATE TABLE Product
(
Product_ID NUMBER NOT NULL,
Price NUMBER NOT NULL,
Brand VARCHAR(30),
Classification VARCHAR(30),
CONSTRAINT Product_PK PRIMARY KEY (Product_ID)
);
CREATE TABLE Employee
(
Employee_ID NUMBER NOT NULL,
Title INT,
Employee_Lname VARCHAR(30),
Employee_Fname VARCHAR(30),
CONSTRAINT Employee_PK PRIMARY KEY (Employee_ID)
);
CREATE TABLE Vendor
(
Vendor_ID NUMBER NOT NULL,
Product_ID NUMBER NOT NULL,
Quantity NUMBER,
CONSTRAINT Vendor_PK PRIMARY KEY (Vendor_ID),
CONSTRAINT Product_FK FOREIGN KEY (Product_ID) REFERENCES Product (Product_ID)
);
CREATE TABLE Retail
(
Retail_ID NUMBER NOT NULL,
Product_ID NUMBER NOT NULL,
Vendor_ID NUMBER NOT NULL,
Order_ID NUMBER NOT NULL,
Price NUMBER NOT NULL,
Quantity NUMBER NOT NULL,
CONSTRAINT Retail_PK PRIMARY KEY (Retail_ID),
CONSTRAINT Product_FK FOREIGN KEY (Product_ID) REFERENCES Product (Product_ID),
CONSTRAINT Vendor_FK FOREIGN KEY (Vendor_ID) REFERENCES Vendor (Vendor_ID),
CONSTRAINT OrderTable_FK FOREIGN KEY (Order_ID) REFERENCES OrderTable (Order_ID)
);
CREATE TABLE Shipping
(
Tracking_Num NUMBER NOT NULL,
Order_ID NUMBER NOT NULL,
Vendor_ID NUMBER NOT NULL,
Address VARCHAR2(50),
Shipping_Date NUMBER NOT NULL,
CONSTRAINT Shipping_PK PRIMARY KEY (Tracking_Num),
CONSTRAINT OrderTable_FK FOREIGN KEY (Order_ID) REFERENCES OrderTable (Order_ID),
CONSTRAINT Vendor_FK FOREIGN KEY (Vendor_ID) REFERENCES Vendor (Vendor_ID)
);
Assuming this is the order you're creating your tables, this is likely your problem:
CREATE TABLE Store (
...
CONSTRAINT Manager_FK FOREIGN KEY (Manager_ID)
REFERENCES Manager (Manager_ID)
...
You're referencing a table in your table definition that doesn't exist yet.

How do I code these relationships in sql without getting am error

Attached is my attempt at creating a database for the following entity relationship diagram. But I keep getting the following error:
"SQL0538N FOREIGN KEY "ADVISOR_STUDENT" does not conform to the
description of the parent key of table or nickname "KISHANPA.STUDENT".
SQLSTATE=42830"
for these 4 tables: advisor, prereq, teaches, takes. The rest of the tables seem to work fine. It would be greatly appreciated if someone can guide me int eh right direction. Thanks
ER Diagram
Schema Diagram
create table department (
dept_name varchar(30) not null,
building varchar(30),
budget numeric(7,2),
constraint department_key primary key (dept_name)
);
create table instructor (
iid char(9) not null,
name varchar(30) not null,
dept_name varchar(30) not null,
salary numeric(6,2),
constraint instructor_key primary key (iid, dept_name),
constraint instructor_dept foreign key(dept_name)
references department on delete no action
);
create table student (
sid char(9) not null,
name varchar(30) not null,
tot_cred smallint,
dept_name varchar(30) not null,
constraint student_key primary key (sid, dept_name),
constraint student_dept foreign key(dept_name)
references department on delete no action
);
create table course (
course_id char(8) not null,
title varchar(30) not null,
dept_name varchar(30) not null,
credits int not null,
constraint course_key primary key (course_id, dept_name),
constraint course_dept foreign key(dept_name)
references department on delete no action
);
create table advisor (
sid char(9) not null,
iid char(9) not null,
constraint advisor_key primary key (sid, iid),
constraint advisor_student foreign key(sid)
references student on delete no action,
constraint advisor_instructor foreign key (iid)
references instructor on delete no action
);
create table prereq (
course_id char(8) not null,
prereq_id char(8),
constraint prereq_key primary key (course_id),
constraint prereq_course foreign key(course_id)
references course on delete no action,
constraint prereq_precourse foreign key(prereq_id)
references course on delete no action
);
create table classroom (
building varchar(30) not null,
room_number varchar(10) not null,
capicity integer,
constraint classroom_key primary key (building, room_number)
);
create table time_slot (
time_slot_id varchar(10) not null,
day varchar(10) not null,
start_time time not null,
end_time time,
constraint time_slot_key primary key (time_slot_id, day, start_time)
);
create table section (
course_id char(8) not null,
sec_id varchar(10) not null,
semester char(1) not null,
year numeric (4,0) not null,
building varchar(30) not null,
room_number varchar(10) not null,
time_slot_id varchar(10) not null,
constraint section_key primary key(course_id, sec_id, year,
building, room_number, time_slot_id),
constraint section_classroom foreign key(building, room_number)
references classroom on delete no action
);
create table teaches (
iid char(9) not null,
course_id char(8) not null,
sec_id varchar(10) not null,
semester char(1) not null,
year numeric(4,0) not null,
constraint teaches_key primary key (iid, course_id, sec_id,
semester, year),
constraint section_instrictor foreign key(iid)
references instructor on delete no action,
constraint teaches_section foreign key(course_id, sec_id, semester, year)
references section on delete no action
);
create table takes (
sid char(9) not null,
course_id char(8) not null,
sec_id varchar(10) not null,
semester char(1) not null,
year numeric(4,0) not null,
grade real,
constraint takes_key primary key (sid, course_id, sec_id,
semester, year),
constraint student_takes foreign key(sid)
references student on delete cascade,
constraint takes_section foreign key(course_id, sec_id,
semester, year) references section on delete cascade
);
Your problem is on your key for student. You must have the same column to join tabes whith FK. Modify you table student like this :
create table student (sid char(9) not null, name varchar(30) not null, tot_cred smallint, dept_name varchar(30) not null, constraint student_key primary key (sid), constraint student_dept foreign key(dept_name) references department on delete no action);
The docs say this:
A foreign key references a primary key or a unique key in the same or
another table. A foreign key assignment indicates that referential
integrity is to be maintained according to the specified referential
constraints.
Your "advisor_student" references "sid", but that is not the primary key. You would need to include the department or change the design.
Your primary keys don't match your schema diagram for several tables.
Instructor, student, and course all have dept_name in the primary key in your DDL, but according to the Schema diagram, and logically as well, this field should not be a part of the primary key for any of those tables. In addition, the primary key of section does not match the Schema diagram. It should be (course_id, sec_id, semester, year) You have a lot of extra fields in there. That will cause problems with the foreign key constraints on teaches and takes.
Finally, the time_slot file both in the schema, and in the DDL have what I would call a questionable, given no other input, primary key. Should just be time_slot_id to my mind, then you can build a foreign key from section to time_slot as well.

SQL ORACLE need assistance-

SQL>
CREATE TABLE Loan(
Loan_ID NUMBER (5)
CONSTRAINT pk_Loan primary key,
Start_Date DATE NOT NULL,
End_Date DATE NOT NULL,
Copy_ID NUMBER(5),
CONSTRAINT fk_Copy_ID references Copy (Copy_ID),
customer_ID NUMBER(5),
CONSTRAINT fk_customer_ID references Customers (Customer_ID),
Evaluation NUMBER(1) NOT NULL,
check (Evaluation>=0 and Evaluation<=5>);
CONSTRAINT fk_Copy_ID references Copy (Copy_ID),
*
ERROR at line 7:
ORA-00907: missing right parenthesis
Try this please:
CREATE TABLE Loan(
Loan_ID NUMBER (5)
CONSTRAINT pk_Loan primary key,
Start_Date DATE NOT NULL,
End_Date DATE NOT NULL,
Copy_ID NUMBER(5),
customer_ID NUMBER(5),
Evaluation NUMBER(1) NOT NULL,
CONSTRAINT fk_Copy_ID foreign key (copy_id) references Copy (Copy_ID),
CONSTRAINT fk_customer_ID foreign key (customer_id) references Customers (Customer_ID),
constraint chk_evaluation check (Evaluation>=0 and evaluation<=5) );

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

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