text
stringlengths
1
81
start
float64
0
10.1k
duration
float64
0
24.9
So I could say something like select * from flights where id= "3."
997.31
4.31
Again, this reads as very English like.
1,001.62
2.29
I'm saying select all of the columns from the flights table,
1,003.91
3.78
but not all of the rows.
1,007.69
1.32
I only want to select the rows where--
1,009.01
2.76
this is a SQL keyword--
1,011.77
1.38
where id= "3."
1,013.15
1.8
ID is the name of a column, 3 is the value,
1,014.95
2.67
and it's going to look through this table, only find me the flights
1,017.62
2.85
where the id is equal to 3, and I'll just get back that one row as a result.
1,020.47
4.77
It finds the row with an ID of 3, and it gives that back to me.
1,025.24
5.129
If, instead, I said select * from flights where origin= "New York,"
1,030.369
3.901
I can see that I don't just need to filter on the primary key--
1,034.27
2.79
the id-- I can also filter on any other column.
1,037.06
2.61
I can say, give me all the flights where New York is the origin.
1,039.67
3.25
So all the flights leaving from New York.
1,042.92
2.054
You might imagine that in New York's airport,
1,044.974
1.875
for example they likely want some sort of query that's
1,046.849
2.721
going to find all the flights leaving from New York such
1,049.57
2.43
that they can display on a screen of some sort all of the departures
1,052
3.48
from that particular airport.
1,055.48
1.29
So you could run a query like this and get back
1,056.77
2.28
only the rows in this database, inside of this table, that happened to have
1,059.05
4.08
an origin of New York, for example.
1,063.13
3.19
So let's go ahead and take a look at how we might actually
1,066.32
2.78
run some of these queries inside of a SQL database.
1,069.1
4.21
So in order to create a SQLite database, which is really just a file,
1,073.31
3.75
I can start just by creating the file.
1,077.06
2.18
It turns out that in the terminal, there is a command called Touch that
1,079.24
3.18
will create a brand new file for me.
1,082.42
2.19
So I'm going to create a file called flights.sql.
1,084.61
4.04
It's going to be a SQLite database file that by default has nothing in it.
1,088.65
4.03
But then, assuming you have SQLite installed,
1,092.68
2.46
I can run SQLite 3 followed by flights.sql,
1,095.14
5.01
and now I'm inside of the SQLite prompt.
1,100.15
2.55
I can now begin to write commands that will be executed on this SQL database.
1,102.7
5.53
So one thing I might do is create a table called "flights."
1,108.23
4.14
In this table, I want to have an id column, which is an integer.
1,112.37
4.29
It should be the primary key, the main way via which I identify a flight.
1,116.66
3.59
I would like to autoincrement it.
1,120.25
2.643
I would also like for the flights table to have an origin, which
1,122.893
2.667
will be a text field that is not null.
1,125.56
2.63
I'll add a destination field, also a text field that is not null.
1,128.19
4.3
And then finally I'll add a duration field,
1,132.49
2.16
which is an integer that also shouldn't be null.
1,134.65
2.76
I'll end with an end parentheses and a semicolon, press Return,
1,137.41
3.42
and that executes that command on my database.
1,140.83
2.61
I have now created a table called "flights."
1,143.44
2.82
To verify this in SQLite, in the prompt I can type .tables,
1,146.26
3.89
and that will show me all of the tables that currently exist in my database.
1,150.15
4.06
It just so happens that I have a table called "flights."
1,154.21
3.125
Of course, there's nothing inside this table, which
1,157.335
2.125
I can verify by running SELECT * from flights,
1,159.46
3.02
meaning get all the data from the flights table.
1,162.48
2.65
If I press Return nothing happens.
1,165.13
2.01
I don't see anything.
1,167.14
1.05
There is no data that happens to be here.
1,168.19
2.55
But now I could say something like INSERT into Flights.
1,170.74
3.613
Then I'll provide in parentheses the names of the columns.
1,174.353
2.417
And on the slide a moment ago I showed this on multiple lines.
1,176.77
2.618
That was just for visuals sake.
1,179.388
1.292
These commands can be entirely on one line if we would like it to.
1,180.68
2.98
So I can say origin, destination, and duration and then provide values.
1,183.66
4.87
I can say let's add New York to London, and I'd
1,188.53
4.01
like for this flight to be duration of 415 minutes.
1,192.54
4.03
So I type in the command, press Return.
1,196.57
2.03
That adds a new row to my flights table.
1,198.6
3.03
I can verify this by running SELECT * from flights.
1,201.63
4.62
And I see that now I have this table whose id is one--
1,206.25
3.09
that's the id column from New York to London--
1,209.34
3.51
with a duration of 415 minutes.
1,212.85
2.46
I could do this again and again.
1,215.31
1.46
I, in advance, prepared a couple of INSERT
1,216.77
1.75
queries just to give us a little bit more data.
1,218.52
2.34
Each of this is just a different INSERT query
1,220.86
2.74
that's going to add one new flight into our database.
1,223.6
2.94
So I'll go ahead and copy those and then into the database
1,226.54
2.72
I'll go ahead and paste in all these INSERT commands
1,229.26
2.31
to insert a whole bunch of flights into this database.
1,231.57
3.36
Now if I run SELECT * from flights what I get is all of the flights.
1,234.93
5.49
This is not formatted particularly nicely.
1,240.42
1.94
SQLite has some additional tricks if you'd like to format it a little nicer.
1,242.36
3.23
So I can say, go ahead and put this into columns mode,
1,245.59
3.32
and it gives me some headers.
1,248.91
2.28
So .headers yes.
1,251.19
3.03
And now if I SELECT * from flights, the data
1,254.22
2.74
is organized a little more nicely where things are really into columns
1,256.96
2.93
and there are headers for each of the rows.
1,259.89
1.792
Where I can see that I kind have a text-based representation of what
1,261.682
3.748
I was displaying graphically a moment ago.
1,265.43
1.75
That inside of my flights table I have four columns-- an id column, origin,
1,267.18
4.24
destination, and duration.
1,271.42
1.67
Each flight is just one of the rows that's inside of this table.
1,273.09
5.22
Now I can begin to run additional SELECT queries.
1,278.31
2.62
If I want to do something like SELECT * from flights where origin= "New York,"
1,280.93
7.19
well then instead of getting back all of the flights,
1,288.12
2.61
I'm only going to get back the flights where the origin is New York.
1,290.73
2.97
So I can very quickly filter down on this data,
1,293.7
2.64
only getting the information that I actually care about taking a look at.
1,296.34
3.45
There are other ways of interacting with SQLite as well,
1,299.79
2.43