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

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.

Related

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

SQL Error: ORA-00904: "CATEGORYID": invalid identifier"

CREATE TABLE CUSTOMER1
(
CUSTOMERID VARCHAR2(2) NOT NULL,
FIRSTNAME VARCHAR2(50) NOT NULL,
LASTNAME VARCHAR2(50) NOT NULL,
PHONENUMBER VARCHAR2(50) NOT NULL,
BIRTHDATE DATE NOT NULL,
DRIVERLICENSENUMBER NUMBER,
STATUS VARCHAR2(50),
CREDITCARENUMBER NUMBER,
CONSTRAINT CustomerID_PK PRIMARY KEY(CUSTOMERID)
);
CREATE TABLE CCATEGORY
(
CATEGORYID INT NOT NULL,
CATEGORYNAME VARCHAR2(50) NOT NULL,
CONSTRAINT categoryID_pk PRIMARY KEY (CATEGORYID)
);
CREATE TABLE TAPE
(
TAPEID NUMBER(5) NOT NULL,
TAPETITLE VARCHAR2(200) NOT NULL,
RELEASEYEAR NUMBER(5) NOT NULL,
DATEPURCHASED DATE NOT NULL,
PRICE NUMERIC(5) NOT NULL,
RENTEDOUT VARCHAR2(20) NOT NULL,
RATING VARCHAR2(10) NOT NULL,
ACTIONONRETURN VARCHAR2(50) NOT NULL,
PRIMARY KEY(TAPEID),
CONSTRAINT FK_CATEGORYID FOREIGN KEY(CATEGORYID) REFERENCES CCATEGORY(CATEGORYID),
CONSTRAINT FK_RESERVEDBY FOREIGN KEY(CUSTOMERID) REFERENCES CUSTOMER(CUSTOMERID)
);
Customer, and CCategory table are created. When I try to create TAPE table I get the following error: SQL Error: ORA-00904: "CATEGORYID": invalid identifier. Can't seem to figure out why. I created the customer table first, then the category table before trying to create the TAPE table.
Problem is that you have not defined CATEGORYID and CUSTOMERID in TAPE table. Add it and it would work.
Complete statement would be
CREATE TABLE TAPE
(
TAPEID NUMBER(5) NOT NULL,
TAPETITLE VARCHAR2(200) NOT NULL,
RELEASEYEAR NUMBER(5) NOT NULL,
DATEPURCHASED DATE NOT NULL,
PRICE NUMERIC(5) NOT NULL,
RENTEDOUT VARCHAR2(20) NOT NULL,
RATING VARCHAR2(10) NOT NULL,
ACTIONONRETURN VARCHAR2(50) NOT NULL,
CATEGORYID INT NOT NULL,
CUSTOMERID VARCHAR2(2) NOT NULL,
CONSTRAINT tape_primary_key PRIMARY KEY(TAPEID),
CONSTRAINT FK_CATEGORYID FOREIGN KEY(CATEGORYID) REFERENCES CCATEGORY(CATEGORYID),
CONSTRAINT FK_RESERVEDBY FOREIGN KEY(CUSTOMERID) REFERENCES CUSTOMER1(CUSTOMERID)
);

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

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