0

I need to create a table in PL/SQL and this table need to have a CONSTRAINT on two attribut. I explain:

One of this two objects "com_name" and "com_nickname" need to be checked, if the both are it's ok, but at least one need to be filled.

I'm a beginner and I can't understand how can I make it work

*

CONSTRAINT ch_com_name_nickname CHECK (com_name = NOT NULL OR com_nickname = NOT NULL)

*

This is not working.

maryo
  • 23
  • 4
  • 'Not working' isn't very helpful, it's better to say what you expect and what you actually get. But you need `IS NOT NULL`, not `= NOT NULL`. Also why have you mentioned PL/SQL? – Alex Poole Oct 05 '18 at 16:24
  • Also [see this](https://stackoverflow.com/questions/5447880/adding-an-one-out-of-two-not-null-constraint-in-postgresql), which is PostgreSQL not Oracle but same syntax' [and this](https://stackoverflow.com/q/5658457/266304). – Alex Poole Oct 05 '18 at 16:31

2 Answers2

1

The correct syntax is column_name IS NOT NULL. You don't need the =.

eaolson
  • 14,717
  • 7
  • 43
  • 58
-1

Why do u want do it by CONSTRAINT?!

This kind of task solves not like that.

You can simply check it in your logic (in procedure or function).

  • +1 I just started to learning SQL that's why I don't know how use others stuff, I will try to improve my code during this week ! Thank you for your advice ! – maryo Oct 05 '18 at 16:46
  • A check constraint is the correct thing to use for this. It's fine and good to have application logic make sure it inserts the right things, but it should be *as well as* a check constraint, not instead of one. Having a check constraint (a) saves you if the application logic is somehow broken, and (b) still applies if the application is bypassed - if someone inserts directly into the table. – Alex Poole Oct 05 '18 at 17:00