Locked lesson.
About this lesson
We will insert our own records into a database using INSERT INTO
Exercise files
Download this lesson’s related exercise files.
INSERT Statement.docx59 KB INSERT Statement - Solution.docx
59.5 KB
Quick reference
INSERT Statement
Insert allows us to put data INTO our table.
When to use
Use it any time you want to add data to your table with SQL.
Instructions
INSERT INTO [dbo].[Current_Customers] ([First Name], [Last Name], [Email Address], [Phone Number], [Age], [City], [Country])
VALUES ('Tracy', 'Smitherton', 'tracy@email.com', '222-667-8645', '42', 'Mt. Vernon', 'USA')
This will insert the information for a new customer into the current customer database.
Hints & tips
- Insert puts data into your table using SQL
- The columns in the first line need to mirror the data in the second line
- 00:05 Up until now, we've been pulling data out of our database, out of our table,
- 00:09 with SQL.
- 00:10 Now we're gonna learn how to insert data into the database using just SQL.
- 00:14 Now, in the past, in order to put stuff in,
- 00:16 we come over here to our table and right-clicked and typed in edit, and
- 00:20 then we just navigated down here and typed it in.
- 00:23 Well that's fine if you're designing the thing, or
- 00:25 if you're just entering some stuff by hand, and you wanna do it like this.
- 00:29 A lot of times, you're gonna need to do this programmatically.
- 00:31 You've got a website somebody fills out a form.
- 00:33 You want that information automatically added to the database.
- 00:36 You don't wanna do it by hand you're gonna use your SQL to do that.
- 00:40 So, in order to do that, we use the insert into statement and
- 00:44 it's pretty simple, it's just INSERT INTO and
- 00:48 then you type in the name of the table, which is dbo Current_Customers.
- 00:54 And then there's two ways to do this.
- 00:56 You can, in here, you type in all of your columns, right.
- 01:01 So in our case, it's first name, last name, email address, etc.
- 01:06 And that's important and I'll tell you why in just a second.
- 01:09 So that's the first line.
- 01:10 And in the second line, we type in values.
- 01:13 And then, we just enter in the values that you wanna add.
- 01:16 So, we've got our first name.
- 01:18 Let's say, each value is surrounded by quotation marks and separated by a comma.
- 01:23 So, let's say we wanna add Tracy, last name Smitherton,
- 01:31 and her email address is tracy@email.com.
- 01:36 Wrap that in single quotation marks,
- 01:39 comma say phone number 444-555-666.
- 01:45 And what's next?
- 01:46 Age, let's make her 42.
- 01:49 And city, Mount Vernon.
- 01:54 Country, USA.
- 01:57 So the reason why we put these columns on this first line is that they need to match
- 02:01 up completely to the second line.
- 02:03 So if we're only gonna add one thing to the first name,
- 02:07 we would just put first name up here.
- 02:09 And we'd leave the rest of these off.
- 02:11 So we're putting everything in, so
- 02:12 we have to name everything more on that in just a second.
- 02:15 We don't necessarily need to do that, but let's run this first and see so
- 02:18 execute this thing.
- 02:19 And I'm gonna go ahead and copy this and delete it and run it again so
- 02:24 we have our select statement.
- 02:26 And boom, there we go.
- 02:27 Tracy Smitherton has been added.
- 02:29 So like I said earlier, we put these columns and they have to match up with
- 02:34 the thing below, except in this case we're adding everything.
- 02:38 Every single column that's in our Table we're adding a value for, so
- 02:41 in that case we don't have to list out everything on top on this first line.
- 02:45 Normally, you may not be adding everything,
- 02:48 every column worth of information, in your statement.
- 02:51 If that's the case, like I said earlier, if we're just gonna do just first name and
- 02:54 last name we'd write out FirstName and
- 02:57 LastName up on this first line and that's that.
- 02:59 So, that's the insert into statement, very easy.
- 03:02 You can see it doesn't go with a select statement.
- 03:05 Up until now we've been doing everything inside of these select statements.
- 03:08 I just put the select statement up here so
- 03:10 that we could get the output down here at the bottom so we could look at it but for
- 03:13 inserting data you don't use a select statement.
- 03:16 You just use this INSERT INTO, second line values, pop in what you want.
- 03:19 Remember, separate every bit of data by your single quotation marks and
- 03:24 commas, and you're good to go, so that's INSERT INTO.
- 03:28 In the next video, we'll look at the update statement.
Lesson notes are only available for subscribers.