From 26537a67171fe480ebea4cd90f09dc95a0c192bc Mon Sep 17 00:00:00 2001 From: jbranchaud Date: Tue, 2 Jun 2020 14:53:02 -0500 Subject: [PATCH] Add Remove Not Null Constraint From A Column as a postgres til --- README.md | 3 ++- ...emove-not-null-constraint-from-a-column.md | 27 +++++++++++++++++++ 2 files changed, 29 insertions(+), 1 deletion(-) create mode 100644 postgres/remove-not-null-constraint-from-a-column.md diff --git a/README.md b/README.md index b424bcf..b5ebde5 100644 --- a/README.md +++ b/README.md @@ -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) diff --git a/postgres/remove-not-null-constraint-from-a-column.md b/postgres/remove-not-null-constraint-from-a-column.md new file mode 100644 index 0000000..00eae0a --- /dev/null +++ b/postgres/remove-not-null-constraint-from-a-column.md @@ -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)