ORA-00904: "BRANCH_ID": invalid identifier - sql

Why is the create tables not allowing me to add the staff tables, the CONSTRAINT seem logical to me.
CREATE TABLE branch
(
Branch_ID VARCHAR(2),
Branch_Name VARCHAR(20),
Branch_Address VARCHAR(40),
Branch_Postcode VARCHAR(15),
Branch_Telephone NUMBER(15),
Branch_email VARCHAR(40),
Branch_Fax NUMBER(15),
PRIMARY KEY ( Branch_ID )
);
CREATE TABLE staff
(
Staff_ID INT NOT NULL PRIMARY KEY,
firstName VARCHAR(20),
lastName VARCHAR(20),
addressLine_1 VARCHAR2(30),
city VARCHAR2(15),
postcode VARCHAR2(7),
telephone VARCHAR2(15),
salary DECIMAL (19,4),
CONSTRAINT BRANCH_fk FOREIGN KEY(Branch_ID ) REFERENCES branch(Branch_ID )
);
ORA-00904: "BRANCH_ID": invalid identifier

I think you forgot to add the Branch_ID field.
You are referencing this one to be your foreign key at the Staff table, but you didn't define it in your staff table yet.
Change staff table definition to:
CREATE TABLE staff
(
Staff_ID INT NOT NULL PRIMARY KEY,
firstName VARCHAR(20),
lastName VARCHAR(20),
addressLine_1 VARCHAR2(30),
city VARCHAR2(15),
postcode VARCHAR2(7),
telephone VARCHAR2(15),
salary DECIMAL (19,4),
Branch_ID VARCHAR2(2),
CONSTRAINT BRANCH_fk FOREIGN KEY(Branch_ID) REFERENCES branch(Branch_ID)
);
About Gordon Linoff's comment, check the link below. I modified my answer to match the 'best-practice'.
Difference VARCHAR and VARCHAR2

Related

Where is the wrong logic in my Create Table statements?

I'm trying to run these statements and I get an error saying no matching unique or primary key for this column-list. Can you please help me how to fix this problem?
I get the problem when I try to create table SITE:
CREATE TABLE OLMP_COUNTRY (
NOC CHAR(3),
TEAM VARCHAR2(100),
CITY VARCHAR2(100),
CONSTRAINT country_pk PRIMARY KEY(NOC)
);
CREATE TABLE ATHLETE (
ATHELTE_ID CHAR(8),
NAME VARCHAR2(100),
AGE CHAR(3),
SEX CHAR(1),
HEIGHT CHAR(3),
WEIGHT DECIMAL(3,1),
NOC CHAR(3),
CONSTRAINT athlete_pk PRIMARY KEY(ATHLETE_ID),
CONSTRAINT country_fk FOREIGN KEY(NOC) REFERENCES OLMP_COUNTRY(NOC)
);
CREATE TABLE SITE (
NOC CHAR(3),
CITY VARCHAR2(100),
SEASON VARCHAR2(20),
YEAR CHAR(4),
CONSTRAINT site_pk PRIMARY KEY(NOC),
CONSTRAINT country_fk FOREIGN KEY(CITY) REFERENCES OLMP_COUNTRY(CITY)
);
CREATE TABLE RESULTS (
RESULT_ID CHAR(8),
MEDAL CHAR(6),
ATHLETE_ID CHAR(8),
SPORT_EVENT VARCHAR2(100),
YEAR CHAR(4),
GAMES VARCHAR2(50),
CONSTRAINT results_pk PRIMARY KEY(RESULTS_ID)
);
CREATE TABLE EVENT (
SPORT_EVENT VARCHAR2(100),
SPORT VARCHAR2(50),
GAMES VARCHAR2(50)
CONSTRAINT event_pk PRIMARY KEY(SPORT_EVENT)
);
A foreign key should be referencing the primary key of the table it is referring to.
So I think you want:
CREATE TABLE SITE (
NOC CHAR(3),
CITY VARCHAR2(100),
SEASON VARCHAR2(20),
YEAR CHAR(4),
CONSTRAINT site_pk PRIMARY KEY(NOC),
CONSTRAINT site_country_fk FOREIGN KEY(NOC) REFERENCES OLMP_COUNTRY(NOC)
);
I have no idea why you are repeating CITY in both tables, but the foreign key constraint should be to the primary key. You can look up the city using JOIN. It should not be repeated.

Foreign key missing parenthesis

CREATE TABLE EMPLOYEE (
EID CHAR(3) NOT NULL PRIMARY KEY,
ENAME VARCHAR2(50) NOT NULL,
JOB_TYPE VARCHAR2(50) NOT NULL,
MANAGER CHAR(3) FOREIGN KEY REFERENCES EMPLOYEE(EID),
HIRE_DATE DATE NOT NULL,
DNO INTEGER FOREIGN KEY REFERENCES DEPARTMENT(DNO),
COMMISSION DECIMAL(10,2),
SALARY DECIMAL(7,2) NOT NULL,
);
CREATE TABLE DEPARTMENT (
DNO INT NOT NULL PRIMARY KEY,
DNAME VARCHAR(50),
LOCATION VARCHAR(50) DEFAULT('NEW DELHI')
);
in creation of the employee table
this is giving me an error of right parenthesis
and the department table is already created
You have an extra comma in the line
SALARY DECIMAL(7,2) NOT NULL,
Delete that comma and the Employee table should be created.
You need to Create the Department Table first to use one of its
Columns as FOREIGN KEY.
Also, check your database. There might already be a Department Table. To avoid getting that error when the table needed is already created, use the keyword IF NOT EXISTS
CREATE TABLE IF NOT EXISTS Department(
DNO INT NOT NULL PRIMARY KEY,
DNAME VARCHAR(50),
LOCATION VARCHAR(50) DEFAULT('NEW DELHI')
);
The error is caused by the trailing comma, but you have other issues:
The FOREIGN KEY is not needed for an inline reference.
You need to define the tables in the right order.
So . . .
CREATE TABLE DEPARTMENT (
DNO INT NOT NULL PRIMARY KEY,
DNAME VARCHAR(50),
LOCATION VARCHAR(50) DEFAULT('NEW DELHI')
);
CREATE TABLE EMPLOYEE (
EID CHAR(3) NOT NULL PRIMARY KEY,
ENAME VARCHAR2(50) NOT NULL,
JOB_TYPE VARCHAR2(50) NOT NULL,
MANAGER CHAR(3) REFERENCES EMPLOYEE(EID),
HIRE_DATE DATE NOT NULL,
DNO INTEGER REFERENCES DEPARTMENT(DNO),
COMMISSION DECIMAL(10,2),
SALARY DECIMAL(7,2) NOT NULL
);
Here is an example of it working.

oracle SQL - ORA-009007 Missing right parenthesis

I have this sql
CREATE TABLE PATIENTS
(
patientID NUMBER NOT NULL,
healthInsID NUMBER,
fname VARCHAR(20) NOT NULL,
minit VARCHAR(15),
lname VARCHAR(30) NOT NULL,
gender CHAR(2),
email VARCHAR(40),
street VARCHAR(40),
postalCode NUMBER,
city VARCHAR(20),
country VARCHAR(20),
PRIMARY KEY (patientID),
FOREIGN KEY (healthInsID) REFERENCES HEALTH_INSURANCES
ON DELETE SET NULL ON UPDATE CASCADE
);
I have no idea why I keep getting this error. I have search a lot but still nothing that can solve it. Any ideas?
Thanks
You have to specify column from HEALTH_INSURANCES table in FOREIGN KEY caluse.
You should also remove ON UPDATE CASCADE. You can use a trigger on update instead of this clause.
CREATE TABLE PATIENTS
(
patientID NUMBER NOT NULL,
healthInsID NUMBER,
fname VARCHAR(20) NOT NULL,
minit VARCHAR(15),
lname VARCHAR(30) NOT NULL,
gender CHAR(2),
email VARCHAR(40),
street VARCHAR(40),
postalCode NUMBER,
city VARCHAR(20),
country VARCHAR(20),
PRIMARY KEY (patientID),
FOREIGN KEY (healthInsID) REFERENCES HEALTH_INSURANCES (healthInsID)
ON DELETE SET NULL
);
More information about constraints

Oracle SQL error ORA-00907: missing right parenthesis

How are you all?
Basically I've written up this bit of SQL code to create a table but I keep getting the error stated in the title, any idea as to why?
Here's the code:
CREATE TABLE staff(
staffID INT NOT NULL PRIMARY KEY,
firstName VARCHAR2(20),
lastName VARCHAR2(20),
addressLine_1 VARCHAR2(30),
city VARCHAR2(15),
postcode VARCHAR2(7),
telephone VARCHAR2(15),
salary DECIMAL (19,4),
branchID INT FOREIGN KEY REFERENCES branches(branchID)
);
Also here is the code for my 'branches' table
CREATE TABLE branches
(branchID int NOT NULL PRIMARY KEY,
addressLine_1 VARCHAR2(30),
city VARCHAR2(15),
postcode VARCHAR2(7),
telephone VARCHAR2(15),
manager VARCHAR2(20));
Any help would be appreciated!
Thank you!
A few suggestions:
First make sure that the branches table has been created.
Second, I would alter the create table code to the following:
CREATE TABLE staff(
staffID INT NOT NULL PRIMARY KEY,
firstName VARCHAR(20),
lastName VARCHAR(20),
addressLine_1 VARCHAR2(30),
city VARCHAR2(15),
postcode VARCHAR2(7),
telephone VARCHAR2(15),
salary DECIMAL (19,4),
branchID INT,
constraint fk_branchId FOREIGN KEY (branchID) REFERENCES branches(branchID)
);
See SQL Fiddle with Demo. The syntax to create a FOREIGN KEY during table creation is:
CREATE TABLE table_name
(
column1 datatype null/not null,
column2 datatype null/not null,
...
CONSTRAINT fk_column
FOREIGN KEY (column1, column2, ... column_n)
REFERENCES parent_table (column1, column2, ... column_n)
);
Here is the creation of table staff1
CREATE TABLE staff
(
staffID INT NOT NULL PRIMARY KEY,
firstName VARCHAR2(20),
lastName VARCHAR2(20),
addressLine_1 VARCHAR2(30),
city VARCHAR2(15),
postcode VARCHAR2(7),
telephone VARCHAR2(15),
branchID int,
salary DECIMAL (19,4),
CONSTRAINT BRANCH_fk FOREIGN KEY(branchID) REFERENCES branches(branchID)
)
SQL> /
Table created.
Please use constraint name such that finding an error becomes easy.
create table medication (
id int not null primary key,
name varchar(20),
mudslig price number (10),
protect date not null default (getdate()),
finish date not null default (getdate()),
company proect varchre2 (20),
shelf id int,
chemistid int,
constraint shelf_fk foreign key (shelf id) refences shelf (shelf id),
constraint chemist_fk foreign key (chemistid) refences chemist (chemistid)
);
Please use constraint name such that finding an error becomes easy.

oracle number of referencing columns must match referenced columns

I keep getting an error saying The number of columns in the foreign-key referenced list is not equal to the number of columns in the referenced list.
This is the line I am getting the error on.
foreign key(EID, Lastname, Firstname, Midinitial) references employee,
Does anyone know why I am getting this error?
create table employee(
EID varchar(20) primary key,
Lastname varchar(20),
Firstname varchar(20),
Midinitial char(1),
gender char(1),
street varchar(20),
city varchar(20)
);
create table works(
EID varchar(20) primary key,
Lastname varchar(20),
Firstname varchar(20),
Midinitial char(1),
company_name varchar(20),
salary numeric(5,0),
foreign key(EID, Lastname, Firstname, Midinitial) references employee,
foreign key(company_name) references company
);
create table company(
company_name varchar(20) primary key,
city varchar(20),
foreign key(city)references employee
);
You need the Primary Key from Employees only:
foreign key(EID) references employee