0

我创建了这个表,我试图为其插入一条记录:

    CREATE TABLE EMPLOYEE(
    Emp_No NUMBER(4),
    Ename VARCHAR2(20) NOT NULL,
    Sex CHAR(1) NOT NULL,
    Job VARCHAR2(20) NOT NULL,
    Mgr_No NUMBER(4) REFERENCES EMPLOYEE(Emp_No),
    DOJ DATE NOT NULL,
    Sal NUMBER(8,2) NOT NULL,
    Comm NUMBER(6,2),
    Dept_No NUMBER(2),
    CONSTRAINTS PK_103 PRIMARY KEY(Emp_no),
    CONSTRAINTS CHK_100 CHECK(Sex LIKE 'M'+'F')
    );

但是,当我进行插入时:

    INSERT INTO EMPLOYEE (Emp_No, Ename, Sex, Job, Mgr_no, DOJ, Sal, Comm, Dept_No) 
      VALUES (100, 'Satish', "M", 'Sr.Developer', 9, '25/FEB/2019',  18000, 5, 10);

我收到以下错误:

第 1 行出现错误:ORA-01722:无效号码

有人可以指出我做错了什么吗?谢谢

4

2 回答 2

2

you have wrong constraint chk_100.

Table must be create like this:

CREATE TABLE EMPLOYEE
( Emp_No NUMBER(4), 
  Ename VARCHAR2(20) NOT NULL, 
  Sex CHAR(1) NOT NULL, 
  Job VARCHAR2(20) NOT NULL, 
  Mgr_No NUMBER(4) REFERENCES EMPLOYEE(Emp_No), 
  DOJ DATE NOT NULL, 
  Sal NUMBER(8,2) NOT NULL, 
  Comm NUMBER(6,2), 
  Dept_No NUMBER(2), 
  CONSTRAINTS PK_103 PRIMARY KEY(Emp_no),
  CONSTRAINTS CHK_100 CHECK(Sex in ('M', 'F'))
  --CONSTRAINTS CHK_100 CHECK(Sex LIKE 'M'+'F')   -- this constraint is not correct

);

Whey you insert first row it will fail if you put any value in Mgr_No because table is empty and reference doesn't exists.

So first insert must be something like this:

INSERT INTO EMPLOYEE5 
        (Emp_No, Ename,   Sex,  Job,          Mgr_no, DOJ,     Sal,   Comm, Dept_No) 
VALUES (100,   'Satish', 'M', 'Sr.Developer', null,      sysdate, 18000, 5,    10);

and than second insert:

INSERT INTO EMPLOYEE5 
        (Emp_No, Ename,   Sex,  Job,          Mgr_no, DOJ,     Sal,   Comm, Dept_No) 
VALUES (101,   'Satish', 'M', 'Sr.Developer - other', 100,      sysdate, 18000, 5,    10);
于 2019-04-11T17:14:03.293 回答
1

你的约束CHK_100是错误 +的,没有为字符串定义。如果你想检查它 value = 'M'value = 'F'

那么你应该像这样定义它:

CONSTRAINTS CHK_100 CHECK(Sex in ('M','F'))
于 2019-04-11T17:13:21.340 回答