SQL进阶---第一单元、Manipulation
第七课、Update
MANIPULATION Update
UPDATE celebs SET age = 22
WHERE id = 1;
The UPDATE
statement edits a row in thetable. You can use the UPDATE
statement when you want tochange existing records.
1. UPDATE
is a clause that edits a rowin the table.
2. celebs
is the name of the table.
3. SET
is a clause that indicates the column to edit.
age
is the name of the column that is going to be updated
22
is the new value that is going to be inserted into the age
column.
4. WHERE
is a clause that indicateswhich row(s) to update with the new column value. Here the row with a 1
in the id
column is the row that will havethe age
updated to 22
.
Instructions
1.Add a new column to the table.In the code editor type
ALTER TABLE celebs
ADD COLUMN twitter_handle TEXT;
SELECT * FROM celebs;
ALTER TABLE celebs
ADD COLUMNtwitter_handle TEXT;
SELECT * FROM celebs ;
id
name
age
twitter_handle
1
Justin Bieber
22
2
Beyonce Knowles
33
3
Jeremy Lin
26
4
Taylor Swift
26
Celebs 4 rows
Id
INTEGER
name
TEXT
age
INTEGER
twitter_handle
TEXT
第八课、Alter
MANIPULATION Alter
ALTER TABLE celebs
ADD COLUMN twitter_handle TEXT;
The ALTER TABLE
statement added a new columnto the table. You can use this command when you want to add columns to a table.
1. ALTER TABLE
is a clause that lets youmake the specified changes.
2. celebs
is the name of the table that isbeing changed.
3. ADD COLUMN
is a clause that lets you add a new column to a table.
twitter_handle
is the name of the new column being added
TEXT
is the data type for the new column
4.
NULL
is a special value in SQLthat represents missing or unknown data. Here, the rows that existed before thecolumn was added have NULL
values for twitter_handle
.
Instructions
1.Update the table to include Taylor Swift's twitter handle. Inthe code editor type:
UPDATE celebs
SET twitter_handle = '@taylorswift13'
WHERE id = 4;
SELECT * FROM celebs;
UPDATE celebs
SET twitter_handle = '@taylorswift13'
WHERE id = 4;
SELECT * FROM celebs;
QueryResults
id
name
age
twitter_handle
1
Justin Bieber
22
2
Beyonce Knowles
33
3
Jeremy Lin
26
4
Taylor Swift
26
@taylorswift13
DatabaseSchema
Celebs 4 rows
Id
INTEGER
name
TEXT
age
INTEGER
twitter_handle
TEXT
2.Delete all of the rows that have a NULL value in the twittercolumn. Replace your code in the code editor with the following:
UPDATE celebs
SETtwitter_handle = '@taylorswift13'
WHERE id = 4;
DELETE FROM celebs
WHERE twitter_handle IS NULL;
SELECT * FROM celebs;
UPDATE celebs
SETtwitter_handle = '@taylorswift13'
WHEREid = 4;
DELETE FROM celebs
WHEREtwitter_handle IS NULL;
SELECT * FROM celebs;
QueryResults
id
name
age
twitter_handle
4
Taylor Swift
26
@taylorswift13
DatabaseSchema
Celebs 1 rows
Id
INTEGER
name
TEXT
age
INTEGER
twitter_handle
TEXT
Contacts 4 rows
id
INTEGER
name
TEXT
birthday
DATE
第九课、Delete
MANIPULATION Delete
DELETE FROM celebs WHERE twitter_handle IS NULL;
The DELETE FROM
statement deletes one or morerows from a table. You can use the statement when you want to delete existingrecords.
DELETE FROM
is a clause that lets youdelete rows from a table.
celebs
is the name of the table we want to delete rows from.
WHERE
is a clause that lets youselect which rows you want to delete. Here we wantto delete all of the rows where the twitter_handle column IS NULL
.
IS NULL
is a condition in SQL thatreturns true when the value is NULL
and false otherwise.
In this lesson we have learned SQL statements that create, edit,and delete data. In the upcoming lessons we will learn how to use SQL to retrieveinformation from a database.
第十课、小结
MANIPULATION Generalizations
Congratulations! You've learned six commands commonly used tomanage data stored in a relational database. What can we generalize so far?
SQL is a programming language designed to manipulate and manage datastored in relational databases.
A relationaldatabase is adatabase that organizes information into one or more tables.
A table is a collection of data organized intorows and columns.
A statement is a string of characters that thedatabase recognizes as a valid command.
CREATE TABLE
creates a new table.
INSERT INTO
adds a new row to a table.
SELECT
queries data from a table.
UPDATE
edits a row in a table.
ALTER TABLE
changes an existing table.
DELETE FROM
deletes rows from a table.