Difference between revisions of "CREATE TABLE"

From SQLZOO
Jump to: navigation, search
 
(8 intermediate revisions by 4 users not shown)
Line 1: Line 1:
 
<h1>CREATE TABLE</h1>
 
 
<p>The table <code>games</code> shows the year and the city hosting the Olympic Games.</p>
 
<p>The table <code>games</code> shows the year and the city hosting the Olympic Games.</p>
 
<table>
 
<table>
Line 7: Line 5:
 
<tr><td>2004</td><td align='left'>Athens</td></tr>
 
<tr><td>2004</td><td align='left'>Athens</td></tr>
 
<tr><td>2008</td><td align='left'>Beijing</td></tr>
 
<tr><td>2008</td><td align='left'>Beijing</td></tr>
 +
<tr><td>2012</td><td align='left'>London</td></tr>
 
</table>
 
</table>
 
<div class='ht'>
 
<div class='ht'>
Line 13: Line 12:
 
The CREATE statement builds an empty table to hold rows.
 
The CREATE statement builds an empty table to hold rows.
 
In this example the column yr is the PRIMARY KEY:  
 
In this example the column yr is the PRIMARY KEY:  
<source lang='sql' class='def e-oracle'>
 
CREATE TABLE games
 
(yr  INT NOT NULL PRIMARY KEY
 
,city VARCHAR(20)
 
);
 
INSERT INTO games(yr,city) VALUES (2004,'Athens');
 
INSERT INTO games(yr,city) VALUES (2008,'Beijing');
 
SELECT * FROM games;
 
</source>
 
 
<source lang='sql' class='def'>
 
<source lang='sql' class='def'>
 
CREATE TABLE games
 
CREATE TABLE games
Line 29: Line 19:
 
INSERT INTO games(yr,city) VALUES (2004,'Athens');
 
INSERT INTO games(yr,city) VALUES (2004,'Athens');
 
INSERT INTO games(yr,city) VALUES (2008,'Beijing');
 
INSERT INTO games(yr,city) VALUES (2008,'Beijing');
 +
INSERT INTO games(yr,city) VALUES (2012,'London');
 
SELECT * FROM games;
 
SELECT * FROM games;
 
</source>
 
</source>

Latest revision as of 21:33, 2 June 2016

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

games
yrcity
2004Athens
2008Beijing
2012London
schema:scott
 DROP TABLE games

The CREATE statement builds an empty table to hold rows. In this example the column yr is the PRIMARY KEY:

CREATE TABLE games
(yr   INT NOT NULL PRIMARY KEY
,city VARCHAR(20)
);
INSERT INTO games(yr,city) VALUES (2004,'Athens');
INSERT INTO games(yr,city) VALUES (2008,'Beijing');
INSERT INTO games(yr,city) VALUES (2012,'London');
SELECT * FROM games;

See also