Manipulation Flashcards

1
Q

SQL

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Relational Database

A

Organizes information into one or more tables

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Table

A

Data organized into rows and columns

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Column

A

Set of data values of a particular type

Field (in Tableau)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Row

A

A single record in a table

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Data Types

A

All data stored in a relational database is of a certain data type

Most common data types:

  • INTEGER
  • TEXT
  • DATE
  • REAL
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

INTEGER

A

A positive or negative whole number

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

TEXT

A

A text string

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

DATE

A

Date formatted as YYYY-MM-DD

Dates must be surrounded by single quotes

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

REAL

A

A decimal value

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Statement

A

Text that the database recognizes as a valid command

Statements always end in a semicolon

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Clause

A

Clauses perform specific tasks in SQL

By convention, clauses are written in capital letters

AKA commands

Go at the beginning of a statement

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Parameter

A

A list of columns, data types, or values that are passed to a clause as an argument

Within the parentheses

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

EXAMPLE

CREATE TABLE table_name (
     column_1 data_type,
     column_2 data_type,
     column_3 data_type
);
A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Structure of SQL Statements

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

CREATE

A

Allows us to create a new table in the database, from scratch

17
Q

EXAMPLE

CREATE TABLE celebs (
     id INTEGER,
     name TEXT,
     age INTEGER
);
A

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

18
Q

INSERT

A

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

19
Q

EXAMPLE

INSERT INTO celebs (id, name ,age)
VALUES (1, ‘Justin Bieber’, 22);

A

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

20
Q

SELECT

A

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

21
Q

EXAMPLE

SELECT * FROM celebs;

A

SELECT: indicates statement is a query

*: special wildcard character that selects every column in a table

FROM celebs: from the celebs table

22
Q

ALTER

A

Adds a new column to a table

Used in conjunction with ADD COLUMN clause

23
Q

EXAMPLE

ALTER TABLE celebs
ADD COLUMN twitter_handle TEXT;

A

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

24
Q

UPDATE

A

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
25
Q

EXAMPLE

UPDATE celebs
SET twitter_handle = ‘@taylorswift13’
WHERE id = 4;

A

UPDATE: you want to edit a row
celebs: table where you want to make the edit
SET: indicates the column to edit
twitter_handle: name of column that will be updated
‘@taylorswift13’: new value that will be inserted
WHERE: indicates which row yo update
4: row with id ‘4’ will be updated

26
Q

DELETE FROM

A

Deletes one or more rows from a table

Use when you want to delete existing records

27
Q

EXAMPLE

DELETE FROM celebs
WHERE twitter_handle IS NULL;

A
DELETE FROM: you want to delete rows
celebs: from the celebs table
WHERE: you're going to indicate the rows
twitter_handle ISNULL: delete all rows where twitter_handle column ISNULL
ISNULL: returns when the value is NULL
28
Q

CONSTRAINTS

A

Adds information about how a column can be used

Invoked after specifying the data type for a column

Can be used to tell the database to reject inserted data that does not adhere to a certain restriction

29
Q

EXAMPLE

CREATE TABLE celebs (
     id INTEGER PRIMARY KEY,
     name TEXT UNIQUE,
     date_of_birth TEXT NOT NULL,
     date_of_death TEXT DEFAULT 'Not Applicable'
);
A

PRIMARY KEY: uniquely identify the row; will not allow you insert row with an identical value already in the table

UNIQUE: columns have a different value for each row

NOT NULL: columns must have a value; rows without a value will not be inserted

DEFAULT: if no value specified, will take the assumed value in quotes

30
Q

CONSTRAINT EXAMPLES

A

PRIMARY KEY

UNIQUE

NOT NULL

DEFAULT ‘value here’