20.07.2013 Views

Beginning SQL

Beginning SQL

Beginning SQL

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

5<br />

Manipulating Data<br />

In Chapter 3, you looked simply at getting raw data from the database using <strong>SQL</strong> queries. This<br />

chapter, however, examines how to use, alter, and manipulate data using <strong>SQL</strong>. It starts off with a<br />

look at math in <strong>SQL</strong>, though don’t worry if math isn’t your favorite subject, as <strong>SQL</strong> math is fairly<br />

basic! The chapter then turns to the manipulation of character data and takes you through a whole<br />

host of functions that cover everything from reversing the order of characters in a string to matching<br />

columns based on words that sound alike. After that, coverage dives into another look at NULL,<br />

in particular how it can alter results in unexpected ways and how to deal with those altered<br />

results. Finally, this chapter finishes with a look at copying data from one table to another using<br />

INSERT INTO and SELECT statements.<br />

Understanding <strong>SQL</strong> Arithmetic<br />

The available math functions in <strong>SQL</strong> are fairly limited, which reflects <strong>SQL</strong>’s use as a tool for<br />

pulling out the raw data to answer questions. Any actual processing of the answers is really something<br />

left to high-level programming languages in middleware or front-end applications. For<br />

example, a Web server might connect to a database to pull back a customer’s name and credit card<br />

details, but you wouldn’t expect the database to process the card details.<br />

You start by looking at the four basic math functions supported by all flavors of <strong>SQL</strong>, and then you<br />

look at some more sophisticated math functions, which, although not part of the <strong>SQL</strong> standards,<br />

are commonly available on most database systems.<br />

Basic Math Operators<br />

It’s no great surprise to learn that the four basic math operators are multiplication, division, subtraction,<br />

and addition, which are listed in the following table, along with the operators <strong>SQL</strong> uses to<br />

represent them:

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!