SQLMastery Flashcards

1
Q

What is a database?

A

database -

Collection of data stored in a format that can be easily accesssed

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

How do we manage our databases?

A

Using a software system -

Database Management System (DBMS)

How?

We connect to a DBMS, give it instructions like query / modify, DBMS executes instructions and sends back results

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

What two types of Database Management Systems (DBMS) do we have?

A

Relational and Non-Relational (noSQL)

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

How is data stored in relational databases?

A

In tables that are linked to eachother using relationships

Each table stores data about a specific object

SQL is language we use to work with these

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

What are the popular relational database management systems?

A

MySQL

SQL Server

Oracle

**all based on standard SQL specification

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

What is the most popular, open source database management system in the world?

A

MySQL

Used in this course!

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

Do noSQL (non relational database management systems) understand SQL?

A

NO!

NoSQL systems don’t understand SQL

They have their own query language!!

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

Where was SQL invented?

A

1970 at IBM

Structured English Query Language

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

What will we be learning in this course?

A

SQL (Query language) with MySQL (DB management)

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

What is covered in this course?

A

Essential Skills -

Retrieve, insert, update, delete data

(90% will apply to other DBMS)

Core skills -

summarize data, complex queries and subqueries, built in MySQL functions, views, stored procedues

(will apply to other DBMS)

Advanced skills -

triggers, events, transactions, concurrency

Top performer skills -

designing databases (systematic approach), indexing for high performance (speed queries), securing databases (user accounts, privledges)

**if you follow and complete excercise - can use SQL in any project and pass any interview questions

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

What is the first step to getting data from a database?

A

Select the database

query will be executed against database

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

Is SQL a case sensitive language?

A

No.

However, by convention:

SQL statments are uppercase,

lowercase for everything else

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

What is the SELECT clause?

A

Allows us to choose the columns

Select

* - all columns in a table

column_names - certain columns

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

How do we put a comment in SQL syntax?

A

double hyphen “ – “

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

Does order matter when using the SELECT statement (SELECT, FROM, WHERE, ORDER BY)?

A

Yes, it does:

SELECT – column or *

FROM table name

WHERE –filter data

ORDER BY – column to order entries

**cannot change the order of these clauses or get syntax error

**FROM, WHERE, ORDER BY ARE OPTIONAL

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

Can we use arithmetic operators with SQL?

A

Yes.

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

How is math executed in SQL?

A

Using order of operations

If you want to change order, can use parentheses

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

How can we change the name of a result column?

A

AS keyword

can give alias

can use ’ ‘ around word if has parentheses

Ex. ‘discount factor’

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

What is the DISTINCT keyword?

A

Removes duplicates

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

What is a SQL excercise?

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

What is the WHERE clause?

A

Used to filter data

Conditional Operaters:

>

<

<=

=

!= or <>

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

What is an excercise using the WHERE statement?

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

What is the AND operator?

A

Used to filter data based on multiple conditions

WHERE (condition) AND (condition)

**BOTH Conditions are TRUE

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

What is the OR operator?

A

WHERE (condition) OR (condition)

satisfy one condition only!

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

How can we combine AND, OR and WHERE clauses?

A

Combining logical operators!

Query execution engine evaluates in order:

AND operator evaluated first!!

Can use ( ) to change order and make code cleaner!

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

What is the NOT operator?

A

Changes expression:

WHERE NOT (birth_date >=‘1990-01-01’ OR points > 1000)

Becomes (flips signs):

WHERE (birth_date <=‘1990-01-01’ AND points < 1000)

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

What is an excercise using logical operators?

A

Can use an arithmetic expression in logical operators

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

What is the IN operator?

A

like chaining OR statements

Compare an attribute to a list of values

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

What is an excercise using the IN operator?

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

What is the BETWEEN operator?

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

What is an excercise using the BETWEEN operator?

A

Can use BETWEEN operator with dates, not only numbers!

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

What is the LIKE operator?

A

Allows us to search for string patterns

% any number of characters

_ single character

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

How can we get all the customers whose last name ends in “y”?

A

LIKE %y

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

What is an excercise using LIKE?

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

What is another excercise using the LIKE keyword?

A

Get all the customers whose phone numbers end in 9

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

What is the REGEX keyword?

A

regular expression

more powerful than LIKE statement

^ represents beginning of a string

$ represents end of a string

[a-h] multiple characters

represents two search criteria

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

What are excercises using the REGEX syntax?

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

What is the NULL keyword?

A

allows us to search for null values

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

What is an excercise using the NULL keyword?

A

get all orders that have not been shipped

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

How can we sort data in our SQL queries?

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

What is an excercise using ORDER BY?

A

Write a query produces result:

item #2, sorted by total price of item descending order

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

What is the LIMIT clause?

A

Allows us to limit number of entries retrieved

LIMIT clause should always come at end!

ORDER matters!

SELECT
FROM

WHERE
ORDER BY

LIMIT

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

How can we offset using LIMIT clause?

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

What is an excercise using the LIMIT clause?

A

Get top 3 customers (highest points)!

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

What is the JOIN clause?

A

Let’s us join tables

JOIN (table) ON (condition)

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

How do we select specific columns across multiple tables using the JOIN clause?

A

qualify them with specific table

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

How can we simplify our code using alias?

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

What is an excercise using the JOIN clause?

A

join the order_items table with the products table

display order ID, product ID, quantity and price

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

How can we join across databases?

A

Real world db admin must do this!

prefix with the database name

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

What is a self JOIN?

A

Joining a table with itself!

HOW?

pre-fix each column with an alias

use a different alias for self join table

Ex. join employee table with itself to get the name of the manager

JOIN reports_to column with employee_id column in employees table

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

How do we join multiple tables?

A

In real world, you’ll join 10 tables!

Ex. Join

orders table

w/ customers table

w/ order_status table

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

What’s an excercise using multiple table JOINS?

A

Show payments with name of client, payment method

JOIN

payments table

w/ clients table

w/ payment_methods table

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

What are compound JOIN conditions?

A

When we have a composite primary key

cannot uniquely identify an order by one column, can use two combined to uniquely identify

Ex. Order_Id and product_ID are duplicated

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

What is another way to write this query using implicit JOIN syntax?

A

Better to use explicit JOIN syntax!

Two queries are equivalent (explicit and implicit JOIN syntax)

HOWEVER:

Mosh, doesn’t recommend using implicity JOIN syntax

WHY?

if forget WHERE clause, get a cross join

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

When do we use an outer JOIN?

A

OUTER keyword is optional

Ex. When we want to see all customers, not only customers who have an order (JOIN condition is based on Order table)

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

What two types of outer JOIN do we have?

A

As a best practice use LEFT joins

avoid RIGHT joins

WHY?

makes code cleaner, easier to read

WHAT?

OUTER keyword is optional

LEFT - all records from left table returned whether JOIN ON condition is true or not (null if not)

RIGHT - all records from right table

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

What’s an excercise using outer JOIN?

A

JOIN products table with order_items table

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

How can we solve this issue?

A

Replace inner JOIN with left JOIN

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

When do we use self outer JOIN?

A

When we are doing a self join

and want to see all entries (even those that do not match our criteria)

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

What is the USING clause?

A

replaces ON clause

ONLY WORKS when column name is exactly the same across different tables!

EX. Cannot use Orders.status = Order_Statuses.order_status_id

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

How can the USING clause replace a compound JOIN?

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

Excercise: use the USING clause to write a query that produces order_date, client_name, amount and payment_method.

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

What are NATURAL JOINS?

A

ALT. Syntax for JOINS

Replaces USING and ON clause

HOWEVER,

not recommended!

WHY?

Dangerous because letting DB engine guess the JOIN

Can produce unexpected RESULTS!

DISCOURAGED FROM USING!!

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

What is a CROSS JOIN?

A

Combine every record from first table with second table

Explicit Synax is more clear!!

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

Excercise: DO a CROSS JOIN between shippers and products

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

What is the UNION operator?

A

Can combine results from multiple queries

Across same table or different tables

CAUTION:

Queries must return same # of columns or you’ll get an error

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

Excercise: Write a Query to product this report (UNION).

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

How do we INSERT a row into a table?

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

What is another way to write this INSERT statement?

A

Don’t need to explicitly state DEFAULT or NULL

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

How can we INSERT multiple rows in a table?

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

EXCERCISE: INSERT three rows into the products table

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

How do we insert data into multiple tables?

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

How can we quickly copy data from one table into another?

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

How do we select a subset from a table to copy into another table?

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

What is a subquery?

A

a SELECT statement used in another query (ex. INSERT)

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

What’s a really cool excercise using INSERT?

EXCERCISE: Create a copy of the invoices with a date in a new table, in new table, display client name.

A

JOIN with clients table to display client_name in new table

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

How do we update a single row (record) in a table?

A

Undo:

reset payment_total = DEFAULT, payment_date = NULL

*DEFAULT value is set to 0, MySQL will set

*can insert NULL into columns that accept NULL values

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

Can we use an expression to update a value in a row?

A

Yes

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

How can we update multiple rows in a table?

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

What’s an excercise with updating multiple rows?

WRITE SQL statement:

Give any customer born befor 1990 an extra 50 points

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

How can we use subqueries in an UPDATE statement?

A

subquery = SELECT statement in another query

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

What is an excercise using a subquery?

Ex. set the status of a customer to gold if they have over 3000 points.

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

How can we delete data in SQL?

A

DELETE FROM table

WHERE condition

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

How do we recreate our databases?

A

execute .sql for creating databases

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

What is a function?

A

A piece of pre-built code we can re-use!

MySQL comes with a bunch of pre-built functions.

Ex. Aggregate Functions - take a series of values and aggregate to produce a single value

MAX ( )

MIN ( )

AVG ( )

SUM ( )

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

What are aggregate functions?

A

Pre-built code we can use!

Aggregate Functions -

take a series of values and aggregate to produce a single value

MAX ( )

MIN ( )

AVG ( )

SUM ( )

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

How can we find the MAX, MIN and AVG invoice total?

A

Use pre-built aggregate functions!

MAX ( )

MIN ( )

AVG ( )

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

Can we use MAX ( ) on non-numeric values?

A

Yes. Can use on dates and strings!

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

What is the COUNT function?

A

Returns count of items with non NULL values

Count(*) give all items count irrespective of NULL values

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

What is the SUM function?

A

give total

can use an expression

Ex. (invoice_total * 1.1) will multiple each row (value) by 1.1 then add them!

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

Can we use aggregate functions on filtered data?

A

Yes, using WHERE clause

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

How can we exclude duplicate data?

A

DISTINCT keyword

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

What is an excercise using aggregate functions?

Generate this table

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

What is the GROUP BY keyword using aggregate functions?

A

can see total sales per client

group by one or more columns

to group data

Ex. by client_id, total sales for each client

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

How can we get the total invoices per client for the first half of the year?

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

How can we see the total sales for each state and city?

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

What is an excercise using the GROUP BY clause?

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

When do we use the HAVING clause?

A

to filter data after we group our rows

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

What is the difference between the HAVING clause and the WHERE clause?

A
  1. the HAVING clause is used to filter data AFTER grouping

the WHERE clause is used to filter data BEFORE grouping

  1. HAVING clause the column must be included in SELECT clause

using a WHERE clause can select any column

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

How can we write a compound search condition in the HAVING clause?

A

can use one or more conditions

HAVING column must be included in SELECT clause

using a WHERE

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

How can we get the customers located in Virginia who have spent more than $100? (use HAVING clause)

A

As a rule of thumb:

whenever you have an aggregate function in a SELECT statement, grouping data, group by all columns in SELECT clause

This excercise very similar to REAL WORLD queries!!

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

What is the with ROLLUP operator?

A

powerful operator for summarizing data!!

Calculates summary for each group and entire result set

ONLY AVAILABLE IN MYSQL (NOT IN ORACLE, other DBMS!!)

Ex. sum for San Francisco, sum for all cities in CA, finally sum for all states (entire result set)

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

Write a query to produce this result (ROLLUP operator):

Total payments received for each payment method (cash, credit card, wire transfer, PayPal)

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

What is a subquery?

A

Can write subqueries in SELECT, FROM or WHERE clauses!

A statement within another SQL query

writing complex queries involves subqueries!!

Ex. get products more expensive than lettuce

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

How can we get all the products that are more expensive than lettuce?

A

subquery!

MySQL will evaluate subquery first, then return result to our main query!

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

How can we find the employees who earn more than average? (HINT: subquery)

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

How can we find the products that have never been ordered?

A
  1. Get unique list of products ordered from the order_items table (subquery)
  2. Get all products NOT IN subquery

**In this example, subquery is returning a list of objects!

(vs. single value)

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

How can we write a query to find the clients without invoices?

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

How can we re-write this subquery as a JOIN?

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

When should we use a JOIN vs. a subquery?

A

Performance vs. readability

Some queries get executed faster! (execution order)

query that is most readable!

Ex. Clients without invoices, first approach is more readable!

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

How can we find the customers who have ordered lettuce using both a subquery and a JOIN?

A

Second query is more readable

Why?

There is a natural relationship between customers, orders and order_items!

Filter clearly explains what customers!

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

How can we select all invoices larger than client 3’s max invoice? (HINT: ALL keyword)

A

subqueries!

ALL keyword

Subquery returns list of values

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

What is the ANY keyword?

A

= ANY is equivalent to IN

Ex. Select clients with at least two invoices

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

How can we write a query to return the employees whose salary is above average in their office?

A

for each employee, will execute subquery (returns value)

Correlated subquery!

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

What is a correlated subquery?

A

a subquery that references the outer query!

It’s executed multiple times! (for each row)

can be slow!

But very powerful!

Vs.

uncorrelated subquery - executed only once! value passed to outter query

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

How can we get the invoices that are larger than the client’s average invoice amount? (HINT: Correlated subquery)

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

What is the EXISTS operator?

A

Ex. select clients that have an invoice

For each client in client table, will check to see if there is a record that matches the subquery

Efficiency?

using the IN operator returns a list (could be millions long)

more efficient to use the EXISTS operator - doesn’t return a list, just a true/false, if true returns the query data

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

When is it more efficient to use the EXISTS operator vs. IN operator?

A

If the subquery after IN operator produces a large results set

more efficient to use EXISTS operator

WHY?

Using the EXISTS operator, subquery doesn’t return a result set to the outter query to the main query

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

How can we find the products that have never been ordered?

(HINT: use EXISTS operator)

A

For each product, checks if condition is true

if we have a product in order items table, subquery returns true, NOT operator becomes FALSE, item not in result set!

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

How can we use subqueries in the SELECT clause?

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

How can we get the total sales, average and difference for each customer?

A

SELECT clause subqueries

Virtual table!

Can store in memory and JOIN with other tables using client_id!

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

How do we use a subquery in the FROM clause?

A

Reserve SELECT clause in FROM clause for simple queries

Why?

Makes our main query complex

Views can replace this!

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

What is the ROUND function?

A

A built in function for working with numeric values

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

What is the TRUNCATE function?

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

What is the CEILING function?

A

Returns the smallest integer greater than or equal to a number

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

What is the FLOOR function?

A

returns largest integer less than or equal to number

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

What is the ABS function?

A

calculates absolute value of a number, always returning positive

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

What is the RAND ( ) function?

A

returns a random number between 0 - 1

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

What is the LENGTH ( ) function?

A

returns length of a string

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

What is the UPPER ( ) function?

A

also have LOWER ( ) convert to lowercase!

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

What is the LTRIM ( ) function?

A

useful for trimming data from the user!

removes whitespace on left

132
Q

What is the RTRIM ( ) function?

A

removes any whitespaces from right side

133
Q

What is the TRIM ( ) function?

A

removes whitespaces from left or right

134
Q

What is the LEFT ( ) function?

A

returns the specified number or characters from left

135
Q

What is the RIGHT ( ) function?

A

returns specified characters from the right

136
Q

What is the SUBSTRING ( ) function?

A

returns a portion of a string given a starting index and char length

137
Q

What is the LOCATE ( ) function?

A

returns position of first occurance of char or sequence of chars

if char doesn’t exist, get 0

138
Q

What is the REPLACE ( ) function?

A
139
Q

What is the CONCAT ( ) function?

A

combines two strings

140
Q

What are the NOW( ), CURDATE ( ) , and CURTIME ( ) functions?

A

functions for working with date/time in MySQL!

141
Q

How do we get the current DAY, MONTH or YEAR, HOUR, MINUTE?

A

YEAR ( NOW ( ) )

MONTH ( NOW( ) )

HOURS ( NOW ( ) )

142
Q

What is the DAYNAME ( ) function?

A

returns the string day of week

143
Q

What is the MONTHNAME ( ) function?

A
144
Q

What is the EXTRACT keyword?

A

part of standard SQL language

145
Q

How can we get all the orders placed before the current year?

A
146
Q

What is the DATE_FORMAT operator?

A

Used for displaying dates to users!!

Takes two values (Date, format string)

%Y four digit year

%d day

%M month name

MySQL has more!!

EX. %H:%i for hour and minutes!

147
Q

What is the TIME_FORMAT operator?

A

used to format dates and times!

Ex. DATE_FORMAT | TIME_FORMAT

148
Q

What’s an example of performing a calculation on a date or time?

A

add a day or hour to a date

calculate difference between two dates

How?

149
Q

What is the DATE_ADD operator?

A

Used to add a day or time to a date

Ex. add one day to current date time

150
Q

What is the DATE_SUB function?

A

subtract date from a date time value

151
Q

How do we calculate the difference between two dates?

A

DATEDIFF

152
Q

What is TIME_TO_SEC function?

A

returns time in seconds since midnight

used to calc difference between two times

153
Q

What is the IFNULL function?

A

Allows us to substitue a value IF NULL

154
Q

What is the COALESCE function?

A

Allows us to return one value or another value or IF both NULL return a custom value

Supply a list of values, returns the first NOTNULL value in the list

155
Q

How do we produce this table?

A

IFNULL

156
Q

What is the IF function?

A

IF (expression, first, second)

evaluates an expression, if TRUE, returns first value

IF false, returns second value

157
Q

How do we put a label on a product that has been ordered many times (more than once)?

HINT: Use IF function

A
158
Q

What is the CASE operator?

A

Used to test multiple expressions (like IF, ELSE IF, ELSE)

Syntax:

CASE

WHEN Expression

WHEN Expression

ELSE Expression

END

159
Q

How do we produce this table?

HINT: CASE operator

A
160
Q

What are views?

A

Complex queries in SELECT, WHERE, etc.

Can save queries in a view, reuse in many places

HOW?

creates a view object (doesn’t return a table)

can select view to see table data!

Can use view like a table!

161
Q

What can we do with views?

A

Use a view just like a table!

Can JOIN with any other tables with client_id

Ex.

SELECT *

FROM sales_by_client

WHERE total_sales > 500

EX.

SELECT *

FROM sales_by_client

JOIN clients USING (client_id)

162
Q

Why are views so powerful?

A

They can greatly simplify our future queries

Don’t have to write a SELECT statement from scratch!!

Can re-use complex queries

163
Q

How are views like virtual tables?

A

They don’t store data!

Data is actually stored in tables

views only provide a view to underlying table

Simplifying our future queries!!

164
Q

How can we create a view to display a client’s balance?

(HINT: CREATE VIEW)

A
165
Q

What if our view has an error?

A

We can delete a view if we realize our query has a problem!

Drop it. Recreate it

Use REPLACE keyword (prefered way)

166
Q

What is a common practice for SQL queries behind views?

A

Save queries in SQL file (view)!

Check these SQL files into a GIT repository

Share repository with others

So, everyone can recreate databases on their machines!

167
Q

Where else can we use views?

(HINT: INSERT, SELECT, DELETE)

A

Can use them in DELETE, INSERT, SELECT

only if….updatable view! can update data in it

  1. No DISTINCT keyword is used in the view
  2. The view has no Aggregate Functions (MIN, MAX, AVERAGE)
  3. Doesn’t use GROUP BY / HAVING
  4. Doesn’t use the UNION operator
168
Q

What is an updatable view

A

INSERT, UPDATE, DELETE

Can use the view

  1. No DISTINCT keyword is used in the view
  2. The view has no Aggregate Functions (MIN, MAX, AVERAGE)
  3. Doesn’t use GROUP BY / HAVING
  4. Doesn’t use the UNION operator

Ex. Invoice total and payment total but we don’t have a column for balance! Let’s make a view : )

169
Q

Can we use this view in an INSERT, UPDATE, DELETE operation?

A

Yes! Its an updatable view!

It doesn’t have:

DISTINCT keyword

Aggregate Functions (AVG, MIN, MAX, etc)

UNION OPERATOR

Therefore:

We can update data in this table

Can delete data from the view

170
Q

When do we use views to insert, update or delete data?

A

When we don’t have access to underlying tables (due to permissions)

But views must be updatable!!

171
Q

What is the WITH CHECK OPTION?

A

Allows us to prevent an INSERT or UPDATE statement from excluding rows from our view

HOW?

if you try to modify data that would update the view to exclude the data, you will get an error!

172
Q

What are the other benefits to using views?

A
  1. Simplify queries - can save for future
  2. Reduce impact of changes - views provide an abstraction over our tables. We can bring a column or column name that no longer exists in a table back using views so that keeps our queries against the view from breaking!
  3. Restrict access to data in underlying table - can provide data security! You may exclude some columns in underlying table in view, or exclude some rows from the view. This makes user not allowed to modify underlying table columns!
173
Q

Should we write SQL code inside our application code (Java, C#, Python)?

A

No!

Don’t write SQL in application code!

Why?

  1. It makes your code messy and hard to maintain - don’t mix SQL with java or python!
  2. Compilation Steps - if you write SQL queries in application code, will have to recompile application code everytime you make a change in your SQL query
174
Q

What is a stored procedure?

A

A database object that contains a block of SQL code

in application code, call these procedures to get or store data!

Why?

Executed faster sometimes due to optimization

Enforce data security (remove direct access to tables or stored procedures)

Where we should write our SQL queries!

175
Q

How do we create stored procedures in MySQL?

A

in SQL server, don’t have to change default delimiter

176
Q

What do we normally call in our application code (Java, C#, python)?

A

Stored procedures!

But sometimes you can call it from SQL code

177
Q

How can we return all the clients with balance greater than zero?

(HINT: Use Stored Procedure)

A
178
Q

How can we create stored procedures in an easier way?

A

use MySQL Workbench

Right click stored procedures

Don’t have to worry about changing the delimiter!

Click Apply!

Now we have a new stored procedure

179
Q

How can we drop a stored procedure if we made a mistake?

A

Save .sql file with dropped procedure and commit to GIT

DROP PROCEDURE IF EXISTS safe way!

180
Q

How can we add parameters to stored procedures?

A

all parameters are required in SQL

181
Q

How can we create a stored procedure for getting the invoices by client?

A
182
Q

How do we return the customers in california by default if no state is specified in our stored procedures?

A
183
Q

How do we return all the customers if no state is passed to our stored procedure?

A
184
Q

How can we make this code look more professional?

A
185
Q

How can we give default values if one or both parameters in our stored procedure are null?

A
186
Q

How can we write a stored procedure to allow customers to make a payment?

(HINT: invoice id, payment amount, payment date params)

A
187
Q

How can we make a payment amount for $100 on Jan 1. 2019?

A
188
Q

How can we validate data so that we prevent a stored procedure from putting invalid data in our database?

A

keep validation logic to absolute min

Why?

Too much of a good thing is a bad thing

it will end up making our stored procedures bloated!

189
Q

When should we do parameter validation?

A

As a last resort in case someone calls stored procedure without validating input

Much easier to validate input in application when user inputting data

before, hitting the database

190
Q

How can we output data from a stored procedure?

A

avoid these as a recommendation

191
Q

What types of variables do we have in SQL?

A

User (session) variables - used when calling stored procedures with output parameters, in memory during entire client session, SET @variable

local variable - variables defined inside a stored procedure or function, as soon as stored procedure finishes execute, they’re freed up, used in calculations

Ex. using local variables in stored procedures - as soon as finish executing stored procedure, they leave memory

192
Q

What cannot a function return?

A

A result set with multiple columns! (like stored procedures can)

Only can return a single value!

How?

right click on functions

Ex. Calculate risk factor per client

193
Q

How do we create a function?

A
194
Q

How can we use our function get_risk_factor_for_client in a SELECT statement?

A

good practice to save functions .sql and push to GIT repo

DROP FUNCTION IF EXISTS to drop functions!

195
Q

What should we be aware of regarding conventions?

A

Different organizations adopt different conventions

Don’t sweat it, adopt convention they’re already using

Don’t argue with colleagues on conventions already there!!

Just use them!

Whatever convention is there, stick to it!

Ex. procGetRiskFactor vs. getRiskFactor vs. get_risk_factor

Ex. DELIMITER $$ vs. //

196
Q

What is a trigger?

A

A block of SQL code that automatically gets executed before or after an insert, update or delete statement

ie.

a trigger that updates a table row if another table’s value is updated (observer pattern)

Uses?

Enforce data consistency

Ex. fire trigger after Insert

197
Q

How can we create a trigger that gets fired after we make a payment that reduces the total payment amount?

A

When a row is deleted from payments table, invoices table payment_total is automatically updated (subtract value of deleted payment)

198
Q

How can we view our triggers in MySQL?

A

SHOW TRIGGERS LIKE ‘table_after_etc%’

199
Q

How can we drop a trigger?

A

DROP TRIGGER IF EXISTS

200
Q

What are other uses for triggers?

(HINT: logging changes to database)

A

logging changes in data as audits

Ex. Whenever someone inserts/deletes can log it somewhere to see who made changes when

In real world,

you’ll log changes in many tables,

you’ll learn how to design general audit tables in database design!

201
Q

What is a task?

A

A block of SQL code that gets executed on a schedule

Very powerful!

Ex. once or a regular basis (1x per month, 1x per day at 10am), etc.

Uses?

automated database maintenance tasks such as copying data from table to archive, aggregating data for reports

202
Q

What is MySQL event scheduler?

A

A process that runs in the background

Constantly looks for events to execute

Ex. SET GLOBAL event_schedule = ON

203
Q

How can we create an event using MySQL event scheduler?

A
204
Q

How can we view, alter or drop events?

A
205
Q

What is a transaction?

A

A group of SQL statements that represent a single unit of work

all statements complete successfully or transaction fails!

Use?

make multiple changes to a database and want all to succeed or fail together as a single unit

Ex. store order with item - if order record is inserted, server crashes, end with incomplete order, database not consistent

206
Q

What are the key properties of transactions?

A

1. Atomicity - unbreakable, each transation is a single unit or work, all statements successful and commit or it’s rolled back and undone

2. Consistency - database always remains in consistent state

3. Isolation - transations are protected from eachother, cannot interfere with eachother, if multiple transactions try to update, others have to wait for transaction to complete

4. Durability - changes made by transactions are permenant

207
Q

How do we create a transaction?

A
208
Q

In the real world, how many users try to access data in the database?

A

Many users at the same time

Soln?

Concurrency

Why?

if a user is trying to access data that another user is updating, could cause problems

209
Q

How does MySql handle concurrency problems?

A

It locks the data

Other users have to wait until transaction is completed before accessing the data

210
Q

What are some of the problems with concurrency?

A
  1. Lost updates - two transactions try to update the same data and no locks are used, later transaction overrides changes from previous

Ex. two transactions try to update a customer concurrently, one updates points, other tries to update state of customer, second transaction commits first and the points update is lost

Soln - locks

  1. Dirty reads - transaction reads data that hasn’t been committed yet

Ex. Transaction A updates points but before a commit, transaction B reads the data that is later rolled back by Transaction A

Soln - Transaction level isolation

Ex. read committed, only read data that has been committed

  1. Non-repeating reads - reading data twice - once initially then again after data is changed by another transaction before a commit, causing inconsistent reads

Ex. transaction A reads points, before completed, transaction B updates points, Transaction A reads points again but they’re different, data is inconsistent

Soln - repeatable reads - data read in a transaction is repeatable even if a change is made so data changes are not visible during a transaction (creates a snapshot)

  1. Phantom Reads - transaction A queries a list of customers, Transaction B updates a customer that now meets Transaction A’s criteria, but the data is not included

Soln - Serializable - transaction will be aware of changes made to data by other transaction and wait for them to complete first, more users, more waits, can hurt performance and scaleability (reserve for only a critical need)

211
Q

What happens as we increase the level of isolation?

A

The more performance and scaleability issues we have

More locks involved in isolation causing transaction to wait

the lower the level of isolation the higher the level of concurrency and higher performance level BUT higher concurrency problems (more users can access data at the same time)

Default level in MySQL is repetable read

Stick to default for most situations!

Read Uncommited / Read Commited - data rarely updated

212
Q

How do we set the transaction isolation level?

A
213
Q

What is a deadlock?

A

A classical problem in database

different transactions cannot complete

each hold a lock the other needs

214
Q

How can we minimize deadlocks?

A
  1. Look at your transactions - look at the order of your updates, follow same order when updating multiple records
  2. Keep transactions small and short so they’re unlikely to collide with other transactions,
  3. Schedule transactions to run during non peak hours when there are not a lot of users
215
Q

Where are we going to use datatypes?

A

In next section - designing databases

String types

numeric types - numbers

date and time types - dates

blob types - storing binary

spatial types - geometric values

216
Q

What are the string types?

A

Char(x) - fixed length like abbreviate of states

VARCHAR(x) - variable length, username, passwords, emails, addresses (max 65, 535 characters 64kb)

*can be indexed

MEDIUMTEXT (x) - JSON, .CSV, short/medium books (Max 16MB)

LONGTEXT(x) - textbooks or years or log files (Max 4GB)

TINYTEXT (x) - 255 bytes max

TEXT (x) - 64KB max but cannot be indexed

**can store zip codes and phone numbers

WHY?

Because not used in mathematical calculations

store hyphens and other delimiters

Conventions?

consistency helps maintain database

VARCHAR (50) - usernames, passwords (short strings)

VARCHAR (255) - addresses (medium strings)

Bytes?

English - 1 byte per char

European / middle eastern - 2 bytes

Asian - 3 bytes

217
Q

What are the Integer types?

A

used to store whole numbers

TINYINT = 1 byte (-128, 127)

UNSIGNED TINYINT = (0, 255) prevent negative value from being stored in database (ex. age)

SMALLINT = 2bytes (-32k, 32k)

MEDIUMINT = 3bytes (-8M, 8M)

INT = 4bytes (-2B, 2B)

BIGINT = 8bytes (-9z, 9z)

*if data type is too small, MySQL will throw an error

*ZEROFILL - numbers will be padded with zeros, not how it’s stored only displayed

Recommendation?

Try to use the smallest data type that suits your needs

consciously choose your bytes

WHY?

keep database small and keep queries fast

218
Q

What three data types do we use for storing decimal numbers?

A

DECIMAL (p,s) - monetary values

p - max digits including decimal (1-65)

s - max digits to right of decimal

Synonyms

DEC

NUMERIC

FIXED

**don’t use, stick to DECIMAL (p,s) but be aware of these

Others -

used in scientific calculations

FLOAT 4bytes

DOUBLE 8bytes

*store approximations, not actual numbers, low precision

219
Q

How do we store “yes” or “no” values?

A

BOOL

BOOLEAN

*synonym for TINYINT

TRUE = 1

FALSE = 0

220
Q

What are ENUMS?

A

Restrict values of a column to specific list of strings

ENUM(‘small’, ‘medium’, ‘large’)

SET(…)

Ex. only allow values “small” “medium” or “large” to be inserted in a column

Recommendations?

Generally, bad practice, should avoid

Why?

Changing members of ENUM is expensive - MySQL will rebuild entire table if updated, can’t get additional information, tricky to work with in application drop down, cannot reuse values in other tables causing duplications and less maintainable code

Soln?

Create a lookup table - separate table with values, can reuse table, can get all sizes with single SELECT statement, can feed drop down lists

Ex. Payment_method

221
Q

What are the four data types for storing date and time values?

A

DATE - store date without time

TIME - time value

DATETIME - 8 bytes (beyond 2038)

TIMESTAMP - 4 bytes (only store up to 2038)

Ex. show when row was last inserted

YEAR

222
Q

What are blob types?

A

Store large binary data

Images, videos, PDFs

TINYBLOB - binary data up to 255bytes

BLOB - binary data up to 65KB

MEDIUMBLOB - binary data up to 16MB

LONGBLOB - binary data up to 4GB

**generally better NOT to store binary data in database

Why?

  1. Database size increases - large binary data in the database
  2. Slower backups - larger database size, slower to backup
  3. Performance issues - reading from database is slower than reading from file system
  4. More code to read/store images in database
223
Q

What is JSON?

A

a lightweight format for storing and transferring data over the internet

Javascript object notation (JSON)

used heavily in mobile and web applications!

Data is sent to backend in JSON format!

used in MySQL to store multiple key-value pairs about a product

224
Q

How do we create a JSON object in SQL?

A
  1. Standard JSON format
  2. Internal JSON functions of MySQL

JSON_OBJECT function

JSON_ARRAY( )

225
Q

How can we access individual properties from JSON objects?

A
226
Q

How do we update JSON objects in SQL?

A

update existing properties

add new properties

227
Q

How do we remove properties from JSON objects?

A

Returns a new JSON object with modified properties

228
Q

What must we do as software engineers or database administrators?

A

Design databases

Requires a bit of planning up front

Going to learn a process for designing databases step by step!

229
Q

What plays a critical role in the success of an application?

A

The database design!

If database is designed properly

  1. Can easily extend to support new biz requirements
  2. Can easily query with fast query results
230
Q

What does a poorly designed database require?

A

A lot of maintenance

cost of maintining increases over time

will prevent you from supporting new features

Extracting data is difficult!

queries take several minutes to execute!

231
Q

What are we learning?

A

A systematic approach to designing well structured databases

232
Q

What is data modeling?

A

A process for creating a model of the data we want to store in our database

233
Q

What are the four steps to data modeling?

A
  1. Understand and analyze the business requirements - solution to problem is a database, critical step many developers skip, takes time to fully understand business requirements (end users, forms, documents, databases, spreadsheets)

*better you understand the business problem, better you can find a solution (database)

*must fully understand the business requirements

  1. Build a conceptual model of the business - identify entities, things, concepts in business and relationships, visual model used to communicate with stakeholders to ensure both on same page
  2. Build a logical model - datastructure for storing data, abstract data model that is independent of database technology (shows tables, columns needed)
  3. Build a physical model - implementation of a logical model for a particular database technology (exact datatypes, default values for columns, primary keys, views, stored procedures, triggers)

*very specific to a database technology

234
Q

What is a conceptual model?

A

A UML or Entity relationship model

Representing entities (people, events, locations, etc.) and their relationships

Ex. Student and Course (online learning diagram)

Modeling Tools - MSvisio, Draw.io, LucidCharts

It’s an iterative process, may add new attributes, etc. later on

constantly going back to stakeholders to add/rename/remove attributes from diagram

Process:

  1. Define entity and attributes

Ex.

Student - name, email, dateRegistered

Course - title, price, instructor, tages

  1. Define relationship between entitites

Ex.

Many to many

one to one

At this step…

No information on data types or database technology

Just a conceptual model - used to communicate with business stakeholders

235
Q

What is a logical model?

A

Refinement of conceptual model

independent of database technology

a more details

-data types (independent of db technology)

ex. string vs. VARCHAR (255)

-relationships (one to many, one to one, etc.)

*We almost know what tables to use to structure our data

Ex. entities in logical model eventually become tables in database

236
Q

What should we always do with names and addresses?

A

break them into smaller parts

Why?

so we can query faster

Ex. name -> firstName, lastName

237
Q

What are the three types of entity relationships?

A

one-to-one

one-to-many

many-to-many

238
Q

How can we change this relationship to contain an attribute?

A

day/time a student enrolls in a course is an attribute of the relationship between the student and the course!

Ex. doesn’t make sense to put in course or student, it’s an attribute of the relationship

Soln?

add new entity that represents relationship between student and course

Ex. student can have many enrollments (1 to many relationship)

Ex. each enrollment is for a particular course

239
Q

What is a physical model?

A

implementation of a logical model for a specific database technology

includes…

-exact types in MySQL (database technology)

-nullable or not, default values set

Ex. MySQl Workbench -> file -> new model

Conventions?

Use plural names in physical models for tables

Why?

Tables are containers storing multiple entities of that type (ex. students)

240
Q

When do we introduce a new column (id column) as the primary key?

A

If no other candidates

Ex. Email not a good primary key as primary key should never change AND it’s several characters (don’t want to duplicate data) in other tables!

241
Q

What is a primary key?

A

column that uniquely identifies each record in a given table

Ex. student_id int

Ex. first_name & last_name = composite primary key (has multiple columns)

BUT…

first_name & last_name are not good primary keys!

Why?

They’re multiple chars, takes up space if referenced by other tables

242
Q

What is the relationship between primary key and foreign keys?

A

When we add a relationship to a table, one end is the parent (primary key) and the other is the child (secondary key)

*primary key - parent

Ex. students table is parent (primary key)

*foreign key - child

Ex. Enrollment table (foreign key)

**Select foreign key first, then primary key when adding a relationship!

243
Q

What is a foreign key?

A

a column in one table that references the primary key in another table

denoted with a red diamond

Ex. student_id in Enrollment references students primary key!

Ex. composite primary key from foreign keys

244
Q

What is normalization?

A

The process of reviewing our design

to ensure we do not have data duplication!

by ensuring it meets 7 rules (7 normalizations)

almost 99% of applications..

only need to apply first 3 normal forms!

245
Q

What does the first normal form say?

A

Each cell should have a single value

AND we cannot have repeated columns

Ex. Tags column in courses table - storing multiple tags, separated by a “,”

Soln?

Take tags column of out table and model it as a separate table called tabs

Then, add a many to many relationship between tags and courses

246
Q

What are link tables?

A

a new table that allows us to have many to many relationships

Why?

in relational databases cannot have many to many relationships (only one to many)

247
Q

What does the second normal form (2NF) say?

A

Every table should describe one entity,

AND every column in that table should describe the entity that the table represents

Ex. courses table - single purpose, store course records, every column is an attribute of a course vs. adding enrollment date (not an attribute of a course, rather of an enrollment)

Soln?

If an attribute doesn’t describe the entity, take it out into a separate table

248
Q

What is the problem with this implementation?

(Hint: not in 2NF)

A

customer_name desribes a customer not an order, it is not an attribute of an order, only order_id and date are!

It’s going to cause duplication, wasting space

updates to a customer name will need to update multiple places

Soln?

put customer name in the customers table, then it’s stored in a single place, only need to update a single record

use customer_id in orders table - duplicating a 4 byte int vs. VARCHAR(50)

249
Q

In what ways does this design violate the second normal form (2NF)?

A

courses table violates 2NF

Why?

Instructor name doesn’t belong!

Where is the single place we need to store the name of the instructor? Instructors table!

Soln?

instructors table w/ one to many relationship with courses

250
Q

What does the third normal form say (3NF)?

A

A column in a table should not be derived from other columns

Ex. invoice_total, payment_total, balance vs. invoice_total - payment_total = balance

*balance shouldn’t be a column, it can be derived from invoice_total and payment_total

Ex. Balance column should be dropped

Ex. full_name column should be dropped

Why?

Reduce data duplication

Increase data integrity

251
Q

What is Mosh’s pragmatic advice for normalization?

A

start with conceptual and logical models!

Don’t jump into creating tables immediately or you’ll end up with a bad design!

in real world, focus on reducing redundency

not applying 2nd, 3rd normalizations

How?

If you see duplication and it’s not foreign keys (ints) your design is not in a normalized form

252
Q

What is a mistake many data modelers make?

A

Trying to generalize models to support future requirements!

Arguing that the model is so flexible don’t need to change in future!

Most of time, future requirements are only in their head, never happen!

These models are useless!

Don’t forget about the scope, context and requirements!

253
Q

What is Mosh’s data modeling advice?

A

Solve today’s problems,

not future problems that may never happen!!

Build a model for your problem domain,

not the real world!

Why?

Changes will happen, whether you predict them or not!

cannot predict all future changes

can always write a script to change database model and migrate data if necessary

What if…

What if…

leads to a design with a lot of baggage you have to carry for a long time!

254
Q

How do we convert or physical model to a real physical database?

A

MySQL wizard (forward engineering)

will create a script for creating our database!

Can save file + check into source control repository!

Copy to clipboard, MySQL workbench query window

255
Q

What can forward engineering do for us?

A

Take our physical model (buildt in my SQL)

generate a script for creating the database

Can save the file in repository (recommended)

Can execute in a query window

256
Q

How do we make changes to our tables?

(Hint: must be consistant across envrionments)

A

Go to physical model in MySQL

Make changes in physical model, synchronize with database

Select synchronize model

New script will be generated!

Save file, check in GIT or source control system (execute script on other db servers to update changes)

257
Q

How can we make changes to a database that doesn’t have a physical model?

A

First, we need to reverse engineer the database to create a model

Use MySQL database -> “reverse engineer”

Ex. sql_store database doesn’t have a model

These models have benefits:

1. Help us understand the design

2. Help us identify problems with the design

3. Allow us to make changes and create a script to execute on other MySQL databases

258
Q

Design a database for a system to record the necessary travel information for flight booking!

(Hint: This is an excercise in database modeling)

A
259
Q

What is STEP ONE for creating a database to MODEL a FLIGHT BOOKING service?

A

Conceptual model

Don’t worry about normalization

Don’t worry about additional use cases (only business case)

Ex. DateTime booking in real world (not in requirements), leave out

Ex. Airline could be entity (normalization), do later

Ex. Many to many relationships for all (will refine later)

260
Q

What is STEP TWO for creating a database to model a FLIGHT BOOKING service?

A

logical model

refine relationships

set type for each attribute

talk to domain experts for wording

as we learn more about the problem domain, model changes (future)

Ex. What they call “flight class”

Ex. Ticket to passenger (one passenger many tickets)

Ex. Remove airline atribute from Flight into separate entity

261
Q

When should we denormalize our tables?

A

Tables that are frequently joined together should be combined into a table

Why?

So we don’t have to continually join the tables together

Also?

Duplication is not a big deal (don’t have a million airports)

Ex. City and State will get duplicated in Airport, proper normal form would be to separate out into table

BUT

our queries will join these tables often to find airports, this will cause performance issues!

262
Q

What is STEP THREE for creating a database to model flight booking?

A

Create a physical model

camel_Case notation

database -> forward engineer -> continue -> save .sql script in GIT

updates?

database -> physical model update -> synchronize model -> save script in GIT

263
Q

How can we design a database model for vidly?

(Hint: Step One)

A

Conceptual model

like a brainstorm

bare minimum (entitites + fields, basic relationship)

Next, refine in logical model

264
Q

How can we design a database model for vidly?

(Hint: Step Two)

A

Logical Model

NumberInStock can be a tinyInt (physical model) bcuz don’t need a billion stock

coupon can be nullable (zero to one)

265
Q

When engineering software what should we consider?

A

don’t over engineer

manage complexity (keep it simple)

don’t bring extra complexity

Why?

extra complexity will stay for lifetime of application (costly)

every update will have to deal with unnecessary complexity

266
Q

How do we design a database model for vidly?

Hint: Step Three (Physical Model)

A
267
Q

If you want to become a database administrator what must you understand?

A

The script that the MySQL Workbench wizard generates

Abile to create databases or modify these scripts by hand

without relying on these visual tools

Why?

Need to understand script tool generates

review it to ensure it doesn’t have any bad impacts on your database

268
Q

How do we create a database by hand?

A

CREATE DATABASE IF NOT EXISTS ‘nameOfDb’

269
Q

How do we delete a database by hand?

A

DROP DATABASE IF EXISTS ‘dbName’

270
Q

What are the four constraints we can apply on columns when creating a table?

A

AUTO_INCREMENT -MySQL handles incrementing

NOT NULL -cannot be null

DEFAULT 0 -if null, gives a default value

UNIQUE - ensures every entry in column is unique

271
Q

How can we alter or modify columns in our tables?

A

can make multiple changes

ADD column

CHANGE existing

UPDATE datatype

272
Q

What should we always remember about updating databases?

A

Never update a database in a production environment

Why?

It can have really bad consequences

Try on a test database first, make sure the changes don’t have any bad impacts

Then, execute on a production database

273
Q

How do we create a relationship between tables by hand?

A

​Ex. add a foreign key relationship to orders table from customers table (orders is child of customer table)

convention:

fk_childTable_parentPrimaryKey (addForeignKeyOnColumn)

274
Q

How do we drop or alter foreign keys on tables by hand?

A
275
Q

What is a character set?

A

a table that maps each character to a number

Why?

When we store a string, MySQL converts each character to its numerical representation using a character set

Default

utf8 - 3 bytes per char (supports international language)

276
Q

How can we change the character set of our database or table by hand?

A

Can set at db level, table level or column level!

utf8 requires 3 bytes!

latin language only 1 byte!

Scenario?

Have a table with CHAR (10) = 10 x 3 bytes = 30 bytes per entry x 1M = 30M bytes

vs.

CHAR (10) = 10 x 1 byte = 10 bytes x 1M = 10M bytes

277
Q

What is a storage engine?

A

Determine how data is stored and what features are available

InnoDB supports transactions, foreign keys (superior)

MyISAM is old, doesn’t support transactions, foreign keys (don’t use)

We can use multiple storage engines for the same database

Caution?

MySQL has to rebuild the table if you change the storage engine, can be an expensive process

don’t do in production unless you have scheduled maintanence

278
Q

Why are indexes extremely important in large databases and high traffic websites?

A

Because they can improve performance of queries dramatically!

know how they work, how to create them, how they speed queries

every developer, db admin must learn and understand!

279
Q

Without an index, how does MySQL find records?

A

It has to scan every record in the table!

Ex. find customers in CA without index

scans ALL 1010 rows

Soln?

Create an index on state (like grouping by state)

way faster than reading every record!

Why?

can often fit into memory vs. disk!

280
Q

Why is it faster to use indexes to find data?

A

They often fit into memory!

reading data from memory is always faster than reading from disk!

281
Q

What are the costs of using indexes?

A
  1. Increase database size - are permentatly stored next to tables
  2. Slow down writes - everytime ADD, UPDATE, DELETE a record, have to update cooresponding indexes
282
Q

For what should we reserve the use of indexes?

A

performance critical queries

Why?

indexes increase database size, slow down writes!

283
Q

What is a common mistake developers make?

A

They add indexes at time of designing tables

Why wrong?

design indexes based on queries NOT tables!

Don’t try to solve problems that don’t exist!

Why?

whole point of using indexes is to speed up slow queries!

284
Q

What do we create indexes based on?

A

queries

speeding up slow queries

design indexes based on queries, not your tables!

don’t blindly add indexes on tables

Why?

increases db size and slows writes!

285
Q

How do we create an index?

A

Ex. reduce query from 1010 records to 112 rows (far faster)

Convention?

(prefix) idx_state (columnName)

Ex. idx_state

286
Q

Write a query to find customers with more than 1000 points

(Hint: use Indexes)

A

Ex. 1010 rows searched (without index) vs. 529 rows (w/ index)!

287
Q

When do we use Prefix indexes?

A

Problem:

If column you want to create an index on is a string

CHAR

VARCHAR

TEXT

BLOB

Why?

Disc space won’t perform well consuming all this data from the disk

small indexes are better because they fit in memory, making queries perform faster

Soln?

When indexing string columns, don’t want to include entire column, just the first few characters (prefix)

Why?

So queries will be smaller and fit in memory vs. disk space

288
Q

How do we create a prefix index?

A

specify number of characters to include in index:

optional for char, varchar

manditory blob, text

289
Q

What does collation mean?

A

How data is sorted in index:

A means ascending

B means descending

290
Q

What is cardinality?

A

represents an estimated number of unique values in the index

Ex. PRIMARY has an estimated 1010 unique values

291
Q

What are secondary indexes?

A

Indexes other than PRIMARY index

each secondary index entry contains two values (references) to the primary index

292
Q

How are foreign keys indexed?

A

a foreign key represents

  • *an index for another table’s PRIMARY key column**
  • *Why?**

So we can quickly join tables

How?

MySQL creates an index on PRIMARY key and stores it as the foreign key

293
Q

How do we find the optimal number of characters to include in our pre-fix indexes?

A

No best practice

Have to look at your data

must include enough characters to uniquely identify each entry

Ex. A one character index on last_name, MySQL might return 100k last names that start with “A”

Then have to go through one by one to find the last name

This doesn’t allow MySQL to quickly find data

many last names will begin with same one character

Procedure?

Use SELECT

COUNT (DISTINCT LEFT (index_column, #chars)

FROM indexed_table

Result?

Look for largest cardinality value with smallest number of chars

Ex. one Char = 25 cardinality

fiv Char = 966 cardinality

ten Char = 996 cardinality (30 more)

*small improvement from when doubled Chars, lower bound is optimal (5 Char)

294
Q

When do we use full-text indexes?

(Hint: to do what?)

A

to build fast and flexible search engines within our applications

to implement a fast and powerful search engine in our application

Ex. Blog db -> allow users to search for blog posts

295
Q

What is the challenge with this code?

(Hint: does posts table have an index? does a prefix index work?)

A

It doesn’t have an index

a pre-fix index would only contain the first few characters of the title or the body but our search query could be anywhere in the body so MySQL would have to search the entire body

Query will only return posts that have ‘react redux’ and not posts that only have ‘react’ or only have ‘redux’ or ‘redux react’ out of order

296
Q

How do full text indexes work?

A

very different from regular indexes:

include entire string column (not just prefix)

ignore stop words

store a list of words and for each word, stores a list of rows the word appears in

Have a relevance score

297
Q

How do we create a full text index?

A
298
Q

How do we use full text indexes?

A

beauty is that they contain a relevancy score

it’s a floating point number between 0 - 1 (no relevance to high relevance)

299
Q

What two modes do full text indexes have?

A

Natural language (default)

Boolean - can include/exclude certain words

300
Q

What problem do composite indexes solve?

A

Part of a query is handled by an index, other part requires a table scan (can get slow if a lot of entries)

Ex. State is customers table has a fast index lookup, points has no index requires a table scan (could be slow if 1M customers in a state)!

301
Q

What can we do with a composite index?

A

Index multiple columns

Should use composite indexes (in reality)

Why?

A query can have multiple filters!

Help us get optimal performance

Indexes can help us sort data faster w/ multiple indexes

Single column indexes waste a lot of space

Ex. can create a composite index with state and points

302
Q

How many columns should we include in a composite index?

A

In MySQL an index can have a max of 16 columns

somewhere between 4-6 columns performs well

should experiment with queries and amount of data!

303
Q

What should we favor?

(HINT: single column indexes or composite indexes)

A

Composite indexes

Why?

Many queries involve two columns in the same table

304
Q

How should we pick the order of columns in a composite index?

A

Basic Rule:

  1. Put the most frequently queried column first to narrow search

Ex. 5 queries use “state” column, 1 query uses “last_name”

choose column involved in 5 queries

2. Put first column with highest cardinality (greatest # of unique values)

Ex. state column (cardinality = 48)

1M records /48 = 20k records to search

vs.

gender column (cardinality = 2)

1M records /2 = 500k records to search

*not always true (take data / queries into account!)

305
Q

Why does highest cardinality first not apply here?

A

Always check how MySQL scans records using different indexes

Try to understand how MySQL would execute your queries with different indexes

How?

USE INDEX (alternative_index)

Ex. reduced rows from 40 to 7

Change order of index from (last_name, state) to (state, last_name)

if last name column first -> MySQL will go through each customer with ‘a’ last name, has to go through each to find customers in ‘state’

vs.

‘state’ first -> go to California, in segment, select customers with ‘a’

306
Q

How can we re-write this query to utilize the index in the best possible way?

A

break into smaller queries

Use composite index for first query

Use a single index for second query

Ex.

create a second index on points

execute first query using state index

union

execut second query using points index

Scan 1010 records vs. 600 (composite index, single index)

307
Q

How can we re-write this query to optimize the index performance?

A

Ex. Full index scan (1000 rows vs. 3 rows)

Why?

Used an expression in the WHERE clause

MySQL couldn’t use index in best possible way

Soln?

always isolate columns in order to have MySQL use indexes

308
Q

How can we use indexes for sorting data?

A

When an index is placed, MySQL automatically sorts data in index

Type ALL = full table scan

Extra = using FileSort (algorithm for data sorting in table)

*expensive operation

Using index prevents MySQL from performing FileSort operation

Rule of thumb?

Columns in the ORDER BY should be same as columns in index

309
Q

How should we design our order by clause to maximize MySQL sorting performance?

A

Using columns that are indexed

Why?

If column is not indexed, MySQL will use fileSort to sort (order)

if column is indexed, MySQL has sorted using binary tree (index)

Ex. Cost of 1000 vs. 100 (sorting cost)

310
Q

How should we design our indexes?

(Hint: ORDER BY clause)

A

for both filtering and sorting data

ORDER BY clause using an index is 10x faster

Why?

fileSort is very expensive

it’s 10x more expensive than using index sort

311
Q

What should we consider when using an index and the ORDER BY DESC?

A

MySQL cannot use index in ORDER BY clause DESC

so it uses expensive fileSort operation

Why?

index is sorted in ASCENDING order

Soln?

sort both columns in DESC order

so that MySQL can use index in backward index scan

312
Q

Can we sort using the second column in a composite index?

(Hint: how expensive?)

A

expensive!

Why?

cannot use index

Soln?

WHERE state

*can narrow our search to a state or group of states to filter first, then use points (uses state_points index)

313
Q

What does a composite index on state, points in the customers table contain?

A

customer_id, state, points

NOT all columns

Why?

MySQL includes the PRIMARY key in each index

so it can access data without touching table

fastest query we can get!

314
Q

What should we consider when designing our indexes?

(Hint: How do we get a covering index?)

A

The columns included in the WHERE clause

the columns included in ORDER BY clause

try to include these columns in index

Finally, try to include columns in SELECT clause

*This will give you a covering index!

MySQL can use index to satisfy your query needs!

315
Q

What should we watch for when working with indexes?

A

Duplicate indexes - my SQL will not stop you from duplicate indexes with columns in same order, will maintain them separately

Tip: SHOW INDEXES IN table before creating index

Redundant indexes -

316
Q

How should we view index maintenance?

A

before creating new indexes,

check the existing ones!

always DROP duplicate, redundent or unused indexes!

Why?

They’re costly, slow down writes and can cause performance issues

317
Q

How can we secure our databases?

A

Create accounts and assign privledges

Why?

If you don’t, people can access and misuse your data

Application access data on a server

must create accounts to allow users to read/write data to the database

BUT

not allow them to change database structure (add tables, delete rows, etc.)

318
Q

How do we create a user that allows someone to connect to our database?

(Hint: from an IP address or domain that has MySQL installed on their machine? )

A

CREATE USER

john@’%.dollarlitterclub.com’ (domain)

john@127.0.0.1 (IP address)

john (no restrictions)

319
Q

How can we view our users?

A
320
Q

How can we drop a user account?

A

Always drop user accounts that are no longer in the organization

321
Q

How can we change a user’s password?

A

Two ways:

  1. Set Password - SET PASSWORD = ‘string’
  2. Navigation Panel - users and privileges
322
Q

How do we allow a web/desktop application permissions to read/write data to our database?

A

Nothing more

Cannot create new table, cannot modify tables

323
Q

How do we give an administrator (admin) application permissions to read/write data to our database?

A

Admin Privledges:

Over one db

more than on db

an entire MySQL

324
Q
A
325
Q

How can we see the privledges assigned to a user account?

A

Two ways:

  1. SHOW GRANTS FOR user - see permissions, access
  2. Navigation pannel - users/privledges and schema privleges
326
Q

How can we revoke a user’s privledges?

A

Do not grant more privledges to a user than necessary.

Take security seriously.

Always grant minimum permissions

Why?

Otherwise opening so many security holes!