0% found this document useful (0 votes)
22 views

SQL101

Uploaded by

shreeshgarg2004
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views

SQL101

Uploaded by

shreeshgarg2004
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 54

SQL: Hands-on introduction

Data evolution
Once Data is generated, the 1st question you will ask is storage related
–Where will I capture this data?
As data capture has evolved, the means to analyze each form of capture has evolved

Captured on paper, Paper data capture Software and


Stored in binders and converted to electronic languages are used to
filing cabinets data capture do data analysis and
Manual extraction of Needed special other operations
data which was very hardware Today
tedious
Floppy drive, hard
drive coming into
existence
Before mid
20th century
What is a Database?

Data DBMS

Collection of structured information Database Management System

Database system - often shortened to just “Database”

Is commonly modelled in rows and columns in a series of tables to


Data
make processing and data querying efficient
What are Relational databases?

• Relational databases became dominant in the 1980s.


• Provides the most efficient and flexible way to access structured information.
• Items in a relational database are organized as a set of tables with columns and rows.
• Therefore, all data points will have x and y coordinates
• Most common example is data stored in excel spreadsheets, e.g. monthly sales report
• Most databases use structured query language (SQL) for writing and querying data.

4 columns, i.e., ID, Name, Degree, Year

This data point will have


Unique x and y
6 rows coordinates
1 for header
5 for data entries
SQL Basics

• SQL : Structured Query Language


• Standard language for accessing and manipulating data
• User can issue queries for performing data operations on Relational Database
• A query is a user-generated request to retrieve data

Query, manipulate & create the data Relational


SQL
And to provide access control Database
Language to Data
perform data structured in
operations set of tables
on Relational with rows and
Database columns

RDBMS : Relational DBMS uses software like MySQL


MySQL : is RDBMS developed by Oracle that is based on SQL
Why learn SQL and how it helps

Business use :
1. Manage database systems without having to write substantial amount of code
2. Queries can be used to retrieve large amounts of records from a database efficiently
3. Standardized as per ANSI (American National Standard Institute) & ISO (International
Organization for Standardization) so any system can run it

Roles in which SQL is required :


• SQL is one of the must have skills for many roles including business/data analyst, data scientist

In this module you will learn how to use queries in SQL for managing data (DBMS)
• Most companies have data, so more often you need to know how to manage the existing
database rather than create databases from scratch
• However, all data related roles will expect you to answer problems based on data query
• Let’s start with the most important skill which is understanding and writing queries
How a query looks like

• 1-11 : are row numbers


• All the text to the right of the
row number is SQL code
which forms a query
• Code is always supported
with comments (reading
notes) which are in green
color to help understand the
code blocks which starts
with - -
• Also notice other colors like
purple, blue, 2nd blue
What is a line of code

• Each code block starts


with an instruction
(purple)
• Followed by what data to
apply this instruction on
(variable – black)
• A code block always ends
with ;
• Can you count the
number of code blocks in
the snippet?
Reading code and interpreting it

• Row 2: A table is created


• Row 2: Table is named as
students which is variable
• Row 3-5: 3 columns are
created i.e. id, name,
gender
• Row 3-5: For all 3 columns
a data type is defined and
a constraint is defined
Reading code and interpreting it

• Row 8-9: 2 values are


inserted into the table
• Row 11: Values are
fetched using SELECT
• Row 11: Values are
filtered using WHERE
• Output is shown for the
given query
Exercise

• We can do SQL coding on a web browser – so, we will start with a browser
- https://www.mycompiler.io/new/sql
• Click on the link above with some excitement and start coding
• Some functions do not work on the browser – so we will be required to
install SQL which we will do later – once we have command over syntax!!!

• To start with – SQL is not case sensitive


• There are many SQL reserve words like create, table, insert etc.
• You cannot use reserve words as variables
SQL can be broken into 4 parts

• DQL : Data Query Language – get information from database!


• DDL : Data Definition Language – defining database schemas!
• DCL : Data Control Language – access permission management!
• DML : Data Manipulation Language – insert, update & delete data!

DQL DDL DCL DML

Gets Defines Access Insert, update


information database permission & delete data
from the schemas management
database
Learning method

Creating a Retrieving Manipulate


Inserting data
table Data Data
You will require knowledge of
datatypes and constraints in SQL
to create a table

First learn syntax of the above 4 most frequently used actions on a database.
Syntax is nothing but structure of statements.

Once you are comfortable with syntax of above, we will download and install SQL and look at
other actions that can be performed.
Data types in SQL

Data types
Used to store numbers, characters, time &
dates into variables

Some examples of data types are as


follows:
• INT – allows you to store whole
number
• DECIMAL(M,N) – allows you to store
decimals (m-total digits, n-digits after
decimal)
• VARCHAR(L) – allows you to store
string of length L
• DATE – ‘YYYY-MM-DD’
• TIMESTAMP – ‘YYYY-MM-DD
HH:MM:SS’
Constraints in SQL

Constraints are used to limit the type of Constraints


data that can go into a table.
Constraints are Table level or Column level.

Some examples are as follows


• NOT NULL – ensures that column will
have no empty cell
• DEFAULT – provides a default value
when not specified
• UNIQUE – ensures all values in a column
to be different
• PRIMARY KEY – unique for each row
(e.g. pan card id)
• CHECK Constraint: The CHECK constraint
ensures that all the values in a column
satisfies certain conditions
Creating a table and inserting data
Say, you are asked to create a table and insert the data as shown.

This is your task 1 – to create this given data in SQL

Creating a
Inserting data
table
Creating a table

The CREATE TABLE statement is used to create a new table in a database.


Syntax:
CREATE TABLE table_name (
column1 datatype constraint,
column2 datatype constraint,
column3 datatype constraint,
....
);

Creating a
table means
defining
columns
Inserting Data
The INSERT INTO statement is used to insert a new row in a table.
Syntax:
INSERT INTO table_name (column1, column2,column3,....) VALUES (value1,value2,value3,….);

Example:
Column names were not required after table_name as our data is non-NULL (nothing is empty in the table)

Insert is used
5 times, to
add data into
5 rows
Retrieving Data

The SELECT statement is used to get some data from a table – will be shown in the Output section
Syntax:
SELECT * from table_name – will retrieve all the records
SELECT column1 from table_name – will retrieve only column1 records
SELECT DISTINCT column1 from table_name – will retrieve only dissimilar column1 records
Example:

Output –
retrieving all
records
Understanding & writing queries

Creating a table Creating a table means Output –


defining columns retrieving all
records
because of
SELECT *

Inserting data Insert is used 5 times, to add


data into 5 rows

Retrieving Data
Summing it up before we move on

CREATE TABLE INSERT INTO SELECT

You will require knowledge of datatypes & constraints in SQL to create a table

Do you remember that we started with this table?


Manipulating Data
Now, you can create a table, insert data into it and retrieve data from it

Its time to modify the table which can be done using many ways out of which these are most commonly used

Command UPDATE DELETE ALTER TABLE

Updates row Deletes row 1. Add column


2. Delete column
Function
3. Modify datatype of a column
4. Rename column

UPDATE table_name DELETE FROM table_name ALTER TABLE table_name


SET column1 = value1, WHERE condition; 1. ADD column_name datatype;
Syntax column2 = value2 2. DROP COLUMN column_name;
WHERE condition; 3. MODIFY COLUMN
column_name datatype;
4. RENAME COLUMN
old_col_name to new_col_name;
Company database intro

Database schemas used in


companies are much more
complex than what we did
till now.

To master SQL, we should


move to a more complex
database.

Let’s go one step at a time


and target the database
shown on this slide. This is
your task 2 – to create this
given data in SQL.
How to go about making a company database

First create 3 tables


1. Employees
2. Client
3. Works with

Followed by inserting data into the tables (i.e. create columns)


along with the datatype of the column and constraint

Here, you also must notice the use of primary key, foreign key to create relationship
between tables. ‘supervisor_id’ is a foreign key which is coming from the same table
‘employees’. Table ‘works_with’ has two primary keys i.e. the combination of the two
will be the primary key. You will be required to deal with more complex data later.
Creating given tables

Notice, the declaration of a


foreign key here

Notice, the declaration


of a primary key here
Inserting data into the tables

This is straightforward, as per the given


data you just have to insert the data
Retrieving data

SELECT * FROM employees;


SELECT * FROM client;
SELECT * FROM works_with;

This is straightforward, you have to use


SELECT command to retrieve the stored data
which is retrieved in the output section
Tables in reality: Complexity
• We have covered a lot of things till now, like creating a table, inserting
values, retrieving values, data types, constraints, primary key, foreign key

• However, each company will have a different set of data needs and
relationships
• E.g. amazon and Facebook have different data needs

• Hence, you need to understand a company’s ER diagram i.e. Entity


Relationship diagram

• This helps you to build schemas to reflect the data requirement


Example: Pan-India sales setup

• Company XYZ is organized into branches. Each branch has a unique number, a name,
and a particular employee who manages it.
• Each client has a name and a unique number to identify it.
• Each employee has a name, birthday, gender, salary and a unique number.
• An employee can work for one branch at a time.
• Each branch will be managed by one of the employees that work there.
• An employee can act as a supervisor for other employees at all branches.
• An employee can have at most one supervisor.
• A branch may handle several clients
• A single client may one be handled by one branch at a time
• Many branches will have to work with Suppliers which has a name and type
• Multiple employees can work with same clients
Solution: Intro to ER diagram

Entity relationship model where entity is an object we want to model and store
information about

Attributes – specific pieces of information about an entity


Primary key – an attribute that uniquely identify an entry in the database table
Composite attribute - an attribute that can be broken into sub-attributes
Multi-valued attribute – that can have more than one value
Derived attribute – that can be derived from other attributes
Relationship – Partial & total participation – as name suggests partial means not all
Cardinality – shows depth of participation in the relationship
Weak entity – cannot be uniquely identified by its attribute alone
Identifying relationship – that serves to uniquely identify the weak entity
Creating an ER diagram

The company is organized into branches. Each branch has unique number, a
name, and a particular employee who manages it.

Cardinality is 1:1
attribute
total partial
branch_name
participation M participation
an
Branch ag
es Employee
1 1
branch_id
entity entity
primary key relationship
Creating an ER diagram

Each client has a name and a unique number to identify it.


Each employee has a name, birthday, gender, salary and a unique number.
An employee can work for one branch at a time. emp_name
attribute

client_name emp_id
Employee primary key
Client
client_id gender
entity entity
primary key
birthday

salary
Combined ER diagram
emp_name

branch_name emp_id
W
or
Branch fo ks Employee
r N
1
branch_id gender

#_employees M birthday
an
ag
es
1 1
salary

client_name
age
Client
client_id
Creating an ER diagram

An employee can act as a supervisor for other employees at all branches.


An employee can have at most one supervisor.
N
supervisee
Su
pe
rv Cardinality is 1:N
Employee isi
on
relationship
supervisor
1
Creating an ER diagram

A branch may handle a number of clients


A single client may one be handled by one branch at a time

Branch
Cardinality is 1:N
total
participation
1

Ha N
n dle Client
s partial
relationship participation
Creating an ER diagram

Many branches will have to work with Suppliers which has a name and type
Multiple employees can work with same clients

supply_type Branch weak Employee


Supplier entity
supplier_name
M

W Cardinality is M:N
M o
wi rks
Su th
identifying pp
li es
relationship N
N

Client
Branch
Final ER diagram
Su
pe
supervisee
rv N
1 isi
on emp_name
supervisor

branch_name emp_id
W
or
Branch fo ks Employee
r N
1
branch_id gender
N M

#_employees W
M o birthday
Su an
ag wi rks
pp es th
li es 1 1
M 1 N salary

Ha N
n Client
supply_type Branch dle
s
age
Supplier
supplier_name client_name client_id
Converting ER diagram to schemas

Step 1: Mapping of regular entity types


From ER Diagram, you can make 3 tables
1. Branch
2. Client
3. Employee

Include all simple attributes as columns

Table 1 : Branch

Table 2 : Client

Table 3 : Employee
Converting ER diagram to schemas

Step 2: Mapping of weak entity types


From ER Diagram, you can make 1 table
Branch Supplier

Include all simple attributes as columns

Table 4 : Branch Supplier


Converting ER diagram to schemas

Step 3: Mapping of binary 1:1 relationship types


Include one side of the relationship as a foreign key in the other
(Always favor total participation, use your own discretion if both are total or both are partial)

From ER Diagram
Manages
This is only 1:1 binary relationship
With total participation from branch, and partial participation from employee

Table 1 : Branch
Converting ER diagram to schemas

Step 4: Mapping of binary 1:N relationship types


Include one side of the relationship as a foreign key on the N side table

From ER Diagram
1. Works for with n side on Employee
2. Supervision with n side on Employee
3. Handles with n side on Client

These are three 1:N relationships

Table 2 : Client

Table 3 : Employee
Converting ER diagram to schemas

Step 5: Mapping of binary M:N relationship types


Create new table who’s primary key is a combination of both entities primary keys. Also include relationship attributes.

From ER Diagram
1. Works with
2. Supplies

These are two M:N relationships


Supplies relationship was already dealt with in step 2 as branch supplier was a weak entity

Table 5 : Works with


These were the data requirements we started with

• The company is organized into branches. Each branch has unique number, a name,
and a particular employee who manages it.
• Each client has a name and a unique number to identify it.
• Each employee has a name, birthday, gender, salary and a unique number.
• An employee can work for one branch at a time.
• Each branch will be managed by one of the employees that work there.
• An employee can act as a supervisor for other employees at all branches.
• An employee can have at most one supervisor.
• A branch may handle a number of clients
• A single client may one be handled by one branch at a time
• Many branches will have to work with Suppliers which has a name and type
• Multiple employees can work with same clients
Data requirement > ER diagram
Su
pe
supervisee
rv N
1 isi
on emp_name
supervisor

branch_name emp_id
W
or
Branch fo ks Employee
r N
1
branch_id gender
N M

#_employees W
M o birthday
Su an
ag wi rks
pp es th
li es 1 1
M 1 N salary

Ha N
supply_type Branch n dle Client age
s
Supplier
supplier_name client_name client_id
Data requirement > ER diagram > Database schema
Next steps: Lab work
Next steps: Lab case study

• Task 1: install MySQL – steps are given to you in the subsequent slides
• Before coming to next session – you are required to install MySQL
• If you are facing issues installing MySQL – you can ask the same to your faculty in
classroom in the next session
• Task 2: you are required to solve a case which will be done in groups
• Utilize your time to solve the case and discuss with peers by working in groups
• Case is divided into 2 parts
• Part 1 : You are given 4 tables – and you are asked to create table – insert data –
retrieve data
• Part 2 : You are given 7 different excel files which contains tables – you are
required to understand the relationship between these 7 tables – identify primary
keys, foreign keys etc. – and create ER diagram – also create database schema
MySQL Installation

•For Windows Using MySQL Installer


•To download MySQL installer, go to the following link
http://dev.mysql.com/downloads/installer/.
• There are two files are available. If you are connecting to the internet while installing
MySQL, you can choose the online installation version mysql-installer-web-community.exe . If
you want to install MySQL offline, you can download the mysql-installer-community.exe file.
•After downloading the MySQL installer, double click on it and then follow the next steps.
•Windows configures MySQL Installer.
•A welcome screen provides several options. Choose the first option: Install MySQL Products.
•MySQL installer checks and downloads the latest MySQL products including MySQL server,
MySQL Workbench, etc.
MySQL Installation (Contd.)

•Click Next button to continue


•Next there are several setup types available. Choose the Full option to install all MySQL products
and features
•It will check for the requirements
•MySQL Installer downloads all selected products. It will take a while, depending on which
products that you selected and the speed of your internet connection
•Next it will show the downloading progress
•Once the downloading is over, click on next button to continue
•Click Next button to configure MySQL Database Server
•Choose Config Type and MySQL port (3306 by default) and click Next button to continue
MySQL Installation (Contd.)

•Choose a password for the root account. Please note the password download and keep it securely
if you are install MySQL database server in a production server. If you want to add more MySQL
user, you can do it in this step
•Choose Windows service details including Windows Service Name and account type, then click
Next button to continue.
•MySQL Installer is configuring MySQL database server. Wait until it is done and click Next button
to continue
•Once Done. Click the Next button to continue
•MySQL Installer installs sample databases and sample models
•The installation completes. Click finish button to close the installation wizard and launch the
MySQL Workbench
•For Reference : http://www.mysqltutorial.org/install-mysql/
MySQL Installation - Error Handling

•Error handling
– Check the MySQL error log
– If there is an error for Installing Visual Code Distributable
– Install it from this location
– Install both the 32bit and the 64bit system
– https://www.microsoft.com/en-in/download/details.aspx?id=40784
MySQL Installation (Contd.)

•For Linux (Ubuntu)


• First, login to your Linux server using SSH
• Then run below commands for installing MySQL
– sudo apt-get install mysql-server for downloading the mysql package
– During the installation process, you will be prompted to set a password for the
MySQL root user. Choose a strong password and keep it in a safe place for future
reference
– sudo mysql_secure_installation script to address several security concerns in a
default
•MySQL installation
•You will be given the choice to change the MySQL root password, remove anonymous user
accounts, disable root logins outside of local host, and remove test databases. It is recommended
that you answer yes to these options
MySQL Installation (Contd.)

After installation, mysql client is accessed through a terminal


To login to the MySQL as the root user:
Run this command, mysql -u root –p
When prompted, enter the root password you assigned when the
mysql_secure_installation script was run
MySQL Installation (Contd.)

After entering the correct password, you will get the access to mysql server and can start running
the query from terminal

You might also like