mirror of
https://github.com/jbranchaud/til
synced 2026-01-02 22:58:01 +00:00
Add Remove Not Null Constraint From A Column as a postgres til
This commit is contained in:
@@ -9,7 +9,7 @@ and pairing with smart people at Hashrocket.
|
||||
|
||||
For a steady stream of TILs, [sign up for my newsletter](https://tinyletter.com/jbranchaud).
|
||||
|
||||
_920 TILs and counting..._
|
||||
_921 TILs and counting..._
|
||||
|
||||
---
|
||||
|
||||
@@ -490,6 +490,7 @@ _920 TILs and counting..._
|
||||
- [Pretty Printing JSONB Rows](postgres/pretty-printing-jsonb-rows.md)
|
||||
- [Prevent A Query From Running Too Long](postgres/prevent-a-query-from-running-too-long.md)
|
||||
- [Print The Query Buffer In psql](postgres/print-the-query-buffer-in-psql.md)
|
||||
- [Remove Not Null Constraint From A Column](postgres/remove-not-null-constraint-from-a-column.md)
|
||||
- [Renaming A Sequence](postgres/renaming-a-sequence.md)
|
||||
- [Renaming A Table](postgres/renaming-a-table.md)
|
||||
- [Restart A Sequence](postgres/restart-a-sequence.md)
|
||||
|
||||
27
postgres/remove-not-null-constraint-from-a-column.md
Normal file
27
postgres/remove-not-null-constraint-from-a-column.md
Normal file
@@ -0,0 +1,27 @@
|
||||
# Remove Not Null Constraint From A Column
|
||||
|
||||
When you want to add a [`not
|
||||
null`](https://www.postgresql.org/docs/current/ddl-constraints.html#id-1.5.4.6.6)
|
||||
constraint to a column, you do so by _setting_ it.
|
||||
|
||||
```sql
|
||||
alter table books
|
||||
alter column publication_date
|
||||
set not null;
|
||||
```
|
||||
|
||||
You can remove a `not null` constraint from a column, by _dropping_ it.
|
||||
|
||||
```sql
|
||||
alter table books
|
||||
alter column publication_date
|
||||
drop not null;
|
||||
```
|
||||
|
||||
Notice this excerpt of syntax from the official Postgres docs:
|
||||
|
||||
```
|
||||
... ALTER [ COLUMN ] column_name { SET | DROP } NOT NULL
|
||||
```
|
||||
|
||||
[source](https://www.postgresql.org/docs/current/sql-altertable.html)
|
||||
Reference in New Issue
Block a user