Join Bill Weinman for an in-depth discussion in this video The INSERT, UPDATE, and DELETE functions, part of SQL Essential Training (2014).
The Insert, Update, and Delete functions are pretty simple in the CRUD application. Let's take a look. I'm going to use my navigation here. I'm using the text wrangler editor here on this Mac. And, we'll start with insert_album_sql. And, this function is no different for any of the different database engines that are supported. The SQL is pretty simple. We're inserting into the album. These question marks in the SQL, these are placeholders that represent the parameters.
And, you notice that in the Execute function, we fill in the actual data for those parameters. Because we're using the PDO driver and PHP, this gets very easy to support the different databases. The only little quirk here is for Postgres. We need to specify a sequence table for getting the last inserted ID. When you insert a row into the table, these tables have the automatic ID feature set up in the various database engines and so.
You still need to get that new id for the new record. The last insert id function in the pdo driver will get you that id which you can then use to display stuff and to then find that record in the database. Postgres just requires that you specify the sequence for it. I just have to select out for Postgres for that one. The same with Insert Track. You'll see the same, I call it a kludge. It's really just a different way of handling it. And other than that, this function is pretty much the same.
I have the SQL here. I have the placeholders, and I fill in the data in the Execute function. Update is even simpler. Again the SQL is the same for the different database engines. And these place holders are used, and all of this works the same in the different database engines. So Update Album, Update Track, those are very straightforward, and likewise with Delete Album, and Delete Track. Here I want to make sure that I'm deleting the tracks before the album so that I don't get widowed tracks without an album, and deleting individual tracks is as simple as sending that delete statement to the SQL engine.
The CRUD application is very simple. And writing a CRUD application is a great way to learn about database operations. I suggest you write your own. Or feel free to use this application as a starting place for your own applications, or as a basis for further experimenting and learning about SQL relational databases or database applications in general.
- Understanding SQL terminology and syntax
- Creating new tables and records
- Inserting and updating data
- Writing basic SQL queries
- Sorting and filtering
- Accessing related tables with JOIN
- Working with strings
- Finding the numeric type of a value
- Using aggregate functions and transactions
- Updating a table with triggers
- Creating views
Skill Level Beginner
Q: For Mac OS X: When I try to start the Apache Web Server from the XAMPP control panel, it doesn't start, and when I open "localhost" in my web browser, I see a white screen that says "It Works!" instead of the XAMPP page.
sudo apachectl stop
Q: I'm on a Mac, and I get an error in SID that says "attempt to write a read only database." How can I fix this?
A: This usually means that the database folder does not have sufficient permissions for writing by the web user. This can happen if you create the SQL folder new, rather than copying it from the Exercise Files. Here's how to fix this:
- Open a Finder window and Navigate to /Applications/XAMPP/htdocs/SQL
- Control-click on the SQL folder and select "Get Info" from the context menu.
- Under "Sharing and Permissions" (you may need to open the disclosure triangle), in the "everyone" row, select "Read & Write."Then you can close the Info window.
- Now repeat the process for the three *.db files inside the folder.