Cheatsheets / SQL and Databases for Web Development
SQL: Creating, Updating, and Deleting
Data
Column Constraints
Column constraints are the rules applied to the values
of individual columns: CREATE TABLE student (
id INTEGER PRIMARY KEY,
●
PRIMARY KEY constraint can be used to name TEXT UNIQUE,
uniquely identify the row. grade INTEGER NOT NULL,
●
UNIQUE columns have a di erent value for age INTEGER DEFAULT 10
every row. );
●
NOT NULL columns must have a value.
●
DEFAULT assigns a default value for the
column when no value is speci ed.
There can be only one PRIMARY KEY column per
table and multiple UNIQUE columns.
CREATE TABLE Statment
The CREATE TABLE statement creates a new table
in a database. It allows one to specify the name of the CREATE TABLE table_name (
table and the name of each column in the table. column1 datatype,
column2 datatype,
column3 datatype
);
INSERT Statement
The INSERT INTO statement is used to add a new
record (row) to a table. -- Insert into columns in order:
It has two forms as shown: INSERT INTO table_name
VALUES (value1, value2);
● Insert into columns in order.
● Insert into columns by name. -- Insert into columns by name:
INSERT INTO table_name (column1,
column2)
VALUES (value1, value2);
ALTER TABLE Statement
The ALTER TABLE statement is used to modify the
columns of an existing table. When combined with the ALTER TABLE table_name
ADD COLUMN clause, it is used to add a new ADD column_name datatype;
column.
/
DELETE Statement
The DELETE statement is used to delete records
(rows) in a table. The WHERE clause speci es which
DELETE FROM table_name
record or records that should be deleted. If the WHERE some_column = some_value;
WHERE clause is omitted, all records will be deleted.
UPDATE Statement
The UPDATE statement is used to edit records (rows)
in a table. It includes a SET clause that indicates the
UPDATE table_name
SET column1 = value1, column2 = value2
column to edit and a WHERE clause for specifying the
record(s).
WHERE some_column = some_value;