Invalid identifier error when trying to create table - sql

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.

Related

Oracle SQL script giving error. I try to create table

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

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

Invalid number SQL 01722

I created this table:
create table user_info(
ID number(3) primary key,
employee_id number(6),
login varchar2(8) not null,
password varchar2(16) not null, check (password>5),
creation_date date default sysdate,
constraint empid_fk foreign key(employee_id) references employees(employee_id)
)
When I want to insert into some values,
insert into user_info values(123,123123,'login','password','08-04-2016');
I receive this error:
01722. 00000 - "invalid number"
*Cause:
*Action:
Can someone explain what is wrong in my insert query?
check (password>5)
Won't work there. Your password is a varchar2, not a number. You can't check if its value is higher than 5, as it is not a number.
If you're trying to check the number of characters in it, you can use :
password varchar2(16) not null CHECK (LENGTH(password) > 5)

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