Difference between revisions of "UPDATE"

From SQLZOO
Jump to: navigation, search
 
(5 intermediate revisions by one user not shown)
Line 14: Line 14:
 
</table>
 
</table>
 
   
 
   
 +
<table class='NoBorder'><td><tr>
 +
The table <code>number</code> shows the year and the city hosting the Olympic Games.
 +
</td><td>
 +
<table style='float:left' class='Bordered'>
 +
<caption align='center'>'''number'''</caption>
 +
<tr><th align='center'>'''yr'''</th><th align='center'>'''number'''</th></tr>
 +
<tr><td>2000</td><td align='left'>1</td></tr>
 +
<tr><td>2004</td><td align='left'>2</td></tr>
 +
<tr><td>2008</td><td align='left'>3</td></tr>
 +
<tr><td>2012</td><td align='left'>4</td></tr>
 +
</table>
 +
</table>
 +
 
<div class='ht'>
 
<div class='ht'>
 
<div class=params>schema:scott</div>
 
<div class=params>schema:scott</div>
Line 24: Line 37:
 
INSERT INTO games VALUES (2008,'Beijing');
 
INSERT INTO games VALUES (2008,'Beijing');
 
INSERT INTO games VALUES (2009,'London');
 
INSERT INTO games VALUES (2009,'London');
 +
</source>
 +
 +
<source lang=sql class='setup'>
 +
CREATE TABLE number(
 +
  yr INTEGER,
 +
  number VARCHAR(20));
 +
INSERT INTO number VALUES (2000,'1');
 +
INSERT INTO number VALUES (2004,'2');
 +
INSERT INTO number VALUES (2008,'3');
 +
INSERT INTO number VALUES (2009,'4');
 +
 +
 
</source>
 
</source>
 
The UPDATE statement can be used to change a values in rows that already exists.
 
The UPDATE statement can be used to change a values in rows that already exists.

Latest revision as of 15:42, 6 May 2014

UPDATE

The table games shows the year and the city hosting the Olympic Games.

games
yrcity
2000Sydney
2004Athens
2008Beijing
2012London

The table number shows the year and the city hosting the Olympic Games.

number
yrnumber
20001
20042
20083
20124
schema:scott
 DROP TABLE games
 CREATE TABLE games(
  yr INTEGER,
  city VARCHAR(20));
INSERT INTO games VALUES (2000,'Sydney');
INSERT INTO games VALUES (2004,'Athens');
INSERT INTO games VALUES (2008,'Beijing');
INSERT INTO games VALUES (2009,'London');
CREATE TABLE NUMBER(
  yr INTEGER,
  NUMBER VARCHAR(20));
INSERT INTO NUMBER VALUES (2000,'1');
INSERT INTO NUMBER VALUES (2004,'2');
INSERT INTO NUMBER VALUES (2008,'3');
INSERT INTO NUMBER VALUES (2009,'4');

The UPDATE statement can be used to change a values in rows that already exists. In this example we move the 2012 games from London to Paris.

UPDATE scott.games SET city='Paris' WHERE yr = 2012;
SELECT * FROM scott.games;
UPDATE games SET city='Paris' WHERE yr = 2012;
SELECT * FROM games;

See also

Personal tools
Namespaces

Variants
Actions
Reference
Toolbox
Google AdSense