-: Hi, guys, welcome back.
In this video we're going to show you
how the INSERT INTO statement is constructed.
So here's an example, INSERT INTO users,
and then the First Name and Surname columns,
VALUES, Rolf, and Smith.
The first part, INSERT INTO users tells the database
that we want to add data,
that's INSERT INTO, the Users table.
Then we've got the columns that we want to insert into.
This is optional, and if we don't provide the columns
that we want to insert data into,
we're going to be inserting into all columns,
and then we will have to provide data for all columns.
So at the end, we tell the database what values we want
to insert in those columns, and remember,
if we skipped the columns earlier on,
then we need to provide values
for all the columns in the table.
So this is a quick deconstruction
of the INSERT INTO command.
There's more information about this command in the e-book,
which is linked in the Resources section of this lecture.
Now, let's get coding and writing
some INSERT INTO commands over in the database viewer.
All right, now let's go into the db browser
and look at how to insert data.
There's a couple of ways.
We've got our Users table already created.
We're gonna go to Browse Data in here,
and here you can see that we've got nothing at the moment.
You can click this icon at any time
to refresh the data just in case
that you've got a Python application changing it,
this will let you see the new data as it comes in.
But, what we're gonna do just now is to make sure
to select the correct table and then click on New Record.
Notice that we get Null and Null as the two values
in these fields, but you can go in them and change them,
so you can add John Smith, for example.
Now, that's in here,
but the changes haven't actually been saved
so we have to go ahead and write the changes
to the database file.
So all of this, at the moment,
is just stored in memory and it will disappear
as soon as you close the programme.
If we write the changes then they will be permanently saved.
Similarly, if you create a new record
and you type something like another name,
now this is still in memory and if you made a mistake,
you can revert the changes by just clicking that,
and now this is going to undo all the changes since it
was last saved, you can press Yes,
and you back to the last time you saved the database.
Now, this is all well and good,
but we're going to do the same with SQL,
so I'm gonna go and select this row here and delete it,
and now we're gonna go over to Execute SQL
and we're going to type our commands in here.
As you can remember, INSERT INTO tells the database
that we're wanting to insert data.
The table that we wanna insert into,
and then the values that we want to insert.
So here we're gonna type,
inside single quotation marks, Rolf Smith.
It's red, but that's just the db browser, the colour,
you don't have to worry about it being red,
it doesn't mean there's an error.
So now we can press Play and the value works,
the query executed successfully.
The data is inserted, we can go over to Browse Data,
Rolf Smith is there.
If you use double quotation marks,
in some databases it won't work.
For example, in PostgreSQL, single quotation marks
and double quotation marks have different meanings,
so I'd recommend to stick to single quotation marks
for text that you want to insert into a table.
Remember that if you want to insert data only into
some columns, like for example, Surname,
then you can do that by putting the column names,
in brackets, after the table name.
Here, we're gonna put only data into Surname,
so we will only provide one value to be inserted.
So if we run this, you'll see
that when we go on Browser Data,
we have Null in the first column
and Smith in the second column.
Depending on how your table is defined,
as we will learn later on,
this can sometimes give you an error.
For example, we can tell the table
that it should not accept empty values, and if we do that,
then this would raise an error at this point
because the First Name column doesn't have a value.
As I said, we're going to learn more about that later on.
Remember that, if you want to add data
into multiple columns, these are, again,
comma separated as before.
And, if you do that, you do have
to provide the values for each column as well.
All right, that's everything
about inserting data into a table.
Thanks for joining me in this video.
And, I'll see you in the next one.