SQL : can not create a table - sql

I do the following:
use oracle developer
create table loan
(
barcode number (20) not null ,
borrowernumber number (7) ,
loancurrentdate date ,
loanreturndate date ,
loanreserveorder number (20) ,
paymentdate date ,
borrower_borrowernumber number not null
);
alter table loan add constraint loan_pk primary key (barcode) ;
Cannot create a table, then change it ...
create table loan
(
loanno INT not null,
loandate date not null,
loanreturndate date null,
loanreserve number (20) null,
paymentdate date null,
);
alter table loan add constraint loan_pk primary key (loanno) ;
I get the following error. Why am I getting this?
Error report
SQL Error: ORA-00942: table or view does not exist
00942. 00000 - "table or view does not exist"
It also shows "invalid identifier"
Error report:
SQL Error: ORA-00904: : invalid identifier
00904. 00000 - "%s: invalid identifier"
Or should I delete paymentdate?
After delete the wrong comma, the table created.

Both examples you posted are correct (apart from that superfluous comma at the end of the second CREATE TABLE statement). This are SQL*Plus examples, but - as SQL Developer emulates it well, I believe that they should work just fine there too.
The first one:
SQL> create table loan
2 (
3 barcode number (20) not null ,
4 borrowernumber number (7) ,
5 loancurrentdate date ,
6 loanreturndate date ,
7 loanreserveorder number (20) ,
8 paymentdate date ,
9 borrower_borrowernumber number not null
10 );
Table created.
SQL> alter table loan add constraint loan_pk primary key (barcode) ;
Table altered.
The second one:
SQL> drop table loan;
Table dropped.
SQL> create table loan
2 (
3 loanno INT not null,
4 loandate date not null,
5 loanreturndate date null,
6 loanreserve number (20) null,
7 paymentdate date null
8 );
Table created.
SQL> alter table loan add constraint loan_pk primary key (loanno) ;
Table altered.
SQL>
What do you exactly mean by saying "Cannot create a table, then change it ..." after the first CREATE TABLE? Any error? If so, which one?
"ORA-00942: table or view does not exist" is probably raised by ALTER TABLE as you can't alter it if it doesn't exist (but the mystery is why you can't create it).
"ORA-00904: : invalid identifier" means that you used a column name which doesn't exist in the table, such as
SQL> alter table loan modify xxx number;
alter table loan modify xxx number
*
ERROR at line 1:
ORA-00904: "XXX": invalid identifier
If possible, do the same as I did - copy/paste SQL*Plus session so that we could see what you did and how Oracle responded.

This should work
drop table loan;
create table loan
(
loanno INT not null,
loandate date not null,
loanreturndate date null,
loanreserve number (20) null,
paymentdate date null
);

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

How do I fix my my table so I don't get that the invalid datatype message?

I am trying to create a table in SQL and every time it I get the following error message:
ORA-00902: invalid datatype
SQL> create table BUSINESS (
2 B_IDINTEGER PRIMARY KEY,
3 B_CITYchar(20) not null,
4 B_NAMECHAR (20) NOT NULL,
5 B_CATEGORY(S) CHAR (25),
6 B_ACCTCHAR (25)
7 );
B_CITYchar(20) not null,
*
ERROR at line 3:
ORA-00902: invalid datatype
It is supposed to say table created but I don't know what is wrong with line 3.
You have several errors in your code. Try something like this:
create table BUSINESS (
B_ID INTEGER PRIMARY KEY,
B_CITY varchar2(20) not null,
B_NAME varchar2(20) NOT NULL,
B_CATEGORY varchar2(25),
B_ACCT varchar2(25)
);
Note that you should generally use variable length strings unless you know the value has a fixed length (which might be true of b_acct but is not true for b_city).

Create Table / Sequence / Trigger in Apex Oracle SQL - ORA-00922 / ORA-00907 / ORA-00922

Using: Application Express 18.1.0.00.45
Please note: I am very new to Oracle Apex and SQL.
For a project I have to create an Application straight in Apex.
I was trying to create a table that works with a Primary Key that auto-increments itself.
Yesterday I created my first Application with a page for user input in a table, a page with table display and filter option and was playing around with forms, dashboard and authentication methods.
I removed the app after playing to start the "Real Work", but I my enthusiasm went quickly away when I realized that I am doing something very wrong but am not sure what.. :)
After lots of googling / reading etc, I am still not sure what is wrong..
See below the code:
-- Create Sequence
CREATE SEQUENCE seq_odm_for_pk
START WITH 1
INCREMENT BY 1
CACHE 100;
-- Create Table for User Input
CREATE TABLE ODM_Progress_v1 (
-- General Details:
ID int NOT NULL AUTO_INCREMENT,
TRAINEE varchar(50) NOT NULL, --1
COACH varchar(50) NOT NULL, --2
STATUS varchar(50) NOT NULL, --3
REGION varchar(5) NOT NULL, --4
-- Actions:
ACTION_TAKEN varchar(100) NOT NULL, --5
ACTION_DETAILS varchar(250), --6
ACTIONED_BY varchar(50) NOT NULL, --7
ACTIONED_DATE DATE NOT NULL, --8
-- Constraints that perform checks for each column:
CONSTRAINT CHK_GeneralDetails CHECK (TRAINEE!=COACH AND (STATUS IN('New', 'In Progress', 'Completed')) AND (REGION IN('EMEA', 'APAC', 'AMER'))),
-- Set Primary Key (Trainee+Coach):
CONSTRAINT PK_ODMProgress PRIMARY KEY (TRAINEE,REGION,ID)
);
-- Create Trigger
CREATE trigger_for_pk_odm_progress
BEFORE INSERT ON ODM_Progress_v1
FOR EACH ROW
WHEN (new.ID is null)
BEGIN
select seq_odm_for_pk.nextval into :new.ID from DUAL;
-- :new.PK_ODMProgress := seq_odm_for_pk.nextval;
END;
The script finishes running with 3 errors, see below:
CREATE OR REPLACE SEQUENCE seq_odm_for_pk START WITH 1
INCREMENT BY 1 CACHE 100
ORA-00922: missing or invalid option
CREATE TABLE ODM_Progress_v1 ( -- General Details: ID int NOT
NULL AUTO_INCREMENT, TRAINEE varchar(50) NOT NULL, --1 COACH
varchar(50) NOT NULL, --2 STATUS varchar(50) NOT NULL, --3
REGION varchar(5) NOT NULL, --4 -- Actions: ACTION_TAKEN
varchar(100) NOT NULL, --5 ACTION_DETAILS varchar(250), --6
ACTIONED_BY varchar(50) NOT NULL, --7 ACTIONED_DATE DATE NOT NULL,
--8 -- Constraints that perform checks for each column: CONSTRAINT CHK_GeneralDetails CHECK (TRAINEE!=COACH AND (STATUS
IN('New', 'In Progress', 'Completed')) AND (REGION IN('EMEA', 'APAC',
'AMER'))), -- Set Primary Key (Trainee+Coach): CONSTRAINT
PK_ODMProgress PRIMARY KEY (TRAINEE,REGION,ID) )
ORA-00907: missing right parenthesis
CREATE OR REPLACE trigger_for_pk_odm_progress BEFORE INSERT ON
ODM_Progress_v1 FOR EACH ROW WHEN (new.ID is null) BEGIN SELECT
seq_odm_for_pk.nextval INTO :new.ID FROM DUAL; --
:new.PK_ODMProgress := seq_odm_for_pk.nextval; END;
ORA-00922: missing or invalid option
Can you please help me unravel this (to me, complete) mystery?
The final application should at least contain 1 table with primary key and sequence (created from scratch, see above) and have at least 2 pages, one is for data input, the other is for data display with use tabs or navigation menu.
Thank you in advance!
That code looks OK, more or less. Here you go:
SQL> CREATE SEQUENCE seq_odm_for_pk
2 START WITH 1
3 INCREMENT BY 1
4 CACHE 100;
Sequence created.
Table: I'm on 11g which doesn't support auto-incremented columns, so I removed that clause:
SQL> CREATE TABLE ODM_Progress_v1 (
2 -- General Details:
3 ID int NOT NULL AUTO_INCREMENT,
4 TRAINEE varchar(50) NOT NULL, --1
5 COACH varchar(50) NOT NULL, --2
6 STATUS varchar(50) NOT NULL, --3
7 REGION varchar(5) NOT NULL, --4
8 -- Actions:
9 ACTION_TAKEN varchar(100) NOT NULL, --5
10 ACTION_DETAILS varchar(250), --6
11 ACTIONED_BY varchar(50) NOT NULL, --7
12 ACTIONED_DATE DATE NOT NULL, --8
13 -- Constraints that perform checks for each column:
14 CONSTRAINT CHK_GeneralDetails CHECK (TRAINEE!=COACH AND (STATUS IN('New', 'In Progress', 'Completed')) AND (REG
ION IN('EMEA', 'APAC', 'AMER'))),
15 -- Set Primary Key (Trainee+Coach):
16 CONSTRAINT PK_ODMProgress PRIMARY KEY (TRAINEE,REGION,ID)
17 );
ID int NOT NULL AUTO_INCREMENT,
*
ERROR at line 3:
ORA-00907: missing right parenthesis
SQL> l3
3* ID int NOT NULL AUTO_INCREMENT,
SQL> c/auto_increment//
3* ID int NOT NULL ,
SQL> /
Table created.
Trigger contains an error in line #1: it is not "trigger_for" but "trigger for" (no underscore):
SQL> CREATE trigger_for_pk_odm_progress
2 BEFORE INSERT ON ODM_Progress_v1
3 FOR EACH ROW
4 WHEN (new.ID is null)
5 BEGIN
6 select seq_odm_for_pk.nextval into :new.ID from DUAL;
7 -- :new.PK_ODMProgress := seq_odm_for_pk.nextval;
8 END;
9 /
CREATE trigger_for_pk_odm_progress
*
ERROR at line 1:
ORA-00901: invalid CREATE command
SQL> l1
1* CREATE trigger_for_pk_odm_progress
SQL> c/er_/er /
1* CREATE trigger for_pk_odm_progress
SQL> l
1 CREATE trigger for_pk_odm_progress
2 BEFORE INSERT ON ODM_Progress_v1
3 FOR EACH ROW
4 WHEN (new.ID is null)
5 BEGIN
6 select seq_odm_for_pk.nextval into :new.ID from DUAL;
7 -- :new.PK_ODMProgress := seq_odm_for_pk.nextval;
8* END;
SQL> /
Trigger created.
SQL>
So:
sequence is OK, but - for vast majority of cases - a simple create sequence seq_odm_for_pk; is enough
for CREATE TABLE remove AUTO_INCREMENT (if you aren't on 12c)
trigger: remove underscore
Now, depending on where you executed those commands, you might have got errors. If you ran them in Apex SQL Workshop, run them one-by-one (and keep only one command in the window). Doing so, it should be OK.
Also, I've noticed that you used VARCHAR datatype - switch to VARCHAR2.
Finally, there's no use in constraining primary key columns with the NOT NULL clause - primary key will enforce it by default.
As of Apex itself: the way you described it, you should create an Interactive Report; the Wizard will create a report (to view data), along with a form (to insert/modify/delete data).
The query you ran in your script is not the same as you posted in your code, as can be read in error text.
Code for creating your sequence as you wrote it in your code should be fine:
CREATE SEQUENCE seq_odm_for_pk
START WITH 1
INCREMENT BY 1
CACHE 100;
As of 11g you cannot use AUTO_INCREMENT in Oracle when creating table. It is not even necessary since you're having a trigger populating your :new.ID with nextval from sequence. So, in your CREATE TABLE remove AUTO_INCREMENT for your ID and everything should be fine.
While creating a trigger you omitted TRIGGER keyword (CREATE OR REPLACE TRIGGER trigger_for_pk_odm_progress).
Also, I'm not sure if you did or did not put END; at the end of your create trigger command. if not, put it.
I hope that helped :)

ORA-00955 during SQL Table creation

I have been getting the error during a table creation. I know it means that the table name needs to change, but I don't see any object with the same name. A copy of the .lst is below.
Thanks
SQL> CREATE TABLE CUSTOMERtable
2 (
3 CUSTOMERID INT NOT NULL,
4 CUSTNAME VARCHAR2 (50) NOT NULL,
5 ADDRESS VARCHAR2 (100) NOT NULL,
6 PHONENUMBER VARCHAR2 (10) NOT NULL,
7 CONSTRAINT IDS_CUST_PK PRIMARY KEY (CUSTOMERID)
8 );
CREATE TABLE CUSTOMERtable
*
ERROR at line 1:
ORA-00955: name is already used by an existing object
SQL>
SQL>
SQL> CREATE TABLE RENTALStable
2 (
3 RENTALID INT NOT NULL,
4 OUTDATE DATE NOT NULL,
5 INDATE DATE NOT NULL,
6 LATEFEE INT,
7 DAMAGEFEE INT,
8 REWINDFEE INT,
9 ID_CUSTOMER INT,
10 CONSTRAINT RentalsTable_IDS_PK PRIMARY KEY (RENTALID),
11 FOREIGN KEY (ID_CUSTOMER) REFERENCES CUSTOMERtable(CUSTOMERID)
12 );
Table created.
This should find the object that's creating the problem:
select *
from user_objects
where object_name = 'CUSTOMERTABLE'
Notice that your statement, even if you write CUSTOMERtable ( upper and lower case), will try to create a table named CUSTOMERTABLE (upper case).
If you want to keep two objects with the same names but different case (and it seems not a good idea to me) you should use double quotes:
CREATE TABLE "CUSTOMERtable" ( ...

Insert fails due to "column not allowed here" error

I am a beginner with SQL. I have created 4 tables and added data to my SHIP table. I am having some issues with inserting data into the CRUISE table. I get the error message at the bottom.
I have researched and can not figure out what i am doing wrong. Is there an issue with my sequence and/or trigger that is not allowing me to do this or is my syntax in the CREATE TABLE CRUISE causing the error? Everything i have done has been successful up until trying to insert the first column into the CRUISE table.
The tables:
CREATE TABLE SHIP
( Ship_Name VARCHAR2(100) PRIMARY KEY,
Ship_Size INTEGER,
Ship_Registry VARCHAR2(50),
Ship_ServEntryDate INTEGER,
Ship_PassCapacity INTEGER,
Ship_CrewCapacity INTEGER,
Ship_Lifestyle VARCHAR2(40),
CONSTRAINT Size_ck CHECK (Ship_Size > 0),
CONSTRAINT Registry_ck CHECK (Ship_Registry IN ('Norway','Liberia','The Netherlands','Bahamas'))
)
CREATE TABLE CRUISE (
Cruise_ID INTEGER Primary Key,
Ship_Name VARCHAR(100),
Cruise_DeptDate DATE NOT NULL,
Cruise_DeptCity VARCHAR(80) NOT NULL,
Cruise_Duration INTEGER,
FOREIGN KEY (Ship_Name) REFERENCES SHIP(Ship_Name)
)
CREATE TABLE PASSENGERS (
Pass_ID INTEGER PRIMARY KEY,
Pass_Name VARCHAR(100) NOT NULL,
Pass_City VARCHAR(80),
Pass_Telephone VARCHAR(15),
Pass_NextOfKin VARCHAR(100)
)
CREATE TABLE RESERVATIONS (
Pass_ID INTEGER NOT NULL,
Cruise_ID INTEGER NOT NULL,
Res_TotalCost NUMERIC(9,2),
Res_BalanceDue NUMERIC(9,2),
Res_SpecialRequest VARCHAR(30),
Res_Room VARCHAR(10),
FOREIGN KEY (Pass_ID) REFERENCES PASSENGERS(Pass_ID),
FOREIGN KEY (Cruise_ID) REFERENCES CRUISE(Cruise_ID),
CONSTRAINT Cost_ck CHECK (Res_TotalCost >= 0),
CONSTRAINT BalanceDue_ck CHECK (Res_BalanceDue >= 0),
CONSTRAINT SpecialRequest_ck CHECK (Res_SpecialRequest IN ('Vegetarian','Vegan','Low salt','Gluten free','Kosher','Other'))
)
The sequence/trigger is an attempt to auto number Cruise_ID.
Create SEQUENCE cruise_id_sq
START WITH 1
INCREMENT BY 1;
CREATE OR REPLACE TRIGGER cruise_id_t
BEFORE INSERT
ON CRUISE
REFERENCING NEW AS NEW
FOR EACH ROW
BEGIN
if(:new.Cruise_ID is null) then
SELECT cruise_id_sq.nextval
INTO :new.Cruise_ID
FROM dual;
end if;
END;
ALTER TRIGGER cruise_id_t ENABLE;
Inserting into SHIP is okay....
INSERT INTO SHIP
(Ship_Name, Ship_Size, Ship_Registry,Ship_ServEntryDate, Ship_PassCapacity,Ship_CrewCapacity,Ship_Lifestyle)
Values ('Carribean Princess',142000,'Liberia',1000,3100,1181,'Contemporary');
INSERT INTO SHIP
(Ship_Name, Ship_Size, Ship_Registry,Ship_ServEntryDate, Ship_PassCapacity,Ship_CrewCapacity,Ship_Lifestyle)
Values ('Carribean Sunshine',74000,'Norway',1992,1950,760,'Premium');
INSERT INTO SHIP
(Ship_Name, Ship_Size, Ship_Registry,Ship_ServEntryDate, Ship_PassCapacity,Ship_CrewCapacity,Ship_Lifestyle)
Values ('Ship of Dreams',70000,'Liberia',2004,1804,735,'Contemporary');
INSERT INTO SHIP
(Ship_Name, Ship_Size, Ship_Registry,Ship_ServEntryDate, Ship_PassCapacity,Ship_CrewCapacity,Ship_Lifestyle)
Values ('Sunshine of the Seas',74000,'The Netherlands',1990,2354,822,'Luxury');
Inserting into CRUISE fails...
INSERT INTO Cruise
(Ship_Name, Cruise_DeptDate,Cruise_DeptCity,CruiseDuration)
Values ('Sunshine of the Seas',25-may-15,'Miami',10);
Error starting at line : 1 in command - INSERT INTO Cruise (Ship_Name,
Cruise_DeptDate,Cruise_DeptCity,CruiseDuration) Values ('Sunshine of
the Seas',25-may-15,'Miami',10) Error at Command Line : 3 Column : 35
Error report - SQL Error: ORA-00984: column not allowed here
00984. 00000 - "column not allowed here"
*Cause:
*Action:
Oracle thinks that 25-may-15 is the expression 25 minus may minus 15. In looking up the value for may Oracle finds that there is nothing there. Thus the error.
You can, but probably don't want to, quote it like so, '25-may-15'. This will make a string that may or may not be implicitly converted to a date, depending on the settings of NLS_DATE_FORMAT and or NLS_TERRITORY.
To form a date independent of session setting one can use the TO_DATE function with explicit date format, to_date('25-may-15', 'DD-Mon-YY'). Another option is a date literal, date '2015-05-25', which is always YYYY-MM-DD no matter the session settings..