
Resource from codecademy.
1 |
CREATE TABLE table_nam( |
Create
1 |
CREATE TABLE celebs ( |
CREATE TABLEclause tells SQL that you want to create a new tablecelebsname of the table(id INTEGER, name TEXT, age INTEGER)a list of parameters defining each column in the table and its data type
idis the first column in the table.It stores values of data typeINTEGERnamestores values of data typeTEXTagestores values of data typeINTEGER
INSERT
1 |
INSERT INTO celebs (id, name, age) VALUES (1, 'William Turner'); |
This statement inserts new rows into a table. You can use the INSERT statement when you want to add new records.
INSERT INTOclause adds the specified row/rowscelebsname of the table the row is added to(id, name, age)is a parameter identifying the columns that data will be inserted intoVALUESindicates the data being inserted(1, 'William Turner, 21')is a parameter identifying the values being inserted
1insert intoidcolumnWilliam Turnerinserted intonamecolumn21inserted intoagecolumn
SELECT
1 |
SELECT name FROM celebs; |
SELECT statements are used to fetch data from a database. Here, SELECT returns all data in the name column of the celebs table.
SELECTclause indicates that the statement is a querynamespecifies the column to query data formFROM celebsspecifies the name of the table to query data from
Query data from all columns in a table with SELECT.
1 |
SELECT * FROM celebs; |
* allows to select every column in a table without having to name each one individually.
SELECT statements always return a new table called the result set.
UPDATE
1 |
UPDATE celebs SET age = 22 WHERE id = 1; |
UPDATE statement edits a row in the table. To change existing records.
UPDATEedits a row in the tablecelebstable nameSETindicates the column to editWHEREindicates which row(s) to update with the new column value.
ALTER
1 |
ALTER TABLE celebs ADD COLUMN twitter_handle TEXT; |
The ALTER TABLE statement added a new column to the table.
ALTER TABLElets you make the specified changescelebsname of tableADD COLUMNlets you add a new column to a tabletwitter_handlename of new column being addedTEXTdata type
NULLis a special value in SQL that represents missing or unknown data.
DELETE
1 |
DELETE FROM celebs WHERE twitter_handle IS NULL; |
DELETE FROM statement deletes one or more rows from a table.
DELETE FROMlets you delete rows from a tableWHERElets you select which rows you want to delete.IS NULLa condition in SQL that returns true when the value isNULLand false otherwise




近期评论