YouTube

Got a YouTube account?

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

English subtitles

← Joins - Web Development

Get Embed Code
4 Languages

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

  1. Let's move on to a new topic that we're promptly going to forget.
  2. This is called Joins.
  3. So this is a type of SQL query that you can use that involves multiple tables.
  4. Remember we have our Link table.
  5. We've been working with that this entire lecture.
  6. It looks something like this.
  7. It's got the columns--ID and Votes and the User ID Submitted,
  8. Data, Title, URL.
  9. So let's make up some values for these.
  10. Now, what I want to talk about right now is this User ID column.
  11. Remember I mentioned before
  12. that if we had another table called Users
  13. that looks something like this--
  14. it's got a column for ID and a column for Name and Password and Date--
  15. the User ID in the Links table
  16. refers to the ID column of the User table.
  17. This should always match up,
  18. because you want everything in your system to be consistent.
  19. So for every unique User ID in the Links table,
  20. there should be a valid user in the User table.
  21. Now, one of the things we can do in most SQL databases is something called a Join,
  22. which is a SQL statement that involves two tables.
  23. So we have a basic SQL statement that looks something like this.
  24. So select from the Link table,
  25. where user ID equals 22.
  26. So that would return all of the links submitted by this user,
  27. assuming there's more than one.
  28. What if we didn't know the user's ID?
  29. What if instead we wanted to select from the link table
  30. all of the links submitted by users with the name Spez?
  31. Well, there's a couple of ways you could do that.
  32. You could first do a lookup for
  33. what is the user ID of the person named Spez,
  34. grab this user ID, then run the SQL query.
  35. So you could do two queries. It might look something like this.
  36. So I could run one query to get the ID from the user table
  37. for the user whose name equals Spez,
  38. and then we can use that ID in a second query.
  39. But there's another way of doing this query.
  40. I could, instead of running two queries,
  41. combine this into one query.
  42. So instead of From Link, we can change this to Link, User--
  43. which will scan both tables.
  44. And we don't want to get all of the attributes.
  45. We want to preserve the same results from the query,
  46. so we don't want to select * from link, user--
  47. we want to select link.*,
  48. which will return just the columns from the link table.
  49. Then we want to change our Where clause
  50. to link these two tables.
  51. So where user_id=user.id.
  52. I could have also said link.user_id
  53. so we include the table names in all of our properties.
  54. That will make things more clear.
  55. I need to add another clause to this.
  56. And user.name = Spez.
  57. So what does this do?
  58. Select all of the columns from link--link.*--
  59. from the link table and the user table,
  60. where user.name=Spez
  61. and link.userid=user.id.
  62. And what this does is what's called a Join Query.
  63. And your SQL engine will decide
  64. which of these tables to scan first,
  65. and in this case, it will probably do the user table first.
  66. So it will find the username Spez, and then it will find all the links
  67. whose user_id property
  68. matches the ID field in the User table.
  69. So this is a handy thing.
  70. Now, the reason I said we were going to learn something we were promptly going to forget
  71. is because we don't use joins very often
  72. when writing web software.
  73. It's something you'll see in almost every SQL tutorial,
  74. but for reasons we'll explain later in this lecture,
  75. joins don't work very well for the types of problems we're going to be solving
  76. writing web stuff.
  77. But you should be familiar with them; you should know what they are.
  78. You may be writing something that requires this,
  79. but they're often--well, as I said,
  80. we'll get into this later in the lecture--why we don't want to use joins.
  81. Let's move on to something new.