mirror of
https://github.com/jbranchaud/til
synced 2026-01-04 23:58:01 +00:00
Add Use Not Valid To Immediately Enforce A Constraint as a postgres til
This commit is contained in:
@@ -7,7 +7,7 @@ variety of languages and technologies. These are things that don't really
|
|||||||
warrant a full blog post. These are mostly things I learn by pairing with
|
warrant a full blog post. These are mostly things I learn by pairing with
|
||||||
smart people at [Hashrocket](http://hashrocket.com/).
|
smart people at [Hashrocket](http://hashrocket.com/).
|
||||||
|
|
||||||
_404 TILs and counting..._
|
_405 TILs and counting..._
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
@@ -243,6 +243,7 @@ _404 TILs and counting..._
|
|||||||
- [Types By Category](postgres/types-by-category.md)
|
- [Types By Category](postgres/types-by-category.md)
|
||||||
- [Use A psqlrc File For Common Settings](postgres/use-a-psqlrc-file-for-common-settings.md)
|
- [Use A psqlrc File For Common Settings](postgres/use-a-psqlrc-file-for-common-settings.md)
|
||||||
- [Use Argument Indexes](postgres/use-argument-indexes.md)
|
- [Use Argument Indexes](postgres/use-argument-indexes.md)
|
||||||
|
- [Use Not Valid To Immediately Enforce A Constraint](postgres/use-not-valid-to-immediately-enforce-a-constraint.md)
|
||||||
- [Using Expressions In Indexes](postgres/using-expressions-in-indexes.md)
|
- [Using Expressions In Indexes](postgres/using-expressions-in-indexes.md)
|
||||||
- [Using Intervals To Offset Time](postgres/using-intervals-to-offset-time.md)
|
- [Using Intervals To Offset Time](postgres/using-intervals-to-offset-time.md)
|
||||||
- [Who Is The Current User](postgres/who-is-the-current-user.md)
|
- [Who Is The Current User](postgres/who-is-the-current-user.md)
|
||||||
|
|||||||
@@ -0,0 +1,30 @@
|
|||||||
|
# Use Not Valid To Immediately Enforce A Constraint
|
||||||
|
|
||||||
|
When adding a constraint to a table, you can optionally include `not valid`.
|
||||||
|
This tells Postgres that it doesn't need to enforce the constraint on
|
||||||
|
existing records in the table. At least not immediately. This constraint
|
||||||
|
will be enforced for any updates and subsequent insertions. Thus, you can
|
||||||
|
immediately enforce the constraint while giving yourself time to clean up
|
||||||
|
or massage any existing records that conflict with the constraint.
|
||||||
|
|
||||||
|
Here is an example of how you would add a constraint this way:
|
||||||
|
|
||||||
|
```sql
|
||||||
|
alter table boxes
|
||||||
|
add constraint check_valid_length
|
||||||
|
check (length > 0) not valid;
|
||||||
|
```
|
||||||
|
|
||||||
|
Eventually, you will want to ensure that all data in the table conforms to the
|
||||||
|
constraint. Once you get to that point, you can mark the constraint as valid
|
||||||
|
with a `validate constraint` command:
|
||||||
|
|
||||||
|
```sql
|
||||||
|
alter table boxes
|
||||||
|
validate constraint check_valid_length;
|
||||||
|
```
|
||||||
|
|
||||||
|
As long as all records are valid with respect to this constraint, it will be
|
||||||
|
marked as valid.
|
||||||
|
|
||||||
|
h/t Chris Erin
|
||||||
Reference in New Issue
Block a user