01 - Course Introduction & Relational Model (CMU Intro to Database Systems / Fall 2021) — Transcript
Full transcript
- 0:00Bad, bad, AND A WICKED AND BAD, BAD, and
- 0:04a wicked and
- 0:05Bad, bad, and a wicked and
- 0:08Bad, bad, and a wicked and
- 0:11Bad, bad, and This is Introduction to
- 0:14Database Systems.
- 0:15Um those of you familiar with the class,
- 0:18um you know that it's usually taught by
- 0:21uh Andy Pavlov, and as I'm sure some of
- 0:24you may realize, I'm not Andy Pavlov. Uh
- 0:28my name is Andrew Crotty. I'm a
- 0:30postdoctoral researcher here at uh CMU,
- 0:34and my research focuses mainly on
- 0:36database systems and data management.
- 0:39Uh you may also have realized that my
- 0:41co-instructor for the course uh Lim Maas
- 0:43is also not Andy Pavlov. Uh he is a
- 0:46postdoc here as well, and he's uh one of
- 0:48Andy's former PhD students. So um
- 0:52at this point, you might be wondering
- 0:53where Andy is, and uh he's actually on
- 0:56leave this semester
- 0:58um because he's filming a uh a Netflix
- 1:01documentary that's a follow-up uh to
- 1:03last year's Tiger King.
- 1:05Um
- 1:06and I unfortunately I can't say anything
- 1:07more uh because of the NDA that I
- 1:10signed, but um you should definitely
- 1:13check this out when it's uh when it's
- 1:15released. So with that, um
- 1:17let's move on to the course material. Uh
- 1:18one last thing before we begin, uh I
- 1:20want to thank um
- 1:22Google BigQuery for helping us with uh
- 1:25course development this semester.
- 1:27Um I'm sure all of you know what what
- 1:29Google is, but uh BigQuery specifically
- 1:32is a uh a database product. It's a a
- 1:35fully managed um
- 1:37cloud data warehouse. We might call it
- 1:39platform as a service.
- 1:41And uh essentially you give them your
- 1:43data, and then you can query it, run all
- 1:46sorts of analytics, machine learning,
- 1:47that kind of stuff.
- 1:49Um and at the end of the semester, uh
- 1:51there's there's going to be someone from
- 1:52Google uh who comes here to give a guest
- 1:54lecture and tell you about kind of all
- 1:56the the cool stuff they're doing Uh and
- 1:58you be able to see a lot of the topics
- 2:00that we cover in the course actually
- 2:01applied in practice and they'll they'll
- 2:04be able to talk more about kind of the
- 2:06the problems that come up when um
- 2:10uh dealing with with data at this scale.
- 2:14So,
- 2:16today's agenda, we're just going to go
- 2:17over the kind of beginning, what is this
- 2:20course, course logistics stuff
- 2:23and then there'll be sort of like a mini
- 2:26path lecture at the end. We'll we'll
- 2:28just kind of go through some of the core
- 2:29concepts
- 2:31that we're going to cover in the course.
- 2:33So, depending on timing we might get out
- 2:35a few minutes early.
- 2:36Um to start off with the waitlist, I do
- 2:40not control the waitlist.
- 2:42It is way above my paygrade.
- 2:44As students are going to be moved off
- 2:47the waitlist based on your position
- 2:50as new spots in the class open up. So,
- 2:53if you if you send us emails, there's
- 2:54nothing that I personally can do about
- 2:56it. It's just going to be based on
- 2:57order.
- 2:58And if you're not currently enrolled in
- 3:00the course,
- 3:01the likelihood that you'll get in and
- 3:03things will probably firm up over the
- 3:06next week or so, but um
- 3:09unfortunately, you might not be able to
- 3:10get in, but we do plan to make all of
- 3:13the
- 3:13course materials public. We're going to
- 3:15put the lectures lecture videos on
- 3:17YouTube and
- 3:19all the material will be there so you'll
- 3:21you'll be able to follow along.
- 3:25Uh in terms of lecture rules,
- 3:27please interrupt me at any time if any
- 3:30of these things happens, either I'm
- 3:32speaking too fast,
- 3:34if you don't understand something that
- 3:35I'm talking about or if you have a
- 3:37database related question, chances are
- 3:39that if you have a question then then
- 3:42um
- 3:43someone else does too. And if this this
- 3:47is kind of a big room, if I if I can't
- 3:49quite hear you or if I don't if I don't
- 3:51see you right away, just uh
- 3:53try to
- 3:54uh interrupt me at a at a a good point
- 3:56where I can uh take the question.
- 4:01So, uh just at a high level, um this
- 4:04course is specifically about the design
- 4:08and implementation of database
- 4:10management systems.
- 4:12So, that means we're going to be looking
- 4:14at the software
- 4:17uh that manages databases. So, this
- 4:20isn't a course about uh how to use a a
- 4:24DBMS to build applications. So, like
- 4:26imagine if you had a building a web
- 4:28application, and you wanted to store uh
- 4:31your user user data in in a database, um
- 4:35this this class is not about that. Uh
- 4:37and it's not a class about how to
- 4:39administer a a database system. So,
- 4:41there are all all sorts of um
- 4:44jobs out there where where people's
- 4:46entire role is essentially uh manage the
- 4:48software that manages the data. Um and
- 4:51that's that's also not not the the topic
- 4:53of this course. So, there are other
- 4:54courses unfortunately, one of them um is
- 4:55not being offered this semester. Um
- 4:59and actually I don't know if it's
- 5:00offered anymore. Um
- 5:02but that that's one of course the one
- 5:04that that uh covers those topics.
- 5:08Okay. So, just at a high level for the
- 5:11rest of the semester, the uh topics that
- 5:13we're going to be covering
- 5:15uh we're going to be starting with um
- 5:18uh the relational model, and that's
- 5:21that's a data model for databases, and
- 5:23that's going to be uh
- 5:25the main theme for the entire course.
- 5:27So, we're going to talk primarily about
- 5:29relational database management systems.
- 5:31There are all sorts of other um data
- 5:33management systems, no SQL, some of you
- 5:35may have heard of graph databases, all
- 5:37sorts of things. Um we're going to be
- 5:39focusing on relational databases in this
- 5:41course. And uh
- 5:44we're going to sort of build up um
- 5:46from the bottom level all the way up
- 5:48through the stack uh to look at the
- 5:50different parts of a database management
- 5:54system starting with the storage, how
- 5:56you actually physically store the data
- 5:58in memory and on on persistent storage
- 6:02like a hard drive or an SSD.
- 6:04Then we're going to move up and talk
- 6:06about query execution and kind of how
- 6:08you can
- 6:09either
- 6:11put data into update the data that
- 6:14you've stored or retrieve the data.
- 6:16Then we're going to move on to kind of
- 6:18concurrency control. So imagine you have
- 6:20several um
- 6:22processes that are concurrently trying
- 6:24to access the database. How can you sort
- 6:27of keep all of those together and manage
- 6:29them?
- 6:30Um
- 6:31the next piece that we're going to cover
- 6:32is recovery. So if your database crashes
- 6:35or if if the machine crashes,
- 6:38um
- 6:39how can you kind of make sure that your
- 6:41data is safe and persistent and you can
- 6:43kind of recover from those types of
- 6:45situations. And then towards the end of
- 6:47this this semester we're going to cover
- 6:48some more advanced topics,
- 6:50distributed database management systems
- 6:52and kind of just a general
- 6:55review of
- 6:57some advanced topics.
- 7:00So in terms of logistics, the course
- 7:03policies and the schedule are all on
- 7:06the web page.
- 7:08Academic honesty I'm going to talk about
- 7:09this
- 7:10in in later in the slides, but please um
- 7:15don't do anything stupid. If if we catch
- 7:19you
- 7:20plagiarizing or copying work or those
- 7:22sorts of things, it's going to be a big
- 7:24problem. I'll have to do a lot of
- 7:26paperwork
- 7:27and it's going to be a big problem for
- 7:29you. So just please it's easier
- 7:31if you don't do it. If you have any
- 7:32questions, if you're not sure about
- 7:33anything,
- 7:35please either send an email to me or
- 7:38Lynn or one of the TAs Um, just ask like
- 7:41is is this thing that I'm doing okay?
- 7:44Um, and I you can also refer to the the
- 7:46CMU policy page.
- 7:48Ironically, um, that link there is
- 7:50broken. I checked it this morning. Their
- 7:52their
- 7:54web page is down, but
- 7:56there's a cached version somewhere or or
- 7:58I can make it available to you if you if
- 8:00you you know me. So,
- 8:02um,
- 8:03all of the discussion and announcements
- 8:05for the course will be on Piazza.
- 8:09Um,
- 8:10and if if there's something that you
- 8:12need to contact either me or Lim about,
- 8:15just private private privately, um,
- 8:18a sensitive subject, then please send us
- 8:20an email,
- 8:21um, and we can coordinate something to
- 8:23to speak.
- 8:26So, the textbook for the course, um, is
- 8:28is the this database systems concepts
- 8:31book, um,
- 8:33also known as the the Silberschatz book.
- 8:35Uh, there are
- 8:36uh,
- 8:37so I'm not sure about the the exact
- 8:40difference between the sixth and the the
- 8:41seventh edition. I think all of the
- 8:43material that we're going to cover in
- 8:44this course is available in the sixth
- 8:45edition. Um,
- 8:48if you have that or want to want to buy
- 8:50that version,
- 8:51um, I don't think that the seventh
- 8:53edition is available as like a hard um,
- 8:57cover book anymore. I think it's like
- 9:00loose leaf pages that are
- 9:03you get sent and they've like a three
- 9:04hole punch in them.
- 9:06Um,
- 9:07but we'll also be providing
- 9:09lecture notes uh, that that will cover
- 9:11all of the topics as well as things that
- 9:13we cover in class that aren't um,
- 9:16covered in the book.
- 9:19So, in terms of grading, uh, this is
- 9:21going to be the breakdown for, um,
- 9:24how your grades are calculated. 15% for
- 9:26homeworks, 45% for projects,
- 9:30um, 20% for the midterm exam, and 20%
- 9:33for the final exam. So, it's it's pretty
- 9:35We weighted towards
- 9:36uh, homeworks and the the projects.
- 9:43Uh for homeworks, there are going to be
- 9:44five assignments that are are spread out
- 9:47over the course of the semester.
- 9:49Um
- 9:50the first homework is is a a SQL
- 9:53assignment. So, SQL is a
- 9:55We're going to talk about it a little
- 9:55bit at the end of this class and um
- 9:58in the the next class, but it's a a
- 10:00query language that you use uh to
- 10:03interact with the database management
- 10:04system. So, the first homework
- 10:06assignment is going to be up to write
- 10:07some SQL queries
- 10:09um to answer some uh
- 10:12questions from a a sample database we'll
- 10:14give you and the rest are going to be um
- 10:17paper and pencil assignments where you
- 10:19kind of like uh apply formulas or uh
- 10:22solve different like conceptual problems
- 10:25uh for topics we cover in class.
- 10:27And again, I mentioned earlier, um
- 10:31all homework should be done
- 10:32individually. Um you shouldn't be
- 10:36you know, sharing answers or doing group
- 10:38assignments. They're all individual
- 10:39assignments um and should be done
- 10:41individually. We'll be checking for
- 10:43um
- 10:44kind of copied uh
- 10:46solutions and things.
- 10:52So, um
- 10:54the projects uh are are focused around
- 10:57this idea that you're going to build up
- 10:58your own
- 11:00um
- 11:01database engine over the course of the
- 11:03semester. So, like I said, kind of we'll
- 11:05start at the lower levels and we'll work
- 11:07our way up through the stack. Um
- 11:09but what this means is that each project
- 11:11is going to build on the previous ones.
- 11:14So, for example
- 11:16if you have to implement a storage
- 11:18manager, or later on, the the later
- 11:21projects may interact with that storage
- 11:23manager to get uh data in and out. So,
- 11:25for that reason, it's it's important
- 11:27that you kind of keep up and go along.
- 11:29If you get too far behind on on, you
- 11:32know, these early projects then uh you
- 11:35it's going to be really difficult or
- 11:36impossible to to catch up later on in
- 11:39the semester. So, an important thing,
- 11:41and this was kind of already
- 11:44mentioned in the announcement for the
- 11:47first project, but
- 11:49we're not going to be teaching you how
- 11:51to write or debug C++ code in this
- 11:55class. It's a prerequisite for the
- 11:56course. We expect um
- 11:59this is a higher level higher level
- 12:00course. We expect that you know
- 12:02um
- 12:03kind of C++ as a as a prerequisite. So,
- 12:06if you go to, you know, TA office hours
- 12:08and are asking things like what is a
- 12:10string or what is a shared pointer or
- 12:11that kind of stuff, they're
- 12:14we've told them not not to answer those
- 12:16kind of questions. So,
- 12:18um
- 12:19we expect you to be comfortable working
- 12:20with C++.
- 12:22That's kind of why this this project
- 12:24zero is released, and it it needs to be
- 12:26completed
- 12:27by September 13th for you to
- 12:30uh continue in the course. So,
- 12:32uh it's kind of meant as like a as like
- 12:34a self-check kind of thing where if
- 12:37you're if you're really struggling with
- 12:38project zero, then um
- 12:41this might might not be the course for
- 12:42you because later on
- 12:45you're not going to be able to do the
- 12:46the more complicated programming
- 12:48projects.
- 12:51So, all of the projects
- 12:54for the course will be implemented using
- 12:57um the CMU DB group Bustle academic
- 13:01DBMS.
- 13:02Um
- 13:03it is kind of a
- 13:06an academic system, and so it's not
- 13:07really a full-fledged
- 13:09database system, but
- 13:11it has these kind of modular pieces, and
- 13:13and
- 13:14we'll go through some of these things
- 13:16that will make more sense later in the
- 13:17course, but it has kind of this
- 13:18disk-based storage architecture where as
- 13:21I said it can read and write data from
- 13:23disk.
- 13:24Um it it has a volcano style
- 13:27pre-processing
- 13:29setup where
- 13:31you can kind of chain together iterators
- 13:33basically
- 13:35to to iterate over your data and answer
- 13:37queries. It has pluggable APIs that will
- 13:40leverage to be able to implement the
- 13:41different pieces of the system that
- 13:43you're going to implement for the
- 13:44projects.
- 13:45Um, and it currently does not support
- 13:48SQL so
- 13:50you won't be interacting with it through
- 13:52SQL. You'll kind of be interacting
- 13:53interacting with it through
- 13:55um, lower um,
- 13:57test test programs that that we provide.
- 14:02Okay, so the next thing is the late
- 14:04policy. Um,
- 14:07you lose 10 point or sorry 10% of the
- 14:10points for a project or homework for
- 14:13every 24 hours
- 14:15late that you you hand it in so
- 14:18um,
- 14:20if
- 14:21you know if you hand it in after the
- 14:23deadline we'll round up.
- 14:25So if it's 4 hours late then it's going
- 14:28to be 10% off and we'll round up to the
- 14:30next
- 14:31um,
- 14:32full day.
- 14:34I
- 14:34you're going to have a total of four
- 14:36late days over the course of the
- 14:37semester to be used
- 14:39um, for projects only so not for the
- 14:41homeworks it's only for the programming
- 14:43projects
- 14:45and you're free to allocate them as you
- 14:48wish.
- 14:49For example, you could
- 14:52turn in one project four days late or
- 14:55four projects each one day late.
- 14:58The only thing that you cannot use them
- 14:59for
- 15:01is is the project number zero. That has
- 15:03to be done by September 13th for you to
- 15:06to continue the course.
- 15:08Um,
- 15:09we're I there's
- 15:11pandemic still going on. I know that
- 15:13there's a lot of
- 15:15uncertainty and things that can come up
- 15:16so
- 15:18we're also going to grant no no
- 15:20extensions due to
- 15:23extreme circumstances. So, like you have
- 15:24a medical emergency or a family
- 15:26emergency,
- 15:27just if something comes up,
- 15:30I realize you know at the time you might
- 15:32not be able to, but as soon as you can,
- 15:34just contact us and let us know what's
- 15:36going on
- 15:37and we can we can try to make
- 15:38arrangements for you.
- 15:43Okay, so again,
- 15:45I want to reiterate the plagiarism
- 15:47warning.
- 15:48I I
- 15:50know this is redundant, but I I really
- 15:52want to get this out there and so you've
- 15:55all seen it and there's there's video of
- 15:57me telling all of you about it. So,
- 15:59later on if you run into any problems,
- 16:02there's no excuse for not knowing what
- 16:04this is. So, the the homework and the
- 16:06projects must be your own original work.
- 16:09They're not group assignments. They're
- 16:10not meant to be done together in groups.
- 16:12You you cannot copy source code from
- 16:16other people, your peers or the web.
- 16:18Um in general, there's
- 16:22not a since since this is kind of a
- 16:24an academic system and we change the um
- 16:29projects each semester, there shouldn't
- 16:31be
- 16:32like
- 16:33one-for-one solutions to
- 16:36the projects that we're going to give
- 16:38you online, but I still
- 16:40if there's any source source code out
- 16:43there, please don't copy it.
- 16:46We're not going to to tolerate
- 16:48plagiarism and it is going to be a big
- 16:50problem. So, please do not do that.
- 16:55Um so, kind of the last administrative
- 16:58thing is office hours. We're still
- 17:00waiting on
- 17:02clarification from the university about
- 17:03what we're supposed to do for in-person
- 17:07versus remote office hours and you may
- 17:08have noticed the website does not have
- 17:11either the instructor or TA office hours
- 17:14yet, Um, but as soon as we get this
- 17:17within the the next day or two, um, we
- 17:20will update you. Uh, so
- 17:24we'll put it on the website and we'll
- 17:25send out a message um, to the class. If
- 17:28If you need to contact us sooner,
- 17:30uh, about anything at all, please just
- 17:32send us an email or uh, you can reach
- 17:35out if it's something that's a more
- 17:37general question, you can reach out um,
- 17:39on on Piazza.
- 17:46Okay, so
- 17:47uh, for the fun stuff, database
- 17:49research. Um,
- 17:51there is a vaccination database tech
- 17:54talks seminar series
- 17:56uh, that will take place
- 17:59uh, I believe on Zoom
- 18:00um,
- 18:02Mondays uh, at 4:30 p.m.
- 18:04uh, starting on 9:13, Monday 9:13.
- 18:08Um,
- 18:10there's a link to the schedule there.
- 18:11You can access it uh, and
- 18:14we are going to have a whole bunch of
- 18:17excellent
- 18:19people from each of these these uh,
- 18:21either database companies or projects
- 18:24uh, come and talk about their system.
- 18:26And these aren't like uh, marketing
- 18:28people that are just going to say, "Use
- 18:29our system." They're They're uh,
- 18:32technical people that are going to come
- 18:33and give technical talks about um, the
- 18:37the actual internals of their system.
- 18:39So, low-level stuff, kind of the the
- 18:41design stuff
- 18:43um, that we're talking about uh, in this
- 18:46course. So, if you're interested in
- 18:47that, please uh,
- 18:49check it out. You can check out the
- 18:50website there.
- 18:52So,
- 18:53that is kind of all of the uh,
- 18:56administrative logistics part of um,
- 19:01this
- 19:02lecture. So, uh, if there are any
- 19:04specific questions so far, I'd be happy
- 19:07to answer them. Then we can move on to
- 19:09the
- 19:11fun database stuff.
- 19:18Cool.
- 19:19Okay, and again, all of this stuff uh
- 19:22that I mentioned should be available on
- 19:25uh the website either currently or as I
- 19:28said in the case of Officer Owl and
- 19:29stuff shortly.
- 19:31Um so you can check there and if if you
- 19:33have any questions, just send us an
- 19:34email or ask on uh uh the answer.
- 19:38Okay, so databases.
- 19:42Let's start with what is a database?
- 19:45Um
- 19:47just out of curiosity, how like you you
- 19:49just raise your hands. How many of you
- 19:51regularly interact with a database
- 19:54management system databases? It doesn't
- 19:56have to be like on the command line
- 19:59writing SQL queries. No one? No no
- 20:01databases? All right.
- 20:03Um
- 20:04I find that a little hard to believe. Uh
- 20:07uh so SQLite is a a database management
- 20:10system. Um
- 20:12it is probably the most widely deployed
- 20:15uh database management system in the
- 20:16world.
- 20:17Um pretty new to the heads uh
- 20:20smartphone. It's used in iOS and
- 20:22Android.
- 20:24Web browsers. I don't know if you use
- 20:26web browsers. Uh there's Chrome and
- 20:29Safari both use uh uh SQLite for um
- 20:34storing data.
- 20:35Um and if uh if you've made a call with
- 20:38Skype, Sk- Skype uses uh SQLite behind
- 20:41the scenes. So
- 20:42I I guess the point I'm trying to make
- 20:44is that um pretty much behind every
- 20:48uh application anywhere where you need
- 20:51to store data,
- 20:52um there's probably a database
- 20:54management system running in the
- 20:55background handling things for you.
- 20:58So um
- 21:00a database is essentially an organized
- 21:02collection of interrelated data
- 21:06um where you're trying to uh model or
- 21:09capture some aspect of
- 21:13uh uh the real world. So, to think about
- 21:15like a real world entity, for example,
- 21:17if I have a database to model uh
- 21:19students in the class, um,
- 21:21each individual student is a data point
- 21:23that would be stored in my uh
- 21:26database.
- 21:28So,
- 21:29just as a as a practical example, uh
- 21:31let's create a database that models a
- 21:35uh a digital music store, something like
- 21:37um
- 21:38the Apple Music Store, iTunes Music
- 21:39Store, or
- 21:41uh something similar where you want to
- 21:43keep track of we'll just for now to
- 21:45simplify things, we'll just keep track
- 21:46of
- 21:47uh artists and the albums that they
- 21:50release.
- 21:52So, uh the things that we're going to
- 21:53need are, you know, to know about
- 21:55individual artists and
- 21:58um
- 21:59kind of uh a record of over time the the
- 22:02different uh albums that they released.
- 22:05And the way we can do this just as a as
- 22:07a straw man sort of example is using uh
- 22:10a flat files.
- 22:11So, uh imagine that we want to store our
- 22:14database in um
- 22:17uh just a a comma comma-separated value
- 22:20CSV file,
- 22:22um kind of where each each uh
- 22:25row or line in the file represents one,
- 22:28you know,
- 22:30data entity. So, an artist or an album,
- 22:33and kind of each uh column is going to
- 22:36represent a um
- 22:39uh
- 22:40an individual attribute about that
- 22:42entity. So, um
- 22:46you know, pretend we don't know anything
- 22:47about any kind of, you know, database
- 22:49management systems that are out there
- 22:51like MySQL, PostgreSQL, Oracle, SQLite,
- 22:54any of that stuff, um
- 22:57pretend we don't know about that, we're
- 22:57just kind of building a an application
- 22:59up ourselves, and what this is going to
- 23:02require is that, um, the application
- 23:05needs to, uh,
- 23:07parse the file each time it wants to
- 23:08read anything, and it's going to have
- 23:10to, you know, read a line, split the
- 23:11CSV,
- 23:12um, and kind of figure out what's going
- 23:13on every single time we want to access
- 23:15the data. So,
- 23:16um, again, here's here's just a simple
- 23:19example using the, um,
- 23:22music store, where we have two
- 23:24essentially tables stored as CSV files.
- 23:27So, there's the artist table, where we
- 23:30have the the name of the artist, uh, the
- 23:32year they they first started, um,
- 23:35releasing music, and the country they're
- 23:36from. And in this, uh,
- 23:39second album table,
- 23:41um,
- 23:42we have the the name of the album, the
- 23:44artist that released it, and the year it
- 23:47was, uh, released.
- 23:51So,
- 23:52now let's say that that we want to issue
- 23:55essentially a query on on the data that
- 23:58we have, and we want to know the year,
- 24:00uh, that Ice Cube went solo. Um,
- 24:04all right, so we know that
- 24:07Ice Cube had had some, uh, disagreements
- 24:09about the amount of money he was making,
- 24:11so he he decided to go solo,
- 24:13um, and start releasing albums on his
- 24:14own. So,
- 24:16um,
- 24:17I guess the the the way that we would do
- 24:19this if we have these CSV files is, um,
- 24:23you write, you know, you could write
- 24:24some kind of like, uh, Python program
- 24:26that looks something like this, where,
- 24:29uh, essentially you open up a file, and
- 24:30then you would iterate over every line
- 24:32in the file.
- 24:33Um, and all you have to do is take the
- 24:34line, remember it's just a CSV, uh,
- 24:37string, you have to parse it into a
- 24:39record, and then what you want to do is
- 24:41check if the,
- 24:43um,
- 24:44uh,
- 24:45value at position zero in the record is
- 24:48Ice Cube, cuz we're looking for when Ice
- 24:49Cube
- 24:51went solo, and, uh, you want to print
- 24:53out the, uh, the year, so we're going to
- 24:55cast the the, um,
- 24:57the second value in the string into an
- 24:59int
- 25:00and then return that.
- 25:02So, I I guess
- 25:05what's the problem with this? It seems
- 25:06pretty simple, pretty straightforward.
- 25:08Um
- 25:10I mean, I guess your your query could
- 25:12change. Maybe you want to know when The
- 25:14Notorious B.I.G. went solo
- 25:17or started releasing his album. You
- 25:18could
- 25:19just swap out the Ice Cube for for a
- 25:22different name and get a different
- 25:24result. Or if you want to know the
- 25:25country that someone's from,
- 25:27just change the position
- 25:30that you're printing out there. So, I
- 25:32guess this seems fine,
- 25:34but can anyone think of, you know, any
- 25:36issues that might come up if if you're
- 25:38managing things this way?
- 25:42Sure.
- 25:43Each query takes linear time in the size
- 25:45of the data set. Uh that is true. So, if
- 25:49the the data gets really large, then
- 25:51what you're going to have to do is kind
- 25:53of open the file and iterate over all of
- 25:55the
- 25:56the the lines here. I guess what you
- 25:58could do is modify this a little bit to
- 26:00as soon as if you know that there's only
- 26:02one
- 26:03record you're looking for in this case,
- 26:05you could modify it a little bit to just
- 26:07return as soon as you find it. But,
- 26:10that's
- 26:11certainly true.
- 26:13If if you're if you're not careful with
- 26:14how you code this, then
- 26:16you might end up
- 26:19iterating over every single line in the
- 26:20file every single time.
- 26:23Yeah.
- 26:30Uh that is another problem that can come
- 26:32up. So, the the
- 26:34statement was that there could be
- 26:37duplicate entries for Ice Cube and
- 26:39there's no way to ensure
- 26:41using this method that
- 26:45someone doesn't come in and you don't
- 26:46put in another record
- 26:48for Ice Cubes. And we there's no way to
- 26:50maintain kind of the um
- 26:54the
- 26:55invariant we want only one record to
- 26:57represent ice cream. Yes.
- 26:59That's another problem.
- 27:02Any others?
- 27:04I have a whole list I can go through.
- 27:06Okay, so
- 27:07the first problem
- 27:09or the first a series of problems
- 27:11relates to
- 27:13data integrity. So
- 27:16one problem might be is how do we ensure
- 27:18that the artist is the name
- 27:21the name of the artist is the same for
- 27:22each
- 27:23album entry. So if you remember
- 27:27in order to figure out you know kind of
- 27:28which
- 27:30artists have released which albums we
- 27:32store the artist name in the album CSV
- 27:36file. So
- 27:39imagine that that
- 27:41you know you you
- 27:44type in a wrong a wrong name to update
- 27:47one of the the album rows or
- 27:52you know an artist changes their name or
- 27:55that sort of thing. So how how do you
- 27:57ensure that there's consistency between
- 27:59the name that an artist lists in the
- 28:03artist file and the name that the artist
- 28:05lists in the albums file.
- 28:08Another problem that can come up is you
- 28:10know what if someone
- 28:11overwrites the album year
- 28:16in the the album file with an invalid
- 28:18string. So in that code I showed you
- 28:20you're expecting an integer
- 28:22that that represents a year when
- 28:27the album was released. Someone comes in
- 28:29and puts in just a regular string
- 28:32then your your code's going to break.
- 28:34There's going to be a problem parsing
- 28:36it.
- 28:37And again there's no kind of I guess you
- 28:41could have access control on your files
- 28:42but you know once someone's able to get
- 28:44in the file they could just make a
- 28:45mistake and put in an invalid value for
- 28:48a particular field.
- 28:51Uh
- 28:52what if there are multiple artists on an
- 28:54album? So, uh everything I showed, um
- 28:58you know, each each album only had an
- 28:59individual artist, so it's just one um
- 29:03field in the CSV string, but um you
- 29:06know, imagine that there's a a
- 29:08um
- 29:09multiple artists on a on an album like a
- 29:11mixtape or something. So, um
- 29:14in that case, I guess, you know, you
- 29:16could
- 29:17um
- 29:18uh
- 29:19change the format instead of being a
- 29:21single string, you could put, you know,
- 29:23a
- 29:23comma-separated list inside uh the field
- 29:26of the CSV file, but then you're, you
- 29:29know, uh
- 29:30complicating your parsing code. You have
- 29:32to go back and adjust your parsing code
- 29:34to now handle um
- 29:36you know, potentially one or more
- 29:38um values in that field.
- 29:41And kind of the last problem, and I
- 29:43these are just a
- 29:45a few that that I, you know, came up
- 29:47with here, but uh there are many, many
- 29:48more, but uh what happens if we delete
- 29:51an artist that has albums? So, imagine
- 29:54that, you know, you you deleted the
- 29:56record for
- 29:57um
- 29:58Ice Cube in the artist file, and now he
- 30:02has a bunch of um
- 30:04records or a bunch of records
- 30:07representing albums in um
- 30:11the album table, and now there there's
- 30:12no
- 30:13uh uh
- 30:14artist committing that they wanted to.
- 30:18Okay. So, that's kind of the data data
- 30:20integrity side. Uh from an
- 30:23information implementation perspective,
- 30:25and uh this was um
- 30:27one of the the points made is how do you
- 30:30find a particular record? So, again, the
- 30:32example,
- 30:33um you end up in in the code that I
- 30:35showed iterating through every single
- 30:38row every single time
- 30:40um
- 30:41you want to find something. I mean, you
- 30:43you there are all sorts of ways to
- 30:44optimize it. For example, if you know
- 30:46the data is sorted, you could do like a
- 30:48binary search or something. Um,
- 30:51or if you have
- 30:53a data structure that stores it, maybe
- 30:55like a a hash table or something, you
- 30:57could um index directly to
- 31:00um
- 31:01the the
- 31:02the record you're looking for. But, just
- 31:04you know, all of these things you're
- 31:06building up additional complexity in
- 31:08your Python program that you have to
- 31:10write
- 31:11um
- 31:12to access the code. So,
- 31:14uh all of that kind of implementation
- 31:16efficiency needs to be taken into
- 31:17consideration when when you're designing
- 31:19your application.
- 31:21Similarly, what if we want to create a
- 31:23new application that uses the same
- 31:24database? So, the database is stored in
- 31:27the CSV files. Imagine that, you know,
- 31:29someone else for some other purpose
- 31:31wants to come along and access the same
- 31:33CSV files
- 31:34uh concurrently. Do you want them to
- 31:36have to rewrite kind of all of the
- 31:38Python code that you've had built up? Or
- 31:42uh maybe they need to do it in a
- 31:43different language. Um, but the point is
- 31:46that kind of you've you've had to do uh
- 31:49the work to build up uh the this
- 31:51application, and now it has to be
- 31:53replicated every time someone else wants
- 31:55to access the same data. And even for
- 31:57you, um
- 31:59you know, like I said, if you want to
- 32:00change the query or or um
- 32:03change change things that you're looking
- 32:05for, you run into the problem of having
- 32:07to kind of re-implement things from
- 32:09scratch every single time.
- 32:12So, um
- 32:13kind of another implementation question
- 32:15is, what if two threads or two programs
- 32:17uh try to write to the the file at the
- 32:20same time? You know, if if I'm editing a
- 32:22file and you're editing the file,
- 32:24um and we both save our work,
- 32:26um
- 32:27who knows what's going to happen? Uh you
- 32:29know, one of us might uh
- 32:31overwrite the other, or maybe both of
- 32:34our writes get partially written, and
- 32:35now you have kind of this garbage re- a
- 32:38record in there. So, what you can end up
- 32:40with is
- 32:42kind of all of this you have to
- 32:44implement some kind of concurrency layer
- 32:46to manage concurrent accesses to
- 32:49the data.
- 32:52So, the final piece
- 32:53that I'm going to talk about is
- 32:55durability, which means like what
- 32:58happens if the machine crashes when when
- 33:00the program is updating a record. So,
- 33:02you're doing right
- 33:05power goes out or something or or the
- 33:07your program crashes and
- 33:09you know, now
- 33:11what happens to the data? Is it
- 33:12consistent? Did you finish writing the
- 33:14whole record you wanted to write or
- 33:17is it kind of
- 33:19half written now you have again this
- 33:20this garbage garbage value
- 33:23um
- 33:24in your your database.
- 33:27Uh
- 33:28what if we want to replicate the
- 33:29database on multiple machines so that we
- 33:31can have high availability?
- 33:33How do we handle kind of concurrent
- 33:34writes to different machines?
- 33:37Because now the machines need to be
- 33:39synchronized across them and kind of the
- 33:41list goes on and on and on with all of
- 33:43these problems that can come up. So,
- 33:45that's kind of the the reason why we
- 33:48want to build a database management
- 33:49system so we don't have to handle all of
- 33:52this complexity, all of those different
- 33:53types of problems that I mentioned in
- 33:55application code.
- 34:01Okay, so I explained what a database is.
- 34:04The software that manages database is
- 34:08a database management system.
- 34:10Sometimes people use the words
- 34:12interchangeably. I will try not to
- 34:13because
- 34:14it's confusing, but a database
- 34:16management system or DBMS is a system,
- 34:19the software that manages
- 34:22um the database and allows applications
- 34:25to store, retrieve, and analyze
- 34:27information that's that's stored in the
- 34:29database. So, rather than managing
- 34:31things in these
- 34:34CSV files, you can
- 34:36access them through the database and it
- 34:38kind of gives you all of these nice
- 34:39properties
- 34:41um
- 34:42that that avoid the problems that that I
- 34:44mentioned before.
- 34:45And a general purpose DBMS is designed
- 34:49to allow you to
- 34:52define, create, query, update, and
- 34:54administer databases
- 34:57in in a generic sense. So, if you have
- 34:59again an application for
- 35:02managing students in a class or for
- 35:04managing
- 35:06um
- 35:07an online music store,
- 35:09kind of it's it's general purpose enough
- 35:11that you can program your applications
- 35:16against the system. So, this really good
- 35:18for
- 35:20like business reasons for your business
- 35:22or startup or your your organization
- 35:24because the the purpose
- 35:26the value add that you're bringing is
- 35:28not
- 35:29the you can store data, lots of people
- 35:31can store data, but
- 35:33what what a
- 35:35database management system allows you to
- 35:37do is focus on, you know, the the core
- 35:39aspect of your business rather than
- 35:40worrying about all of these
- 35:43um
- 35:44uh
- 35:45issues surrounding actually managing the
- 35:47data. And, you know, DBMSs are widely
- 35:51tested and deployed
- 35:53so that they, you know, find all the
- 35:55bugs, the concurrency bugs, the
- 35:58consistency bugs, all those problems
- 35:59that come up
- 36:01because kind of, you know, the the
- 36:03chances that that every single Python
- 36:05program that you write is going to be
- 36:07100% bug free all the time are pretty
- 36:10much zero. So,
- 36:12kind of the the the whole point of a
- 36:14database management system is that you
- 36:16can leverage kind of this this core set
- 36:19of functionality
- 36:21for managing databases so you don't have
- 36:23to do it manually at in in application
- 36:25code.
- 36:28So, I I
- 36:30database management systems are not new.
- 36:34Um the first one came out in 1965
- 36:38um at General Electric was called IDM.
- 36:40And essentially
- 36:42um
- 36:44they were they were used kind of to to
- 36:46manage data, but they were really uh
- 36:49writing applications um they were really
- 36:51difficult to build and maintain.
- 36:54And that's because people were working
- 36:56in very low level and there was you know
- 36:58really tight coupling between
- 37:00um the logical layer, so kind of the the
- 37:03data model layer and and the physical
- 37:05layer that like the implementation
- 37:07writing to disk or retrieving it from
- 37:08disk query and that kind of stuff.
- 37:10Um
- 37:12So,
- 37:14kind of the the the big problem is that
- 37:16you had to know the the queries that you
- 37:18wanted your uh application execute
- 37:20before you deployed the database and um
- 37:23they were just really cumbersome and not
- 37:25not easy to work with. So,
- 37:27um kind of the things that that we're
- 37:29going to talk about over the course of
- 37:30the semester um might seem obvious in
- 37:34retrospect, but at the time uh you know
- 37:36people were struggling to build these
- 37:37applications and and they were kind of
- 37:39controversial. The ideas were were kind
- 37:41of controversial. So,
- 37:43there was this guy um at IBM named Ted
- 37:45Codd. Uh that's him.
- 37:47Uh who kind of observed that people were
- 37:50were re-implementing the same things
- 37:52over and over again reinventing the
- 37:53wheel
- 37:54um and and you know making a lot of
- 37:56mistakes uh along the way. So, he
- 37:58proposed kind of this this uh high-level
- 38:01idea called the relational model.
- 38:04And there is this original uh and it
- 38:06came out as a technical report in 1969,
- 38:09but nobody uh reads this one. This is
- 38:11the one that everyone references came
- 38:13out the year later in 1970.
- 38:15Um
- 38:16and it it's a paper called A Relational
- 38:19Model of Data for Large Shared Data
- 38:21Banks.
- 38:22Um
- 38:23and you know it's old because people
- 38:27when when when they wrote in the the
- 38:29text they spelled data and base as two
- 38:32separate words and now we have it
- 38:34combined as one. But um kind of the the
- 38:37the core idea that he proposed um
- 38:41was the relational model and he actually
- 38:44won the Turing Award in 1981 if you know
- 38:47what that is. Um it's like
- 38:50the
- 38:51people call it the Nobel Prize for
- 38:52computing. It's like the highest um
- 38:56honor you can receive in in computer
- 38:57science. And he he he he won it for um
- 39:03the relational model and
- 39:04as I said it was kind of
- 39:06um
- 39:07controversial at the time. I mean you
- 39:08can look at it now and say oh yeah sure
- 39:09that makes sense but
- 39:11um at the time I it it wasn't so
- 39:14obvious. So kind of the relational model
- 39:16at a high level is
- 39:18a database abstraction to avoid avoid a
- 39:20lot of low-level maintenance
- 39:22um and problems that people were running
- 39:24into writing
- 39:25um
- 39:26database applications. So
- 39:29uh
- 39:29the the the
- 39:31kind of three main points um that
- 39:34they're going to touch on are are the
- 39:37the the databases database databases
- 39:39should be stored in a simple uh data
- 39:42structure called a relation
- 39:44um
- 39:45where a relation essentially represents
- 39:48uh the relationship among attributes
- 39:51stored in a table. So like the
- 39:53relationship if if you um are thinking
- 39:55about the the artist example um the
- 39:59relationship between the artist name um
- 40:02the the year
- 40:04uh that they that they started producing
- 40:06music and the the country they're from.
- 40:07So the relationship between those
- 40:08attributes.
- 40:09Um
- 40:11kind of the the the next piece is that
- 40:13he thought you should access data
- 40:14through a high-level language um
- 40:18rather than kind of
- 40:20telling the database database management
- 40:22system explicitly how
- 40:25how you want it to get the data.
- 40:27Um you could just specify what data you
- 40:29want it and let the database management
- 40:31system figure out kind of the best
- 40:33strategy to do it
- 40:35because you know if if
- 40:37you're building a piece of software
- 40:39that's specialized to interact with
- 40:40databases, it should it should be pretty
- 40:43good at figuring out the best way to get
- 40:45you the data you want.
- 40:47Um and and finally
- 40:50the the last piece is that the physical
- 40:52storage of the data either at the time
- 40:57on on disk or secondary storage um now
- 41:01in memory also should be left up to
- 41:04the the DBMS to implement. So you don't
- 41:06have to worry about whether it's laid
- 41:07out in in a row format in CSV file, it's
- 41:11just completely abstracted to you. You
- 41:12just know
- 41:14what the data is that's in there and get
- 41:15that and let the the DBMS kind of figure
- 41:17out the specifics of of storing it. And
- 41:20you know at the time like I said people
- 41:22argued that um
- 41:24a DBMS would never be able to generate a
- 41:26query plan as efficient as kind of what
- 41:30what a human could do kind of like how
- 41:32people used to argue about you know
- 41:33compilers. No one could ever
- 41:35produce code that's uh
- 41:38you know no no
- 41:40computer program compiler could ever
- 41:41produce code that's as efficient as what
- 41:44uh
- 41:44assembly that a human could write. Now
- 41:47almost no one writes at that level. So
- 41:49kind of it's the same idea
- 41:51with databases. There there was a lot of
- 41:53human effort that went into designing
- 41:56programs to efficiently access it
- 41:57because they thought that um
- 42:00you couldn't design a
- 42:01a system to do it efficiently.
- 42:05So um
- 42:08I I mentioned that the relational model
- 42:10is a type of data model. So a data model
- 42:13is just essentially at a high level a
- 42:15collection of concepts
- 42:17uh for describing the data that's stored
- 42:20in in a database. So, it's like a
- 42:22high-level abstraction
- 42:24um for for how we're going to represent
- 42:27data.
- 42:28Now, a schema
- 42:30um is more specifically a description of
- 42:33a a particular collection of data
- 42:37given a a data model. So, what does that
- 42:39mean? Um
- 42:41it means that the the schema
- 42:44defines exactly what we're going to
- 42:46store in the database. So, for example,
- 42:48in the the music store example, um
- 42:51the schema would be the artist
- 42:54table followed by the different uh
- 42:57attributes that are associated with an
- 42:59artist. So, that's the schema that you
- 43:01would define
- 43:02uh that describes the data that's stored
- 43:04in the database.
- 43:08So, there are as I said lots of lots of
- 43:10uh different data models. Um most DBMSs
- 43:14that you may have heard of
- 43:16um are based on this first relational uh
- 43:19data model and um
- 43:23for a lot of reasons, but uh probably
- 43:25the biggest one is that the the
- 43:27relational model is
- 43:29uh probably the most flexible of all of
- 43:32them. Um the relational model can kind
- 43:34of model all of these other or uh um you
- 43:37you you can represent all of these other
- 43:39data models using the relational model.
- 43:43Um and the the the exact, you know,
- 43:45uh
- 43:46uh API or or what's going on behind the
- 43:49scenes might not be as efficient, but um
- 43:51it's still general enough to be able to
- 43:54uh handle all of these other models.
- 43:56So, um
- 44:00NoSQL is a is a a popular term. Um
- 44:04it means a lot of things uh to a lot of
- 44:07different people, but just at kind of a
- 44:09high level
- 44:11um
- 44:14like
- 44:15for example
- 44:16it's more than just a data model. A lot
- 44:18of people when they hear NoSQL think
- 44:19about things like transactions or
- 44:21consistency or not having
- 44:23certain APIs, but just in terms of the
- 44:26actual NoSQL data model it covers kind
- 44:28of this key value
- 44:31stores, graph database management
- 44:33systems, document databases,
- 44:35uh MongoDB,
- 44:37um and and kind of
- 44:38broader column family databases. And
- 44:41um
- 44:44kind of these these data models are more
- 44:48restrictive than what you get with the
- 44:50relational model.
- 44:51And they they don't give you as many as
- 44:53I said kind of guarantees in terms of
- 44:56um
- 44:57different properties that we're going to
- 44:59discuss over the course of the semester.
- 45:01I There are some just
- 45:04often I would say there are some
- 45:05application domains where
- 45:08these data models might make sense. So
- 45:10imagine I don't know you're storing
- 45:12video data or you're storing
- 45:15log data or something. You just want to
- 45:16shove it into a key value store. That's
- 45:19that's
- 45:20perfectly valid and and might be better
- 45:23for that particular type of application
- 45:25than than a relational database.
- 45:29Um array and matrix database management
- 45:33systems are kind of specialized more
- 45:36towards machine learning or
- 45:39scientific applications. There are a few
- 45:41SciDB,
- 45:43TileDB, but they're not they're not
- 45:44really widespread and and kind of
- 45:46they're
- 45:47narrowly focused on these sorts of use
- 45:49cases. And And the last group,
- 45:53hierarchical, network, and multi-value,
- 45:56these are kind of either obsolete data
- 45:58models that people tried out in the past
- 46:00and
- 46:01they find and found didn't work well or
- 46:04they had other problems but they you
- 46:06know they might still be hanging around
- 46:07in legacy systems.
- 46:10So,
- 46:11for the purposes of this course, we're
- 46:13going to be focusing exclusively on the
- 46:16relational model
- 46:18and um
- 46:20how database management system works in
- 46:22in that context.
- 46:29Okay.
- 46:30So,
- 46:31what is the relational model exactly?
- 46:34Um
- 46:36the the
- 46:38relational model
- 46:40uh
- 46:41defines
- 46:42the relations inside the database. So,
- 46:45like I mentioned, the individual tables.
- 46:48Sometimes they in in SQL they're called
- 46:50tables. Um
- 46:52in the relational model they're called
- 46:55um
- 46:56uh relations. I I'll probably end up
- 46:59using them interchangeably um
- 47:01but you can think of them just
- 47:03concretely as those examples from the
- 47:05music store.
- 47:07There's the the artist relation on table
- 47:10and the album
- 47:12relation.
- 47:13And the the structure of the relational
- 47:16model defines those relations and the
- 47:19contents inside them.
- 47:20So, the next piece as I mentioned is
- 47:22integrity
- 47:23which ensures that the database's
- 47:26contents
- 47:28satisfy some constraints. So, um
- 47:31with the the concern about you know
- 47:34uh uh
- 47:34whether or not there could be a garbage
- 47:36string value
- 47:38inserted or overwritten on a on a um
- 47:42uh
- 47:43when you're expecting an integer year,
- 47:46kind of the integrity aspect seeks to to
- 47:49mitigate that problem
- 47:50by forcing all of the the year values to
- 47:53be integers and forcing that property.
- 47:56Uh and finally, there's the the
- 47:58manipulation aspect which is the
- 48:00programming interface or API that you
- 48:01use to access and modify um the contents
- 48:06of the the database. And uh
- 48:09this is what we're going to talk about
- 48:10later
- 48:12in in the lecture uh
- 48:15with relational algebra. So, relational
- 48:17algebra is a is a um
- 48:19uh
- 48:20programming programming interface for
- 48:22interacting with um the relational
- 48:24model.
- 48:26So, uh again kind of just uh uh
- 48:29going through the example, a relation is
- 48:31an unordered set. So, that's important.
- 48:34Um
- 48:35uh
- 48:36there's no order to the values. Uh we
- 48:39don't necessarily care what order they
- 48:41appear in
- 48:42um in the database.
- 48:44Um
- 48:45they could be sorted, they could not be
- 48:46sorted. Uh it doesn't matter. It's just
- 48:48a set.
- 48:50Um and it contains the relationship uh
- 48:55uh of the attributes that represent
- 48:57entities. So, as I said a relation or
- 49:00table has many entries that are called
- 49:04uh
- 49:04um tuples, tapples some people say. I
- 49:07don't Either's fine with me.
- 49:09Um but I'll I'll probably end up saying
- 49:10both, but uh tuples or records also in a
- 49:14table. Tuple or record is
- 49:16interchangeable. There's There's another
- 49:18word that sometimes people use uh called
- 49:20rows.
- 49:21Um
- 49:22I will try not to use that. I might
- 49:23accidentally, but uh a row implies
- 49:26something specific about how the data is
- 49:28stored. So, in the the um CSV example,
- 49:31you know, every every uh data was stored
- 49:33as a individual line. Um but the the
- 49:38uh tuples and records represent
- 49:40individual uh data entries
- 49:43in a table.
- 49:44Um
- 49:45The the values are uh normally um scalar
- 49:51values, things like integers, strings.
- 49:53That's not necessarily true anymore. Um,
- 49:55the original specification they all had
- 49:57to be kind of these scalar values, but
- 49:59um, any popular systems uh have started
- 50:03relaxing it to store things like uh
- 50:05store a, you know, array maybe in a
- 50:07individual column or like a a JSON
- 50:09document um, in an individual individual
- 50:12um,
- 50:13attribute.
- 50:14Um, and there's also this notion of uh a
- 50:18null, which is a member of every single
- 50:21uh domain. It can be any um, attribute
- 50:24can be set to null, and it it's not
- 50:26exactly like null in uh
- 50:28like a null pointer or something, but uh
- 50:31it used to signify that we don't know
- 50:34what a particular value is. So, for
- 50:36example, if if um,
- 50:39the the country that I I SKU is from
- 50:42uh were null, it it just means that we
- 50:43don't we don't know what that value uh
- 50:46what
- 50:47we don't know the specific value for
- 50:48that um, record.
- 50:51So,
- 50:52uh
- 50:53uh uh
- 50:54again, just a a relation is a is a a
- 50:56mathematical term that represents um,
- 50:58this unordered set, and
- 51:02uh
- 51:03SQL the the SQL equivalent is is a
- 51:05table.
- 51:08So, in n-ary relation, if I say n-ary
- 51:11relation, it means that there's a table
- 51:13with n individual columns and individual
- 51:16attributes. So, that's just it's a
- 51:18three columns.
- 51:24Okay, so a a big piece of um, the
- 51:27relational model is um,
- 51:30that every relation should have a
- 51:33primary key that uniquely identifies uh
- 51:37a single tuple.
- 51:39So, um,
- 51:41some DBMSs will will automatically
- 51:43create this for you. If you don't
- 51:45specify it, they'll do it behind the
- 51:46scenes. Kind think about like a you
- 51:48know, auto incrementing uh um unique
- 51:50integer um for for keys.
- 51:54So, in this this uh table example here,
- 51:57um
- 51:58there's not really anything that we can
- 52:00use to uniquely identify. I guess I
- 52:02mean, you could potentially use the name
- 52:04of the artist um
- 52:07to uniquely identify, but I there's no
- 52:09guarantee that, you know, all of those
- 52:11values are going to be unique. So, um
- 52:15what you can do is add kind of this
- 52:17surrogate primary key that's essentially
- 52:19just a just an integer. And like I said,
- 52:21it could be auto generating or random or
- 52:23whatever. Um
- 52:25kind of just to to keep uh each each
- 52:28record or tuple um unique.
- 52:32So,
- 52:34foreign keys uh
- 52:37are are related to primary keys in that
- 52:40they specify
- 52:41um that an attribute from one relation
- 52:44has a mapping uh to to some tuple in
- 52:47another relation.
- 52:49So,
- 52:50just concretely, um if we have this
- 52:54artist key here, and we have the album
- 52:57key,
- 52:58we know that uh the artists, so the the
- 53:02new primary key ID numbers that we've
- 53:04added to the album table, um the artists
- 53:08uh
- 53:091 2 3 7 8 9 reference artists stored
- 53:14tuples stored in the uh artist table.
- 53:16So, if we want to know
- 53:18um
- 53:19you know,
- 53:20who wrote one of these or who released
- 53:22one of these albums, we can go look up,
- 53:24okay, what's the number um
- 53:26of the artist, and then go look up which
- 53:28artist that is uh in in the artist
- 53:30table. But, you know, there's a problem
- 53:31you can run into
- 53:33uh which is I I mentioned earlier, which
- 53:35is what if you want to have um
- 53:38multiple artists uh uh, on on a single
- 53:41album. So, again, you can release like a
- 53:43mixtape or something where you have a
- 53:45lot of different artists contributing,
- 53:47but we only have this one,
- 53:49um,
- 53:49attribute to store, uh, the artist ID.
- 53:53And the solution we come up with in, uh,
- 53:56the relational model
- 53:58is to create this, uh, a third table,
- 54:01sometimes called a join table, which is
- 54:03going to join or link,
- 54:06the artist table with the album table.
- 54:09So, we're going to do is we're going to
- 54:10we're going to get rid of that artist
- 54:12column in the album table
- 54:14and instead, uh, create this new artist
- 54:17album table that has only two things in
- 54:19it. So, it has an artist ID and an album
- 54:22ID.
- 54:23And now, if if you look at it, we can
- 54:25figure out, okay, here is how all of
- 54:27the, uh, artists link to the the albums
- 54:31that they they release. So,
- 54:33in the case where you have, um,
- 54:36you know,
- 54:37one,
- 54:38uh, uh,
- 54:40artist on an album, then there's only
- 54:42going to be one, uh, tuple in the artist
- 54:45album relation. In the case where you
- 54:47have multiple, you'll have now multiple
- 54:49tuples.
- 54:58Okay, so, data manipulation languages or
- 55:01DML
- 55:03are, um, the specific methods that that
- 55:05we use to store and retrieve, um,
- 55:07information from a database.
- 55:10And they come specifically in in two
- 55:13flavors. So, the first is procedural,
- 55:16uh, and that's
- 55:17uh,
- 55:18that that a high-level query should
- 55:21specify how the DBMS should compute the
- 55:25answer to our query.
- 55:27So, the keyword here is how. You're
- 55:29telling the database how you want it to
- 55:31retrieve the data.
- 55:33And this is going to be, uh, uh,
- 55:35relational algebra that we talked about,
- 55:37uh, in in a couple slides here, um,
- 55:40you're saying specifically how you want
- 55:42the data to retrieve from the database.
- 55:46Uh, the alternative is a non-procedural
- 55:48or declarative language,
- 55:50um, where, uh, you only specify what
- 55:55data you want retrieved from the
- 55:56database. You know, you don't tell the
- 55:57database management system how you want
- 55:59to get it back.
- 56:01You just say, uh, what data you want,
- 56:03and then the the system goes and figures
- 56:05out how to how to do that for you.
- 56:07So,
- 56:08um, an example of this
- 56:11non-procedural, uh, or declarative
- 56:13language, um,
- 56:15is relational calculus. I
- 56:17We're not going to talk too much about
- 56:18relational calculus in this class.
- 56:20Um, it's important. It's, uh, really
- 56:23important for query optimization,
- 56:25um, but it's it's not really being
- 56:27covered in this class, so don't worry
- 56:28too much about it. Um,
- 56:30relational algebra and relational
- 56:32calculus are are,
- 56:35like they're they're logically
- 56:36equivalent, um, but we're, like I said,
- 56:39only going to be focusing on on, uh,
- 56:41this first one. Another example of, uh,
- 56:44a declarative language is SQL, um, which
- 56:46we're going to talk about next class.
- 56:51So,
- 56:52relational algebra defines the original
- 56:55the original, um, specification defines
- 56:57these, uh,
- 56:58seven operators, and they're all
- 57:01fundamental operations that you can use,
- 57:04uh, to retrieve and manipulate the the
- 57:07tuples in a relation. This is this These
- 57:10are the the fundamental operators
- 57:11proposed by, um, Codd.
- 57:14And,
- 57:15um,
- 57:16they're based on, uh, mathematical ideas
- 57:19of, uh, set algebra.
- 57:22So, each operator listed here is going
- 57:24to take in one or more relations as
- 57:27input, and it's going to output a new
- 57:29relation.
- 57:31So, um, in order to build up a query to
- 57:34to get the data you want out of your
- 57:35database, uh you can kind of chain these
- 57:37operators together um to create more
- 57:40complex operations.
- 57:43So, we'll just kind of go through each
- 57:44one starting with uh select.
- 57:46Um
- 57:48Sorry, is there a question? No?
- 57:50Um so,
- 57:53uh
- 57:54starting with select, essentially the
- 57:55the goal of the select operator is to uh
- 57:59choose a subset of the the tuples from a
- 58:01relation
- 58:03um that satisfy a particular predicate
- 58:05that you uh provide.
- 58:08So, the predicate, you can think about
- 58:09it acting like a filter or kind of like
- 58:11an if statement um that's only going to
- 58:14retain
- 58:15uh the tuples that qualify um
- 58:18for for the the user-specified
- 58:20predicate. And you can of course combine
- 58:24um multiple multiple predicates using um
- 58:27conjunctions or disjunctions uh to get
- 58:29exactly the
- 58:31the uh
- 58:32subset that you want. Uh I think it's
- 58:34called restrict in the original um
- 58:37relational model paper, but um
- 58:40everyone everyone calls it a select. Uh
- 58:43the the symbol is the lowercase sigma.
- 58:45Um
- 58:47uh
- 58:48in in relational algebra. So, just as an
- 58:50example, we have this really simple
- 58:52relation here. Uh R, it only has two
- 58:56attributes or columns, AID and BID.
- 59:00Uh so, if we want to, for example, uh
- 59:05restrict um
- 59:07or filter the uh relation to only cases
- 59:11where AID equals A2, then we apply that
- 59:16uh predicate, and we get back
- 59:18uh just that subset that satisfies the
- 59:21predicate that we that we specified.
- 59:24And as I said, you you kind of chain
- 59:25things together and then
- 59:27"Okay, give me all of the the records
- 59:29where AID equals A2 and
- 59:32BID equal or is greater than 102." So,
- 59:36in this case, you can see you're you're
- 59:37only going to get that one
- 59:39tuple.
- 59:41Uh and then if you're wondering how this
- 59:43how this works with SQL, I I apologize.
- 59:45I didn't come up with the the naming
- 59:47convention, but the the select
- 59:49relational algebra operator maps to the
- 59:52where clause in a SQL statement.
- 59:55Um
- 59:56might be a little confusing, but select
- 59:58in relational algebra maps to where
- 1:00:00clause.
- 1:00:01Uh
- 1:00:02and again, we'll talk more about SQL
- 1:00:04next time, so we'll go through these
- 1:00:06individually. But, um
- 1:00:08the projection statement is essentially
- 1:00:10going to generate a relation
- 1:00:13with tuples that only contain the
- 1:00:15attributes that you asked for. And what
- 1:00:18does that mean? You can do all sorts of
- 1:00:19things like rearrange the order of the
- 1:00:21attributes.
- 1:00:22Um you can manipulate the values of the
- 1:00:24attributes.
- 1:00:25Um
- 1:00:27do different types of modifications.
- 1:00:29It's It's specified with the lowercase
- 1:00:30pi symbol and and essentially a list of
- 1:00:33all of the different modifications
- 1:00:35you're making.
- 1:00:37So, um
- 1:00:39here is just a an example query
- 1:00:42where
- 1:00:43again, we're doing the selection first.
- 1:00:45So, I mentioned you could chain them
- 1:00:46together. We're doing the selection
- 1:00:48first to get only the records that have
- 1:00:50AID equals A2, and then we're performing
- 1:00:53a projection um
- 1:00:56where we're going to modify B by
- 1:00:58subtracting 100, and we're going to put,
- 1:01:01you know, swap the order of
- 1:01:03BID and AID. And that's that's kind of
- 1:01:05the result you get. So, again, this is
- 1:01:08I didn't come up with the names,
- 1:01:10but the projection operator maps to the
- 1:01:13select clause of the SQL statement. So,
- 1:01:16the the selection operator maps to the
- 1:01:18where clause, the projection operator
- 1:01:20maps to the select clause.
- 1:01:26The union operator is essentially going
- 1:01:29to um
- 1:01:31generate a relation that contains all
- 1:01:33the tuples that appear in um
- 1:01:36either or or both relations. Uh it's
- 1:01:40just like a set union.
- 1:01:42Um so for example, imagine we have
- 1:01:43another second table here S which has
- 1:01:45the same schema as the first one. Um
- 1:01:49to perform uh R union S
- 1:01:52is going to give us essentially all of
- 1:01:53the tuples that appear in R, all of the
- 1:01:55tuples that appear in S.
- 1:01:57And uh the the SQL syntax for this is a
- 1:01:59little bit different. It's union all in
- 1:02:01order to get um potential duplicates
- 1:02:05um
- 1:02:06because of a a difference between
- 1:02:08um
- 1:02:09uh set algebra and like a
- 1:02:11uh a bag or multi-set algebra.
- 1:02:14So you can see here that there are there
- 1:02:16is the the the duplicate that appears in
- 1:02:18in the
- 1:02:20union.
- 1:02:22So in in this example, I the the output
- 1:02:24is ordered, but that's again not
- 1:02:27necessarily be true um because it's a
- 1:02:29set they could appear in uh the output
- 1:02:32relation could appear in any order.
- 1:02:34And the answer would still, you know,
- 1:02:36still be considered correct. So you
- 1:02:37can't count on that the ordering there.
- 1:02:40Uh the intersection operator um is going
- 1:02:43to generate a relation that only
- 1:02:45contains the tuples that appear in both
- 1:02:48of the input relations. So this is R
- 1:02:50intersect S. Again, it's the same um
- 1:02:53example where R and S both have the same
- 1:02:55schema, and what we want to do is find
- 1:02:57kind of the intersection.
- 1:02:59Uh it's going to give us only A3 appears
- 1:03:03in both R and S. So you can do this in
- 1:03:07uh in SQL using the the intersect
- 1:03:10operator. So just as an example of when
- 1:03:12this might be useful, uh if you think
- 1:03:14about the the music store example, um
- 1:03:17maybe we have one relation representing
- 1:03:19the rap artists and another one
- 1:03:20representing
- 1:03:22rock or country artists and you want to
- 1:03:24know which artists have
- 1:03:27you know both rap albums and you know
- 1:03:29rock or country albums. So, you would
- 1:03:31just do an intersection of the two
- 1:03:33relations to get that answer.
- 1:03:38The difference operator
- 1:03:40is going to generate a relation that
- 1:03:42contains only the tuples that appear in
- 1:03:44the first relation and not the second
- 1:03:46relation.
- 1:03:48So, in this example here, you end up
- 1:03:50with only the the unique ones that
- 1:03:52appear in appear in R and not in S.
- 1:03:57And you can do that in SQL using the
- 1:03:59except
- 1:04:00keyword.
- 1:04:05Okay, the product
- 1:04:06is essentially going to generate a
- 1:04:08relation that contains all the possible
- 1:04:10combinations of tuples from
- 1:04:13the both input relations. So,
- 1:04:17sometimes it's called a Cartesian
- 1:04:18product but it's the it's the product of
- 1:04:21two relations. It's going to be all the
- 1:04:22pairwise combinations.
- 1:04:25So, essentially what you're going to get
- 1:04:27from doing
- 1:04:29the product of R and S is kind of all of
- 1:04:31these
- 1:04:33pairwise combinations of tuples from
- 1:04:35from both relations.
- 1:04:36This might seem kind of useless but it
- 1:04:39it does show up sometimes.
- 1:04:42You know, if you want to get generate
- 1:04:44all the possible combinations
- 1:04:46in in two tables. But more importantly,
- 1:04:49what what this is going to be used for
- 1:04:50is just from a conceptual or theoretical
- 1:04:53perspective,
- 1:04:54it's going to let us model the next
- 1:04:56operator we're going to talk about which
- 1:04:58is a join. So, kind of you can get all
- 1:05:00these these pairwise
- 1:05:03combinations.
- 1:05:05This is the the syntax in SQL um
- 1:05:08for how to do it.
- 1:05:10You can get all of pairwise combinations
- 1:05:12but now let's say that you just want the
- 1:05:14pairwise combinations where your
- 1:05:17primary key that we talked about in your
- 1:05:19foreign key match. So, you have two keys
- 1:05:21that match. So, the join operator is
- 1:05:24going to generate a relation that um
- 1:05:27contains all all the two poles that are
- 1:05:29combination
- 1:05:30of um
- 1:05:32the the two inputs where there's a
- 1:05:34common value for one or more attributes
- 1:05:37depending on on what you specify.
- 1:05:39So,
- 1:05:40uh
- 1:05:41what we're going to do is we're going to
- 1:05:41look for um
- 1:05:43matches here and again these two tables
- 1:05:46have the same schema, but you could do
- 1:05:47it in an arbitrary case in the
- 1:05:49music store example you have
- 1:05:51the artist uh relation and the the
- 1:05:53albums relation and they share the um
- 1:05:57uh
- 1:05:58artist uh primary key that they join on.
- 1:06:01So, um
- 1:06:03what's happening here with the join uh
- 1:06:05operator is that we're we're only
- 1:06:07finding the matches like I said
- 1:06:09uh that appear in both
- 1:06:12uh the the R table and the S table.
- 1:06:17So, like I said kind of this this
- 1:06:19conceptually if you think about what
- 1:06:20what the the um product operator does,
- 1:06:23you could produce all of these pairwise
- 1:06:25optimizations I already said so you
- 1:06:27produce all these pairwise combinations
- 1:06:29and then um filter it down to just uh
- 1:06:33uh the the set that matches uh where the
- 1:06:36keys match. But of course you never do
- 1:06:38this um
- 1:06:40uh
- 1:06:41because you know you have to first
- 1:06:43enumerate all the the different uh
- 1:06:46uh combinations and then perform the
- 1:06:47filter. So, there are different
- 1:06:49optimizations you you might want to do
- 1:06:50to short circuit this, but from a
- 1:06:52theoretical perspective conceptual
- 1:06:53perspective it's useful um to think
- 1:06:56about
- 1:06:57in this way.
- 1:06:59So, uh the the SQL syntax for this and
- 1:07:01again we'll go over these in in more
- 1:07:04detail um next class, but the SQL syntax
- 1:07:07to do this join here
- 1:07:08um is the natural join operator.
- 1:07:13Um so uh over time, like I said, there
- 1:07:16there have been um
- 1:07:18extensions to the original uh relational
- 1:07:21algebra where they built in more
- 1:07:22operators. Uh there's now
- 1:07:25a bunch that people have added.
- 1:07:27Rename, assignment, duplicate
- 1:07:28elimination, aggregation. Imagine you
- 1:07:31want to count how many
- 1:07:32um
- 1:07:33albums a particular artist has released.
- 1:07:36Uh sorting, so you can actually impose
- 1:07:38some kind of ordering
- 1:07:40uh on the unordered set.
- 1:07:41Um and and set division.
- 1:07:44So, there are analogs for all of these
- 1:07:46in SQL that that we'll talk about. Don't
- 1:07:48don't worry too much about them. They're
- 1:07:49not um going to be super important for
- 1:07:52uh the course.
- 1:07:55Um so, just an observation about
- 1:07:57relation relational algebra. Um
- 1:07:59kind of we've gone through all the
- 1:08:00operators, but
- 1:08:02it it's still defining more or less the
- 1:08:05high-level steps that are necessary for
- 1:08:08how to compute a query. So, essentially,
- 1:08:12we're telling um
- 1:08:15the the database management system
- 1:08:16exactly how it should go about computing
- 1:08:19the query. And in the example here, um
- 1:08:23we have two,
- 1:08:25you know, more or less equivalent
- 1:08:26queries. On on uh the one side, you're
- 1:08:29doing uh the join between R and S first
- 1:08:33before applying the filter to only the
- 1:08:36tuples that have BID 102.
- 1:08:39And on the other side, uh you're
- 1:08:40filtering S to only the tuples that have
- 1:08:43102 uh BID equals 102 and then
- 1:08:45performing the join. So, kind of these
- 1:08:48these two, while they're going to
- 1:08:49produce the exact same answer, one might
- 1:08:52be a lot more um efficient than the
- 1:08:55other. You know,
- 1:08:56if you have, again, a billion tuples in
- 1:08:59the relation, um you might want to do
- 1:09:01the filtering first Uh, so you get that
- 1:09:03as one rather than having to, you know,
- 1:09:06find all the billion matches and then
- 1:09:08then do the filter. So, in this way, um,
- 1:09:11the the the
- 1:09:13uh relational algebra is still
- 1:09:15procedural and not declarative. So,
- 1:09:18really what we'd like is is a
- 1:09:19declarative language like SQL for
- 1:09:21accessing
- 1:09:23um, the database. And that's kind of
- 1:09:25this idea where you want to state the
- 1:09:28the high-level answer or exactly what
- 1:09:31you want the database management system
- 1:09:34to compute for you rather than telling
- 1:09:36it how it should go about doing it. And
- 1:09:39essentially, you can just leave the
- 1:09:41low-level details about, you know, how
- 1:09:44to actually do it to the database
- 1:09:46management system. And if it's well
- 1:09:48implemented, it should do it um, a lot
- 1:09:50better than than um, the kind of
- 1:09:53high-level procedural
- 1:09:55uh query you would produce.
- 1:10:02So, the relational model just as a
- 1:10:03concept is independent of any kind of
- 1:10:06specific um, query language
- 1:10:08implementations. There were a lot that
- 1:10:10got proposed over the years.
- 1:10:12Um, I think there was an Alpha proposed
- 1:10:14originally proposed by Cod, but no one
- 1:10:16talks about that anymore. Uh, there's
- 1:10:18Quel, um, which was produced by someone
- 1:10:19from Berkeley. Um,
- 1:10:22and
- 1:10:24SQL kind of emerged as the de facto
- 1:10:26standard. Some people call it SQL. Um,
- 1:10:28some people call it SQL. Both are fine.
- 1:10:30Uh, I'll still know what you're talking
- 1:10:31about if you say either.
- 1:10:33Um, so it's kind of emerged as the de
- 1:10:35facto standard. Um, and there are
- 1:10:37standard specifications, but uh, pretty
- 1:10:40much every um,
- 1:10:43uh system implements its own uh
- 1:10:47variant. So,
- 1:10:49uh if you write a a query for MySQL, it
- 1:10:51might not exactly match something that
- 1:10:53runs on Postgres or Oracle or SQLite or
- 1:10:56whatever. Uh so, kind of that's the
- 1:10:59tricky part. There is a standard that
- 1:11:01everyone like, you know, 98% adheres to,
- 1:11:04but there's this kind of wiggle room
- 1:11:07where people implement their own special
- 1:11:09um divergencies. So, going way back to
- 1:11:13the Python example I gave earlier, this
- 1:11:15was the uh code where you're going to
- 1:11:17iterate over all the lines in the file
- 1:11:19um to find the year that Ice Cube went
- 1:11:22solo. And if we rewrite that in in uh a
- 1:11:27SQL query, we get this a lot more
- 1:11:28compact specification for what we want,
- 1:11:32and not how we want the database
- 1:11:35management system uh to to give it to
- 1:11:37us. So, we say we want to select the
- 1:11:39year from the artists table where the
- 1:11:41name of the artist is Ice Cube. So,
- 1:11:44that's a lot uh uh higher-level, and we
- 1:11:46don't have to worry about kind of the
- 1:11:48low-level details about reading the
- 1:11:50lines, parsing the lines, all that
- 1:11:52stuff. We just tell the database
- 1:11:54management system, "This is what we
- 1:11:55want. This is the answer that we want.
- 1:11:58You figure out how to how to get it back
- 1:12:00to me."
- 1:12:02So,
- 1:12:03that kind of wraps everything up. Um
- 1:12:07the the two takeaways are that, you
- 1:12:09know, databases are ubiquitous. They're
- 1:12:10used in all sorts of applications all
- 1:12:12over the place. There's any amount of
- 1:12:14data being managed in the application
- 1:12:17you're using,
- 1:12:18um there is
- 1:12:20almost certainly a database management
- 1:12:22system behind the scenes.
- 1:12:24Um relational algebra kind of defines
- 1:12:26the the primitives for processing
- 1:12:28queries on a relational database.
- 1:12:31And uh
- 1:12:32we're going to see relational algebra
- 1:12:33again when we talk about um query
- 1:12:35optimization and execution,
- 1:12:37um but
- 1:12:39for now, the next class is going to be
- 1:12:41uh specifically about SQL and we're
- 1:12:42going to go through some more advanced
- 1:12:44uh topics with with SQL.
- 1:12:45So, um that's it. And then uh I will see
- 1:12:48you on Wednesday. Yeah.
- 1:12:59This R C K talking about the Saint
- 1:13:01Ides brew.
- 1:13:03Share with my crew is magnificent.
- 1:13:05Busters mellow. And for the rest of the
- 1:13:07commercial, pass the mic on to my
- 1:13:09fellow.
- 1:13:09for a mic check, Frosty. The beat is all
- 1:13:11set to grab a 40. The funky Yorkers snap
- 1:13:13they necks. Saint Ides. Take a sip and
- 1:13:15wipe your lips till my 40 is getting
- 1:13:17warm. I'm outie, got to split. Drink a
- 1:13:19drink a drink a Then I burp after I
- 1:13:21slurp ice cold. I'm putting much work
- 1:13:23with the B M T and the E double get us a
- 1:13:26Saint Ides brew on the job.
About this transcript
This page contains the full transcript of 01 - Course Introduction & Relational Model (CMU Intro to Database Systems / Fall 2021) by CMU Database Group, generated from the public captions YouTube serves with the video. The transcript has 11,396 words across 1,974 segments, with the original timestamps preserved so you can click any line to jump to that moment in the embedded player.
What you can do with it
Use the transcript to take notes, quote the speaker, build a study guide, generate a summary with ChatGPT or Claude via the YouTube Summary tool, or export it as a timed subtitle file with YouTube to SRT. You can also re-open it in the transcriber to choose a caption track available on the video.
Free YouTube transcript tool
YouTube2Text is a free YouTube transcript generator — no signup, no daily limit. Paste any YouTube link and get the full transcript instantly, with timestamps, click-to-jump, caption language selection, AI prompts for ChatGPT, Claude, and Gemini, and exports to TXT, SRT, VTT, or Markdown.