简体   繁体   English

如何在日期字段Oracle SQL Developer中插入NULL

[英]How to insert NULL in to date field Oracle SQL Developer

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:

The SQL Error: ORA-00904: : invalid identifier is probably being caused because your FOREIGN KEY s 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). SQL Error: ORA-00904: : invalid identifier可能导致SQL Error: ORA-00904: : invalid identifier因为您的FOREIGN KEY引用了一个不存在的列 - 检查列名是否拼写正确且应解决它(然后您的CREATE TABLE语句将工作)。

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. 如果您只使用'1-FEB-2016'的字符串,那么Oracle将隐式尝试使用TO_DATE()函数转换字符串文字,并使用NLS_DATE_FORMAT会话参数作为格式掩码。 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). 简单的答案是确保您使用TO_DATE()和指定格式掩码(根据上面的查询)比较日期值,或使用ANSI日期文字DATE '2016-02-01' (它独立于NLS设置)。

Insert an empty string '' to a NUMBER column, Oracle will insert NULL . 将空字符串''插入NUMBER列,Oracle将插入NULL

For example, col_2 has data type is date field or number(12,0) (any datatype what not date field), SQL statement set NULL value for col_2 like this: 例如, col_2数据类型是日期字段或number(12,0) (任何数据类型不是日期字段),SQL语句为col_2设置NULL值,如下所示:

insert into foo (col_1, col_2) values ('abc', '');

or 要么

insert into foo (col_1, col_2) values ('abc', NULL);

(it is NULL , not 'NULL' or "NULL" ) (它是NULL ,不是'NULL'"NULL"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM