Resource from codecademy.
1 |
CREATE TABLE table_nam( |
Create
1 |
CREATE TABLE celebs ( |
CREATE TABLE
clause tells SQL that you want to create a new tablecelebs
name of the table(id INTEGER, name TEXT, age INTEGER)
a list of parameters defining each column in the table and its data type
id
is the first column in the table.It stores values of data typeINTEGER
name
stores values of data typeTEXT
age
stores 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 INTO
clause adds the specified row/rowscelebs
name of the table the row is added to(id, name, age)
is a parameter identifying the columns that data will be inserted intoVALUES
indicates the data being inserted(1, 'William Turner, 21')
is a parameter identifying the values being inserted
1
insert intoid
columnWilliam Turner
inserted intoname
column21
inserted intoage
column
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.
SELECT
clause indicates that the statement is a queryname
specifies the column to query data formFROM celebs
specifies 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.
UPDATE
edits a row in the tablecelebs
table nameSET
indicates the column to editWHERE
indicates 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 TABLE
lets you make the specified changescelebs
name of tableADD COLUMN
lets you add a new column to a tabletwitter_handle
name of new column being addedTEXT
data type
NULL
is 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 FROM
lets you delete rows from a tableWHERE
lets you select which rows you want to delete.IS NULL
a condition in SQL that returns true when the value isNULL
and false otherwise
近期评论