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
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
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.
Youngest employee in the table
select * from employees order by birth_date ASC limit 1
Common mistakes with SQL
” for Tables
‘ for Strings
Order of operations
FROM | WHERE | SELECT
AND is chained with previous query
OR starts a new search
NOT
Comparison operators: > < >= <= != = 'abc' > 'ace' -- true
Logical Operators:
Operator Precedence
() */ -+ NOT AND OR