Column constraints are the rules applied to the values of individual columns:
PRIMARY KEYconstraint can be used to uniquely identify the row.
UNIQUEcolumns have a different value for every row.
NOT NULLcolumns must have a value.
DEFAULTassigns a default value for the column when no value is specified.
There can be only one
PRIMARY KEY column per table and multiple
CREATE TABLE student (id INTEGER PRIMARY KEY,name TEXT UNIQUE,grade INTEGER NOT NULL,age INTEGER DEFAULT 10);
CREATE TABLE statement creates a new table in a database. It allows one to specify the name of the table and the name of each column in the table.
CREATE TABLE table_name (column1 datatype,column2 datatype,column3 datatype);
INSERT INTO statement is used to add a new record (row) to a table.
It has two forms as shown:
-- Insert into columns in order:INSERT INTO table_nameVALUES (value1, value2);-- Insert into columns by name:INSERT INTO table_name (column1, column2)VALUES (value1, value2);
ALTER TABLE statement is used to modify the columns of an existing table. When combined with the
ADD COLUMN clause, it is used to add a new column.
ALTER TABLE table_nameADD column_name datatype;
DELETE statement is used to delete records (rows) in a table. The
WHERE clause specifies which record or records that should be deleted. If the
WHERE clause is omitted, all records will be deleted.
DELETE FROM table_nameWHERE some_column = some_value;
UPDATE statement is used to edit records (rows) in a table. It includes a
SET clause that indicates the column to edit and a
WHERE clause for specifying the record(s).
UPDATE table_nameSET column1 = value1, column2 = value2WHERE some_column = some_value;
AND operator allows multiple conditions to be combined. Records must match both conditions that are joined by
AND to be included in the result set. The given query will match any car that is blue and made after 2014.
SELECT modelFROM carsWHERE color = 'blue'AND year > 2014;
Columns or tables can be aliased using the
AS clause. This allows columns or tables to be specifically renamed in the returned result set. The given query will return a result set with the column for
name renamed to
SELECT name AS 'movie_title'FROM movies;
OR operator allows multiple conditions to be combined. Records matching either condition joined by the
OR are included in the result set. The given query will match customers whose state is either
SELECT nameFROM customersWHERE state = 'CA'OR state = 'NY';
% wildcard can be used in a
LIKE operator pattern to match zero or more unspecified character(s). The given query will match any movie that begins with
The, followed by zero or more of any characters.
SELECT nameFROM moviesWHERE name LIKE 'The%';
SELECT * statement returns all columns from the provided table in the result set. The given query will fetch all columns and records (rows) from the
SELECT *FROM movies;
_ wildcard can be used in a
LIKE operator pattern to match any single unspecified character. The given query will match any movie which begins with a single character, followed by
SELECT nameFROM moviesWHERE name LIKE '_ove';
ORDER BY clause can be used to sort the result set by a particular column either alphabetically or numerically. It can be ordered in two ways:
DESCis a keyword used to sort the results in descending order.
ASCis a keyword used to sort the results in ascending order (default).
SELECT *FROM contactsORDER BY birth_date DESC;
LIKE operator can be used inside of a
WHERE clause to match a specified pattern. The given query will match any movie that begins with
Star in its title.
SELECT nameFROM moviesWHERE name LIKE 'Star%';
Unique values of a column can be selected using a
DISTINCT query. For a table
contact_details having five rows in which the
city column contains Chicago, Madison, Boston, Madison, and Denver, the given query would return:
SELECT DISTINCT cityFROM contact_details;
BETWEEN operator can be used to filter by a range of values. The range of values can be text, numbers, or date data. The given query will match any movie made between the years 1980 and 1990, inclusive.
SELECT *FROM moviesWHERE year BETWEEN 1980 AND 1990;
LIMIT clause is used to narrow, or limit, a result set to the specified number of rows. The given query will limit the result set to 5 rows.
SELECT *FROM moviesLIMIT 5;
Column values can be
NULL, or have no value. These records can be matched (or not matched) using the
IS NULL and
IS NOT NULL operators in combination with the
WHERE clause. The given query will match all addresses where the address has a value or is not
SELECT addressFROM recordsWHERE address IS NOT NULL;
WHERE clause is used to filter records (rows) that match a certain condition. The given query will select all records where the
SELECT titleFROM libraryWHERE pub_year = 2017;