Manipulation Flashcards
(30 cards)
SQL
Structured Query Language
Manage data stored in relational databases
Simple, declarative statements
Keeps data accurate and secure
Helps maintain the integrity of databases
Widely used across web frameworks and database applications
Relational Database
Organizes information into one or more tables
Table
Data organized into rows and columns
Column
Set of data values of a particular type
Field (in Tableau)
Row
A single record in a table
Data Types
All data stored in a relational database is of a certain data type
Most common data types:
- INTEGER
- TEXT
- DATE
- REAL
INTEGER
A positive or negative whole number
TEXT
A text string
DATE
Date formatted as YYYY-MM-DD
Dates must be surrounded by single quotes
REAL
A decimal value
Statement
Text that the database recognizes as a valid command
Statements always end in a semicolon
Clause
Clauses perform specific tasks in SQL
By convention, clauses are written in capital letters
AKA commands
Go at the beginning of a statement
Parameter
A list of columns, data types, or values that are passed to a clause as an argument
Within the parentheses
EXAMPLE
CREATE TABLE table_name ( column_1 data_type, column_2 data_type, column_3 data_type );
CREATE TABLE is the clause
table_name is the name of the table the command is applied to
(column_1 data_type…column_3 data_type) is the parameter; the list of column names and associated data types
; marks the end of the statement
Structure of SQL Statements
The structure varies
The number of lines DOES NOT matter
A statement can be written all on one line or split up across multiple lines to make it easier to read
CREATE
Allows us to create a new table in the database, from scratch
EXAMPLE
CREATE TABLE celebs ( id INTEGER, name TEXT, age INTEGER );
CREATE TABLE: you want to create a new table
celebs: the name of the table
(id INTEGER, name TEXT, age INTEGER): list of parameters defining each column
id INTEGER: 1st column with INTEGER data type
name TEXT: 2nd column with TEXT data type
age INTEGER: 3rd column with INTEGER data type
INSERT
Inserts a new row into a table
Use when you want to add new records
Usually used as INSERT INTO follow by VALUES clause to specify the values you are inserting
EXAMPLE
INSERT INTO celebs (id, name ,age)
VALUES (1, ‘Justin Bieber’, 22);
INSERT INTO: you’re adding a new row
celebs: into the celebs table
(id, name, age): parameters identifying data types
VALUES: data is being inserted
(1, ‘Justin Bieber’, 22): the values being inserted
1: integer added to id column
‘Justin Bieber’: text added to name column
22: integer added to age column
SELECT
Used to fetch data from a database
Use every time you query data from a database
SELECT statements always return a new table called the “result set”
Used in conjunction with FROM to specify which table
EXAMPLE
SELECT * FROM celebs;
SELECT: indicates statement is a query
*: special wildcard character that selects every column in a table
FROM celebs: from the celebs table
ALTER
Adds a new column to a table
Used in conjunction with ADD COLUMN clause
EXAMPLE
ALTER TABLE celebs
ADD COLUMN twitter_handle TEXT;
ALTER TABLE: you want to add a new column
celebs: name of table you want to change
ADD COLUMN: clause that lets you add a new column
twitter_handle: name of new column
TEXT: data type for new column
NULL: represents missing or unknown data; since no values are being inserted for existing rows, they all will have NULL values for this new column
UPDATE
Edits a row in a table
Use when you want to change existing records
Used in conjunction with SET and WHERE
- SET indicates the column to edit
- WHERE indicates which rows to update