Is there a way to declare Status and Level in Oracle? [duplicate] - sql

This question already has answers here:
getting error while creating table as ORA-00904: : invalid identifier in oracle database sql
(2 answers)
Closed 9 months ago.
I usually use SQL Server so declare variables in Oracle seem different a little.
Here is the table Im trying to create:
CREATE TABLE EMPLOYEE(
EmpNo char(10) CONSTRAINT PK_EmpNo PRIMARY KEY,
EmpName varchar2(30),
Birthday DATE not null,
DeptNo number,
MgrNo varchar2(30) not null,
StartDate DATE not null,
Salary number(7,2) not null,
Level number CONSTRAINT Level_ck check (Level > 0 AND Level < 8) not null,
Status number CONSTRAINT Status_ck check (Status >= 0 AND Status <= 2) not null,
Note varchar2(4000));
This is the error:
I think I declare Level and Status wrong because when I remove them out of the Table, the table can be created.Any suggestion?

LEVEL is a reserved word that cannot be used as an identifier. Every database has them; the complete list of reserved words for Oracle is here: https://docs.oracle.com/en/database/oracle/oracle-database/19/sqlrf/Oracle-SQL-Reserved-Words.html#GUID-55C49D1E-BE08-4C50-A9DD-8593EB925612
STATUS is not a reserved word, and you should be able to create a column with that name.
Also note that table and column names in Oracle are not case-sensitive by default, so using camelCase isn't really appropriate (it will only be visible in your CREATE TABLE DDL command). A more standard naming convention is to use underscores to separate components of a label, such as START_DATE or DEPT_NR or TABLE_A.

Related

creating a table with Foreign key in it gives error ORA-00904: : invalid identifier in oracle 10g [duplicate]

This question already has answers here:
Creating table via SQL Command Line, invalid identifier
(3 answers)
Closed 3 years ago.
Image of the code with table, query and error
I have oracle 10g installed on my computer. I have created a table in it named STUDENT and this STUDENT table has a primary key called RNO and now I want to create another table named FEE and make this RNO key into a foreign key in FEE table with the following query:
CREATE TABLE FEE ( RNO NUMBER(2), Amount number(20) Not Null, Date varchar2(10) Not Null, Receipt Number(10) Not Null, CONSTRAINT FEEFK FOREIGN KEY (RNO) REFERENCES STUDENT (RNO));
Now I have done all I could to correct it but just can't seem to find any problem or error with this query above. The Query gives the following error in Oracle 10g:
ORA-00904: : invalid identifier
Column name can't be DATE, it is reserved for datatype. Rename it to, say, CDATE.
SQL> CREATE TABLE student (rno NUMBER (2) PRIMARY KEY);
Table created.
SQL> CREATE TABLE FEE
2 (
3 RNO NUMBER (2),
4 Amount NUMBER (20) NOT NULL,
5 cDate VARCHAR2 (10) NOT NULL,
6 Receipt NUMBER (10) NOT NULL,
7 CONSTRAINT FEEFK FOREIGN KEY (RNO) REFERENCES STUDENT (RNO)
8 );
Table created.
SQL>
Use double qoutes "Date" or rename your column Date as some other name like DateColumn as Date is a reserved name fpr date types in oracle

Oracle SQL - "missing keyword"

I am trying to create a simple table in Oracle SQL. The counterpart code works fine in SQL management studio, but not in Oracle APEX.
The following query:
CREATE TABLE Conference(
ConferenceID NUMBER GENERATED ALWAYS AS IDENTITY
(START WITH 100
INCREMENT BY 10
MINVALUE 100
MAXVALUE 100000
NO CYCLE),
Director VARCHAR(25) NOT NULL,
School Size NUMBER NOT NULL,
Location VARCHAR(50) NOT NULL,
CONSTRAINT pk_Conference PRIMARY KEY (ConferenceID)
);
is repeatedly met with the following error:
ORA-02000: missing ( keyword
I have done my due diligence searching for a solution to this problem here, here, and here.
I checked over the identity column section, as well as the Primary Key syntax here, and everything appears to look right. Despite this, I cannot find a solution.
Okay, in the docs, take a look at the identity options for a column definition. It's a small typo in this case - the NOCYCLE option is one word; there's no space.
You have one other problem, which is that School Size is not a valid column name. There's a space in it, and it's not quoted. You could do either School_Size or "School Size". I'd recommend the first one, since double-quoted column names are case-sensitive and really annoying to use.
Edit: Also, they're technically synonyms, but Oracle recommends using VARCHAR2 instead of VARCHAR.
CREATE TABLE Conference(
ConferenceID NUMBER GENERATED ALWAYS AS IDENTITY
(START WITH 100
INCREMENT BY 10
MINVALUE 100
MAXVALUE 100000
NOCYCLE),
Director VARCHAR2(25) NOT NULL,
School_Size NUMBER NOT NULL,
Location VARCHAR2(50) NOT NULL,
CONSTRAINT pk_Conference PRIMARY KEY (ConferenceID)
);

Sql error while creating tables - Firebird

I have simple sql code for create table and then add constraint to it. It looks like this:
CREATE TABLE bills (
id INTEGER NOT NULL,
code VARCHAR2(25) NOT NULL,
dateOfGeneration DATE NOT NULL,
job_id INTEGER NOT NULL
);
ALTER TABLE bills ADD CONSTRAINT bills_pk PRIMARY KEY ( id,job_id );
I am using IBExpert - client for Firebird. When I execute this code I get 2 errors:
First error: - in code VARCHAR2(25) NOT NULL
Invalid token.
Dynamic SQL Error.
SQL error code = -104.
Token unknown - line 3, column 29.
(.
Second error: - in code ALTER TABLE ...
Invalid token.
Dynamic SQL Error.
SQL error code = -104.
Token unknown - line 8, column 1.
ALTER.
The first one I think is because i am using varchar2 instead of varchar. What about second error? How to fix this?
There is no VARCHAR2 type in Firebird - https://firebirdsql.org/file/documentation/reference_manuals/fblangref25-en/html/fblangref25-datatypes-chartypes.html
If you want to run two commands - you have to run TWO commands. You try to run two commands in one, but that is not a way to do it. You have to split them and run one after another. Or you have to wrap them into one EXECUTE BLOCK command.
Also IBExpert has a separate window of Script Executive for multiple commands running. It is not SQL Editor which is designed to execute ONE command, it is a separate window in another menu - https://www.ibexpert.net/ibe/pmwiki.php?n=Doc.ScriptExecutive
Table creation command is described here: https://firebirdsql.org/file/documentation/reference_manuals/fblangref25-en/html/fblangref25-ddl-tbl.html
Basically what you trying to do looks like this, if to do it in one command:
CREATE TABLE bills (
id INTEGER NOT NULL,
code VARCHAR(25) NOT NULL,
dateOfGeneration DATE NOT NULL,
job_id INTEGER NOT NULL,
PRIMARY KEY ( id,job_id )
)
or if you insist on naming then perhaps
CREATE TABLE bills (
id INTEGER NOT NULL,
code VARCHAR(25) NOT NULL,
dateOfGeneration DATE NOT NULL,
job_id INTEGER NOT NULL,
CONSTRAINT bills_pk PRIMARY KEY ( id,job_id )
)

"ORA-00984: column not allowed here" when attempting to create table using SQL [duplicate]

This question already has answers here:
Comparing Dates in Oracle SQL
(5 answers)
Closed 6 years ago.
I am attempting to run this script to create a table, however I get a column not allowed error. After doing some research it seems that it could be that it could be a syntax error concerning values, however I am not inserting any values.
CREATE TABLE SALESPERSON (
sales_id VARCHAR2(10) PRIMARY KEY,
sales_fname VARCHAR2(35) NOT NULL,
sales_lname VARCHAR2(35) NOT NULL,
sales_email VARCHAR2(35) NOT NULL,
sales_region VARCHAR2(35) NOT NULL CHECK(sales_region IN ('NORTH','SOUTH','EAST','WEST')),
sales_phone CHAR(10) NOT NULL,
hire_date DATE DEFAULT 01-JAN-2001 NOT NULL);
What am I overlooking?
I am getting a different error message, but probably due to the same mistake. You can't set the default to 01-JAN-2001. Perhaps just putting it in single quotes will fix it; better, to_date('01-JAN-2001', 'DD-MON-YYYY').
Put quotes on your default date:
hire_date DATE DEFAULT '01-JAN-2001' NOT NULL);

Invalid Datatype trying to alter table [duplicate]

This question already has answers here:
Set ORACLE table fields default value to a formular
(2 answers)
Closed 8 years ago.
I got a table which I used the below code to create.
create table Meter (MeterID CHAR(8) CONSTRAINT MeterPK PRIMARY KEY,
Value CHAR(8) CONSTRAINT ValueNN NOT NULL,
InstalledDate Date CONSTRAINT InDateNN NOT NULL);
Then I tried adding a derived column that adds 6 months to the installeddate.
alter table meter add ExpiryDate as (add_months(installedDate,6)) not null;
This returns an error of invalid datatype.
I read somewhere that I do not have to specify the datatype of ExpiryDate as it can be derived from the function. So where did I go wrong?
EDIT: Turns out Mike was right. I used the trigger method to get things going, but I was confused whether I'm using mysql or oracle. Think in the end I'm using oracle actually. Have problems with the trigger but turns out I do not need to have the command "set" in the trigger. Below is the code that works.
CREATE OR REPLACE
TRIGGER trigexpdate1
BEFORE INSERT ON Meter
FOR EACH ROW
BEGIN
:NEW.ExpiryDate := ADD_MONTHS(:NEW.InstalledDate, 6);
END;
If I don't have the begin and end in the statement, it will throw an error saying illegal trigger specification.
MySQL doesn't support
derived columns in table definitions,
a function named add_months(), or
inline constraints.
This is a more or less standard way to write that statement in MySQL.
create table `Meter` (
`MeterID` CHAR(8) NOT NULL,
`Value` CHAR(8) NOT NULL,
`InstalledDate` Date NOT NULL,
primary key (`MeterID`)
);
You have two options for a derived column like "ExpiryDate".
Create a view, and do the date arithmetic in the view. Use date_add().
Add the column "ExpiryDate" to the table, and keep it up-to-date with a trigger.
BEFORE INSERT trigger example
create table `Meter` (
`MeterID` CHAR(8) NOT NULL,
`Value` CHAR(8) NOT NULL,
`InstalledDate` Date NOT NULL,
`ExpiryDate` Date not null,
primary key (`MeterID`)
);
create trigger trigexpdate1
before insert on `Meter`
FOR EACH ROW
SET NEW.`ExpiryDate` = date_add(NEW.`InstalledDate`, interval 6 month);
Note how ExpiryDate changes from the insert statement to the select statement below.
insert into Meter
values ('1', '1', '2014-07-01', '2014-07-01');
select * from Meter;
MeterID Value InstalledDate ExpiryDate
--
1 1 2014-07-01 2015-01-01