16 Inserting data

 

For 15 chapters, we’ve examined ways to read data using the SELECT statement. But all that data we read using the SELECT statement had to get into the tables somehow, so in this chapter, we’ll learn how to insert data.

Throughout this book, we’ve seen that the syntax of SQL is often like the English language. When it comes to inserting rows of data, this similarity holds true because the new keyword we’ll be using is INSERT. Let’s look at some ways to use INSERT to add data to the tables in our database.

16.1 Inserting specific values

The first way to insert data is to use specific values. The main idea to keep in mind is that when we insert data, we’re inserting a new row into an existing table.

Chapter 2 talked about rows, columns, and values. All tables in our relational database management system (RDBMS) have rows of data, and each row has a specific set of properties defined by the columns of the table. Each property in the columns is represented by some value of a particular data type, sometimes using NULL to represent the absence of a value for a particular column.

I hope that by now, the preceding paragraph makes total sense to you, especially given all the SQL queries you’ve written so far. If anything about it is unclear, I encourage you to review chapter 2 to solidify your understanding of rows, columns, and values. If everything makes sense, you can proceed to inserting some data.

16.1.1 Inserting a new row

16.1.2 Inserting multiple new rows

16.1.3 Inserting a partial row

16.1.4 A word of caution about omitting columns

16.2 Inserting a row with a query

16.3 Inserting a row with variables

16.4 Lab

16.5 Lab answers