Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
145 views
in Technique[技术] by (71.8m points)

sql - How to enforce a constraint in a table already created?

I am trying to figure out the correct constraint syntax for the following statement:

"For a given measurement, when Value is present, the Outlier Indicator must be NULL (and, conversely, the Value must be NULL if the Outlier Indicator is present).

'Value' and 'Outlier Indicator' are two columns that I have in a table.

What I have been doing so far is

  1. Right Click on table name in the left pane to open "Edit Table"
  2. Click "Edit"
  3. Click on "Constraints" in the left pane
  4. Click on the Green Plus > "New Check Constraint"

What this does open up a new text section in the "Edit Table" box called "Check Condition" where I have to specify the constraint in a syntax that Oracle SQL Developer can understand.

This method has worked so far with other constraints that I have enforced but for some reason I cannot figure out how to word this particular one.

Any help will be appreciated, thanks.

This is all happening in Oracle SQL Developer, btw.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You would do this in code as;

alter t add constraint chk_t_cols
    check ( (Value is null and outlier is not null) or (value is not null and outlier is null) )

The part after the check is what you would type in the box in the GUI.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...