
How to use the SQL INSERT Statement
How to use the SQL INSERT Statement
Learn how to use the SQL INSERT Statement. There are 2 syntaxes for the INSERT statement depending on whether you want to insert one record or multiple records into a table. We will cover both of these syntaxes in this tutorial with easy to follow examples.
SQL INSERT Statement (and sample data):
https://www.techonthenet.com/sql/inse…
SQL Tutorials:
https://www.techonthenet.com/sql/inde…
Don’t forget to subscribe to our channel for more great SQL tutorials!
Visit us at: https://www.techonthenet.com
or follow us on:
Facebook: https://www.facebook.com/techonthenetcom
Twitter: https://twitter.com/tech_on_the_net
Pinterest: https://www.pinterest.com/techonthene…
Content
0.02 -> The INSERT statement
(or commonly referred to as the "INSERT query")
4.48 -> is the SQL statement that is used
to add records to your database
8.48 -> There are two syntaxes for the INSERT
statement depending on whether
12.34 -> you want to insert one record
or multiple records into a table
16.76 -> We will cover both of these
syntaxes in this tutorial
20.46 -> The best way to understand the INSERT statement
is to look at an example
24.74 -> So let's get started
26.5 -> The first syntax for the INSERT statement allows us
to insert one record at a time into the database
33.08 -> Here we have a table called categories
containing the following data
37.4 -> Let's write an INSERT statement that inserts
a new record into the categories table
42.26 -> We'll start by entering
INSERT INTO categories
46.42 -> This lets the database know that we want to write an
INSERT query and add records to the categories table
52.64 -> Next, we need to list our column names
from the categories table
56.84 -> These are the columns that we
will insert values into
60.06 -> In this example, we will enter the category_id
and category_name as our columns
65.94 -> These column names will be comma
separated and enclosed in parentheses
70.74 -> Now we will enter
the VALUES keyword
74.14 -> We will enter 150 and 'Miscellaneous'
as our values
79.34 -> 150 is the value that will be inserted
into the category_id column
84.72 -> and 'Miscellaneous' is the value that will be inserted
into the category_name column in the categories table
91.14 -> Because 'Miscellaneous' is a string value,
it must be enclosed inquotes
96.54 -> Now let's run our INSERT statement
and see what happens
100.22 -> The database will return a message to let us know
that one record has been inserted
105.9 -> If we look at the categories table, we will see
that the new record has been added
111.2 -> Now let's try out our second syntax
which is a bit more complicated
115.54 -> but allows us to perform
multiple inserts quickly
119.18 -> Here we have a table called employees
with the following data
126.34 -> We also have a second table
called customers
130.81 -> Let's insert some of the employee information
into the customers table
138.89 -> We will start by entering
INSERT INTO customers
142.6 -> which lets the database know that we wish to
add new records to the customers table
147.2 -> We will enter customer_id, last_name and first_name
as the columns from the customers table
155.28 -> that we will be inserting
values into
157.98 -> Now instead of typing the VALUES keyword
like we did in the previous example
162.94 -> we will enter a SELECT statement that will be used
to populate the records for the INSERT query
168.42 -> We will enter employee _number, last_name
and first_name as the columns
173.82 -> Note that we have aliased
the employee_number as customer_id
177.64 -> but this alias is not required
179.74 -> When writing this type of INSERT statement, there must
be the same number of columns in the SELECT
185.48 -> as there is in the INSERT statement
187.58 -> and the corresponding data types
must also match
191.64 -> Next, we will enter employees
as the table
194.98 -> and we will include a WHERE clause so that only
the records from the employee's table
199.56 -> where the employee_number is less than 1003
are inserted into the customers table
205.56 -> Now let's run our
INSERT statement
208.74 -> In this example, the database will return a message
to let us know that two records have been inserted
214.96 -> and if we look at the customers table, we should see
our two new records added
221.98 -> This covers examples of how to use the SQL INSERT
statement to insert a single record or multiple records
229.4 -> If you would like to
see more INSERT examples
232.54 -> or would like to download the sample data
we used for this tutorial
235.7 -> please visit our website at
TechOnTheNet.com
238.989 -> You can can also try the examples
in our SQL editor on our website
243.12 -> Look for the "Try It" button
next to each example
247.24 -> If you found her tutorial helpful,
please leave a like on this video
251.38 -> and don't forget to subscribe
to our YouTube channel for more great SQL tutorials
Source: https://www.youtube.com/watch?v=UrbItNGZU48