Hi! Today, we’re going to take a brief look at the fundamental data manipulation commands in the database world. You know, sometimes you write a program, everything works smoothly, and then you realize the data in the database has gone all weird… That’s when these commands come to the rescue. In fact, you could say almost everything revolves around these three commands.
First, let’s clarify this: INSERT, UPDATE, and DELETE. These three are used for adding data to our database, modifying existing data, and removing the data we no longer need. Of course, it’s not just that simple. Each one has its own syntax, and using them correctly is really important for the health of your database.
Let’s start with our first command: INSERT. As the name suggests, this command is used to add new records to a table. For example, imagine creating a new user record with a name, surname, email, etc. This command helps place all that information into the table. Pretty neat, isn’t it?
Now, let’s go into a bit more detail. We specify the table to insert data into using the INSERT INTO command. Then, in parentheses, we list the columns we want to insert data into. After that, using the VALUES keyword, we specify the corresponding values in order. Imagine filling out a form—you could say this is what happens behind the scenes with this command. In a small project I built, I used this logic to add new products, and everything seemed to work fine.
Here’s an example:
INSERT INTO Products (ProductName, Price, StockQuantity) VALUES ('Laptop', 15000.00, 50);
In this example, we’re adding a new product called ‘Laptop,’ priced at 15,000 TL, with a stock quantity of 50 into the ‘Products’ table. Simple but effective, right?
Moving on to our second major hero: UPDATE. This command is used to change existing data. For instance, if you update a user’s phone number, you would use the UPDATE command. You need to specify which record to update and what value the column should have. Also, be very careful when using UPDATE because if you’re not cautious, you might accidentally change all data in the table. Trust me, I’ve been there 🙂
In the UPDATE command, we again specify the table name, then use the SET keyword to indicate which column to update and what new value to assign. Most importantly, we use the WHERE clause to specify which record to update precisely. Not using WHERE means you’ll change every record in the table, and then you’ll be wondering, “What just happened?”
Let’s clarify with an example:
UPDATE Products SET Price = 16000.00, StockQuantity = 45 WHERE ProductName = 'Laptop';
Here, in the ‘Products’ table, the product named ‘Laptop’ has its price increased to 16,000 TL, and stock quantity reduced to 45. Sometimes a product’s price increases due to inflation, and this command works behind the scenes at those moments. Though, the reduction in stock quantity is interesting too.
Finally, we come to the DELETE command. As the name suggests, it’s used for deleting data. When you want to remove a product that is no longer sold or a user who has left the system, you invoke the DELETE command. Just like throwing away unnecessary items in life, you need to clean your database of unnecessary data.
Again, we specify the table name, and use the WHERE clause to indicate which record to delete. And yes, I must warn you again: if you omit the WHERE clause, you’ll wipe the entire table! I once learned this the hard way when I accidentally deleted all users during a test. It took me days to restore it 🙂
Let’s give an example:
DELETE FROM Products WHERE ProductName = 'Laptop';
This command deletes the product named ‘Laptop’ from the database entirely. And done! Isn’t it simple? Actually, it’s straightforward but very dangerous. Be cautious when using these commands and always test in a safe environment first.
These three commands form the foundation of database management. When you learn to use them correctly and safely, you’ll be able to communicate much more comfortably with your database. Remember, your database is your treasure of information, and you need to protect and manage it well. Believe me, understanding these basic commands will benefit you greatly. If you want to explore more, you can search on Google for SQL INSERT UPDATE DELETE and find many resources. Good luck!