28.10.2014 Views

SQL Injection Attacks and Defense - 2009

SQL Injection Attacks and Defense - 2009

SQL Injection Attacks and Defense - 2009

SHOW MORE
SHOW LESS

Create successful ePaper yourself

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

418 Chapter 10 • References<br />

SELECT username, password FROM tblUsers UNION SELECT username, password FROM<br />

tblAdmins<br />

UNION SELECT will automatically compare the values returned by each SELECT<br />

statement <strong>and</strong> return only distinct values. To permit duplicates <strong>and</strong> prevent the database<br />

from comparing the returned data, use UNION ALL SELECT:<br />

SELECT username, password FROM tblUsers UNION ALL SELECT username, password<br />

FROM tblAdmins<br />

INSERT Statement<br />

As you have probably guessed already, you use the INSERT statement to insert data into<br />

a table. You can structure the INSERT statement in two different ways to achieve the same<br />

goal. The following INSERT statement will insert the values 5, john, smith, <strong>and</strong> 0 into the<br />

tblUsers table:<br />

INSERT INTO tblUsers VALUES (5,'john','smith',0)<br />

In this example, the data to be inserted into the table is arranged in the correct order to<br />

correspond with each column in the table. The most significant problem with this approach<br />

is that if the table structure is changed (e.g., columns are added or deleted) data could be<br />

written to the wrong column. To avoid potentially harmful mistakes the INSERT statement<br />

can accept a comma-separated list of target columns following the table name:<br />

INSERT INTO tblUsers(id, username, password, priv) VALUES (5, 'john','smith',0)<br />

In this example, each target column is listed to ensure that the supplied data is inserted<br />

in the correct column. If the table structure changes, the INSERT statement will still target<br />

the correct columns.<br />

UPDATE Statement<br />

You use the UPDATE statement to modify existing data within a database table. The following<br />

UPDATE statement will change the priv column value to 0 for all records that have the<br />

username value of sarah:<br />

UPDATE tblUsers SET priv=0 WHERE username = 'sarah'<br />

It is important to note that all UPDATE statements should include a WHERE clause<br />

to indicate which rows should be updated. If you omit the WHERE clause, all rows are affected.<br />

DELETE Statement<br />

You use the DELETE statement to delete rows from a table. The following DELETE<br />

statement will delete all rows from tblUsers that have a username value of admin:<br />

DELETE FROM tblUsers WHERE username = 'admin'

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

Saved successfully!

Ooh no, something went wrong!