YouTube

Got a YouTube account?

New: enable viewer-created translations and captions on your YouTube channel!

English subtitles

← Views - Intro to Relational Databases

Get Embed Code
4 Languages

Showing Revision 3 created 05/25/2016 by Udacity Robot.

  1. You've probably noticed that SQL
    queries can get pretty complicated once
  2. the clauses and
    sub-queries start to stack up.
  3. As programmers, when we're writing
    code and a function starts to
  4. get too complex, we look for ways to
    re-factor it into smaller functions.
  5. Well we can do that in SQL too.
  6. Every select query returns a table.
  7. So, we can have the database
    remember a select query for us, and
  8. make it available to later queries.
  9. When we do this, it's called a view.
  10. Here's the syntax for
    the create view command.
  11. Views are really useful with
    aggregations like counts or sums.
  12. For instance, if we have a table of
    students enrolled in courses, and
  13. we want to know how many
    students are in each course,
  14. we can find that out
    with an aggregation.
  15. But then if we're going to use that
    query a lot, we can store it in a view.
  16. Using a view instead
    of making a subquery,
  17. is kind of like using a function
    call instead of copying code.
  18. Another use of views is to
    display only particular columns,
  19. from a table that has a lot of columns.
  20. If you have a table with 20 columns,
    and you have a lot of queries that only
  21. refer to four of them, making a view
    can make your code a lot more clear.
  22. Here's something to think about.
  23. Do you think that a database system like
    PostgreSQL, should let us update and
  24. delete rows in a view?