text stringlengths 1 81 | start float64 0 10.1k | duration float64 0 24.9 |
|---|---|---|
as sold all automatically. | 5,164.37 | 3.88 |
Let's think to maybe the museum
acquires some piece of artwork. | 5,168.25 | 4.07 |
Maybe they actually add something
new to their collection. | 5,172.32 | 3.12 |
We want that to show up in our
transactions as well with the action of | 5,175.44 | 4.44 |
bought. | 5,179.88 | 0.69 |
Well, let's try it. | 5,180.57 | 0.87 |
I could probably add a new row here. | 5,181.44 | 2.05 |
I could say let's add "Peonies and
Butterfly" to my collection here. | 5,183.49 | 4.79 |
Now, if I have the
appropriate trigger, I | 5,188.28 | 3.42 |
could run an insert into on
transactions automatically | 5,191.7 | 4.62 |
that would insert this same
title with the action of bot, | 5,196.32 | 3.87 |
thereby keeping a log of all of our
sold pieces of artwork and all of our | 5,200.19 | 4.92 |
bought pieces of artwork. | 5,205.11 | 2.25 |
Now to create what we're
calling a trigger here, | 5,207.36 | 2.64 |
we can use the following syntax. | 5,210 | 2.43 |
I could say first create
trigger, and then some name. | 5,212.43 | 5.19 |
So I tend to give triggers
names to identify them | 5,217.62 | 2.64 |
among all of my database [INAUDIBLE],,
like my tables and so on. | 5,220.26 | 4.41 |
Here, after I say create
trigger, I have to specify. | 5,224.67 | 4.17 |
Should this trigger, this statement,
run after or before some other SQL | 5,228.84 | 6.24 |
statement? | 5,235.08 | 1.11 |
Let's keep going with before here. | 5,236.19 | 2.16 |
Maybe it runs before an
insert on some table. | 5,238.35 | 4.5 |
That's fine. | 5,242.85 | 1.11 |
It could also run before an
update of a column on some table. | 5,243.96 | 5.23 |
It could even run before a delete
on some table, and keep in mind, | 5,249.19 | 5 |
this could be before or after any
of these kinds of SQL statements. | 5,254.19 | 4.26 |
In this case, we have
insert, update, and delete. | 5,258.45 | 4.32 |
Now after we say this, what we
should say, too, is for each row. | 5,262.77 | 5.28 |
For each row means that if I were
to maybe delete multiple rows, | 5,268.05 | 5.16 |
I should run my SQL statement
for each row that I delete. | 5,273.21 | 4.2 |
If I delete two rows, I should run
this statement coming soon two times. | 5,277.41 | 6.57 |
OK, so now we've set up our trigger,
at least kind of the data, the setup | 5,283.98 | 5.49 |
for it here. | 5,289.47 | 1.08 |
What we haven't done yet is specify
what statement should run whenever | 5,290.55 | 5.07 |
we see a delete on particular table. | 5,295.62 | 2.4 |
Well, let's try this. | 5,298.02 | 1.62 |
Here I could say begin. | 5,299.64 | 1.95 |
Begin means here comes my
statement I want to run | 5,301.59 | 3.27 |
whenever I hear a delete on this table. | 5,304.86 | 3.51 |
Now, all right. | 5,308.37 | 1.47 |
That query down below. | 5,309.84 | 1.59 |
That statement inside my
begin, and I'll finish it off | 5,311.43 | 3.27 |
with an end to say this is
the end of my statement, | 5,314.7 | 3.21 |
and my entire trigger looks like
all of this lines of code here | 5,317.91 | 3.69 |
to say listen for a delete and run
this statement in here for each row | 5,321.6 | 5.61 |
that I delete, or update, or insert. | 5,327.21 | 3.32 |
So let's try actually
implementing in this case | 5,330.53 | 3.37 |
the museum's collections where
they're trying to sell or buy | 5,333.9 | 3.56 |
pieces of artwork, and
automatically add them | 5,337.46 | 2.34 |
to their transaction table whenever
they create or delete some piece of data | 5,339.8 | 3.96 |
from their collections. | 5,343.76 | 1.95 |
Go back to my terminal here,
and let me pull up a database. | 5,345.71 | 4.59 |
I'll do SQLite 3 mfa.db,
and you'll notice | 5,350.3 | 3.96 |
that this is the same schema
that we've had before. | 5,354.26 | 3.12 |
We have collections, artists,
and the created table. | 5,357.38 | 4.11 |
What's missing here, though,
is our transactions table. | 5,361.49 | 3.13 |
So let me make that for us here. | 5,364.62 | 1.91 |
I'll say let me go create
table, transactions, | 5,366.53 | 4.05 |
and now let me give it an
ID column of type integer. | 5,370.58 | 5.42 |
Now, I'll also give it a title column
as we saw before called "Text." | 5,376 | 5.03 |
It's in this column
[INAUDIBLE],, the title | 5,381.03 | 2.28 |
of the piece we either bought or sold. | 5,383.31 | 2.79 |
Now, let me also include the
action column, also of type text | 5,386.1 | 4.86 |
to say whether we bought
or sold this piece. | 5,390.96 | 4.25 |
OK, finally, I'll make the
ID column my primary key. | 5,395.21 | 4.84 |
And if I say semicolon here and hit
Enter, I now have created this table. | 5,400.05 | 6.03 |
So if I say dot schema, notice
how I can now see my transactions | 5,406.08 | 4.13 |
table down below, and
I'm safe to run insert | 5,410.21 | 3.78 |
on this table to add some data to it. | 5,413.99 | 4.11 |
Well, let's try now creating
our very first trigger. | 5,418.1 | 3.39 |
We're going to try to create a trigger
that whenever an item is deleted | 5,421.49 | 3.81 |
from our collections table, we actually
add it to our transactions table | 5,425.3 | 3.69 |
with the action of sold, meaning
we sold this particular item. | 5,428.99 | 4.21 |
So let me say create
trigger, as we saw before, | 5,433.2 | 3.35 |
and give it some name to identify it. | 5,436.55 | 2.28 |
I'll call this one sell. | 5,438.83 | 2.2 |
Now, I'll say I want to run this trigger
before I delete on the collections | 5,441.03 | 6.26 |
table. | 5,447.29 | 0.88 |
So before I run the delete
query on my collections table, | 5,448.17 | 4.7 |
I want to run this statement instead. | 5,452.87 | 4.11 |
I'll say for each row, begin. | 5,456.98 | 3.85 |
I want to now give you some
statement to run for each row | 5,460.83 | 3.35 |
that I delete from collections. | 5,464.18 | 2.55 |
Let's make it this query here. | 5,466.73 | 1.62 |
On 1, 2, 3, 4 spaces
to indent and make sure | 5,468.35 | 3.81 |
this is clearly the query I want to run
after I run the delete on collections. | 5,472.16 | 6.69 |
I'll say I want to
insert into transactions. | 5,478.85 | 4.17 |
I want to insert into the
title and action columns. | 5,483.02 | 4.71 |
What value should I insert? | 5,487.73 | 1.68 |
Well, I should insert the old
title, and actually, in triggers, | 5,489.41 | 4.8 |
you get access to this
keyword called old. | 5,494.21 | 3.12 |
This is the title that we've
just deleted from our collection. | 5,497.33 | 4.27 |
So old.title gives me access to
the old row, the row we deleted, | 5,501.6 | 4.94 |
its title column in particular. | 5,506.54 | 2.62 |
Now, let me add in the action, which
is just plain and simple "sold." | 5,509.16 | 5.05 |
So this is the query to run
before I delete on collections. | 5,514.21 | 4.61 |
I will take the title of the row
I'm about to delete, and insert it | 5,518.82 | 4.53 |
into transactions along
with the action sold. | 5,523.35 | 3.6 |
Let me type a semicolon here to say this
is my entire query, hit Enter again, | 5,526.95 | 4.12 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.