Mastering check constraints in Postgres to keep your database clean!

Поделиться
HTML-код
  • Опубликовано: 5 авг 2024
  • Data hygiene is important when managing a database. Postgres has build in mechanism to ensure you can only enter properly formatted data into your database using check constraints.
    In this video, / dshukertjr shows four different ways, plus a bonus, to use check constraints to restrict what type of data can be entered into the database. Starting with numeric constraints, regex constraints for text columns, time-based constraints for timestamps and date, and using check constraints to simulate enum values.
    00:00 Why we should be using check constraints
    00:13 Numeric constraints
    01:54 Constraints using regex
    02:46 Time based constraints with timestamps and date
    04:14 Using check constraints instead of enums
    07:31 Lead to JSON constraints
    💻 Videos to watch next:
    ▶ • Use this to lock down ...
    ▶ • Make your queries 43,2...
    ▶ • Self-host Maps with Pr...
    👇 Learn more about Supabase 👇
    🕸 Website: supabase.com/
    🏁 Get started: app.supabase.com/
    📄 Docs: supabase.com/docs
    🔔 Subscribe for more tutorials and feature updates from Supabase: / @supabase
    📱 Connect with Us:
    🐙 Github: www.github.com/supabase
    💬 Discord: www.discord.supabase.com/
    🐦 Twitter: / supabase
    ▶ Instagram (follow for memes): / supabasecom
    ABOUT SUPABASE:
    Supabase is the open source Firebase alternative. Supabase provides a full Postgres database for every project with pgvector, backups, realtime, and more. Add and manage email and password, passwordless, OAuth, and mobile logins to your project through a suite of identity providers and APIs.
    Build in a weekend, scale to millions.
    #Supabase #AppDevelopment #RealtimeApps #DeveloperTools
  • НаукаНаука

Комментарии • 27

  • @dshukertjr
    @dshukertjr Месяц назад +9

    The one with date column example was my favorite! Which example was your favorite? Do you have other ways you are utilizing check constraints? Let us know in the comments 👇

  • @Trashmanatx
    @Trashmanatx Месяц назад +9

    Holy cow... I can't believe this kind of content is coming from the actual team that is making the software/service. Keep up the great work!

    • @dshukertjr
      @dshukertjr Месяц назад

      A lot more Postgres content coming from the Supabase team 💪

    • @charliesta.abc123
      @charliesta.abc123 25 дней назад +1

      Right? This is rare. We appreciate

  • @mrrolandlawrence
    @mrrolandlawrence Месяц назад +3

    Keeping the rules in the sql datastore. This makes me happy.

  • @funkdefied1
    @funkdefied1 Месяц назад +6

    Love the content. It should be said that not everything needs to be constrained at the DB level. It’s not extensible.

    • @dshukertjr
      @dshukertjr Месяц назад

      Every usecase has a different solution for sure, and every solution has its pros and cons. What do you mean extensible here?

    • @MennoB-sk8tv
      @MennoB-sk8tv Месяц назад +1

      @@dshukertjrI also would put my constraints in my code, so a user will get the right message in the right text box while adding data, instead of throwing a db exception on submit

    • @dshukertjr
      @dshukertjr 29 дней назад

      @@MennoB-sk8tv Yeah, you should always add the same constraint on your frontend code for maximum user experience!

    • @MennoB-sk8tv
      @MennoB-sk8tv 29 дней назад

      @@dshukertjr yes cbut i would like to write things just once for maintainability

  • @ryanmouritz
    @ryanmouritz Месяц назад +4

    Perfectly timed. I was only wondering last night whether I should add enums for my Web app or use constraints

  • @EmielvanGoor
    @EmielvanGoor Месяц назад +5

    Thanks for the video, i learn a lot from them... Keep it up please! Kudo's on the quality, really good!

    • @dshukertjr
      @dshukertjr Месяц назад +1

      Glad to hear it's helping you out! I'm also learning a lot through creating these videos! Thanks for such a nice comment!

  • @nerrydanna
    @nerrydanna Месяц назад +3

    Very interesting! Thanks

  • @andreh.9300
    @andreh.9300 Месяц назад +3

    Super useful! Thank you! 🙌🏾

    • @dshukertjr
      @dshukertjr Месяц назад

      Glad to hear that you liked it!

  • @pcnerd5994
    @pcnerd5994 29 дней назад +2

    Man, I love postgresql.

  • @kamil_supabase_enjoyer
    @kamil_supabase_enjoyer Месяц назад +3

    ❤❤❤

  • @guilhemheinrich9110
    @guilhemheinrich9110 Месяц назад +1

    I liked those simple and clean exemples.
    But for the enums part, while what you have shown seems convenient from a migration point of view, you loose the generated typings no?

    • @dshukertjr
      @dshukertjr Месяц назад

      Yes, that is one thing you lose, but it's not that hard to modify your generated Typescript file to add those types manually. Also, if you prefer to just keep using enums, that is totally fine too.

  • @akash-kumar737
    @akash-kumar737 11 дней назад

    Check for enums is best

  • @erdemarslan3371
    @erdemarslan3371 Месяц назад +1

    ts support pls

    • @Supabase
      @Supabase  Месяц назад

      For the enum?

    • @erdemarslan3371
      @erdemarslan3371 Месяц назад

      ​@@Supabase Nah, for the check constraints. (type gen)