1. 程式人生 > >SQL進階---第一單元(第七到第十課)、Manipulation

SQL進階---第一單元(第七到第十課)、Manipulation



SQL進階---第一單元、Manipulation

第七課、Update

MANIPULATIONUpdate

UPDATE celebs SET age = 22
WHERE id = 1;

The UPDATEstatement edits a row in thetable. You can use the UPDATEstatement when you want tochange existing records.

1. UPDATEis a clause that edits a rowin the table.
2. 
celebsis the name of the table. 
3. 

SETis a clause that indicates the column to edit.

  • ageis the name of the column that is going to be updated

  • 22is the new value that is going to be inserted into the agecolumn.

4. WHEREis a clause that indicateswhich row(s) to update with the new column value.Here the row with a 1in the idcolumn is the row that will havethe 

ageupdated to 22.

Instructions

1.Add a new column to the table.In the code editor type

ALTERTABLE celebs

ADD COLUMN twitter_handle TEXT;

SELECT * FROM celebs;

ALTER TABLE celebs

ADD COLUMNtwitter_handle TEXT;

SELECT * FROM celebs ;

Query Results

id

name

age

twitter_handle

1

Justin Bieber

22

 

2

Beyonce Knowles

33

 

3

Jeremy Lin

26

 

4

Taylor Swift

26

 

Database Schema

Celebs4rows

Id

INTEGER

name

TEXT

age

INTEGER

twitter_handle

TEXT

第八課、Alter

MANIPULATIONAlter

ALTERTABLE celebs 
ADD COLUMN twitter_handle TEXT;

The ALTER TABLEstatement added a new columnto the table. You can use this command when you want to add columns to a table.

1. ALTER TABLEis a clause that lets youmake the specified changes.2. celebsis the name of the table that isbeing changed. 
3. 
ADD COLUMNis a clause that lets you add a new column to a table. 

  • twitter_handleis the name of the new column being added

  • TEXTis the data type for the new column

    4. NULLis a special value in SQLthat represents missing or unknown data. Here, the rows that existed before thecolumn was added have NULLvalues 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

Celebs4 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;

DELETEFROM celebs

WHERE twitter_handle ISNULL;

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

Celebs1 rows

Id

INTEGER

name

TEXT

age

INTEGER

twitter_handle

TEXT

Contacts 4 rows

id

INTEGER

name

TEXT

birthday

DATE

第九課、Delete

MANIPULATIONDelete

DELETEFROM celebs WHERE twitter_handle ISNULL;

The DELETE FROMstatement deletes one or morerows from a table. You can use the statement when you want to delete existingrecords.

  1. DELETE FROMis a clause that lets youdelete rows from a table.

  2. celebsis the name of the table we want to delete rows from.

  3. WHEREis 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.

  4. IS NULLis a condition in SQL thatreturns true when the value is NULLand 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.

第十課、小結

MANIPULATIONGeneralizations

Congratulations! You've learned six commands commonly used tomanage data stored in a relational database. What can we generalize so far?

  • SQLis a programming language designed to manipulate and manage datastored in relational databases.

    • relationaldatabase is adatabase that organizes information into one or more tables.

    • table is a collection of data organized intorows and columns.

  • statement is a string of characters that thedatabase recognizes as a valid command.

    • CREATE TABLEcreates a new table.

    • INSERT INTOadds a new row to a table.

    • SELECTqueries data from a table.

    • UPDATEedits a row in a table.

    • ALTER TABLEchanges an existing table.

    • DELETE FROMdeletes rows from a table.