sql - Oracle table create error ORA-00904 invalid identifier - sql

My code is giving an "ORA-00904 invalid identifier" error. What might be the cause?
CREATE TABLE student
(
s_ID int,
S_NAMe varchar2(10),
S_major varchar2(20),
CONSTRAINT PK_s_ID PRIMARY KEY (s_ID),
CONSTRAINT FK_D_ID FOREIGN KEY (D_ID) REFERENCES dep (D_ID)
);

Your last constraint, D_ID references a column that doesn't exist. You need to add it and it's definition to your column list. Also, int is not a valid data type for Oracle.

thank you guys, i appreciate your help , i forget to write the foreign key as a column on student table and i think int is valid on my oracle 11g the table already created , here is code it worked with me
create table student (
s_ID int ,
S_NAMe varchar2 (10),
S_major varchar2(20),
D_ID number (10) ,
Constraint PK_s_ID primary key (s_ID),
constraint FK_D_ID foreign key (D_ID) references dep (D_ID) );

Related

Oracle SQL "invalid identifier error" when trying to create a table

I have successfully built these tables:
CREATE TABLE Tables
(
tnum NUMBER(4) PRIMARY KEY,
floor NUMBER(1), CHECK (floor >= 0 AND floor <= 4),
capacity NUMBER(2), CHECK(capacity >= 0 AND capacity <= 25),
location VARCHAR(50)
);
CREATE TABLE Customer
(
name VARCHAR(25),
phone VARCHAR(16),
city VARCHAR(25),
PRIMARY KEY(name, phone)
);
CREATE TABLE Menu
(
pid NUMBER(4) PRIMARY KEY,
pname VARCHAR(25),
price NUMBER(4, 1)
);
BUT when I try build this one, I got an error:
Error report -
ORA-00904: : invalid identifier
00904. 00000 - "%s: invalid identifier"
*Cause:
*Action:
CREATE TABLE Orders
(
orderid NUMBER(4) PRIMARY KEY,
custname VARCHAR(25),
CONSTRAINT fk_customer
FOREIGN KEY (custname)
REFERENCES Customer(customer.name),
phone VARCHAR(16),
CONSTRAINT fk_customer
FOREIGN KEY(phone)
REFERENCES Customer(customer.phone),
date DATE,
tnum NUMBER(4),
CONSTRAINT fk_tables
FOREIGN KEY(tnum)
REFERENCES Tables(tables.tnum),
numofdiners NUMBER(2)
);
I tried to understand what can cause the problem, even though I checked this link and some more other links, I didn't manage to solve the problem.
You can specify a column level constraint directly after the column, but then you have to get rid of the comma and the foreign key part as it's clear which column is meant.
But you need to include all columns of the primary key in the foreign key definition. And because the primary key of the customer table consist of two columns, you can't use an inline foreign key in the table orders.
The foreign key "references" part needs to specify the column name in the target table without a prefix for the table (the table is already clear because of the references table_xxx part, so there is no need to prefix the target column with the table name).
Additionally DATE is a reserved keyword.
CREATE TABLE Orders
(
orderid NUMBER(4) PRIMARY KEY,
custname VARCHAR(25) not null
phone VARCHAR(16) not null,
reservation_date DATE,
tnum NUMBER(4) --<< no comma here!
-- no FOREIGN KEY keyword here
CONSTRAINT fk_tables
REFERENCES Tables(tnum), --<< only the column name between the (...)
numofdiners NUMBER(2), --<< you need a comma before the table level constraints
CONSTRAINT fk_customer
FOREIGN KEY(name, phone) --<< list both columns here
REFERENCES Customer(name, phone) --<< and here
);
Note that the columns in a multi-column foreign key are matched by position, not by name.
Therefore, the following would be wrong
CONSTRAINT fk_customer
FOREIGN KEY(phone, name)
REFERENCES Customer(name, phone)

How do I fix a ORA-00902: invalid datatype creating a table

I am creating a table in a command SQL sections into a script already populated I have created several tables already but in this one I get a message saying
ORA-00902: invalid datatype
CREATE TABLE Weapons
(
id NUMBER(4),
name VARCHAR2(30),
damage NUMBER(4),
company_id VARCHAR2 (10),
CONSTRAINT pk_Weapons PRIMARY_KEY(id),
CONSTRAINT fk_Weapons_company
FOREIGN_KEY(company_id) REFERENCES Company(id),
CONSTRAINT fk_Weapons_ammo
FOREIGN_KEY(ammo_id) REFERENCES Ammo(id)
);
In the CONSTRAINT, it should be FOREIGN KEY and not FOREIGN_KEY. Also it should be PRIMARY KEY, not PRIMARY_KEY.
There is no underscore required as per syntax. So the query will be:
CREATE TABLE Weapons (
id NUMBER(4),
name VARCHAR2(30),
damage NUMBER(4),
company_id VARCHAR2(10),
CONSTRAINT pk_Weapons PRIMARY KEY(id),
CONSTRAINT fk_Weapons_company FOREIGN KEY(company_id) REFERENCES Company(id),
CONSTRAINT fk_Weapons_ammo FOREIGN KEY(ammo_id) REFERENCES Ammo(id)
);
About Foreign Keys: https://www.techonthenet.com/oracle/foreign_keys/foreign_keys.php
About Primary Keys: https://www.techonthenet.com/oracle/primary_keys.php
Here's a working example. I've created the AMMO table (whose description you didn't post, so I used only the ID column so that the foreign key constraint wouldn't fail). Pay attention to comments I wrote within the code.
SQL> create table ammo
2 ( id VARCHAR2(10),
3 CONSTRAINT pk_ammo PRIMARY KEY(id) );
Table created.
SQL> CREATE TABLE Company
2 ( id VARCHAR(3),
3 name VARCHAR(30), --> switch from CHAR to VARCHAR2
4 CONSTRAINT pk_Company PRIMARY KEY(id) );
Table created.
SQL> CREATE TABLE Weapons
2 ( id NUMBER(4),
3 name VARCHAR2(30),
4 damage NUMBER(4),
5 company_id VARCHAR2(3), --> should match COMPANY.ID datatype
6 ammo_id VARCHAR2(10), --> should match AMMO.ID datatype
7 CONSTRAINT pk_Weapons PRIMARY KEY(id),
8 CONSTRAINT fk_Weapons_company FOREIGN KEY(company_id) REFERENCES Company(id),
9 CONSTRAINT fk_Weapons_ammo FOREIGN KEY(ammo_id) REFERENCES ammo(id) );
Table created.
SQL>
In referential integrity constraint, you should match datatypes of the foreign and primary key columns. There's no sense in having a VARCHAR2(10) in the detail table which points to a VARCHAR2(3) column in the master table; you won't be able to put anything longer than 3 characters into the detail table's column anyway (foreign key constraint won't let you).

ORA-01748: only simple column names allowed here in Oracle

What I am trying to do ?
I am trying to create two tables and at the same time i am trying to link them together using foreign and primary keys. However I successfully create my parent table ( Student with primary key ) but failed to create child table ( Attendence with foreign key ).
What is the problem ?
I get the following error while creating Attendence table:
ERROR at line 5: ORA-01748: only simple column names allowed here
My code:
Student table:
create table Student (
ST_ROLLNO NUMBER(6) constraint s_pk primary key,
ST_NAME VARCHAR(30) not null,
ST_ADDRESS varchar(35) not null
);
Attendence table:
create table Attendence (
ST_ROLLNO NUMBER(6),
ST_DATE VARCHAR(30) not null,
ST_PRESENT_ABSENT varchar(1) not null,
constraint f_pk Attendence.ST_ROLLNO foreign key references Student(ST_ROLLNO)
);
Your foreign key constraint syntax is wrong; it should be:
constraint f_pk foreign key (ST_ROLLNO) references Student(ST_ROLLNO)
You are preceding the FK column name with the table name, which is wrong in itself, but also have it in the wrong place.
create table Student (
ST_ROLLNO NUMBER(6) constraint s_pk primary key,
ST_NAME VARCHAR(30) not null,
ST_ADDRESS varchar(35) not null
);
Table STUDENT created.
create table Attendence (
ST_ROLLNO NUMBER(6),
ST_DATE VARCHAR(30) not null,
ST_PRESENT_ABSENT varchar(1) not null,
constraint f_pk foreign key (ST_ROLLNO) references Student(ST_ROLLNO)
);
Table ATTENDENCE created.
According to oracle documentation,
ORA ERR
ORA-01748 only simple column names allowed here
The following is the cause of this error:
This SQL statement does not allow a qualified column name, such as
username.table.column or table.column.
Action you can take to resolve this issue: Remove the qualifications
from the column and retry the operation.
In your case, you are trying to refer to the table name while defining a constraint -
Attendence.ST_ROLLNO - WRONG.
It must contain a simple name without the table name or schema name.

"no matching unique or primary key for this column-list". The primary key does exist though

So i'm practicing some sql coding for a test and I can't get a foreign key to reference a primary key.
Here's the table that doesn't work:
CREATE TABLE ASSIGNMENT(
ASSIGN_ID NUMBER(2) NOT NULL,
START_DATE DATE,
END_DATE DATE,
BUDGET NUMBER (10,2),
MANAGER_ID NUMBER(2),
PRIMARY KEY (ASSIGN_ID,MANAGER_ID),
FOREIGN KEY (MANAGER_ID) REFERENCES EMPLOYEE(EMP_ID)
);
Here's the table it is referencing:
CREATE TABLE EMPLOYEE(
EMP_ID NUMBER(2) NOT NULL,
NAME VARCHAR(40),
OFFICE VARCHAR(20),
EXPERT_ID NUMBER(2),
PRIMARY KEY (EMP_ID,EXPERT_ID),
FOREIGN KEY (EXPERT_ID) REFERENCES EXPERTISE(EXPERT_ID)
);
Whenever I try to run the script it always comes back with:
Error report -
SQL Error: ORA-02270: no matching unique or primary key for this column-list
02270. 00000 - "no matching unique or primary key for this column-list"
*Cause: A REFERENCES clause in a CREATE/ALTER TABLE statement
gives a column-list for which there is no matching unique or primary
key constraint in the referenced table.
*Action: Find the correct column names using the ALL_CONS_COLUMNS
catalog view
I've looked around but can't seem to find the problem. Any help would be appreciated.
Here's the full code (haven't tested the last table yet):
CREATE TABLE EXPERTISE(
EXPERT_ID NUMBER(2) NOT NULL,
DESCRIPTION VARCHAR(50),
HOURLY_RATE NUMBER(3,2),
CHARGE_RATE NUMBER(3,2),
PRIMARY KEY(EXPERT_ID)
);
CREATE TABLE EMPLOYEE(
EMP_ID NUMBER(2) NOT NULL,
NAME VARCHAR(40),
OFFICE VARCHAR(20),
EXPERT_ID NUMBER(2),
PRIMARY KEY (EMP_ID,EXPERT_ID),
FOREIGN KEY (EXPERT_ID) REFERENCES EXPERTISE(EXPERT_ID)
);
CREATE TABLE ASSIGNMENT(
ASSIGN_ID NUMBER(2) NOT NULL,
START_DATE DATE,
END_DATE DATE,
BUDGET NUMBER (10,2),
MANAGER_ID NUMBER(2),
PRIMARY KEY (ASSIGN_ID,MANAGER_ID),
FOREIGN KEY (MANAGER_ID) REFERENCES EMPLOYEE(EMP_ID)
);
CREATE TABLE ALLOCATION(
EMP_ID NUMBER(3) NOT NULL,
ASSIGN_ID NUMBER(3) NOT NULL,
DAYS_WORKED_ON DATE,
HOURS_WORKED_ON DATE,
PRIMARY KEY(EMP_ID,ASSIGN_ID),
FOREIGN KEY(EMP_ID) REFERENCES EMPLOYEE(EMP_ID),
FOREIGN KEY(ASSIGN_ID) REFERENCES ASSIGNMENT(ASSIGN_ID)
);
I'm using Oracle SQL Developer to make it
*Cause: A REFERENCES clause in a CREATE/ALTER TABLE statement gives a column-list for which there is no matching unique or primary key
constraint in the referenced table.
The problem is that EMP_ID (by itself) isn't a primary or unique key of table Employees, instead, you have a compound primary key (EMP_ID, EXPERT_ID).
To fix the issue either make EMP_ID the primary key of the Employees table (which seems intuitive as each employee ought to have a unique id) or add a separate unique constraint on EMP_ID.
As pointed out in the comments, if you make EMP_ID the primary key, then (EMP_ID, EXPERT_ID) will also be unique by extension.
As the error suggest, the column you've referenced a foreign key doesn't match a unique constraint/pk on the parent table. Specifically for the primary key EMP_ID, EXPERT_ID you reference only EMP_ID.

SQLite Syntax error

Hi I'm having a problem with one of the tables in my database. I'm loading the tables from a .txt file, when I load the database i get
ERROR NEAR line 49: near "(": syntax error
the table below starts from line 49
create table LEASE(
P_ID integer,
I_ID varchar2,
C_ID integer,
DATE date,
TRENT decimal(6,2),
RENTPM decimal(4,2),
RENTUTD varchar2 constraint rentutd_value (RENTUTD in ('Y','N')),
LENGTH varchar2(15),
SDATE date,
EDATE date,
NOTE varchar2(150),
G_ID integer,
A_ID integer,
constraint fkey_lea1 foreign key (P_ID) references PROPERTY(P_ID),
constraint fkey_lea2 foreign key (I_ID) references INSTITUTION(I_ID),
constraint fkey_lea3 foreign key (C_ID) references CLIENT(C_ID),
constraint fkey_lea4 foreign key (G_ID) references GUARANTOR(G_ID),
constraint fkey_lea5 foreign key (A_ID) references AGENT(A_ID),
constraint pkey_lea primary key (P_ID,I_ID,C_ID,DATE)
);
Looks like the syntax on your rentutd column needs to be a little different:
RENTUTD varchar2 constraint rentutd_value CHECK ( RENTUTD in ('Y','N'))
See this sqlfiddle.
The SQLite syntax diagrams are great to figure this stuff out.