SQL Flashcards
What is a database?
Collection of data, a method for accessing and manipulating that data.
DBMS
RDBMS
SQL
Database Management System
Relational DBMS
Structured Query Language
Types of DBs
- Relational – works well with SQL
- Document – MongoDB, CouchBase, FireBase
- KeyValue – Redis, DynamoDB
- Graph – Neo4j, Nephew - for social media looking at relations.
- Wide Columnar – BigTable, Apache Cassandra
What is Query?
Instructions for the RDBMS that will return us output.
SQL is a declarative language.
– What will happen?
Imperative:
– How will happen?
Database Models
Hierarchial - IBM 60s 70s - One-to-many -- XML Networking - Many-to-many Entity-Relationship Relational Object Oriented Flat Semi-Structured
CRUD Operation
Create
Read
Update
Delete
DBMS Functions
- Management of Data
- Encryption
- Transaction Management
Relational Model
Relation Schema
Attribute - data in a cell
Degree - collection of all columns
Cardinality - collection of all rows
Tuple/Row - a single set of data
Column - may or may not store a specific type of data
Relation Key
– Primary key - uniquely identifies a row
– Foreign key - reference primary key of another table
Domain/Constraint - what kind of data can be stored in a column
Table - collection of tables and rows
Relation Instance
OLTP
OLAP
Online Transaction Processing - Day to day business
Online Analytical Processing - Predict/Analyse
\du
List role names and attributes
DCL
DDL
DQL
DML
Data Control Language -- Grant -- Revoke Data Definition Language -- Create -- Alter -- Drop -- Rename -- Truncate -- Comment Data Query Language -- Select Data Modification Language -- Insert -- Update -- Delete -- Merge -- Call -- Explain Plan -- Lock Table
Change name of a column
select title as “JOB TITLE” from titles where emp_no = 10006
Concat two columns and print in a custom column
select CONCAT(first_name, ‘ ‘, last_name) AS “Employee Name” from employees
Function in SQL
e.g. CONCAT
Receives an input and generates an output.
- Aggregate - receives multiple values and generates 1 output.
- —AVG
- —COUNT
- —MIN
- —MAX
- —SUM
- Scalar - runs against each row and generates multiple output.
Youngest employee in the table
select * from employees order by birth_date ASC limit 1