Manipulation
Create
CREATE statements allow us to create a new table in the database.
The statement below creates a new table named celebs:
CREATE TABLEis a clause that tells SQL you want to create a new table.celebsis the name of the table.(id INTEGER, name TEXT, age INTEGER)is a list of parameters defining each column, or attribute in the table and its data type:idis the first column in the table. It stores values of data type INTEGERnameis the second column in the table. It stores values of data type TEXTageis the third column in the table. It stores values of data type INTEGER
Insert
The INSERT statement inserts a new row into a table, you can use it when you want to add new records.
The statement below enters a record for Justin Bieber into the celebs table:
INSERT INTOis a clause that adds the specified row or rows.celebsis the table the row is added to.(id, name, age)are parameters identifying the columns that data will be inserted into.VALUESis a clause that indicates the data being inserted.(1, 'Justin Bieber', 22)are parameters identifying the values being inserted.1: an integer that will be added to id column;'Justin Bieber': text that will be added to name column;22: an integer that will be added to age column.
To go further
It is possible to insert multiple rows at once using one INSERT statement:
INSERT INTO MyTable
( Column1, Column2, Column3 )
VALUES
('John', 123, 'Lloyds Office'),
('Jane', 124, 'Lloyds Office'),
('Billy', 125, 'London Office'),
('Miranda', 126, 'Bristol Office');
Select
SELECT statements are used to fetch data from a database.
In the statement below, SELECT returns all data in the name column of the celebs table.
SELECTis a clause that indicates that the statement is a query. You will useSELECTevery time you query data from a database.namespecifies the column to query data from.FROM celebsspecifies the name of the table to query data from. In this statement, data is queried from thecelebstable.SELECTstatements always return a new table called the result set.
You can also query data from all columns in a table with SELECT:
Important
* is a special wildcard character. It allows you to select every column in a table without having to name each one individually.
Alter
The ALTER TABLE statement adds a new column to a table.
The statement below adds a new column twitter_handle to the celebs table:
ALTER TABLEis a clause that lets you make the specified changes.celebsis the name of the table that is being changed.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;
Important
NULL is a special value in SQL that represents missing or unknown data. Here, the rows that existed before the column was added have NULL (∅) values for twitter_handle.
Update
The UPDATE statement edits a row in a table, you can use it when you want to change existing records.
The statement below updates the record with an id value of 4 to have the twitter_handle @taylorswift13.
UPDATEis a clause that edits a row in the table.celebsis the name of the table.SETis a clause that indicates the column to edit.twitter_handleis the name of the column that is going to be updated;@taylorswift13is the new value that is going to be inserted into thetwitter_handlecolumn.WHEREis a clause that indicates which row(s) to update with the new column value. Here the row with a4in theidcolumn is the row that will have thetwitter_handleupdated to@taylorswift13.
Delete
The DELETE FROM statement deletes one or more rows from a table. You can use it when you want to delete existing records.
The statement below deletes all records in the celeb table with no twitter_handle:
DELETE FROMis a clause that lets you delete rows from a table.celebsis the name of the table we want to delete rows from.WHEREis a clause that lets you select which rows you want to delete. Here we want to delete all of the rows where thetwitter_handlecolumnIS NULL.IS NULLis a condition in SQL that returnstruewhen the value isNULLandfalseotherwise.
Constraints
Constraints that add information about how a column can be used are invoked after specifying the data type for a column. They can be used to tell the database to reject inserted data that does not adhere to a certain restriction.
The statement below sets constraints on the celebs table:
CREATE TABLE celebs2 (
id INTEGER PRIMARY KEY,
name TEXT UNIQUE,
date_of_birth TEXT NOT NULL,
date_of_death TEXT DEFAULT 'Not Applicable'
);
PRIMARY KEYcolumns can be used to uniquely identify the row. Attempts to insert a row with an identical value to a row already in the table will result in a constraint violation which will not allow you to insert the new row.UNIQUEcolumns have a different value for every row. This is similar toPRIMARY KEYexcept a table can have many differentUNIQUEcolumns.NOT NULLcolumns must have a value. Attempts to insert a row without a value for aNOT NULLcolumn will result in a constraint violation and the new row will not be inserted.DEFAULTcolumns take an additional argument that will be the assumed value for an inserted row if the new row does not specify a value for that column.