[ Team LiB ] Previous Section Next Section

A (Very) Brief Introduction to SQL

SQL stands for Structured Query Language. It provides a standardized syntax by which different types of databases can be queried. Most SQL database products provide their own extensions to the language, just as many browsers provide their own extensions to HTML. Nonetheless, an understanding of SQL enables you to work with a wide range of database products across multiple platforms.

This book cannot describe all the intricacies of SQL. In this section we attempt to cover a little background, however.

Most database applications such as MySQL run as a server daemon to which users on the same or even remote machines can connect. Once connected to the server, you can select a database if you have the privileges to do so.

A database has a varying number of tables of data, and each table is arranged in rows and columns. The intersection between a row and a column is the point at which each item of data you want to store and access sits. Each column accepts only a predefined type of data, INT for integer, for example, or VARCHAR for a variable number of characters up to a defined limit.

To create a new table within a database we have selected, we might use a SQL query like the following:

CREATE TABLE people ( first_name VARCHAR(30), second_name VARCHAR(30), age INT);

Our new table has three columns. first_name and second_name can contain strings of up to 30 characters, and age can contain any integer.

To add data to this table, we could use an INSERT statement:

( first_name, second_name, age )
VALUES ( 'John', 'Smith', 36 );

The field names to which we want to add data are defined in the first set of parentheses. The values we want to insert are defined in the second.

To acquire all the data in a table, we would use a SELECT statement:

SELECT * FROM mytable;

The * symbol represents a wildcard, which means "all fields." To acquire the information from a single field, you can use the column name in place of the wildcard:

SELECT age FROM mytable;

To change the values already stored in a table, you can use an UPDATE statement:

UPDATE mytable SET first_name = 'Bert';

This changes the first_name field in every row to Bert. We can narrow the focus of the SELECT and UPDATE statements with a WHERE clause. For example

SELECT * FROM mytable WHERE first_name = 'Bert';

returns only those rows whose first_name fields contain the string Bert. This next example

UPDATE mytable SET first_name = 'Bert' WHERE second_name = 'Baker';

changes the first_name fields of all rows whose second_name fields contain Baker.

For more information on SQL, see Sams Teach Yourself SQL in 21 Days by Ryan K. Stephens et. al.

    [ Team LiB ] Previous Section Next Section