简体   繁体   English

简单的Oracle(sqlplus)约束问题…需要更​​改并添加变量> 0

[英]Simple Oracle (sqlplus) constraint issue… need to ALTER and add variable >0

I'm trying to add this into my Oracle database (in SQL*Plus) but how do i make the constraint? 我正在尝试将其添加到我的Oracle数据库(在SQL * Plus中),但是如何进行约束? i tried check but it wasn't working. 我尝试检查,但是没有用。 Here's what i got though: 这是我得到的:

-- Add a new attribute years of experience to table Tech_personnel, and enforce it to be a positive value (ie, > 0). -向表Tech_personnel添加新的经验年限,并将其强制为正值(即> 0)。

my solution: 我的解决方案:

ALTER TABLE Tech_personnel ADD years_of_experience NUMBER(30);

ALTER TABLE Tech_personnel ADD CONSTRAINT non_neg
CHECK(years_of_experience > 0);

What, exactly, is not working? 到底是什么不起作用?

SQL> create table tech_personnel( col1 number );

Table created.

SQL> ALTER TABLE Tech_personnel ADD years_of_experience NUMBER(30);

Table altered.

SQL> ALTER TABLE Tech_personnel ADD CONSTRAINT non_neg CHECK(years_of_experience > 0);

Table altered.

SQL> insert into tech_personnel values( 1, -1 );
insert into tech_personnel values( 1, -1 )
*
ERROR at line 1:
ORA-02290: check constraint (SCOTT.NON_NEG) violated


SQL> insert into tech_personnel values( 1, null );

1 row created.

SQL> insert into tech_personnel values( 1, 1 );

1 row created.

Perhaps you also want to ensure that the YEARS_OF_EXPERIENCE is not NULL? 也许您还想确保YEARS_OF_EXPERIENCE不为NULL?

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

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