loading..
Русский    English
09:12

UPDATE statement page 1

UPDATE statement changes existing data in a table.

Syntax 

  1. UPDATE < TABLE name >
  2. SET {< COLUMN name > = {< expression >
  3. | NULL
  4. | DEFAULT},...}
  5. [ {WHERE < predicate >}]

With a single UPDATE statement, arbitrary number of columns can change their values. However a column cannot be changed in the same UPDATE statement more than once. All rows of a table will be affected if a WHERE clause is eliminated from an UPDATE statement.

If the column is defined to allow NULL values, NULL can be specified explicitly. Moreover, existing value can be changed by default value (DEFAULT keyword is used) for a given column.

An expression may refer to current values in a table to be changed. For example, we may decrease prices by 10 percent on all the laptops with the following statement:

  1. UPDATE Laptop
  2. SET price = price*0.9;

The values of one column are allowed to assign to another column. For example, it is required to change HD less than 10 Gb in laptops. New HD capacity must be one-half of available RAM. The solution may be as follows:

  1. UPDATE Laptop
  2. SET hd = ram/2
  3. WHERE hd < 10;

Clearly, data types of the hd and ram columns need to be compatible. The CAST expression (item 5.9) may be used for conversion.

If the data needs to be changed depending on contents of some column, we can use the CASE expression (item 5.10). Say, if it is needed to setup HD with capacity of 20 Gb in laptops with RAM less than 128 Mb and HD with capacity of 40 Gb in remaining laptops, we can write the query:

  1. UPDATE Laptop
  2. SET hd = CASE
  3. WHEN ram < 128
  4. THEN 20
  5. ELSE 40
  6. END;

Use of subquery is also allowed to define the column value. For example, we need to complete all laptops with most fast available processor. Then we write:

  1. UPDATE Laptop
  2. SET speed = (SELECT MAX(speed)
  3. FROM Laptop
  4. );

Some words about autoincrementable columns ought to be said. If the code column in the Laptop table is defined as  IDENTITY(1,1), then the following statement 

  1. UPDATE Laptop
  2. SET code = 5
  3. WHERE code = 4;
will not be executed, since autoincrementable column does not allow modification, but execution error will be returned. To solve above task, we can proceed as follows. At first, let us insert the needed row using SET IDENTITY_INSERT, then delete the old row:

  1. SET IDENTITY_INSERT Laptop_ID ON;
  2. INSERT INTO Laptop_ID(code, model, speed, ram, hd, price, screen);
  3. SELECT 5, model, speed, ram, hd, price, screen
  4. FROM Laptop_ID WHERE code = 4;
  5. DELETE FROM Laptop_ID
  6. WHERE code = 4;

Clearly, another row with the value code=5 must not be in the table.

Bookmark and Share
Pages 1 2
Tags
aggregate functions Airport ALL AND AS keyword ASCII AVG Battles Bezhaev Bismarck C.J.Date calculated columns Cartesian product CASE cast CHAR CHARINDEX Chebykin check constraint classes COALESCE common table expressions comparison predicates Computer firm CONSTRAINT CONVERT correlated subqueries COUNT CROSS APPLY CTE data type conversion data types database schema DATEADD DATEDIFF DATENAME DATEPART DATETIME date_time functions DDL DEFAULT DEFAULT VALUES DELETE DISTINCT DML duplicates edge equi-join EXCEPT exercise (-2) More tags
The book was updated
month ago
продажа связной банк . ремонт ноутбука
©SQL-EX,2008 [Evolution] [Feedback] [About] [Links] [Team]
All right reserved.