简体   繁体   中英

Insertion in database of type timestamp and data containing apostrophe using ORACLE-11g

I have creater the table successfully as follows:

CREATE TABLE TOY_STORE
(
  TOY_STORE_ID NUMBER(3) PRIMARY KEY,
  TOY_STORE_NAME VARCHAR2(30) NOT NULL,
  CITY VARCHAR2(30) DEFAULT 'Delhi',
  PHONENUMBER NUMBER(10) NOT NULL UNIQUE,
  STORE_OPENING_TIME TIMESTAMP,
  STORE_CLOSING_TIME TIMESTAMP
);

ALTER TABLE TOY_STORE ADD CHECK (EXTRACT(HOUR FROM CAST (TO_CHAR (STORE_OPENING_TIME, 'YYYY-MON-DD HH24:MI:SS') AS TIMESTAMP)) > 8 || NULL);

ALTER TABLE TOY_STORE ADD CHECK (EXTRACT(HOUR FROM CAST(TO_CHAR(STORE_CLOSING_TIME, 'YYYY-MON-DD HH24:MI:SS') AS TIMESTAMP)) < 22 || NULL);

Now I want to enter data in the table. I executed the following command (here the second data is "Kid's Cave"),

INSERT INTO TOY_STORE VALUES(1, 'Kid''s Cave', 'Delhi', 9912312312, 2014-04-01 09:10:12, 2014-04-01 21:42:05); 

But it showed the following error..

ORA-00917: missing comma

Please explain

You need to put the dates inside '' . Try this:

INSERT INTO TOY_STORE 
VALUES(1, 'Kid''s Cave', 'Delhi', 9912312312, '2014-04-01 09:10:12', '2014-04-01 21:42:05'); 

On a side note:

I will suggest you to use varchar() to store PhoneNumbers instead of Number datatype

日期在查询中没有单引号-像这样使用:

'2014-04-01 09:10:12', '2014-04-01 21:42:05'

Try using to_date to convert string into date

INSERT INTO TOY_STORE 
VALUES (1, 'Kid''s Cave', 'Delhi', 9912312312, 
        to_date('2014-04-01 09:10:12', 'yyyy-mm-dd hh24:mi:ss'),
        to_date('2014-04-01 21:42:05', 'yyyy-mm-dd hh24:mi:ss'));

you can also use TIMESTAMP literal

INSERT INTO TOY_STORE 
VALUES (1, 'Kid''s Cave', 'Delhi', 9912312312, 
        TIMESTAMP '2014-04-01 09:10:12',
        TIMESTAMP '2014-04-01 21:42:05');

The problem isn't the ' , it's the fact that you are not quoting the date literals. Oracle interprets it as several integer literals with operators between them, and fails because there's no comma separating them.

Surrounding them by quotes ( ' ) should work:

INSERT INTO TOY_STORE VALUES 
(1, 
 'Kid''s Cave', 
 'Delhi', 
 9912312312, 
 '2014-04-01 09:10:12', 
 '2014-04-01 21:42:05'); 

But it's a bad practice, as it assumes the format matches the database's default date format, which makes your code error prone and unprortable. A better approach would be to explicitly convert these values to timestamp s with an explicitly stated format:

INSERT INTO TOY_STORE VALUES 
(1, 
 'Kid''s Cave', 
 'Delhi', 
 9912312312, 
 TO_TIMESTAMP('2014-04-01 09:10:12', 'YYYY-MM-DD HH24:MI:SS'), 
 TO_TIMESTAMP('2014-04-01 21:42:05', 'YYYY-MM-DD HH24:MI:SS')); 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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