Delete Data from the Table

MySQL Database Development Mastery Manipulating Tables and Data - CRUD Operations
10 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 how we can go about removing records from our database. Now this is basically the last letter in the word crud. And I think this is the first time I'm actually mentioning that word crud, but the lesson section would have been entitled CRUD operations. And crud is essentially an acronym for create, read, update, and delete. So we've looked at how we insert Records, which is create, we have looked at how we select these records, which is reading the data, we've looked at how we update or make changes. So that's the you and so now we look at how we remove or delete which is the D. Now much like the other commands in SQL, the Delete command starts off with the word that characterizes the command itself, which is delete.

So you see at this point that SQL really just looks like Standard English. They're just certain keywords and they're is a specific pattern which with each type of command, that if you just follow that pattern and know those keywords, you will have no problem actually writing these commands. Now the Delete command takes a bit more to it says DELETE FROM and then we specify a table. So in this case, we only have data in the students table, or at least I only have data in the students table. And then we say we're and so we notice that the Where is going to become far more prominent. So when we filter reading data or selecting data we use where when we want to update specific records we use where and when we want to delete it is even more important to use the weird because without the where it will actually just remove everything from the database meaning if you were to execute this right now, everything from the table sorry, if you were to execute this command right now as it is, this will just remove every single student from the students table.

Which is not what we want, we want to probably remove specific records from this table. So we have to specify our where, and then give a condition. Now let's quickly refresh ourselves with what is in the students table. And you would have slightly different data from me right now because you would have completed the update challenge from the previous lesson. And so you would have updated the enrollment dates and changed some of the values for first name and last name, but I'll just the principles will remain the same as long as you know how to get the ID, which is the best way to target a rule as opposed to using any other column. Now let's say I wanted to delete student test, Id student ID and test it.

Alright, so student ID test ID, which is student with ID nine, I want to remove that students. So I just say DELETE FROM students, where and then I specify My column and the actual column condition which would be equals nine. So I want where ID equals nine, delete that record, and it with a semicolon. And then once I execute, this will again just execute this entire script, then we see here that one row was affected. So if I refreshed my query to select all from the students table, then I see I no longer have a student with ID nine. Once again, if I leave this filter or this condition off, then it will remove every single thing from this table, which is not necessarily what you desire, at least not right now.

Of course, everything is done on a case by case basis. Now, what if I wanted to remove more than one rows and I only want to direct one command? So in the activity and the answer sheet that I would have uploaded with the update activity, you'd have seen where in the comments I included statements using a keyword in. So even if you didn't pay much attention to that, that's fine, we can look at using the keyword in right here. So if I wanted to remove the students from three to six with IDs three through six, so that means I want to remove 345, and six, all of those students and I'm writing one command to do that. Then instead of writing Well, I could write DELETE FROM and then this and then have a delete from statement for each of those ideas.

That will be fine. But then I can also use the keyword in and then in brackets or parentheses actually lists out all of the values that could be met or could possibly meet this condition evaluates to true. So in other words, I want to delete from students where Id, the value, Id can be found somewhere in a list of values and I did say I wanted 345 and six So this command will actually just say, look for all the records with an ID value that matches something in this list, essentially, that's what it's saying. And so all of these will come back, all of these will evaluate to true. And the expectation is that if I execute this, all of those rules will be deleted. And I'm going to execute it with just a theory.

And we see here that four rules have been affected. If I refresh my original query, then I see that I no longer have students with IDs, three through six. Now expanding on the capabilities of this condition, we can actually specify more than one columns in our condition. And here we see where we are seeing lead from students where and the conditions is first name must be equal to this value, and last name be equal to that value. And I left all my underscores here. But essentially what this is doing is evaluating To columns and only returning a matching condition when both columns meet this criteria.

So, in other words, if one if I find a j in a first name column, but her last name is read, then that this condition will not hold true because that means both first name and last name are not equal to the values that have been specified. So, the and the key word actually says both both condition and value both column and value pairs must be true. And that is when all of this evaluates to true. All right, in the same breath, if I said I wanted either or then I have the keyword or and this filtering these keywords and and or also apply to selecting so anytime you have a where clause, you can actually specify as many column and value pairs as you need to and you can always just join them as and meaning everything must match both sides or as many sides as there are most much for to evaluate to true.

And if you use an or means that any one of these could be true, so if I execute this and it finds finds a student with a first name j doesn't matter what the last name is, then it will delete it and if it finds somebody with the last name, Dermott, then it will delete it. Alright, so that's what the art does. So you have that. So in this case, where I want a student named Tajima dura mater, I have to make sure that both the first name and the last name match the specified conditions. Another example to look at is with using our wildcards. So the same way we can use the wild card when selecting we can use the wild card, once again, when we're deleting and even when we're updating.

The fact is that or WHERE clause can be used when we're selecting updating or deleting and essentially, it helps us to narrow down the exact conditions that need to be met, whenever we are about to execute. So in this scenario, we're actually using the or clause because I'm seeing delete all students with the names containing students. So that means the first name could probably contain students, or the last name could probably contain students. So literally, I'm seeing lead from students where the first name and we already established that the modelers means I don't care what comes before the word student or after the word students, but it contains the word students, or the last name can contain the words students. And that's essentially how that works. Now once again, workbench actually gives us an interface to help us to manipulate or edit or even remove the data that we are looking at when we run a query.

So the final step to this is how to remove our record without writing or needing to write a script. So if we look at This results grid, we can actually just highlight a column. And then we can remove. And I think we did something like this before, we can remove that row by just right clicking that row and clicking Delete rows. Or we can just click this icon here that says delete selected rows. So if you have to select multiple at once, and then you just delete selected rows, that will work.

So I'm just going to remove student one, test one, and I just right click and say delete rows. And of course we have to apply and applying will actually generate the code that matches the operation that we're carrying on. So you see here that they're very intent on putting in that condition to make sure that they're getting that right record. So I click Apply, and it would have executed successfully and so that students would no longer be among the lot. So essentially, that's how we go about deleting records from our database. I will include this script file, as part of this The resources for this lesson, and I encourage you to just practice find some data, insert it and try and delete it and look at many different ways to use up your conditions.

And if you need any assistance or guidance, feel free to hit me up via comments message or q&a.

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.