Database Course
Database Course
Introduction to Databases
Learning Objectives:
Understand the fundamental concept of databases.
Explore different types of databases and how they store data with examples.
Gain a basic understanding of relational databases and SQL.
Be introduced to non-relational databases and their data structures with
examples.
Learn about database queries and their purpose.
What is a Database?
A database is a collection of related information.
Examples include:
o Phone book: Stores names, phone numbers (related information for
contacting people).
o Shopping list: Lists items you need to buy (related information for a
shopping trip).
o To-do list: Tasks you need to complete (related information for staying
organized).
o Social media user base: Stores user profiles, posts, and connections (all
related information about users).
Databases can be stored electronically on computers (most common), on paper
(traditional phone books), or even memorized (limited use).
Why Use Databases on Computers?
Computers excel at storing and managing large amounts of data efficiently.
They provide features like security, easy backups, and data duplication for
redundancy.
Example: Amazon.com needs to handle millions of user accounts, product
details, and purchase history. A computer database efficiently stores and
manages this information.
Database Management Systems (DBMS)
A DBMS is a specialized software application designed to create, maintain, and
manage databases on computers.
It simplifies handling large amounts of information by:
o Organizing data into structures like tables (explained later).
o Implementing security measures (e.g., passwords) to control access.
o Enabling data backups (for disaster recovery) and imports/exports from
other sources (data exchange).
o Interacting with software applications (e.g., websites like Amazon.com can
access and modify product data in the database).
CRUD Operations
CRUD stands for Create, Read, Update, and Delete.
These are the four fundamental operations performed on database information:
o Create: Add new entries to the database. (Example: Adding a new
product to an online store's database).
o Read: Retrieve existing information from the database. (Example: A
customer searching for a specific product on an online store website).
o Update: Modify existing information in the database. (Example: Updating
a customer's address in their account settings).
o Delete: Remove unwanted information from the database. (Example:
Deleting an old blog post from a website).
A good DBMS should be able to perform all CRUD operations efficiently.
Types of Databases
1. Relational Databases
o Organize data into tables with rows and columns.
Tables: Act like spreadsheets with specific categories (columns)
and individual entries (rows).
Example: A "Customers" table might have columns for customer
ID, name, email address, and phone number. Each row represents
a single customer.
o Each table has a unique identifier (key) for each row (ensures no duplicate
entries).
o Structured Query Language (SQL) is the standardized language for
interacting with relational databases (covered in later courses).
o Popular relational database management systems (RDBMS) include
MySQL, Oracle, PostgreSQL, and MariaDB.
2. Non-Relational Databases
o Encompass any database that doesn't follow the relational model (tables,
rows, columns).
o Store data in various structures like:
Documents (JSON, XML): Store data in a flexible format like key-
value pairs or nested objects. (Example: User information on a
social media platform can be stored as a JSON document).
Graphs: Represent relationships between data points using nodes
(data points) and edges (connections). (Example: A social network
can use a graph database to show connections between users).
Key-value pairs: Simple data storage with a unique key linked to a
specific value. (Example: An online shopping cart can use a key-
value store to keep track of products and their quantities).
o Examples of non-Relational databases include MongoDB, DynamoDB,
Apache Cassandra, and Firebase.
o No single standard language exists for interacting with non- relational
databases; each system typically uses its own language.
Database Queries
A database query is a specific request made to the DBMS to retrieve information.
As database structures become more complex (many tables with relationships),
queries become essential for efficiently extracting the desired data.
Similar to a Google search, a database query specifies the information you need,
but it uses a specific language (SQL for relational databases).
Example: A customer on a shopping website searches for "blue jackets." The
website interacts with the database using a query to find all products in the
"clothing" table where the "color" column value is "blue" and the "category"
column value is "jackets."
Benefits of Database Queries:
o Allow for retrieval of specific data without needing to access the entire
database.
o Enable filtering and sorting of data based on various criteria.
o Can be used for complex data analysis by combining information from
multiple tables.
Conclusion
Key takeaways include:
Databases store collections of related information.
Computers are efficient for storing and managing large databases.
DBMS simplifies database management (creation, maintenance, security).
CRUD operations (Create, Read, Update, Delete) manipulate data.
Two main database types exist: relational (uses SQL) and non-relational (various
structures).
Database queries are used to request specific information from the database.
By understanding these core concepts, you have a solid base for further learning about
database creation, data storage methods (tables, documents, etc.), and data retrieval
techniques (queries).
Learning Objectives:
Understand the fundamental concept of relational databases.
Explore how data is stored in relational databases using tables and diagrams.
Gain a basic understanding of primary keys, foreign keys, and composite keys.
See examples of how these concepts are applied in a company database
scenario.
What is a Relational Database?
A relational database is a collection of related data organized into tables. It allows for
efficient storage, retrieval, and manipulation of data.
Tables: The Building Blocks
Tables are like spreadsheets with rows and columns.
Rows (horizontal entries): Represent individual records or entries in the
database (e.g., a student, an employee).
Columns (vertical sections): Represent specific attributes or characteristics of
the data (e.g., student name, employee ID).
Each table focuses on a specific entity (e.g., "Students," "Employees").
Example: Student Table
2. Branch Table
Branch Table
Explanation:
"Manager ID" in the Branch table is a foreign key. It references the "Employee
ID" (primary key) in the Employee table, establishing a connection between
employees and the branches they manage.
Composite Keys: When One Isn't Enough
A composite key is a primary key consisting of multiple columns used together to
uniquely identify a row.
Example: Branch Supplier Table
Imagine a table that tracks which suppliers provide products to which branches:
Explanation:
In this example, both "Branch ID" and "Supplier Name" are required together to
uniquely identify a supplier for a specific branch. Neither column alone would be
sufficient.
Benefits of Relational Databases:
Efficient data storage and retrieval.
Enforces data integrity by preventing duplicate entries.
Enables easy data manipulation through queries (covered in later courses).
Creates flexible data models by establishing relationships between tables.
Conclusion
Relational databases provide a structured and organized way to store and manage
large amounts of data. Understanding tables, primary keys, foreign keys, and composite
keys is fundamental for working with relational databases.
Learning Objectives:
Understand the fundamental concepts of SQL and its role in relational
databases.
Gain an overview of the different functionalities of SQL.
Learn basic query structure for retrieving data from a database.
What is SQL?
SQL stands for Structured Query Language.
It's a specialized language used to interact with relational databases.
Unlike traditional programming languages, SQL focuses on querying,
manipulating, and managing data within relational database structures.
Relational Database Management Systems (RDBMS)
Relational databases are organized collections of data stored in tables with rows
and columns.
A Relational Database Management System (RDBMS) is a software application
used to create, maintain, and interact with these databases.
SQL acts as the communication bridge between you and the RDBMS, allowing
you to perform various operations on the data.
Why Use SQL?
Efficient data retrieval: SQL allows you to target specific data from vast amounts
of information stored within the database.
Data manipulation: Insert, update, and delete operations on data can be
performed using SQL.
Data control: Control access to the database by managing users and
permissions through SQL.
Data definition: Define the structure of the database, including tables, columns,
and data types using SQL.
The Four Faces of SQL
SQL can be categorized into four main functionalities:
1. Data Query Language (DQL): Enables retrieval of data from the database
based on specific criteria. This is the most commonly used aspect of SQL.
2. Data Definition Language (DDL): Used to define the database schema, which
outlines the structure of the database. This includes creating tables, specifying
columns within tables, and defining the data types each column can hold.
3. Data Control Language (DCL): Focuses on managing user access and
permissions within the database. This ensures data security and controls who
can view, modify, or delete information.
4. Data Manipulation Language (DML): Allows you to modify the contents of the
database. This includes inserting new data, updating existing data, and deleting
unwanted data.
Understanding SQL Queries
A core concept of SQL is the query. A query is a structured instruction written in SQL
that tells the RDBMS exactly what data you want to retrieve.
Example Query:
Imagine a database table named "Employees" containing information about employees,
including their name, age, and salary. Here's a sample query to retrieve data:
SQL
SELECT employee_name, employee_age
FROM Employees
WHERE employee_salary > $30,000;
Explanation:
SELECT: This keyword specifies the columns you want to retrieve from the table.
employee_salary > $30,000: This condition ensures the query only returns data
I. Creating Tables
CREATE TABLE Statement:
o Syntax: CREATE TABLE table_name (column1 data_type
[constraint1, constraint2, ...], column2 data_type
you're storing.
data_type: Defines the kind of information the column can hold
characters within the specified length (N) are mandatory and filled
with spaces if necessary.
DATE: Stores dates in YYYY-MM-DD format.
DATETIME: Stores dates and times in YYYY-MM-DD HH:MM:SS
format.
TEXT: Stores large amounts of text data.
Setting Constraints:
o PRIMARY KEY: Uniquely identifies each row in a table. It's usually assigned
to an integer column that has unique values (e.g., student ID). A table can
only have one primary key.
o UNIQUE: Ensures that no duplicate values exist within a specific column or
set of columns.
o NOT NULL: Prevents null values from being inserted into a column.
V. Important Considerations
Choose data types carefully, considering the type of data you'll be storing and the
amount of storage space required.
Setting a primary key is essential for efficient data retrieval and manipulation.
Use constraints like NOT NULL to ensure data integrity and prevent invalid entries.
Foreign keys help establish relationships between tables, reducing data
redundancy and improving data consistency.
Consider real-world scenarios when defining column lengths (e.g., 50 characters
for a name might be more practical than 20).
Additional Notes
This guide focuses on fundamental data types and constraints. MySQL offers a
wider range depending on your specific needs.
Explore additional data types like SET, ENUM, and TIME for specialized use cases.
Always back up your data before making significant changes to your tables.
By understanding these concepts, you'll be well-equipped to create and manage
relational database tables in MySQL effectively.
Inserting Information
Basic Syntax:
o INSERT INTO table_name VALUES (value1, value2, ...);
value2, ...);
NOT NULL:
o Ensures a column cannot have empty (NULL) values.
o Example: name VARCHAR(50) NOT NULL
UNIQUE:
o Ensures all values in a column are different.
o Example: major VARCHAR(50) UNIQUE
Additional Constraints
DEFAULT:
o Sets a default value if no value is provided during insertion.
o Example: major VARCHAR(50) DEFAULT 'Undecided'
AUTO_INCREMENT (for Primary Keys):
o Automatically increments the primary key value with each new row.
o Example: student_id INT PRIMARY KEY AUTO_INCREMENT
Error Handling
"Cannot be null" Error: Occurs when trying to insert a NULL value into a column
defined as NOT NULL.
"Duplicate entry" Error: Occurs when trying to insert a duplicate value into a column
defined as UNIQUE.
Key Points
key differences between primary keys and unique keys in a database context:
Null Values
No Yes (multiples allowed)
Allowed
Automatic
Yes (usually clustered index) Yes (usually non-clustered index)
Indexing
Example Use Student ID, Employee ID, Email address, Social Security
Cases Order Number Number, License Plate
Key Takeaway:
Primary Key: The fundamental way to uniquely identify and reference each row
in a table. It's your go-to for reliable record identification.
Unique Key: Primarily used to enforce data integrity by preventing duplicate
values within a specific column (with the exception of one NULL value). It's useful
when you want to ensure certain data points are distinct.
What is a Clustered Index?
A clustered index is a way of organizing the information in the database so that it’s
sorted in a specific order. This makes it easier and faster to find the information you
need.
Example:
Let's say the library has the following books:
Book ID: 3, Title: "Harry Potter", Author: "J.K. Rowling", Published Date: "1997"
Book ID: 1, Title: "The Hobbit", Author: "J.R.R. Tolkien", Published Date: "1937"
Book ID: 2, Title: "1984", Author: "George Orwell", Published Date: "1949"
If the library creates a clustered index on the Book ID, the database will store these
books in the following order:
1. Book ID: 1, Title: "The Hobbit", Author: "J.R.R. Tolkien", Published Date: "1937"
2. Book ID: 2, Title: "1984", Author: "George Orwell", Published Date: "1949"
3. Book ID: 3, Title: "Harry Potter", Author: "J.K. Rowling", Published Date: "1997"
Updating Rows
Basic Syntax:
o UPDATE table_name SET column1 = value1, column2 = value2, ...
WHERE condition;
Examples:
o Update a specific column for all rows:
SQL
UPDATE student SET major = 'Bio';
o Update based on a condition:
SQL
UPDATE student SET major = 'Bio' WHERE major = 'Biology';
Key Points:
o The WHERE clause is optional. If omitted, the update applies to all rows.
o Use logical operators like AND and OR to create more complex conditions.
Deleting Rows
Basic Syntax:
o DELETE FROM table_name WHERE condition; (optional WHERE)
Examples:
o Delete a specific row:
SQL
DELETE FROM student WHERE student_id = 5;
Key Points:
o Be cautious when deleting without a WHERE clause, as it removes all rows.
Important Note: Always double-check your UPDATE and DELETE statements before
executing them, especially when not using a WHERE clause, to avoid unintended data
loss.
This enhanced guide empowers you to wield the SELECT statement, a fundamental tool
in your SQL arsenal. It allows you to precisely extract the data you need from relational
databases.
Tailoring Your Data Selection:
Retrieve everything: Use SELECT * to grab all columns from a table, but be
mindful of potential performance implications for large datasets.
Pick specific data points: Specify individual column names for a more focused
approach (e.g., SELECT name, major, student_id).
Maintain clarity: Prefix table names with a dot (.) to enhance readability (e.g.,
student.name).
Example:
SQL
SELECT student.name, student.major
FROM student;
This retrieves name and major columns from the student table, explicitly referencing the
table name for clarity.
Filtering for Specific Data (WHERE Clause):
Target your results: Utilize the WHERE clause to filter data based on conditions.
Comparison operators: Leverage operators like =, <, >, <> (not equal) to
compare values.
Combining conditions: Link multiple conditions with AND (both true) or OR (at
least one true) for precise filtering.
Multiple value comparisons: Use IN to efficiently check a column against a list
of values (e.g., major IN ('CS', 'Math')).
Example:
SQL
SELECT name
FROM student
WHERE major = 'Biology' AND student_id > 2
OR name = 'Mike';
This retrieves the name for students with major Biology and student ID greater than 2,
or for students named Mike.
Organizing Your Retrieved Data (ORDER BY Clause):
Sort efficiently: Utilize the ORDER BY clause to arrange retrieved data in a
specific order.
Specify sorting direction: Use ASC for ascending (default) or DESC for
descending order.
Multi-column ordering: Sort based on multiple columns separated by commas
(e.g., ORDER BY major ASC, name DESC).
Example:
SQL
SELECT *
FROM student
ORDER BY major DESC, student_id ASC
LIMIT 10;
This retrieves all columns from the student table, ordered first by major (descending)
and then by student ID (ascending), limited to the top 10 results.
Limiting the Number of Results (LIMIT Clause):
Control data volume: Restrict the number of retrieved rows with LIMIT.
Example:
SQL
SELECT name, major
FROM student
LIMIT 5;
This retrieves only the first 5 rows (students) from the student table, including name and
major columns.
By mastering these core concepts, you'll be well on your way to crafting powerful and
efficient SQL queries to retrieve the exact data you need from your databases.
Table: student
This video introduced a complex company database schema that will be used
throughout the rest of the course for learning advanced SQL queries.
Employee Table:
1967- 250,000.
100 David Wallace M NULL 1
11-17 00
1964- 75,000.0
102 Michael Scott M 100 2
03-15 0
1971- 63,000.0
103 Angela Martin F 102 2
06-25 0
1958- 69,000.0
105 Stanley Hudson M 102 2
02-19 0
1969- 78,000.0
106 Josh Porter M 100 3
09-05 0
1973- 65,000.0
107 Andy Bernard M 106 3
07-22 0
1978- 71,000.0
108 Jim Halpert M 106 3
10-01 0
Branch Table:
Client Table:
402 FedEx 3
403 John Daly Law, LLC 3
406 FedEx 2
Works_With Table:
107 405
assignment)
Branch table stores information about the company branches and their
managers.
o Primary Key: branch_id (Branch ID)
o Foreign Key:
mgr_id: references emp_id in the Employee table (manager)
Client table stores client information and the branch they work with.
o Primary Key: client_id (Client ID)
o Foreign Key:
branch_id: references branch_id in the Branch table (branch)
For a deeper understanding, refer to the instructor's recommendation to review the PDF
provided in the course description. This will help you visualize the connections between
the tables.
Introduction:
This document summarizes the key concepts from a video tutorial on building a
complex database schema in SQL. It focuses on the design, creation, and population of
tables within a company database.
Database Design:
The schema consists of five interrelated tables:
o Employee: Stores employee information (ID, name, birthdate, sex, salary,
supervisor ID, branch ID).
o Branch: Stores branch details (ID, name, manager ID, manager start
date).
o Client: Holds client information (ID, name, branch ID).
o Works_With: Links employees to clients with their total sales (composite
primary key: employee ID, client ID).
o Branch_Supplier: Associates branches with suppliers and the type of
supplies provided (composite primary key: branch ID, supplier name).
Foreign Keys:
Foreign keys ensure data consistency by referencing existing data in other
tables.
o Employee.branch_id references Branch.branch_id.
supervisor).
o Client.branch_id references Branch.branch_id.
Table Creation:
The tutorial demonstrates creating each table using SQL CREATE TABLE
statements, specifying:
o Column names and data types.
o Primary key constraints (unique identifier for each row).
o Foreign key constraints (references to existing tables and columns).
Data Insertion:
Due to circular foreign key dependencies, a specific order is required for data
insertion:
1. Insert employees with a temporary value for branch_id.
2. Insert branches referencing the previously inserted employee IDs for
managers.
3. Update employee entries with the correct branch_id values.
The tutorial showcases using SQL INSERT statements to populate each table with
sample data.
Data Verification:
The video concludes by demonstrating how to verify data insertion using SQL
SELECT statements to retrieve information from the populated tables. This
Mention limitations of the provided information (e.g., sample data might not be
exhaustive).
Note: This video introduced basic SQL select statements using a sample company
database.
Key Concepts:
SELECT: This clause retrieves data from a table.
*SELECT : Retrieves all columns from a table.
FROM: This clause specifies the table to retrieve data from.
ORDER BY: This clause sorts the results based on a specified column
(ascending by default, use DESC for descending).
LIMIT: This clause limits the number of returned rows.
SELECT (column1, column2, ...): This clause specifies individual columns to
retrieve.
AS: This keyword renames a column in the results.
DISTINCT: This keyword returns only unique values from a column.
Examples:
Get all employees:
SQL
SELECT * FROM employee;
Additional Notes:
The video mentions using as to alias columns for readability.
The video also covers the DISTINCT keyword to get unique values.
Absolutely, here are the enhanced notes incorporating additional details from the
transcript:
employees)
o Example 2 (Summing by Employee): SELECT employee_id,
SUM(total_sales) FROM work_shift GROUP BY employee_id;