Insert Data into the Table with SQL Script

MySQL Database Development Mastery Manipulating Tables and Data - CRUD Operations
8 minutes
Share the link to this page
Copied
  Completed
You need to have access to the item to view this lesson.
One-time Fee
$69.99
List Price:  $99.99
You save:  $30
€64.96
List Price:  €92.81
You save:  €27.84
£55.77
List Price:  £79.68
You save:  £23.90
CA$95.68
List Price:  CA$136.70
You save:  CA$41.01
A$106.02
List Price:  A$151.47
You save:  A$45.44
S$94.41
List Price:  S$134.88
You save:  S$40.47
HK$546.80
List Price:  HK$781.18
You save:  HK$234.37
CHF 63.34
List Price:  CHF 90.49
You save:  CHF 27.15
NOK kr761.11
List Price:  NOK kr1,087.35
You save:  NOK kr326.23
DKK kr485.02
List Price:  DKK kr692.92
You save:  DKK kr207.89
NZ$116.42
List Price:  NZ$166.33
You save:  NZ$49.90
د.إ257.06
List Price:  د.إ367.25
You save:  د.إ110.18
৳7,660.01
List Price:  ৳10,943.35
You save:  ৳3,283.33
₹5,835.78
List Price:  ₹8,337.18
You save:  ₹2,501.40
RM331.75
List Price:  RM473.95
You save:  RM142.20
₦86,437.65
List Price:  ₦123,487.65
You save:  ₦37,050
₨19,416.31
List Price:  ₨27,738.77
You save:  ₨8,322.46
฿2,572.74
List Price:  ฿3,675.50
You save:  ฿1,102.76
₺2,264.43
List Price:  ₺3,235.04
You save:  ₺970.61
B$356.70
List Price:  B$509.60
You save:  B$152.89
R1,295.44
List Price:  R1,850.72
You save:  R555.27
Лв127.05
List Price:  Лв181.51
You save:  Лв54.46
₩94,909.58
List Price:  ₩135,590.93
You save:  ₩40,681.35
₪259.50
List Price:  ₪370.74
You save:  ₪111.23
₱3,993.87
List Price:  ₱5,705.78
You save:  ₱1,711.90
¥10,712.31
List Price:  ¥15,303.96
You save:  ¥4,591.65
MX$1,187.89
List Price:  MX$1,697.07
You save:  MX$509.17
QR254.57
List Price:  QR363.69
You save:  QR109.12
P950.82
List Price:  P1,358.38
You save:  P407.55
KSh9,247.76
List Price:  KSh13,211.65
You save:  KSh3,963.89
E£3,352.12
List Price:  E£4,788.95
You save:  E£1,436.83
ብር4,006.43
List Price:  ብር5,723.72
You save:  ብር1,717.28
Kz58,511.64
List Price:  Kz83,591.64
You save:  Kz25,080
CLP$65,950.47
List Price:  CLP$94,219
You save:  CLP$28,268.52
CN¥506.53
List Price:  CN¥723.64
You save:  CN¥217.11
RD$4,055.76
List Price:  RD$5,794.19
You save:  RD$1,738.43
DA9,420.16
List Price:  DA13,457.95
You save:  DA4,037.79
FJ$157.70
List Price:  FJ$225.30
You save:  FJ$67.59
Q542.52
List Price:  Q775.06
You save:  Q232.54
GY$14,601.52
List Price:  GY$20,860.22
You save:  GY$6,258.69
ISK kr9,764.23
List Price:  ISK kr13,949.49
You save:  ISK kr4,185.26
DH703.98
List Price:  DH1,005.73
You save:  DH301.75
L1,236.34
List Price:  L1,766.28
You save:  L529.93
ден3,998.59
List Price:  ден5,712.52
You save:  ден1,713.92
MOP$561.77
List Price:  MOP$802.57
You save:  MOP$240.79
N$1,291.99
List Price:  N$1,845.78
You save:  N$553.78
C$2,569.36
List Price:  C$3,670.67
You save:  C$1,101.31
रु9,319.09
List Price:  रु13,313.56
You save:  रु3,994.46
S/260.54
List Price:  S/372.22
You save:  S/111.67
K269.79
List Price:  K385.44
You save:  K115.64
SAR262.50
List Price:  SAR375.02
You save:  SAR112.51
ZK1,882.68
List Price:  ZK2,689.66
You save:  ZK806.98
L323.40
List Price:  L462.03
You save:  L138.62
Kč1,628.77
List Price:  Kč2,326.92
You save:  Kč698.14
Ft25,305.79
List Price:  Ft36,152.68
You save:  Ft10,846.88
SEK kr755.02
List Price:  SEK kr1,078.64
You save:  SEK kr323.62
ARS$61,468.17
List Price:  ARS$87,815.44
You save:  ARS$26,347.26
Bs483.33
List Price:  Bs690.51
You save:  Bs207.17
COP$271,845.87
List Price:  COP$388,367.89
You save:  COP$116,522.02
₡35,672.25
List Price:  ₡50,962.55
You save:  ₡15,290.29
L1,724.16
List Price:  L2,463.20
You save:  L739.03
₲522,510.75
List Price:  ₲746,475.93
You save:  ₲223,965.17
$U2,674.97
List Price:  $U3,821.56
You save:  $U1,146.58
zł281.37
List Price:  zł401.98
You save:  zł120.60
Already have an account? Log In

Transcript

Hey guys, in this lesson we will be looking at inserting records into our tables. So to begin this exercise, we will use a script and we will look at the syntax to insert records into our student table. Now do recall that any table should be a collection of rows are records relating to the entity as described by the tables name. So the table students should only be storing data on students. So each record needs to be representative of students. In building our table, we would have stated or columns and the minimum data needed to build a report on a student and by the minimum data required.

I mean, what did we set to be nullable and not plausible. So we did specify that only the enrollment date was nullable healing, meaning we could always add a student's without specifying and enrolling date. So let's look at the syntax to insert a row of data. So the key word is literally insert into. All right. But then before we do all of this, we should always use the database.

So we use school underscore dB. And then we continue with our syntax. So we insert into the table, and in this case, or to his students. And then what we do is list the columns. And they are wrapped inside of parentheses. And then we specify the values.

And we also wrap those inside parentheses. All right, so I'm just writing it like this. So we see exactly what so this is the format for any insert, statement, insert into the table name, and then the columns listed and then the key word values and then the corresponding values with you. columns. So the columns as I have here would be last name. Comma, and each one is comma separated first name, comma, date of birth, and then comma, enrollment date.

Right, so these are the columns in which into which we want to insert some amount of data. So we can actually break the line so that we can see everything happening at once. So insert into the table, the columns values, and then we specify the values now the order that we specify the values must correspond with the columns. So in other words, whatever value I put here first, and remember that last name is a string value. So whenever we're going to be writing out string value or a word who wants to use quotation marks are single quotation marks, the double quotes can work also, but I prefer to use the single quotes first. Alright, and then we see, let's say for instance, I'm going to insert test as the last name, and the first name would be student.

And then the date of birth and date of birth or date, data type could also be in a string, and most commonly accepted formats would be here as in yy, yy dash models as an M M dash D, D, D. So it would be a four digit year, dash, a two digit month, two digit day, and that is almost acceptable in almost any database management engine that you would be using. And then for the enrollment date, let's say for argument's This student is not enrolled as yet, but we want them on record. So I just put no. So whenever we have another field, we can actually just say no. And even if we omitted this row this column from the insertion, then it would just get another value by default. Also, it's worthy to note that if you have four columns here, then you should have four corresponding values.

So there should be no mismatch between the number of columns outlined and the number of values all tied, if you have a mismatch, then you get an error. All right, so that is how you would go about inserting one rule of data into the database and of course, we end with a semicolon. And then if I execute, then you will see that it was successful, according to this log down here in the output, and one row was affected. So if I were to look in the students table, I should see at least one student record. Now this can get tedious if you Have 567 students, our 1020 students to enter. And it would be get tedious trying to change all these values with each student record.

So SQL actually gives us a mechanism by which we can actually insert more than one. So I'm just going to copy this code and paste it down here. And then I'm going to highlight that you can specify the columns one time. And then you can have multiple sets of values going into those columns. So using sorry, Xin control and the letter D, as in dog, you can actually duplicate each line see that I'm just duplicating. And I'm just showing you that you can actually have values and you can have multiple sets of values being inserted at once.

So this is how you do a multiple row insert. And then all you have to do is just make sure that each one is separated by a comma, a bunch of the last one which we know does not If como boats would get more like a semicolon to signify the end of the file. So I'm just going to go ahead and refine these values a bit. Alright, so I changed the numbering on the names here. So I just have students one, test one through eight. And oh, I didn't modify the student numbers.

And there we go. So that's better. So we have student, test one through student test it, and I changed up the date of birth. And then you notice that for enrollment data, put in some dates, and left some notes. So the fact is that as many rules as you need to insert, you can actually write one statement that will execute them all. All right.

Now, you also may have noticed that I didn't include an ID column here. And that's because once again, when we were creating our database, we specify that Id should be auto incrementing. So we wouldn't be specifying a data value for ID IDs taking care of itself. So once we insert a record, Id will automatically count up to have a value to uniquely identify that record that is being inserted in that command. so in this situation where we're inserting eight, that's a threat. It's real rolls.

When I execute this, then Id will automatically increment for each of those rules. And to run this statement by itself, because I have this statement that I ran before, but I don't want to run this one. Again, I just want to run this portion of the script, then, like we discussed in our previous video, we can just highlight that text and then we click the lightning bolts that has the cursor up affixed to it. And once we do that, it will only execute the portion of the code that is highlighted. And then here we see that we have the answer was affected, DNS records being inserted with no duplicates and no warning. So that was very successful.

And that is essentially how you go about inserting data into your data table. So I'm going to save this script. Alright, and I added some comments to it. So at least you'll have that for friends that appreciate exactly what's going on. And once again, we could have written all of this in one line as depicted by this template here or even for the multiple insert as long as you comma separates the section with the values up until the semi colon at the end, but we can also break the line and the blue dots will keep us in check as to which statement belongs to which part. So that's it for inserting records using the scripts.

Next we look at how we insert records using the workbench interface.

Sign Up

Share

Share with friends, get 20% off
Invite your friends to LearnDesk learning marketplace. For each purchase they make, you get 20% off (upto $10) on your next purchase.