23 Using someone else’s script

 

I hope you’re feeling confident about all the SQL you’ve learned in this book. I’ve covered the most basic and frequently used keywords and statements, so you should be well prepared to fulfill requests to retrieve and even manipulate data in a relational database.

As with a foreign language, though, you need to be able to listen and read as well as speak or write. You need to be able to read existing SQL in stored procedures and elsewhere in whatever databases your organization has. Because this book is an introduction to the SQL language, you’ll likely even find yourself looking on the internet for examples of SQL scripts that use keywords and concepts you haven’t been exposed to yet.

To practice these vital skills and apply what you’ve learned, in this chapter, you’ll review SQL examples written by someone else. Know that the examples will work, but you have to look at them closely to determine what the author intended. Also, these scripts go against the best practices you’ve learned, so you’ll also be considering how to improve the SQL in the scripts.

There’s no lab section at the end of the chapter because this chapter is like one large review. There aren’t even any “Try it now” sidebars, but you’re welcome to try these scripts if you want. Your main task here is to walk through the scripts, understand what they’re meant to do, and improve them based on everything you’ve learned in this book.

23.1 Someone else’s script: Creating a table

23.1.1 The CREATE TABLE script

23.1.2 Reviewing the CREATE TABLE script

23.1.3 Improving the CREATE TABLE script

23.2 Someone else’s script: Inserting data

23.2.1 The INSERT stored procedure

23.2.2 Reviewing the INSERT stored procedure

23.2.3 Improving the INSERT stored procedure

23.2.4 Improving the INSERT stored procedure even more