The data inside your database server is stored in large, compressed files, which are read in by the DBMS and manipulated through the DBMS. The way we read and write data is through a special language known as Structured Query Language (SQL). SQL, pronounced S-Q-L (not "Sequel"), is a very simple language designed to let you specify how you want to manipulate your data without needing to specify the steps the DBMS should go through - that is, you say "I want this", not "I want this, so do A, B, and C, then give me D", which makes SQL a 4th-generation language (4GL). SQL can be surprisingly powerful when you get skilled with it, so it is strongly recommended you read this whole section thoroughly.

If we did not have SQL, data extraction would be much more difficult. For example, the SQL syntax to select the Name attribute of all fields in a table is this:

SELECT Name FROM sometable;

That would automatically return an array of all the values. If we did not have SQL, we would need to do something like this pseudo-code:

Open file sometable.db
Search from line one to end of file {
    Split each line into fields
    Get field "Name" and add to our array of return values
Return array of names

It's not exactly hard , but it's certainly a lot more work than is necessary, and that was only for a simple query - some really complicated SQL queries are three or four lines long, and it'd be very chaotic to have to do the same using a generic language such as PHP or C++.

Author's Note: "Sequel" is actually an entirely separate language, which is why the pronunciation of SQL (ess-que-ell) needs to be preserved.

All databases, from Oracle all the way down to the lowly Microsoft Access, support SQL, although most have their own dialect of it - a slightly different version to implement various special features. Using SQL you can extract data from any DBMS, regardless of who makes it, which makes it easy to learn and use wherever you go.

Note that SQL is case insensitive, but for the purpose of clarity I have tried to print all SQL keywords in uppercase in this book.


Want to learn PHP 7?

Hacking with PHP has been fully updated for PHP 7, and is now available as a downloadable PDF. Get over 1200 pages of hands-on PHP learning today!

If this was helpful, please take a moment to tell others about Hacking with PHP by tweeting about it!

Next chapter: SQL comments >>

Previous chapter: Microsoft SQL Server

Jump to:


Home: Table of Contents

Copyright ©2015 Paul Hudson. Follow me: @twostraws.