SQL101
SQL101
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
Data DBMS
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
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
• 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!!!
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
Creating a
Inserting data
table
Creating a table
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
Retrieving Data
Summing it up before we move on
You will require knowledge of datatypes & constraints in SQL to create a table
Its time to modify the table which can be done using many ways out of which these are most commonly used
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
• However, each company will have a different set of data needs and
relationships
• E.g. amazon and Facebook have different data needs
• 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
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
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
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
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
Table 1 : Branch
Table 2 : Client
Table 3 : Employee
Converting ER diagram to schemas
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
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
Table 2 : Client
Table 3 : Employee
Converting ER diagram to schemas
From ER Diagram
1. Works with
2. Supplies
• 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
•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.)
After entering the correct password, you will get the access to mysql server and can start running
the query from terminal