Oracle SQL having troubles inserting values into a table - sql

Trying to insert some values into a table and always showing me some errors, i think it has something to do with the date or smth but not quite sure about that, if anyone knows what's wrong, please let me know...
The error that currently shows me is:
ORA-01861: literal does not match format string ORA-06512: at "SYS.DBMS_SQL", line 1721
INSERT INTO Cats VALUES ('JACEK','M','CAKE','CATCHING','BALD','2008-12-01',67,NULL,2);
Here's the table:
CREATE TABLE Cats (
name VARCHAR2(15) CONSTRAINT cat_name_nn NOT NULL,
gender VARCHAR2(1) CONSTRAINT cat_gen_ch CHECK (gender IN('M', 'W')),
nickname VARCHAR2(15) CONSTRAINT cat_pk PRIMARY KEY,
function VARCHAR2(10),
chief VARCHAR2(15),
in_herd_since DATE DEFAULT SYSDATE CONSTRAINT cat_inherd_nn NOT NULL,
mice_ration NUMBER(3),
mice_extra NUMBER(3),
band_no NUMBER(2),
CONSTRAINT cat_banno_fk FOREIGN KEY (band_no) REFERENCES Bands(band_no),
CONSTRAINT cat_chief_fk FOREIGN KEY (chief) REFERENCES Cats(nickname),
CONSTRAINT cat_fun_fk FOREIGN KEY (function) REFERENCES Functions(function)
);

when entering character values for a column whose data type is Date you have to convert it into a DATE by using the TO_DATE function. In you case the column in_herd_since is of DATE type , you should try this
TO_DATE('2008-12-01', 'YYYY-MM-DD') -- untested
INSERT INTO Cats VALUES ('JACEK','M','CAKE','CATCHING','BALD',
TO_DATE('2008-12-01', 'YYYY-MM-DD'),67,NULL,2);

Related

ORA-00907: missing right parenthesis but I cant see it?

So this is my code for a homework
CREATE TABLE workorders
(
wo# NUMBER(5) PRIMARY KEY,
proj# VARCHAR(10) NOT NULL FOREIGN KEY REFERENCES project(proj#),
wo_desc VARCHAR(30) NOT NULL UNIQUE,
wo_assigned VARCHAR(30),
wo_hours NUMBER(5) NOT NULL CHECK(wo_hours>0),
wo_start DATE,
wo_due DATE,
wo_complete CHAR(1),
CONSTRAINT workorders_wo_complete_chk CHECK(wo_complete in('Y','N'))
);
I could not figure out why oracle apex won't let me create this table, it says
ORA-00907: missing right parenthesis
But I double-checked so many times and I think I do have all the parenthesis? What did I do wrong here?
Thanks in advance
I just want to create this table under these constreaints but I could find any errors that I know of.
Oracle's error messages are often not particularly helpful, and the best thing to do is to go through your code, line by line, commenting out each line until you isolate the problem.
In this case the offending line is
proj# VARCHAR(10) NOT NULL FOREIGN KEY REFERENCES project(proj#),
and the problem is that you don't use the words FOREIGN KEY to define a foreign key - you just need to specify what it references. So this should be
proj# VARCHAR(10) NOT NULL REFERENCES project(proj#),
and then you'll be fine.
db<>fiddle here
When defining an inline constraint, the FOREIGN KEY terms are not used:
proj# VARCHAR2(10) NOT NULL
REFERENCES project(proj#)
Note: Oracle uses VARCHAR2 and VARCHAR is an alias to VARCHAR2 and it is considered best-practice to use VARCHAR2 throughout.
However, those keywords are required when you define an out-of-line foreign key (but then you would be missing a comma and the column identifier):
proj# NOT NULL,
FOREIGN KEY (proj#) REFERENCES project (proj#)
Note: If you are using a foreign key constraint then you do not need to define the data type and Oracle will implicitly use the data type of the column being referenced.
The complete code using (named) inline constraints throughout would be:
CREATE TABLE workorders
(
wo# NUMBER(5)
CONSTRAINT workorders__wo#__pk PRIMARY KEY,
proj# -- Note: do not need the data type as the FK will define it.
NOT NULL
CONSTRAINT workorders__proj#__fk REFERENCES project(proj#),
wo_desc VARCHAR2(30)
NOT NULL
CONSTRAINT workorders__wo_desc__u UNIQUE,
wo_assigned VARCHAR2(30),
wo_hours NUMBER(5)
NOT NULL
CONSTRAINT workorders__wo_hours__chk CHECK(wo_hours>0),
wo_start DATE,
wo_due DATE,
wo_complete CHAR(1)
CONSTRAINT workorders_wo_complete_chk CHECK(wo_complete in('Y','N'))
);
and using (named) out-of-line constraints throughout:
CREATE TABLE workorders
(
wo# NUMBER(5),
proj# -- Note: do not need the data type as the FK will define it.
NOT NULL,
wo_desc VARCHAR2(30)
NOT NULL,
wo_assigned VARCHAR2(30),
wo_hours NUMBER(5)
NOT NULL,
wo_start DATE,
wo_due DATE,
wo_complete CHAR(1),
CONSTRAINT workorders__wo#__pk PRIMARY KEY(wo#),
CONSTRAINT workorders__proj#__fk FOREIGN KEY (proj#) REFERENCES project(proj#),
CONSTRAINT workorders__wo_desc__u UNIQUE(wo_desc),
CONSTRAINT workorders_wo_complete_chk CHECK(wo_complete in('Y','N')),
CONSTRAINT workorders__wo_hours__chk CHECK(wo_hours>0)
);
fiddle

cant reference sport CHAR(6) to other column error

As the title says, for some reason it gives me the error that the sport variable cant reference to another column when I am not even referencing it, any reason for this?
CREATE TABLE club_rates
(club_id NUMBER(4)
CONSTRAINT rates_club_fk REFERENCES club_subscriptiontypes(club_id,subscriptiontype)
,sport CHAR(6)
,subscriptiontype CHAR(11)
,subscription_startdate DATE
,subscription_rate_existing NUMBER(2)
,subscription_rate_new NUMBER(2)
,subscription_enddate DATE
,registration_startdate DATE
,registration_enddate DATE,
CONSTRAINT rates_clubsportsub_pk PRIMARY KEY (club_id, sport, subscriptiontype, subscription_startdate)
);
Assuming you have the table:
CREATE TABLE club_subscriptiontypes(
club_id NUMBER(4),
subscriptiontype CHAR(11),
PRIMARY KEY (club_id,subscriptiontype)
);
Then you do not want to declare a composite primary key column inline against a single column or you get the error:
ORA-02256: number of referencing columns must match referenced columns
Instead, declare it out-of-line at the end of the statement:
CREATE TABLE club_rates(
club_id NUMBER(4)
, sport CHAR(6)
, subscriptiontype CHAR(11)
, subscription_startdate DATE
, subscription_rate_existing NUMBER(2)
, subscription_rate_new NUMBER(2)
, subscription_enddate DATE
, registration_startdate DATE
, registration_enddate DATE
, CONSTRAINT rates_club_fk
FOREIGN KEY (club_id, subscriptiontype)
REFERENCES club_subscriptiontypes(club_id,subscriptiontype)
, CONSTRAINT rates_clubsportsub_pk
PRIMARY KEY (club_id, sport, subscriptiontype, subscription_startdate)
);
As an aside, you probably don't want to use fixed-length CHAR strings and, instead, want variable-length VARCHAR2 strings.
db<>fiddle here

I'm getting "Invalid Identifier Error" for one of my Oracle Apex Queries

I'm trying to run a script on Oracle Apex and so far all the tables and queries work except the last one. It returns the error "ORA-00904: : invalid identifier ORA-06512: at "SYS.WWV_DBMS_SQL_APEX_200200", line 626 ORA-06512: at "SYS.DBMS_SYS_SQL", line 1658 ORA-06512: at "SYS.WWV_DBMS_SQL_APEX_200200", line 612 ORA-06512: at "APEX_200200.WWV_FLOW_DYNAMIC_EXEC", line 1749." What should I do to fix this error?
CREATE TABLE customer(
VIN VARCHAR2(17)
CONSTRAINT vehicles__VIN__fk
REFERENCES vehicles (VIN) ON DELETE SET NULL,
sale_date DATE
CONSTRAINT sales__sale_date__fk
REFERENCES sales (sale_date) ON DELETE SET NULL,
c_name VARCHAR2(50)
CONSTRAINT sales__c_name__nn NOT NULL,
address VARCHAR2(50)
CONSTRAINT sales__address__nn NOT NULL,
phone VARCHAR2(11)
CONSTRAINT sales__phone__nn NOT NULL,
gender VARCHAR2(6)
CONSTRAINT sales__gender__nn NOT NULL,
a_income VARCHAR2(30)
CONSTRAINT sales__a_income__nn NOT NULL,
);
I don't know if it helps but VIN and sale_date reference these two working queries:
CREATE TABLE vehicles(
VIN VARCHAR2(17)
CONSTRAINT vehicles__VIN__pk PRIMARY KEY,
brand VARCHAR2(20)
CONSTRAINT vehicles__brand__nn NOT NULL,
model VARCHAR2(20)
CONSTRAINT vehicles__model__nn NOT NULL,
color VARCHAR2(10)
CONSTRAINT vehicles__color__nn NOT NULL
);
CREATE TABLE sales(
sale_date DATE,
price VARCHAR2(30)
CONSTRAINT sales__price__nn NOT NULL,
VIN VARCHAR2(17)
CONSTRAINT vehicles__VIN__fk
REFERENCES vehicles (VIN) ON DELETE SET NULL,
d_id VARCHAR2(10)
CONSTRAINT dealer__d_id__fk
REFERENCES dealer (d_id) ON DELETE SET NULL,
CONSTRAINT sales__sale_date__pk PRIMARY KEY (sale_date)
);
Remove the last comma.
Also, if your constraints have the naming convention <tablename>__<columnname>__<constrainttype> then don't just copy/paste from another table and update the column name; you need to update the table name as well or you will find you have duplicate constraint names which will raise an exception.
CREATE TABLE customer(
VIN VARCHAR2(17)
CONSTRAINT customer__VIN__fk
REFERENCES vehicles (VIN) ON DELETE SET NULL,
sale_date DATE
CONSTRAINT customer__sale_date__fk
REFERENCES sales (sale_date) ON DELETE SET NULL,
c_name VARCHAR2(50)
CONSTRAINT customer__c_name__nn NOT NULL,
address VARCHAR2(500)
CONSTRAINT customer__address__nn NOT NULL,
phone VARCHAR2(11)
CONSTRAINT customer__phone__nn NOT NULL,
gender CHAR(1)
CONSTRAINT customer__gender__nn NOT NULL
CONSTRAINT customer__gender__chk
CHECK ( gender IN ( 'M', 'F' /*, 'A', 'B', 'C'*/ ) ),
a_income NUMBER(12,2)
CONSTRAINT customer__a_income__nn NOT NULL
);
Then comes the other questions:
Why does a customer have a VIN (vehicle identification number)? A customer is not limited to owning a single car.
Why does a customer have a sale_date? If you are referring to a car sale then why is the customer limited to a single sale? You probably want to fix both these two by moving the data to a separate table called customer_cars (or something similar) so that each customer can own multiple cars and each car can be owned by multiple customers (at different times).
Do you expect all customers' addresses to fit in 50 characters?
Why is gender a VARCHAR(6) and not a CHAR(1) with values M/F (extend with additional character codes as appropriate)?
Why is a_income a string and not a NUMBER?

Attempting to create a table with composite keys and date and time data types. Only get errors

OK so here is the table and data types I am attempting to create in sql developer:
Table_Name interview
Field_Name e_number, pos_id, date, time, awarded
Data_Type varchar2(9), varchar2(15), date, number(9), varchar2(3)
Nulls_Allowed
Primary_Key y, y, n, n, n
Unique y, y, n, n, n
Foreign_Key
Comments
Here is the sql I am trying to use:
CREATE TABLE interview(
e_number VARCHAR2(9) NOT NULL UNIQUE,
pos_id VARCHAR2(15) NOT NULL UNIQUE,
date DATE NOT NULL,
time NUMBER(8) NOT NULL,
awarded VARCHAR2(3),
CONSTRAINT pk_interview PRIMARY KEY (e_number, pos_id),
CONSTRAINT fk_e_number FOREIGN KEY (e_number) REFERENCES student
(e_number),
CONSTRAINT fk_pos_id FOREIGN KEY (pos_id) REFERENCES position
(pos_id)
Can someone to tell me where I have "invalid identifier"? Cause I am completely lost.
CREATE TABLE interview(
e_number VARCHAR2(9) NOT NULL UNIQUE,
pos_id VARCHAR2(15) NOT NULL UNIQUE,
"date" DATE NOT NULL,
"time" NUMBER(8) NOT NULL,
awarded VARCHAR2(3),
CONSTRAINT pk_interview PRIMARY KEY (e_number, pos_id),
CONSTRAINT fk_e_number FOREIGN KEY (e_number) REFERENCES student (e_number),
CONSTRAINT fk_pos_id FOREIGN KEY (pos_id) REFERENCES position (pos_id)
);
As suggested by #Remy_Lebeau, #CLifford, and #amdixon
date and time are reserved words (see this post), so enclose them in quotations. Also, add right parentheses and semicolon to close your table declaration.

Violated - parent key not found 02291. 00000 - "integrity constraint

Hi I am developing a database in Oracle SQL dev, that is trying to access foriegn keys from another table. I am currently working on the ItemOrdered table which I've created with the following CREATE statement
CREATE TABLE ItemOrdered(OrderID varchar2(9) REFERENCES Ords(OrderID),
BeltID varchar2(9) REFERENCES BeltID(BeltID),
Quantity varchar(4) NOT NULL,
PRIMARY KEY(OrderID, BeltID))
As you can See I have the following foriegn keys Ords and BeltID.
Now when I try to run the following statement
INSERT INTO ItemOrdered VALUES(401565981,234489212,'2')
It gives me the following error
violated - parent key not found
02291. 00000 - "integrity constraint (%s.%s) violated - parent key not found"
I have provided my Ords CREATE statement if its needed
CREATE TABLE Ords(OrderID varchar2(9) PRIMARY KEY,
CustomerID varchar(9) REFERENCES Customers(CustomerID),
Expected_Delivery_Date date DEFAULT sysdate NOT NULL,
Actual_Delivery_Date date DEFAULT sysdate NOT NULL,
Payment_Due_Date date DEFAULT sysdate NOT NULL,
Order_Date date DEFAULT sysdate NOT NULL, Price Varchar(10),
Order_Placed varchar2(1) CONSTRAINT OrderPlaced
CHECK(Order_Placed IN('Y','N')) NOT NULL,
Order_Confirmed varchar2(1)
CONSTRAINT Order_Confirmed CHECK(Order_Confirmed IN('Y','N')) NOT NULL,
Order_Completed varchar2(1) CONSTRAINT Order_Completed
CHECK(Order_Completed IN('Y','N')) NOT NULL)
And I have also provided my BeltID CREATE statement
CREATE TABLE BeltID(BeltID varchar2(9) PRIMARY KEY,
BeltLengthID varchar2(9) REFERENCES BeltLength(BeltLengthID),
ColourID varchar2(9) REFERENCES Colour(ColourID),
DesignID varchar2(9) REFERENCES Design(DesignID),ComponentID varchar2(9) REFERENCES Component(ComponentID))
I don't seem to quite understand why I am getting this error. Is there an clear explanation why?
Here is the http link of what I am trying to do.
link text
Due to the foreign key constraints you specified when you created table ItemOrdered, when you perform this insert:
INSERT INTO ItemOrdered VALUES(401565981,234489212,'2')
... the values 401565981 and 234489212 must correspond to key values in the Ords and BelitId tables respectively - i.e. these 2 queries should return rows:
select *
from Ords
where OrderId = 401565981;
select *
from BeltId
where BeltId = 234489212;
The error message suggests this is not the case.
I have no Oracle installation available so I can't test, but does it matter if you enclose the OrderID and BeltID in single quotes (which you should do anyway since the columns are declared as varchars)? I haven't tested it, but one idea would be that Oracle barfs on the fact that you are inserting a numeric value into a varchar column.
insert
into ItemOrdered VALUES('401565981', '234489212','2')
Another option would be that the order of the columns in the table does not correspond to the order you gave them in the insert statement. To rule this out, try rewrite the statement as:
insert
into ItemOrdered(OrderID,BeltID,Quantity) values('401565981', '234489212','2')