SQL Error: ORA-00904: : invalid identifier 3 - sql

I am trying to create a table (customer) to database by using this query,
CREATE TABLE customers(
"customer_id" VARCHAR2(20),
f_name VARCHAR2(30),
CONSTRAINT f_name_not_null NOT NULL,
l_name VARCHAR2(30),
CONSTRAINT l_name_not_null NOT NULL,
mobile_no VARCHAR2(30),
CONSTRAINT mobile_no_not_null NOT NULL,
address VARCHAR2(30),
CONSTRAINT address_not_null NOT NULL,
CONSTRAINT customer_pk PRIMARY KEY(customer_id),
CONSTRAINT mobile_no_address_unique UNIQUE(mobile_no,address));
i get the following back:
Error report - SQL Error: ORA-00904: : invalid identifier
00904. 00000 - "%s: invalid identifier"

There are two problems in this statement
first is the inconsistent usage of quoted column (as noted in comment by #a_horse_with_no_name )
second problem is that the column constraint should not be separated with a comma from the column definition
The correct statement with changes in comments is
CREATE TABLE customers(
/*"*/customer_id/*"*/ VARCHAR2(20),
f_name VARCHAR2(30) /*,*/
CONSTRAINT f_name_not_null NOT NULL,
l_name VARCHAR2(30)/*,*/
CONSTRAINT l_name_not_null NOT NULL,
mobile_no VARCHAR2(30)/*,*/
CONSTRAINT mobile_no_not_null NOT NULL,
address VARCHAR2(30) /*,*/
CONSTRAINT address_not_null NOT NULL,
CONSTRAINT customer_pk PRIMARY KEY(customer_id),
CONSTRAINT mobile_no_address_unique UNIQUE(mobile_no,address));
Interesting is that the error you see concerns the second issue, after resolving it you will get the error for the first issue which is more specific:
ORA-00904: "CUSTOMER_ID": invalid identifier

Related

Invalid identifier error when trying to create table

Hi I am using Oracle SQL to make a table, however I am getting this error and I'm not sure why.
Error:
Error report -
ORA-00904: : invalid identifier
00904. 00000 - "%s: invalid identifier"
sql:
CREATE TABLE users(
user_id number(8)
NOT NULL
CONSTRAINT user_id_pk
PRIMARY KEY,
first_name varchar(64)
gender number(1)
NOT NULL,
);
You have multiple errors. You want:
CREATE TABLE users (
user_id number(8) NOT NULL CONSTRAINT user_id_pk PRIMARY KEY,
first_name varchar2(64),
gender number(1) NOT NULL
);
Note that the name is varchar2(). This is the recommended string type in Oracle.

Error report - ORA-00904: : invalid identifier 00904. 00000 - "%s: invalid identifier"

This is my SQL Oracle script and I cannot figure out why I keep getting the following error when I try and run this code:
I keep getting this exact code on each table I try to run. Can anyone point me in the right direction as to why my code will not run? I have tried researching this myself ALL DAY LONG and cant get it.**
Error starting at line : 12 in command -
CREATE TABLE COURSE (
CRS_CODE VARCHAR (8) NOT NULL,
CRS_DESCRIPTION VARCHAR (35) NOT NULL,
CONSTRAINT CRS_CREDIT NOT NULL,
CHECK (CRS_CREDIT IN (1,2,3,4)),
PRIMARY KEY (CRS_CODE))
Error report -
ORA-00904: : invalid identifier
00904. 00000 - "%s: invalid identifier"
*Cause:
*Action:
SQL>
**CODE BEFORE ERROR:**
/* This script file creates the following tables:*/
/*COURSE, CLASS, ENROLL, STUDENT */
set echo on;
set serveroutput on ;
/*DROP TABLE COURSE;
DROP TABLE CLASS;
DROP TABLE STUDENT;
DROP TABLE ENROLL;*/
CREATE TABLE COURSE (
CRS_CODE VARCHAR (8) NOT NULL,
CRS_DESCRIPTION VARCHAR (35) NOT NULL,
CONSTRAINT CRS_CREDIT NOT NULL,
CHECK (CRS_CREDIT IN (1,2,3,4)),
PRIMARY KEY (CRS_CODE));
CREATE TABLE CLASS (
CLASS_CODE NUMBER (5) NOT NULL,
CONSTRAINT CLASS_SECTION_Ck NOT NULL,
CLASS_TIME VARCHAR (25) NOT NULL,
CLASS_ROOM CHAR (6),
CHECK (CLASS_SECTION IN (1,2,3,4,5,6,7,8)),
PRIMARY KEY (CLASS_CODE),
FOREIGN KEY (CRS_CODE) REFERENCES COURSE (CRS_CODE));
CREATE TABLE STUDENT (
STU_NUM INTEGER NOT NULL,
STU_LNAME VARCHAR (25) NOT NULL,
STU_FNAME VARCHAR (20) NOT NULL,
STU_INIT CHAR (1),
STU_DOB DATE NOT NULL,
CONSTRAINTS STU_HRS_Ck DEFAULT 0 NOT NULL,
STU_CLASS CHARACTER(2) REFERENCES "Fr", "So", "Jr", "Gr" NOT NULL,
CONSTRAINT STU_GPA_Ck DEFAULT 0.00 NOT NULL,
STU_PHONE VARCHAR (4) NOT NULL,
PRIMARY KEY (STU_NUM),
CHECK (STU_HRS <= 0),
CHECK (STU_HRS >= 1000),
CHECK (STU_GPA BETWEEN 0.00 AND 4.00));
CREATE TABLE ENROLL (
ENROLL_GRADE_Ck VARCHAR (1) REFERENCES "A","B","C","D","F","I","W", "Z", DEFAULT Z NOT NULL,
FOREIGN KEY (STU_NUM) REFERENCES STUDENT (STU_NUM),
FOREIGN KEY (CLASS_CODE) REFERENCES CLASS (CLASS_CODE),
PRIMARY KEY (CLASS_CODE));
commit;
It is not clear from your question whether you are trying to create a COLUMN level CONSTRAINT or not. If you are trying to create a COLUMN level CONSTRAINT, then there seems to be a syntax error in your CONSTRAINT definition (a comma before the word CONSTRAINT).
See the following link from Oracle:
https://docs.oracle.com/javadb/10.8.3.0/ref/rrefsqlj13590.html
It has examples on how to define constraints including COLUMN level constraints. You may want to compare your CONSTRAINT clause with the examples to see if there are errors in your CONSTRAINTS.

Error when setting Foreign Key in SQL Oracle

I've looked this up and I see that I'm doing it right but when I go to execute it, I get this error:
Error report -
SQL Error: ORA-00904: "HOSPITALCODE": invalid identifier
00904. 00000 - "%s: invalid identifier"
*Cause:
*Action:
Here is my code:
CREATE TABLE "Hospital" (
HospitalCode number,
HospitalName varchar(75),
HospitalStreet varchar(100),
HospitalCity varchar(75),
HospitalState varchar(12),
HospitalZip number,
HospitalPhone varchar(15),
CONSTRAINT HospitalCode_pk
PRIMARY KEY(HospitalCode));
CREATE TABLE "Doctor" (
DoctorID number,
DoctorFirstName varchar(75),
DoctorLastName varchar(75),
DoctorOfficeNumber number,
DoctorPhone varchar(10),
CONSTRAINT DoctorID_pk
PRIMARY KEY(DoctorID),
CONSTRAINT HospitalCode_fk
FOREIGN KEY (HospitalCode)
REFERENCES "Hospital" (HospitalCode));
You are creating the foreign key HospitalCode_fk in the table doctor without creating the column HospitalCode in this table:
CREATE TABLE "Doctor" (
DoctorID number,
DoctorFirstName varchar(75),
DoctorLastName varchar(75),
DoctorOfficeNumber number,
DoctorPhone varchar(10),
HospitalCode number, /* added */
CONSTRAINT DoctorID_pk
PRIMARY KEY(DoctorID),
CONSTRAINT HospitalCode_fk
FOREIGN KEY (HospitalCode)
REFERENCES "Hospital" (HospitalCode)
);

oracle sql dev: SQL Error: ORA-00905: missing keyword 00905. 00000 - "missing keyword"

I am using oracle sql developer.
On executing the following code, I get a vague sounding error.
CREATE TABLE students
(
student_id INT NOT NULL,
username VARCHAR2(30),
email VARCHAR2(80),
password VARCHAR2(30),
f_name VARCHAR2(30),
l_name VARCHAR2(30),
bio VARCHAR2(350),
dp VARCHAR2(15),
is_suspended CHAR(1) DEFAULT '0' NOT NULL,
suspension_reason VARCHAR2(50),
role_id INT NOT NULL,
created_on TIMESTAMP DEFAULT SYSDATE NOT NULL,
updated_on TIMESTAMP,
is_active CHAR(1) DEFAULT '1' NOT NULL,
city VARCHAR2(15) NOT NULL,
state VARCHAR2(15) NOT NULL,
zip VARCHAR(6) NOT NULL,
b_day DATE,
CONSTRAINT students_id_pk PRIMARY KEY(student_id),
CONSTRAINT students_role_id_fk FOREIGN KEY(role_id),
CONSTRAINT students_username_uq UNIQUE(username),
CONSTRAINT students_email_uq UNIQUE(email)
);
The error is:
Error report -
SQL Error: ORA-00905: missing keyword
00905. 00000 - "missing keyword"
*Cause:
*Action:
please advice.
The correct syntax for a foreign key constraint is:
CONSTRAINT students_role_id_fk FOREIGN KEY(role_id) REFERENCES roles(role_id)
----------------------------------------------------^
You are missing the REFERENCES keyword and the appropriate table/column afterwards (which may not be correct in the above answer).
This is at least one error.

Oracle create table with foreign key error - Invalid Identifier

I'm getting an error saying invalid identifier when I try to add this table. Its been bugging me for too long now so I thought I'd ask.
CREATE TABLE HORSE
(
horse_id numeric PRIMARY KEY,
horse_name character(30) not null,
horse_gender character(1) not null,
horse_height decimal not null,
horse_image character(40),
CONSTRAINT horse_breed FOREIGN KEY (breed_id) REFERENCES breed(breed_id)
);
The error message is;
Error at Command Line:34 Column:37
Error report:
SQL Error: ORA-00904: "BREED_ID": invalid identifier
00904. 00000 - "%s: invalid identifier"
*Cause:
*Action:
Thanks and sorry for asking what is probably a really dumb question.
You need breed_id in HORSE table
CREATE TABLE HORSE
(
horse_id numeric PRIMARY KEY,
horse_name character(30) not null,
horse_gender character(1) not null,
horse_height decimal not null,
horse_image character(40),
breed_id numeric null
CONSTRAINT horse_breed FOREIGN KEY (breed_id) REFERENCES breed(breed_id)
);