Chapter 18. Building Databases with SQL Scripts
The previous chapter provided an introduction to using SQL to create and manage databases. That chapter also hinted at techniques for using SQL scripts to make database maintenance easier.
This chapter goes a little further. It discusses some of the details that you need to take into account when you use scripts to manage a database.
In this chapter you learn how to:
Know when scripts can be useful.
Build tables in a valid order.
Insert data into tables in a valid order.
Drop tables in a valid order.
Why Bother with Scripts?
SQL statements let you create, populate, modify, and delete the tables in a database. In many database products, SQL statements even let you create and destroy the database itself. For example, MySQL's CREATE DATABASE
and DROP DATABASE
statements create and destroy databases.
If you put these SQL commands in a script, you can rerun that script whenever it's necessary. You can easily rebuild the database if it gets corrupted, make copies of the database on other computers, fill the tables with data to use when running tests, and reinitialize the data after the tests are finished.
Being able to reinitialize the data to a known state can also be very helpful in tracking down bugs. It's extremely hard to find a bug if it just pops up occasionally and then disappears again. If you can reinitialize the database and then make a bug happen by following a series of predictable steps, it's much easier to find and fix the problem. ...
Get Beginning Database Design Solutions now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.