text
stringlengths
1
81
start
float64
0
10.1k
duration
float64
0
24.9
What table would I like to update?
1,593.153
1.417
I'd like to update the flights table.
1,594.57
2.34
What would I like to do?
1,596.91
1.47
Well I'd like to set the duration equal to 430.
1,598.38
4.29
So whatever the value of duration happens to be right now,
1,602.67
2.7
change it to 430.
1,605.37
2.04
But of course I don't want to change it to 430 for every single flight.
1,607.41
3.75
Just as in a SELECT clause, I could say WHERE something = something or WHERE
1,611.16
3.73
and then some other clause to specify where
1,614.89
2.57
I want the rows to be selected from.
1,617.46
2.14
Likewise, to an Update I can say set duration equal to 430
1,619.6
4.22
WHERE a particular condition is true.
1,623.82
2.29
So here I'm going to look through the flights table,
1,626.11
2.45
find myself all of the flights where the origin is New York
1,628.56
3.6
and the destination is London.
1,632.16
2.34
For those rows I'm going to update the value of the duration column
1,634.5
3.51
setting the duration column equal to 430.
1,638.01
2.91
So using that syntax I can take data and update it,
1,640.92
2.61
changing one value to another value by pinpointing which row or rows I
1,643.53
4.32
would like to change.
1,647.85
0.99
If I only want to change one row, I might do Update flights SET duration=
1,648.84
4.77
something WHERE id= a particular id.
1,653.61
3.33
To be able to pinpoint one id and then take that row
1,656.94
3.36
and make some modification to it.
1,660.3
2.46
In addition to inserting data, selecting data, and updating data,
1,662.76
3.31
the last main command we'll concern ourselves with
1,666.07
2.24
is the ability to delete data.
1,668.31
1.538
The ability to take a row and say, I'd like to get rid of it.
1,669.848
2.542
Or take multiple rows and get rid of them.
1,672.39
2.31
And so a command like delete from flights, where destination= "Tokyo"
1,674.7
4.41
as you might imagine deletes from the flights table all of the rows that
1,679.11
3.54
satisfy this condition where the destination is equal to Tokyo.
1,682.65
4.482
So a number of different operations now that we have the ability to do.
1,687.132
2.958
The ability to delete from a particular table where a condition is true,
1,690.09
3.6
the ability to update a table based on particular conditions, the ability
1,693.69
3.45
to select data from a table, and insert data into a table as well.
1,697.14
4.62
There are a couple other clauses that can be used to add SQL queries as well.
1,701.76
3.81
Just to add additional functionality.
1,705.57
2.28
If I don't want all of the rows to come back from a particular SQL query,
1,707.85
3.6
I can limit the results that come back.
1,711.45
2.04
So normally SELECT * from flights would get me
1,713.49
2.67
all of the flights inside of the table.
1,716.16
2.16
But I could say SELECT * from flights, LIMIT 5 to just say,
1,718.32
3.9
I only want five results to come back from this table.
1,722.22
3.9
ORDER BY allows me to decide how the results are ordered inside the results
1,726.12
4.02
to come back.
1,730.14
0.84
So I can say SELECT * from flights, ORDER BY destination, or ORDER BY
1,730.98
3.99
duration to get all of the flights in order by how long they are.
1,734.97
3.9
GROUP BY allows me to group a whole bunch of rows together.
1,738.87
3.97
So if I wanted to group all of the flights by their origin
1,742.84
2.913
so I can get all of the flights leaving New York and all the flights leaving
1,745.753
3.167
London and so forth, I could do something like SELECT *
1,748.92
2.73
from flights GROUP BY origin to group flights by their origin as well.
1,751.65
4.83
HAVING is a constraint I can place on GROUP BY
1,756.48
2.73
to say that I would like to select all of the flights grouping them
1,759.21
4.62
by their origin, but they need to have a count of at least three.
1,763.83
3.27
Meaning there needs to be at least three flights that
1,767.1
2.55
are leaving from that particular city.
1,769.65
1.74
For all of these particular clauses, these
1,772.23
1.83
are helpful to know if you're going to be directly writing SQL.
1,774.06
2.64
We won't worry about them too much here, in particular
1,776.7
2.31
because fairly shortly we're not going to be writing SQL ourselves.
1,779.01
3.09
We're going to just be writing Python code
1,782.1
2.13
and Django is going to, under the hood, be manipulating the database
1,784.23
3.27
and creating the SQL commands that it is going
1,787.5
2.43
to run on the underlying database.
1,789.93
2.09
So we will see how we don't actually need to worry
1,792.02
2.29
about writing the specific syntax.
1,794.31
1.8
But Django is going to handle much of that for us.
1,796.11
3.97
So here now we have a flights table, a table
1,800.08
2.42
that keeps track of all of the flights that we have,
1,802.5
2.46
organizing them by their id and their origin and their destination
1,804.96
3.54
and their duration.
1,808.5
1.2
But oftentimes when we're dealing with data, especially in a larger database,
1,809.7
3.61
we don't just have one table of data.
1,813.31
2.15
We have multiple tables of data.
1,815.46
1.87
And those multiple tables might relate to each other in some way.
1,817.33
4.198
Let's take a look at an example of how that might come about.
1,821.528
2.542
We're going to introduce a concept that will call foreign keys,
1,824.07
2.7
and we'll see what that means in just a moment.
1,826.77
2.77
So here again is our flights table.
1,829.54
2.15
The flights table has four columns-- an id, origin, destination, and duration.
1,831.69
5.58
But of course in New York, there are multiple airports.
1,837.27
3.945
And so it might not make sense for me to just
1,841.215
1.875
label each origin or each destination just by the name of the city.
1,843.09
3.75
Maybe I also want to give the three letter airport
1,846.84
2.7
code that corresponds to the airport to which I'm referring in this case.
1,849.54
4.53
So how would I encode into this table, not only the origin, but also
1,854.07
3.66
that city's airport code?
1,857.73
1.44
And not only for the destination the name of the city,
1,859.17
2.26
but also the airport code for that airport as well?
1,861.43
3.02
Well I could just add more columns.
1,864.45
2
I could say something like, all right, now
1,866.45
1.75
we have this table that has an id, an origin, an origin code, a destination,
1,868.2
6.33
a destination code, and a duration.
1,874.53
2.88
But here now, the table is starting to get fairly wide.
1,877.41
3.64
There are a lot of columns here and in particular, there
1,881.05
2.68
is some duplicate data.
1,883.73
3.37
Paris is associated with this particular three letter
1,887.1
2.91
code, and the same thing for New York and other airports as well.
1,890.01
3.67