Oracle插入脚本错误

时间:2014-08-18 14:35:14

标签: sql oracle

好的所以我正在寻找上学的oracle,并且在创建数据库时遇到了很多问题。我正在使用SQL开发人员。我的插入有多个错误。通过浏览我没有看到脚本有任何问题。有人可以看一遍并告诉我我做错了什么吗?我是新来的,所以对我很轻松。

`DROP TABLE ENROLLMENT CASCADE CONSTRAINTS PURGE;
DROP TABLE COURSE_SECTION CASCADE CONSTRAINTS PURGE;
DROP TABLE COURSE CASCADE CONSTRAINTS PURGE;
DROP TABLE TERM CASCADE CONSTRAINTS PURGE;
DROP TABLE STUDENT CASCADE CONSTRAINTS PURGE;
DROP TABLE FACULTY CASCADE CONSTRAINTS PURGE;
DROP TABLE LOCATION CASCADE CONSTRAINTS PURGE; 

CREATE TABLE Location
(Locid NUMBER (5) Primary Key,
Bldg_code VARCHAR2 (10) NOT NULL,
Room VARCHAR2 (6) NOT NULL,
Capacity NUMBER (5) 
);

INSERT INTO Location VALUES
(53, BUS, 424, 45);
INSERT INTO Location VALUES
(54, BUS, 402, 35);
INSERT INTO Location VALUES
(55, BUS, 433, 100);
`

2 个答案:

答案 0 :(得分:1)

您的VARCHAR2值必须使用单引号:

insert into location values
(53, 'BUS', '424', 45);

等...

答案 1 :(得分:0)

BUS必须是'BUS':

INSERT INTO Location VALUES
(53, 'BUS', 424, 45);
INSERT INTO Location VALUES
(54, 'BUS', 402, 35);
INSERT INTO Location VALUES
(55, 'BUS', 433, 100);

将数值传递给varchar2列时,oracle会自动将其转换为varchar2:

第一次会议:

SQL> conn hr/hr
Connected.
SQL> create table table_for_mine(
  2    id varchar2(20)
  3  );

Table created.

SQL> insert into table_for_mine values (123);

1 row created.

第二节:

SQL> conn / as sysdba

SQL> l
  1* SELECT distinct member LOGFILENAME FROM V$LOGFILE
SQL> /

LOGFILENAME
--------------------------------------------------------------------------------
/u01/app/oracle/oradata/orcl/redo01.log
/u01/app/oracle/oradata/orcl/redo03.log
/u01/app/oracle/oradata/orcl/redo02.log

SQL> execute DBMS_LOGMNR.ADD_LOGFILE('/u01/app/oracle/oradata/orcl/redo01.log');

PL/SQL procedure successfully completed.

SQL> execute DBMS_LOGMNR.ADD_LOGFILE('/u01/app/oracle/oradata/orcl/redo02.log');

PL/SQL procedure successfully completed.

SQL> execute DBMS_LOGMNR.ADD_LOGFILE('/u01/app/oracle/oradata/orcl/redo03.log');

PL/SQL procedure successfully completed.

SQL> execute DBMS_LOGMNR.START_LOGMNR (options => dbms_logmnr.dict_from_online_catalog);

PL/SQL procedure successfully completed.

SQL> select sql_redo from v$logmnr_contents where table_name = 'TABLE_FOR_MINE' and sql_redo like 'insert%';

SQL_REDO
--------------------------------------------------------------------------------
insert into "HR"."TABLE_FOR_MINE"("ID") values ('123');

SQL> execute DBMS_LOGMNR.END_LOGMNR();

PL/SQL procedure successfully completed.

如您所见,Oracle为您的数值添加了单引号,并将其写入联机重做日志文件。因此,如果出现停电,下次Oracle启动时他将执行此命令:

insert into "HR"."TABLE_FOR_MINE"("ID") values ('123');

如果您只传递数值:123或字符串值:'123'

,则无关紧要