ORA-00904: : Invalid Identifier in SQL - sql

I am currently working on a SQL assignment, but first I need to create tables which it is not letting me do. I have 3 tables which are Customer, PurchasedDeal, and Usage. I manage to create the first 2 tables successfully, but I am having a little difficulty with creating the Usage table. For some reason it is giving me this error.
Error at line 2:
ORA-00904: : Invalid Identifier
If anyone could help me understand why it is giving me this error I would really appreciate it. Thanks. It is saying it is having problems with uID INT and I am using putty to create these tables.
CREATE TABLE Customer(
CustomerID INT NOT NULL PRIMARY KEY,
CustomerName VARCHAR(100),
Phone VARCHAR(15)
);
CREATE TABLE PurchasedDeal(
DID INT NOT NULL PRIMARY KEY,
dealName VARCHAR(100),
cost FLOAT,
totalValue FLOAT,
balance FLOAT,
CustomerID INT,
FOREIGN KEY(CustomerID) REFERENCES Customer(CustomerID)
);
CREATE TABLE Usage(
uID INT,
uDate DATE,
cost FLOAT,
DealID INT,
PRIMARY KEY(DealID, uID),
FOREIGN KEY(DealID) REFERENCES PurchasedDeal(DID)
);

The term ‘uid’ is reserved in Oracle. This term cannot be used as a column name in the Oracle environment.
Query: SELECT uid FROM t1
Result: will execute correctly
Query: SELECT U.”uid” FROM x.t1 U
Result: will through error:
ORA-00904: “U”.”uid”: invalid identifier 00904. 00000 – “%s: invalid identifier”
Query: SELECT U.”UID” FROM x.t1 U
Result: will execute correctly as uid is replaced with UID (in capital)
Rectification in below table and we are good to go.
CREATE TABLE Usage(
uID1 INT,
uDate DATE,
cost FLOAT,
DealID INT,
PRIMARY KEY(DealID, uID1),
FOREIGN KEY(DealID) REFERENCES PurchasedDeal_test(DID)
);

Related

Can we use foreign key as primary key along with another attribute as below?

I have to create this project for one of my assignments and I have to use Transact-SQL to create this database. I have changed this below code many times to avoid this error but its still keep coming and can't think of any reason for this.
CREATE TABLE Job
(
Job_ID INT NOT NULL IDENTITY (500, 1),
Pickup_Address VARCHAR(255),
Destination_Address VARCHAR(255),
Crew_Name VARCHAR(255),
Customer_Name VARCHAR(255),
PRIMARY KEY(Job_ID)
)
GO
INSERT INTO Job(Pickup_Address,Destination_Address,Crew_Name,Customer_Name)
VALUES ('Ceylinco Centre Building, 3rd Floor, Nawam Mawatha','NO.50, Sumanagala Road,Rathmalana','Maharagama Crew 1','Dilan'),
('3/82, ST.JUDE LANE,Dalugama','19A, 4th Lane,Koswatthe Rd','Maharagama Crew 2','Kasun'),
('19 Saunders Place,Colombo 12','54/3, Elapitiwala,Ragama','Kottawa Crew 1','Kelly'),
('381 Prince of Wales Avenue,Colombo 14','51, SEA STREET,Colombo','Kottawa Crew 2','Kasun'),
('250 Galle Road,Colombo 03','34, Pepiliyana Road,Nugegoda','Nugegoda Crew 2','Alan')
GO
CREATE TABLE Loads
(
Load_ID INT NOT NULL IDENTITY (1, 1),
Job_ID INT NOT NULL FOREIGN KEY REFERENCES Job(Job_ID),
Load_Type VARCHAR(255),
Product_Name VARCHAR(255),
Loaded_Time DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP
PRIMARY KEY (Load_ID, Job_ID),
)
GO
I have executed the above SQL code, previously I couldn't create the Loads table and got an error
Invalid referencing table
After I tried to avoid this now it can be executed although there is an error and it's in below. what I need to know is the reason for this and will it be a problem if I keep this as it is. Thank you
Foreign key 'FK_Loads_c6b32eef601ef719ed3' References invalid table 'job'
The code works, you may have the case sensitive collation. To know the collation you can run this code:
SELECT SERVERPROPERTY('collation');
If this is the case, you can change it (2008 or higher, except azure) with code like the following (adding the desired collation):
ALTER DATABASE YOURS_DATABASE
COLLATE Modern_Spanish_CS_AS ;
more information here here

Invalid table name error when creating a table

Very new to SQL, but I thought that I had at least mastered how to make tables. I am trying to create the following table and get the error 'ORA-00903: invalid table name'. I'm not sure what is wrong.
Create table order (
order_id int,
item_type varchar(50),
item_name varchar(50),
item_price decimal(10,2),
primary key(order_id)
);
I am testing this on Oralce Live SQL and it is ok as well as on my Oracle 12c Database EE, all you need to add are "". But even so, I would not recommend it to use reserved words for naming tables.
Create table "order" (
order_id int,
item_type varchar(50),
item_name varchar(50),
item_price decimal(10,2),
primary key(order_id)
);
insert into "order" values (1, 'Item', 'Name', '20.2');
select * from "order";

error: value larger than specified precision allowed for this column

I am creating tables based on the SQL Developer, however, got couple problems from my code:
My table can be established one by one, but cannot run as a whole and get error:
ORA-00922: missing or invalid option
When I am trying to insert values, get the error:
ORA-01438: value larger than specified precision allowed for this column
I have no idea about how these two error come from and everything look like fine on my code.
create table FACULTY (
faculty_id VARCHAR(25),
fac_name VARCHAR(25),
office VARCHAR(25),
salary NUMBER(9,2),
primary key (faculty_id)
)
create table student (
student_id VARCHAR(25),
std_name VARCHAR(25),
home_phone CHAR(12),
total_credits NUMBER,
gpa NUMBER(1,2),
advisor_id VARCHAR(25),
PRIMARY KEY (student_id),
foreign key (advisor_id) REFERENCES FACULTY (faculty_id)
)
INSERT INTO student table, I have already insert values successfully for FACULTY table;
insert into student
values('111111111', 'Marcus', '21-4748363', 0, 3.11, '90421');
For 1st question:
Error report -
ORA-00922: missing or invalid option
00922. 00000 - "missing or invalid option"
For 2nd question:
Error starting at line : 40 in command -
insert into student
values('111111111', 'Marcus', '21-4748363', 0, 3.11, '90421')
Error report -
ORA-01438: value larger than specified precision allowed for this column
For 1st question
Use semicolon ; after the first table definition.
For 2nd question
You should define your table like this:
create table student (
student_id VARCHAR(25),
std_name VARCHAR(25),
home_phone CHAR(12),
total_credits NUMBER,
gpa NUMBER(3,2),
advisor_id VARCHAR(25),
PRIMARY KEY (student_id),
foreign key (advisor_id) REFERENCES FACULTY (faculty_id)
)
For gpa column it should be NUMBER(3,2) instead of NUMBER(1,2) to support 3.11 Value.

Postgres SQL syntax error near invisible 'REFERENCES'?

I am trying to create two tables with the following code.
CREATE TABLE assessments (
id INT UNIQUE PRIMARY KEY,
name VARCHAR(100),
type VARCHAR(10),
total_points NUMERIC,
weight NUMERIC
CHECK(weight >= 0)
CHECK(weight <= 100),
due_date DATE,
section_id INT REFERENCES sections(id)
);
CREATE TABLE enrollment_assessments (
id INT UNIQUE PRIMARY KEY REFERENCES enrollments(id),
assignment_id REFERENCES assignments(id),
assessment_type REFERENCES assessments(type),
points NUMERIC
CHECK (points >= 0)
CHECK (points <-100),
);
However, I get the error :
[Code: , SQL State: 42601] ERROR: syntax error at or near
"REFERENCES" Position: 112
which is saying the error is near the first line of code? I don't understand this error at all.
I do not like the comma in this part:
CHECK (points >= 0)
CHECK (points <-100),
you miss data types and extra comma has to be removed:
CREATE TABLE enrollment_assessments (
id INT UNIQUE PRIMARY KEY REFERENCES enrollments(id),
assignment_id INT REFERENCES assignments(id),
assessment_type VARCHAR(10) REFERENCES assessments(type),
points NUMERIC
CHECK (points >= 0)
CHECK (points <-100)
);

SQL: Creating table with Enum-like attribute that is one of only several types

Does SQL have an OR or | equivalent for attribute types that are only one of a few possibliities (ie Enum types)?
Example using my best guess for the status attribute in the following table:
CREATE TABLE Rental (
status ("open" | "closed"),
date datetime,
id int PRIMARY KEY
)
I want status to be either "open" or "closed", nothing else. Is there syntax for this, or should I use CHAR(6) or should I use a constraint instead?
You may use check constraints when defining you table to narrow the domain of the attribute.
CREATE TABLE Rental (
status char(6),
date datetime,
id int PRIMARY KEY
);
alter table Rental
add constraint status_valid_value
check (status is null or (status in ('open','closed')));
CREATE TABLE Rental (
status char(6),
date datetime,
id int PRIMARY KEY
Check (status='open' OR status='closed')
)
It's also better to store status like int (1- open, 0 - closed, an so on)