Oracle SQL script giving error. I try to create table - sql

I have script in Oracle SQL:
CREATE TABLE "CONNECTIONS_DB"
("USER" VARCHAR2(4) NOT NULL,
"TIME" DATE NOT NULL,
"STATUS" BOOLEAN NOT NULL,
CONSTRAINT "CONNECTIONS_DB_PK" PRIMARY KEY ("USER", "TIME", "STATUS"),
);
and it returns next error:
Error starting at line : 616 in command -
CREATE TABLE "CONNECTIONS_DB"
("USER" VARCHAR2(4) NOT NULL,
"TIME" DATE NOT NULL,
"STATUS" BOOLEAN NOT NULL,
CONSTRAINT "CONNECTIONS_DB_PK" PRIMARY KEY ("USER", "TIME", "STATUS"),
)
Error report -
ORA-00904: : недопустимый идентификатор
00904. 00000 - "%s: invalid identifier"
*Cause:
*Action:
How to fix this?

Problems with your code:
1) there is a trailing comma at the end of the line that declares the CONSTRAINT
2) datatype BOOLEAN does not exists in Oracle; you can use NUMBER(1) instead
Consider:
CREATE TABLE "CONNECTIONS_DB" (
"USER" VARCHAR2(4) NOT NULL,
"TIME" DATE NOT NULL,
"STATUS" NUMBER(1) NOT NULL,
CONSTRAINT CONNECTIONS_DB_PK PRIMARY KEY ("USER", "TIME", "STATUS")
);
Side note: USER is a reserved word in Oracle. I would suggest using another identifier (like USERNAME), and removing the double quotes around the identifiers. This will save you the work of double quoting that column in all further queries:
CREATE TABLE CONNECTIONS_DB (
USERNAME VARCHAR2(4) NOT NULL,
TIME DATE NOT NULL,
STATUS NUMBER(1) NOT NULL,
CONSTRAINT CONNECTIONS_DB_PK PRIMARY KEY (USERNAME, TIME, STATUS)
);
Demo on DB Fiddle

Related

SQL apex oracle error message ORA-00902: invalid datatype

CREATE TABLE sessions
(
Ses_Start TIME NOT NULL,
Ses_Finish TIME NOT NULL,
A_ID NUMBER(2) NOT NULL,
Vl_ID NUMBER(4) NOT NULL,
Expense_per_head NUMBER(3) NOT NULL,
CONSTRAINT pk_sessions PRIMARY KEY(A_ID, Vl_ID)
);
I'm trying to create the above table on oracle but I repeatedly get the following error
ORA-00902: invalid datatype
Any help would be appreciated!
Oracle does not have a TIME data type. You must use DATE or TIMESTAMP, each of which includes both time and date components.

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.

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.

SQL Error: ORA-00904: : invalid identifier 3

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