Data manipulation with SQL

5.3 Data manipulation with SQL

This section discusses how to perform read, update and delete operations with SQL.

5.3.1 Selecting data

Selecting data in SQL is an operation that allows you to read (retrieve) rows and columns from a relational table. Selecting data is performed using the SELECT statement.

Assuming a table name of myTable, the simplest statement to select data from this table is:

select * from myTable

The special character ‘*’, represents all the columns from the table. Using the ‘*’ in a query is not recommended unless specifically required because you may be asking more information than what you really need. Typically, not all columns of a table are required; in which case, a selective list of columns should be specified. For example,

select col1, col2 from myTable

retrieves col1 and col2 for all rows of the table myTable where col1 and col2 are the names of the columns to retrieve data from.

5.3.1.1 Ordering the result set

A SELECT statement returns its result set in no particular order. Issuing the same SELECT statement several times may return the same set of rows, but in different order. To guarantee the result set is displayed in the same order all the time, either in ascending or descending order of a column or set of columns, use the ORDER BY clause.

For example this statement returns the result set based on the order of col1 in ascending order:

Chapter 5 – Introduction to SQL 123 SELECT col1 FROM myTable ORDER BY col1 ASC

ASC stands for ascending, which is the default. Descending order can be specified using DESC as shown below:

SELECT col1 FROM myTable ORDER BY col1 DESC

5.3.1.2 Cursors

A cursor is a result set holding the result of a SELECT statement. The syntax to declare, open, fetch, and close a cursor is shown below:

DECLARE <cursor name> CURSOR [WITH RETURN <return target>] <SELECT statement>; OPEN <cursor name>; FETCH <cursor name> INTO <variables>; CLOSE <cursor name>;

Rather than returning all the rows of an SQL statement to an application at once, a cursor allows the application to process rows one at a time. Using FETCH statements within a loop in the application, developers can navigate through each row pointed by the cursor and apply some logic to the row or based on the row contents. For example, the following code snippet sums all the salaries of employees using a cursor.

... DECLARE p_sum INTEGER; DECLARE p_sal INTEGER; DECLARE c CURSOR FOR

SELECT SALARY FROM EMPLOYEE; DECLARE SQLSTATE CHAR(5) DEFAULT '00000'; SET p_sum = 0; OPEN c; FETCH FROM c INTO p_sal; WHILE(SQLSTATE = '00000') DO

SET p_sum = p_sum + p_sal; FETCH FROM c INTO p_sal;

END WHILE; CLOSE c;

... Cursors are the most widely used method for fetching multiple rows from a table and

processing them inside applications.

5.3.2 Inserting data

To insert data into a table use the INSERT statement. There are different ways to insert data. For instance, you can insert one row per INSERT statement, multiple rows per INSERT statement or all the rows of a result set from another query as illustrated in the following examples.

Database Fundamentals 124 In this first example, the statements insert one row at a time into the table myTable.

insert into myTable values (1); insert into myTable values (1, ‘myName’, ‘2010-01-01’);

In this second example, the statements insert multiple (three) rows into the table myTable.

insert into myTable values (1),(2),(3); insert into myTable values (1, ‘myName1’,’2010-01-01’),

(2, ‘myName2’,’2010-02-01’), (3, ‘myName3’,’2010-03-01’);

Finally, in this third example, the statement inserts all the rows of the sub-query “select * from myTable2” into the table myTable.

insert into myTable (select * from myTable2)

5.3.3 Deleting data

The DELETE statement is used to delete rows of a table. One or multiple rows from a table can be deleted with a single statement by specifying a delete condition using the WHERE clause. For example, this statement deletes all the rows where col1 > 1000 in table myTable.

DELETE FROM myTable WHERE col1 > 1000

Note that care should be taken when issuing a delete statement. If the WHERE clause is not used, the DELETE statement will delete all rows from the table.

5.3.4 Updating data

Use the UPDATE statement to update data in your table. One or multiple rows from a table can be updated with a single statement by specifying the update condition using a WHERE clause. For each row selected for update, the statement can update one or more columns.

For example:

UPDATE myTable SET col1 = -1 WHERE col2 < 0 UPDATE myTable SET col1 = -1,

col2 = ‘a’, col3 = ‘2010-01-01’

WHERE col4 = ‘0’

Note that care should be taken when issuing an update statement without the WHERE clause. In such cases, all the rows in the table will be updated.

Chapter 5 – Introduction to SQL 125