Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should every field in an Oracle database have a check constraint if possible?

If I know the correct format of fields, should I create check constraints for all of those fields, or will this affect the performance of inserts/updates too much? Would it be a good idea to use regular expressions for complex rules, or should I only use simple constraints like case and length?

The fields are already being validated at the application level.

like image 884
Chris B Avatar asked Sep 24 '09 15:09

Chris B


People also ask

What will happen if you are trying to add a check constraint to a table?

The CHECK constraint is used to limit the value range that can be placed in a column. If you define a CHECK constraint on a column it will allow only certain values for this column. If you define a CHECK constraint on a table it can limit the values in certain columns based on values in other columns in the row.

What are the limitations of check constraint?

Limitations of CHECK ConstraintsIf you insert the value NULL into MyColumn, the Database Engine inserts NULL and does not return an error. A CHECK constraint returns TRUE when the condition it is checking is not FALSE for any row in the table. A CHECK constraint works at the row level.

Can we specify check constraint at column level?

You can define CHECK constraints at the column level, where the constraint applies only to a single column, and at the table level. You can also add CHECK constraints to a table using ADD CONSTRAINT .

What is the difference between a check constraint and a rule?

In short: Rules is a feature that executes some validation functions based on some other data or systems. CHECK constraint is used to restrict the values in a column to allow only if it meets the condition based on this particular value.


1 Answers

In general it is best not to trust the application and use the check constraints. The data must maintain integrity (who knows what rogue script may run, or what program bug may slip through).

However, if you have many complex check constraints and you notice an insert/update slowdown, you may want to reevaluate. Is it really necessary to have one on every field? No. The column data type and length act as constraints too.

like image 103
KM. Avatar answered Oct 06 '22 18:10

KM.