SQL Tutorial - Full Database Course for Beginners

Share

Summary

This comprehensive tutorial provides a full course on SQL for beginners. It covers fundamental concepts of databases, how to set up a MySQL database, perform various queries, and design database schemas using ER diagrams.

Highlights

What is a Database?
0:02:36

This section introduces databases as any collection of related information, from a phonebook to Facebook's user base. It explains why computers are ideal for storing vast amounts of data, highlighting the advantages of security, backup, and large-scale information management over physical storage methods like paper or memory. It also introduces Database Management Systems (DBMS) as software designed to create and maintain databases, enabling CRUD (Create, Read, Update, Delete) operations.

Relational vs. Non-Relational Databases
0:13:11

The tutorial differentiates between relational (SQL) and non-relational (NoSQL) databases. Relational databases organize data into tables with columns and rows, using unique keys to identify each row, similar to spreadsheets. SQL is the standardized language for interacting with relational DBMS. Non-relational databases store data in various formats like JSON, XML, graphs, or key-value stores. While SQL is standard for relational databases, NoSQL databases often have their own unique languages for CRUD operations.

Tables and Keys
0:23:13

This part delves into the core components of relational database tables: columns (attributes) and rows (entries). It defines a primary key as an attribute that uniquely identifies each row, differentiating between surrogate keys (arbitrary identifiers) and natural keys (real-world identifiers like SSN). Foreign keys are introduced as attributes that link tables by storing the primary key of another table, establishing relationships between data. Composite keys, which use multiple columns to uniquely identify a row, are also explained.

SQL Basics
0:43:34

SQL (Structured Query Language) is presented as the language for interacting with relational DBMS. It's described as a hybrid language encompassing data query, definition, control, and manipulation. The tutorial emphasizes that SQL commands allow users to create, retrieve, update, and delete data, manage databases, design tables, and perform administrative tasks. While SQL is standardized, implementation can vary slightly between different DBMS.

MySQL Setup (Windows & Mac)
0:52:29

The tutorial provides a step-by-step guide for installing MySQL Community Server and PopSQL (a SQL text editor) on both Windows and Mac operating systems. It covers downloading the installer, custom installation options for MySQL Server and Shell, setting a root password, creating a database (e.g., 'giraffe'), and connecting to the database server using PopSQL.

Creating Tables
1:15:52

This section explains how to define database tables using the CREATE TABLE command. It introduces common SQL data types like INT, DECIMAL, VARCHAR, BLOB, DATE, and TIMESTAMP. It demonstrates how to specify column names, data types, primary keys, and how to alter (add/drop columns) and delete tables using ALTER TABLE and DROP TABLE commands.

Inserting Data
1:31:09

The tutorial covers inserting data into tables using the INSERT INTO command. It shows how to insert values for all columns or selectively for specific columns. It also introduces constraints like NOT NULL (ensuring a column cannot be empty), UNIQUE (ensuring all values in a column are distinct), DEFAULT (setting a default value if none is provided), and AUTO_INCREMENT (automatically generating unique numeric IDs).

Update & Delete
1:48:14

This part focuses on modifying and removing existing data. The UPDATE command is used to change values in specific rows or groups of rows, often combined with a WHERE clause to specify conditions. The DELETE FROM command is used to remove rows, also frequently paired with a WHERE clause to target specific records.

Basic Queries
1:56:14

Fundamental querying techniques using the SELECT statement are explained. It demonstrates retrieving all columns (*), selecting specific columns, ordering results with ORDER BY (ascending/descending), limiting the number of results with LIMIT, and filtering data based on conditions using the WHERE clause with various comparison operators (e.g., =, <, >, !=, AND, OR). The IN keyword for checking against multiple values is also covered.

Company Database Introduction
2:08:40

This section transitions to a more complex database schema designed for a company, moving beyond the simple student table. It introduces multiple interconnected tables (Employee, Branch, Client, Works_With, Branch_Supplier) with foreign keys establishing relationships. The purpose is to provide a rich environment for learning advanced SQL querying techniques.

Creating the Company Database
2:17:08

A detailed walkthrough is provided for implementing the company database schema in MySQL. It shows how to create each table, defining primary keys, and setting up foreign key relationships. Special attention is given to handling circular dependencies between tables (e.g., Employee and Branch) by adding foreign keys after initial table creation. Data is then inserted into all tables to populate the database for subsequent examples.

More Basic Queries
2:30:30

Further basic SELECT statements are demonstrated using the company database. This includes retrieving all employees or clients, ordering employees by salary (ascending/descending) or by sex then name, limiting results, and renaming columns using the AS keyword. The DISTINCT keyword is introduced to find unique values in a column, such as different genders or branch IDs.

Functions
2:36:28

SQL functions like COUNT, AVG, and SUM are introduced to perform calculations on data. Examples include counting employees, finding the average salary, and summing all salaries. The GROUP BY clause is also explained for aggregation, allowing users to group results by certain criteria (e.g., counting males and females or summing sales per employee or client).

Wildcards
2:45:16

This segment covers wildcards and the LIKE keyword for pattern matching in SQL queries. The percent sign (%) represents any number of characters, while the underscore (_) represents a single character. Examples include finding clients whose names end with 'LLC', branch suppliers with 'Label' in their name, employees born in a specific month, and clients who are schools.

Union
2:53:55

The UNION operator is explained as a way to combine the results of multiple SELECT statements into a single result set. Critical rules for using UNION are discussed, such as requiring the same number of columns and similar data types in each SELECT statement. Examples include combining employee and branch names, client and supplier names, and combining money spent or earned by the company.

Joins
3:01:39

JOINs are introduced as a powerful mechanism to combine rows from two or more tables based on a related column. The tutorial explains INNER JOIN (returning only matching rows), LEFT JOIN (returning all rows from the left table and matching rows from the right), and RIGHT JOIN (returning all rows from the right table and matching rows from the left). The concept of a FULL OUTER JOIN is also mentioned, even though it's not directly supported in MySQL.

Nested Queries
3:11:53

Nested queries (subqueries) are explained as queries where one SELECT statement is embedded within another. This allows for complex data retrieval by using the results of an inner query to inform the outer query. Examples include finding employees who have sold over a certain amount to a single client and identifying clients handled by a specific branch manager.

On Delete
3:21:56

This section explains how to handle foreign key relationships when deleting data. Two strategies are presented: ON DELETE SET NULL, which sets the foreign key to NULL if the referenced primary key is deleted, and ON DELETE CASCADE, which deletes the entire row containing the foreign key if the referenced primary key is deleted. The choice between these depends on whether the foreign key is also part of a primary key.

Triggers
3:30:08

Triggers are introduced as blocks of SQL code that automatically execute in response to specific database events (INSERT, UPDATE, DELETE). The tutorial explains how to create triggers in MySQL, including changing the delimiter for multi-statement triggers. It demonstrates triggers for logging new employee additions, accessing new row data (NEW.column_name), and using conditional logic (IF, ELSEIF, ELSE) within triggers.

ER Diagrams Intro
3:42:16

ER (Entity-Relationship) diagrams are presented as a visual tool for designing database schemas. It explains how ER diagrams translate data storage requirements into a structured model using entities (objects to store information about), attributes (properties of entities), composite attributes (attributes broken into sub-attributes), multi-valued attributes (can have multiple values), and derived attributes (calculated from other attributes). Relationships between entities, participation (partial vs. total), and cardinality (one-to-one, one-to-many, many-to-many) are also covered. Weak entities and identifying relationships are introduced.

Designing an ER Diagram
3:55:57

This segment walks through the process of creating an ER diagram based on a detailed 'Company Data Requirements' document. It systematically identifies entities (Branch, Client, Employee), their attributes, and the relationships between them. For each relationship, it defines participation (e.g., all branches must have employees) and cardinality (e.g., an employee can work for one branch, a branch can have many employees). Complex relationships like supervision (self-referencing) and weak entities (Branch_Supplier) are also modeled.

Converting ER Diagrams to Schemas
4:08:37

The final section details the five-step process for converting an ER diagram into a relational database schema. This includes mapping regular entities to tables, handling weak entities by incorporating the owner's primary key, and mapping binary relationships (1:1, 1:N, M:N). For 1:1 and 1:N relationships, primary keys from one side are added as foreign keys to the other. For M:N relationships, a new join table is created with a composite primary key formed from the primary keys of the involved entities. The result is a set of interconnected tables that form the database schema.

Recently Summarized Articles

Loading...