Return to Video

02-02-querying-relational-databases.mp4

  • 0:00 - 0:04
    In this video, we're going to learn about querying relational databases.
  • 0:04 - 0:07
    We're not going to focus on a specific query language, we'll do that later.
  • 0:07 - 0:11
    We're just going to talk about querying relational databases in general.
  • 0:11 - 0:12
    Let's start by talking about
  • 0:12 - 0:14
    the basic steps in creating
  • 0:14 - 0:16
    and using a relational database.
  • 0:16 - 0:17
    So, by the way, I
  • 0:17 - 0:19
    should mention that database people
  • 0:19 - 0:21
    have this habit of drawing databases
  • 0:21 - 0:23
    and database systems as gigantic
  • 0:23 - 0:26
    disks. So, I'll be using that same habit.
  • 0:26 - 0:28
    So, the first step is to
  • 0:28 - 0:29
    design the schema of the
  • 0:29 - 0:30
    database and then create
  • 0:30 - 0:33
    the schema using a data definition language.
  • 0:33 - 0:34
    So as we discussed in previous
  • 0:34 - 0:36
    videos in a relational database
  • 0:36 - 0:38
    the schema consists
  • 0:38 - 0:40
    of the structure of
  • 0:40 - 0:43
    the relations and the attributes of those relations.
  • 0:43 - 0:45
    So we set those up inside our big disk.
  • 0:45 - 0:47
    Once that's ready, the next
  • 0:47 - 0:50
    step is to load up the database with the initial data.
  • 0:50 - 0:52
    So it's fairly common for the
  • 0:52 - 0:53
    database to be initially loaded
  • 0:53 - 0:55
    from data that comes from an outside source.
  • 0:55 - 0:56
    Maybe the data is just stored
  • 0:56 - 0:58
    in files of some type, and
  • 0:58 - 1:01
    then that data could be loaded into the database.
  • 1:01 - 1:02
    Once the data is loaded, then
  • 1:02 - 1:03
    we have a bunch of tuples in
  • 1:03 - 1:05
    our relation. Now, we're ready
  • 1:05 - 1:08
    for the fun part which is to query and modify the data.
  • 1:08 - 1:10
    And so that happens continuously
  • 1:10 - 1:13
    over time as long as the database is in existence.
  • 1:13 - 1:14
    So let's just say for now
  • 1:14 - 1:15
    that we're going to have human
  • 1:15 - 1:17
    users that are directly querying the database.
  • 1:17 - 1:19
    In reality, that typically happens
  • 1:19 - 1:22
    through say an application or a website.
  • 1:22 - 1:23
    So, a user will come along and
  • 1:23 - 1:27
    we'll ask a question of the database and we will get an answer.
  • 1:27 - 1:28
    He might come along and
  • 1:28 - 1:31
    ask another question Q2 and he'd get another answer back.
  • 1:31 - 1:32
    The same human or maybe a
  • 1:32 - 1:35
    different human might ask to modify the database.
  • 1:35 - 1:37
    So, they might want
  • 1:37 - 1:38
    to insert new data or
  • 1:38 - 1:39
    update some of the data
  • 1:39 - 1:40
    and the database will come back
  • 1:40 - 1:43
    and say, "Okay, I made that change for you."
  • 1:43 - 1:45
    So that's the basic paradigm
  • 1:45 - 1:49
    of querying and updating relational databases.
  • 1:49 - 1:50
    Relational databases support ad
  • 1:50 - 1:53
    hoc queries and high-level languages.
  • 1:53 - 1:54
    By ad hoc, I mean that
  • 1:54 - 1:57
    you can pose queries that you didn't think of in advance.
  • 1:57 - 2:00
    So it's not necessary to write long programs for specific queries.
  • 2:00 - 2:02
    Rather the language can be
  • 2:02 - 2:03
    used to pose a query
  • 2:03 - 2:05
    as you think about what you want to ask.
  • 2:05 - 2:06
    And as mentioned in previous videos
  • 2:06 - 2:08
    the languages supported by relational
  • 2:08 - 2:10
    systems are high level, meaning
  • 2:10 - 2:12
    you can write in a fairly compact
  • 2:12 - 2:14
    fashion rather complicated queries
  • 2:14 - 2:15
    and you don't have to write the
  • 2:15 - 2:18
    algorithms that get the data out of the database.
  • 2:18 - 2:19
    So, let's look at an example of
  • 2:19 - 2:22
    a few queries. Let's go to again to our
  • 2:22 - 2:25
    imaginary database of students who are applying to colleges.
  • 2:25 - 2:27
    And here's just three examples of the types of things
  • 2:27 - 2:29
    that you might ask of a relational database.
  • 2:29 - 2:31
    You might want to get all
  • 2:31 - 2:32
    students whose GPA is greater
  • 2:32 - 2:34
    than 3.7 who are applying
  • 2:34 - 2:37
    to Stanford and MIT only.
  • 2:37 - 2:38
    You might want to get all
  • 2:38 - 2:39
    engineering departments in California
  • 2:39 - 2:41
    with fewer than 500 applicants or
  • 2:41 - 2:42
    you might ask for the
  • 2:42 - 2:44
    college with the highest average
  • 2:44 - 2:46
    accept rate over the last five years.
  • 2:46 - 2:48
    Now these might seem
  • 2:48 - 2:49
    like a fairly complicated queries
  • 2:49 - 2:51
    but all of these can be
  • 2:51 - 2:52
    written in a few lines
  • 2:52 - 2:54
    in say the SQL language or
  • 2:54 - 2:57
    a pretty simple expression in relational algebra.
  • 2:57 - 2:58
    So, some queries are
  • 2:58 - 3:00
    easier to pose than others, that's certainly true.
  • 3:00 - 3:02
    Though the 3 queries you
  • 3:02 - 3:04
    see here are as I said pretty easy to pose.
  • 3:04 - 3:06
    Now some queries are easier
  • 3:06 - 3:09
    for the database system to execute efficiently than others.
  • 3:09 - 3:12
    And interestingly it's not necessarily.
  • 3:12 - 3:13
    These two things aren't necessarily correlated.
  • 3:13 - 3:15
    There are some queries that are easy
  • 3:15 - 3:16
    to post but hard to execute
  • 3:16 - 3:20
    efficiently and some that are vice-versa.
  • 3:20 - 3:22
    Now, just a bit about terminology.
  • 3:22 - 3:24
    Frequently, people talk about the
  • 3:24 - 3:26
    query language of the database system.
  • 3:26 - 3:28
    That's usually used sort
  • 3:28 - 3:29
    of synonymously with the DML
  • 3:29 - 3:32
    or Data Manipulation Language which
  • 3:32 - 3:36
    usually includes not only querying but also data modifications.
  • 3:36 - 3:38
    In all relational query languages, when
  • 3:38 - 3:39
    you ask a query over a
  • 3:39 - 3:42
    set of relations, you get a relation as a result.
  • 3:42 - 3:43
    So let's run a query
  • 3:43 - 3:44
    cue say over these three
  • 3:44 - 3:46
    relations shown here and what
  • 3:46 - 3:50
    we'll get back is another relation.
  • 3:50 - 3:51
    When you get back the
  • 3:51 - 3:53
    same type of object that
  • 3:53 - 3:55
    you query, that's known as closure of the language.
  • 3:55 - 3:57
    And it really is a nice feature.
  • 3:57 - 3:59
    For example, when I want
  • 3:59 - 4:01
    to run another query, say Q2,
  • 4:01 - 4:02
    that query could be posed over
  • 4:02 - 4:04
    the answer of my first query
  • 4:04 - 4:05
    and could even combine that answer
  • 4:05 - 4:08
    with some of the existing relations in the database.
  • 4:08 - 4:10
    That's known as compositionality, the
  • 4:10 - 4:12
    ability to run a query
  • 4:12 - 4:15
    over the result of our previous query.
  • 4:15 - 4:18
    Now, let me talk briefly about two query languages.
  • 4:18 - 4:19
    We'll be learning these languages in
  • 4:19 - 4:20
    detail later, but I'm just
  • 4:20 - 4:23
    going to give the basic flavor of the languages here.
  • 4:23 - 4:25
    Relational algebra is a formal language.
  • 4:25 - 4:28
    Well, it's an algebra as you can tell by its name.
  • 4:28 - 4:31
    So it's very theoretically well-grounded.
  • 4:31 - 4:34
    SQL by contrast is
  • 4:34 - 4:37
    what I'll call an actual language or an implemented language.
  • 4:37 - 4:41
    That 's the one you're going to run on an actual deployed database application.
  • 4:41 - 4:44
    But the SQL language does have
  • 4:44 - 4:46
    as its foundation relational algebra.
  • 4:46 - 4:50
    That's how the semantics of the SQL language are defined.
  • 4:50 - 4:51
    Now let me just give you
  • 4:51 - 4:53
    a flavor of these two languages and
  • 4:53 - 4:57
    I'm going to write one query in each of the two languages.
  • 4:57 - 5:00
    So, let me get rid of this little line here.
  • 5:00 - 5:02
    Let's start in relational algebra.
  • 5:02 - 5:03
    So we're looking for the
  • 5:03 - 5:05
    ID's of students whose GPA
  • 5:05 - 5:09
    is greater than 3.7 and they've applied to Stanford.
  • 5:09 - 5:10
    In relational algebra, the basic
  • 5:10 - 5:12
    operators language are Greek symbols.
  • 5:12 - 5:13
    Again, we'll learn the details later,
  • 5:13 - 5:15
    but this particular expression will
  • 5:15 - 5:19
    be written by a Phi followed by a Sigma.
  • 5:19 - 5:20
    The Phi says we're going to
  • 5:20 - 5:21
    get the ID, the Sigma
  • 5:21 - 5:22
    says we want students whose
  • 5:22 - 5:25
    GPA is greater than 3.7
  • 5:25 - 5:27
    and the college that the
  • 5:27 - 5:31
    students have applied to is Stanford.
  • 5:31 - 5:33
    And then that will operate
  • 5:33 - 5:35
    on what's called the
  • 5:35 - 5:37
    natural join of the
  • 5:37 - 5:43
    student relation with the apply relation.
  • 5:43 - 5:46
    Again, we'll learn the details of that in a later video.
  • 5:46 - 5:49
    Now, here's the same query in SQL.
  • 5:49 - 5:50
    And this is something that you would
  • 5:50 - 5:51
    actually run on a deployed
  • 5:51 - 5:53
    database system, and the SQL
  • 5:53 - 5:55
    query is, in fact, directly
  • 5:55 - 5:58
    equivalent to the relational algebra query.
  • 5:58 - 5:59
    Now, pedagogically, I would
  • 5:59 - 6:01
    highly recommend that you learn
  • 6:01 - 6:02
    the relational algebra by watching
  • 6:02 - 6:04
    the relational algebra videos before
  • 6:04 - 6:06
    you move on to the SQL videos,
  • 6:06 - 6:08
    but I'm not going to absolutely require
  • 6:08 - 6:10
    that. So, if you're in a big hurry
  • 6:10 - 6:11
    to learn SQL right away
  • 6:11 - 6:14
    you may move ahead to the SQL videos.
  • 6:14 - 6:15
    If you're interested in the formal
  • 6:15 - 6:17
    foundations and a deeper understanding,
  • 6:17 -
    I recommend moving next to the relational algebra video.
Title:
02-02-querying-relational-databases.mp4
Video Language:
English
Duration:
06:21
Amara Bot added a translation

English subtitles

Revisions