SJammy
12/3/2019 - 11:55 AM

MANIPULATION

MANIPULATION
Review
Congratulations! We’ve learned six commands commonly used to manage data stored in a relational database and how to set constraints on such data. What can we generalize so far?

SQL is a programming language designed to manipulate and manage data stored in relational databases.

A relational database is a database that organizes information into one or more tables.
A table is a collection of data organized into rows and columns.
A statement is a string of characters that the database recognizes as a valid command.

CREATE TABLE creates a new table.
INSERT INTO adds a new row to a table.
SELECT queries data from a table.
ALTER TABLE changes an existing table.
UPDATE edits a row in a table.
DELETE FROM deletes rows from a table.
Learn SQL : Manipulation
Column Constraints

Column constraints are the rules applied to the values of individual columns:

PRIMARY KEY constraint can be used to uniquely identify the row.
UNIQUE columns have a different value for every row.
NOT NULL columns must have a value.
DEFAULT assigns a default value for the column when no value is specified.
There can be only one PRIMARY KEY column per table and multiple UNIQUE columns.

CREATE TABLE student (
  id INTEGER PRIMARY KEY,
  name TEXT UNIQUE,
  grade INTEGER NOT NULL,
  age INTEGER DEFAULT 10
);
CREATE TABLE
Statement

The CREATE TABLE statement is used to create 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
Statement

The INSERT INTO statement is used to add a new record (row) to a table.

It has two forms as shown in the code block:

Insert values based on the order of the columns in the table.
Define the columns to insert values into.
-- Insert into columns in order:
INSERT INTO table_name
VALUES (value1, value2, value3);

-- Insert into columns by name:
INSERT INTO table_name (column1, column2, column3)
VALUES (value1, value2, value3);
ALTER TABLE
Statement

The 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 to a table.

-- Syntax:
ALTER TABLE table_name
ADD column_name datatype;

-- Example:
ALTER TABLE employees
ADD first_name TEXT;
DELETE
Statement

The DELETE statement is used to delete records (rows) in a table. This statement does not delete the whole table.

Inside, 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_name
WHERE some_column = some_value;
UPDATE
Statement

The UPDATE statement is used to edit records (rows) in a table. It usually includes a SET clause that indicates the column to edit and a WHERE clause for specifying which record(s) should be updated.

UPDATE table_name
SET column1 = value1, column2 = value2
WHERE some_column = some_value;