Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to insert NULL in to date field Oracle SQL Developer

Tags:

sql

oracle

CREATE TABLE pledge
(
    pledge_ID            NUMBER NOT NULL ,
    pledge_endDate       DATE NULL ,
    pledge_startDate     DATE NULL  ,
    pledge_amount        DECIMAL(9,2) NULL  CONSTRAINT  Currency_1322638346 CHECK (pledge_amount >= 0),
    artist_userID        NUMBER NOT NULL,
    follower_userID      NUMBER NOT NULL, 
CONSTRAINT  XPKPledge PRIMARY KEY (pledge_ID),
CONSTRAINT gets FOREIGN KEY (artist_userID) REFERENCES ArtistMember (user_ID),
CONSTRAINT makes FOREIGN KEY (follower_userID) REFERENCES FollowerMember (user_ID)
);

When I try to insert a null value I get the error below.

INSERT INTO pledge VALUES(559, 'null','1-FEB-2016', 3850, 85275, 88128);

Error report -
SQL Error: ORA-00904: : invalid identifier
00904. 00000 -  "%s: invalid identifier"
*Cause:    
*Action:
Error starting at line : 209 in command -
INSERT INTO pledge VALUES(559, 'NULL','1-FEB-2016', 3850, 85275, 88128)
Error at Command Line : 209 Column : 13
Error report -
SQL Error: ORA-00942: table or view does not exist
00942. 00000 -  "table or view does not exist"
*Cause:    
*Action:
like image 219
meeshi Avatar asked Mar 18 '16 22:03

meeshi


Video Answer


1 Answers

The SQL Error: ORA-00904: : invalid identifier is probably being caused because your FOREIGN KEYs are referencing a column that does not exist - check that the column names are spelt correctly and that should solve it (and then your CREATE TABLE statement will work).

CREATE TABLE ArtistMember (
  user_ID INT PRIMARY KEY
);

CREATE TABLE FollowerMember (
  user_ID INT PRIMARY KEY
);

CREATE TABLE pledge (
    pledge_ID            INT CONSTRAINT  XPKPledge PRIMARY KEY,
    pledge_endDate       DATE NULL,
    pledge_startDate     DATE NULL,
    pledge_amount        DECIMAL(9,2) NULL  CONSTRAINT  Currency_1322638346 CHECK (pledge_amount >= 0),
    artist_userID        INT NOT NULL CONSTRAINT gets REFERENCES ArtistMember (user_ID),
    follower_userID      INT NOT NULL CONSTRAINT makes REFERENCES FollowerMember (user_ID)
);

INSERT INTO ArtistMember VALUES ( 85275 );
INSERT INTO FollowerMember VALUES( 88128 );
INSERT INTO pledge VALUES(
  559,
  NULL,              -- Use NULL and not 'NULL'
  DATE '2016-02-01', -- Use a Date literal and not a string literal
  3850,
  85275,
  88128
);

If you just use the string in '1-FEB-2016' then Oracle will implicitly try to convert the string literal using the TO_DATE() function with the NLS_DATE_FORMAT session parameter as the format mask. If they match then it will work but this is a client variable so can be changed and then the query will break without the code having changed (and be a pain to debug). The simple answer is to ensure that you compare date value by either using TO_DATE() and specifying the format mask (as per the query above) or to use an ANSI date literal DATE '2016-02-01' (which is independent of the NLS settings).

like image 102
MT0 Avatar answered Sep 25 '22 13:09

MT0