SQL Notes PDF
SQL Notes PDF
SQL Notes PDF
[ 1. Intro ]
1–1. What is database?
Database is a collection of related information that can be stored in different
ways.
We can interact with a DBMS to Create, Read, Update, and Delete information.
- Key-Value stores
- Graphs
- Flexible tables
Most NRDBMS will use their own language for performing C.R.U.D. and
administrative operations on database.
As database’s structures become more and more complex, it becomes more difficult to get
specific pieces of information we want. A google search is a query.
2018 ©️FreeCodeCamp
2018 ©️FreeCodeCamp
“Composite Key” is “a combination of two or more columns in a table that can be used to
uniquely identify each row in the table. Uniqueness is only guaranteed when the columns
are combined; when taken individually the columns do not guarantee uniqueness.”
(Techopedia)
2018 ©️FreeCodeCamp
2018 ©️FreeCodeCamp
Data Query Language (DQL) : Used to query the database for information. Can get
information that’s already stored in the database.
Data Definition Language (DDL) : Used for defining database schemas.
Data Control Language (DCL) : Used for controlling access to data in the database.
Handles user & permissions management.
Data Manipulation Language (DML) : Used for inserting, updating, and deleting data
from the database.
3–2. Queries
A Query is a set of instructions given to the RDMBS (written in SQL) that tell
the RDMBS what information a developer wants it to retrieve for the
developer.
FROM employee
mysql -u root -p
- DATE : 'YYYY-MM-DD'
name VARCHAR(20),
major VARCHAR(40)
);
View the table : DESCRIBE student;
);
name VARCHAR(20),
);
name VARCHAR(20),
PRIMARY KEY(student_id)
> If you get this error, Run the following: SET SQL_SAFE_UPDATES = 0;
> You can reset the safe mode with SET SQL_SAFE_UPDATES = 1;
UPDATE student
WHERE student_id = 9;
UPDATE student
UPDATE student
WHERE student_id = 1;
[ 9. Basic Queries (1:56:11) ]
SELECT name
FROM tutorial.student;
SELECT name
FROM tutorial.student
ORDER BY name;
SELECT name
FROM tutorial.student
SELECT *
FROM tutorial.student
SELECT *
FROM tutorial.student
LIMIT 2;
SELECT *
FROM tutorial.student
SELECT *
FROM tutorial.student
SELECT *
FROM tutorial.student
2017 © M
️ ike Dane
2017 © M
️ ike Dane
first_name VARCHAR(40),
last_name VARCHAR(40),
birth_day DATE,
sex VARCHAR(1),
salary INT,
super_id INT,
branch_id INT
);
[STEP 2] Create “branch” table & add a foreign key.
2017 © M
️ ike Dane
branch_name VARCHAR(40),
mgr_id INT,
mgr_start_date DATE,
);
> ON DELETE SET NULL : “It specifies that the child data is set to NULL when the parent
data is deleted. The child data is NOT deleted.” (Tech on the Net)
2017 © M
️ ike Dane
REFERENCES employee(emp_id)
REFERENCES branch(branch_id)
2017 © M
️ ike Dane
CREATE TABLE client (
client_name VARCHAR(40),
branch_id INT,
);
2017 © M
️ ike Dane
emp_id INT,
client_id INT,
total_sales INT,
);
2017 © M
️ ike Dane
branch_id INT,
supplier_name VARCHAR(40),
supply_type VARCHAR(40),
);
To make composite keys, assign more than one key as primary keys.
ON DELETE CASCADE : “It specifies that the child data is deleted when the parent
data is deleted.” (Tech on the Net)
11–2. Insert data.
[STEP 1] Insert data of “Corporate” branch.
2017 © M
️ ike Dane
SET branch_id = 1
2017 © M
️ ike Dane
SET branch_id = 2
SET branch_id = 3
2017 © M
️ ike Dane
2017 © M
️ ike Dane
INSERT INTO client VALUES(400, 'Dunmore Highschool', 2);
2017 © M
️ ike Dane
FROM employee;
FROM employee;
SELECT COUNT(super_id)
FROM employee;
SELECT COUNT(emp_id)
FROM employee
SELECT AVG(salary)
FROM employee;
13–4. Find out how many males & females there are.
SELECT COUNT(sex), sex
FROM employee
GROUP BY sex;
FROM works_with
GROUP BY emp_id;
©️W3Schools
SELECT *
FROM client
SELECT *
FROM branch_supplier
The tutorial showed a way to find any employee born in October with the following query:
SELECT *
FROM employee
However, I got the following response : Error Code : Incorrect DATE value:
‘____-10%’.
SELECT *
FROM employee
FROM client
UNION
FROM branch_supplier;
FROM employee
JOIN branch
ON employee.emp_id = branch.mgr_id;
FROM employee
ON employee.emp_id = branch.mgr_id;
FROM employee
ON employee.emp_id = branch.mgr_id;
FROM employee
FROM employee
ON employee.emp_id = branch.mgr_id);
SELECT works_with.emp_id
FROM works_with
FROM employee
SELECT works_with.emp_id
FROM works_with
);
17–2. Find all clients handled by a branch that Michael
Scott manages when you know his ID.
# Step 1 : Find out the branch ID.
SELECT branch.branch_id
FROM branch
SELECT client.client_name
FROM client
WHERE client.branch_id = (
SELECT branch.branch_id
FROM branch
LIMIT 1
);
Let’s delete Michael Scott(ID: 102)’s information. When we created the branch table,
we set mgr_id that referenced emp_id in the employee table as ON DELETE SET NULL.
branch_name VARCHAR(40),
mgr_id INT,
mgr_start_date DATE,
);
DELETE FROM employee
Let’s delete the Scranton branch(ID : #2)’s information. When we created the
branch_supplier table, we set branch_id that referenced branch_id in the branch table
as ON DELETE CASCADE.
branch_id INT,
supplier_name VARCHAR(40),
supply_type VARCHAR(40),
);
18–3. When to use “On Delete Set Null” and “On Delete
Cascade”
For the branch table, we used “ON DELETE SET NULL". It was okay to do that, because
the mgr_id in the branch table is just a foreign key. It’s not a primary key, which means
that mgr_id is not essential for the branch table.
However, in the branch_supplier table, the branch_id is a foreign key and one of primary
keys. That means that branch_id is crucial for the branch_supplier table. Because a
primary key can NOT have a null value, we should delete data when associated
branch_id is deleted.
message VARCHAR(100)
);
CREATE
ON employee
END$
DELIMITER ;
This worked for me. But, if you’re using ‘PopSQL’, this might not work. In that
case, enter the following code in terminal.
> “tutorial” is the name of the database I created to follow this tutorial.
CREATE
ON employee
END$
DELIMITER ;
Let’s add a new employee!
CREATE
ON employee
ELSE
END IF;
END$
DELIMITER ;
20–2. Attributes
Attributes : Specific pieces of information about an entity
2018 © M
️ ike Dane
2018 © M
️ ike Dane
2018 © M
️ ike Dane
2018 © M
️ ike Dane
2018 © M
️ ike Dane
2018 © M
️ ike Dane
20–8. Relationships
Relationship : defines a relationship between two entities
2018 © M
️ ike Dane
Types : 1 : 1, 1 : N, N : M
2018 © M
️ ike Dane
2018 © M
️ ike Dane
2018 © M
️ ike Dane
# Step 2
The company makes it’s money by selling to clients. Each client has a name and a
unique number to identify it.
# Step 3
The foundation of the company is it’s employees. Each employee has a name, birthday,
sex, salary and a unique number.
# Step 4
# Step 5
Each branch will be managed by one of the employees that work there. We’ll also want to
keep track of when the current manager started as manager.
# Step 6
An employee can act as a supervisor for other employees at the branch, an employee
may also act as the supervisor for employees at other branches. An employee can have at
most one supervisor.
# Step 7
2018 ©️Mike Dane
A branch may handle a number of clients, with each client having a name and a unique
number to identify it. A single client may only be handled by one branch at a time.
# Step 8
Employees can work with clients controlled by their branch to sell them stuff. If necessary,
multiple employees can work with the same client. We’ll want to keep track of how many
dollars worth of stuff each employee sells to each client they work with.
N : M : A client can work with any number of employees and vice versa.
Partial Participation : Not all employees need to have a client.
Total Participation : A client must be handled by a employee.
# Step 9
2018 ©️Mike Dane
Many branches will need to work with suppliers to buy inventory. For each supplier we’ll
keep track of their name and the type of product they’re selling the branch. A single
supplier may supply products to multiple branches.
Weak Entity : An entity that can NOT be uniquely identified by its attributes alone
Identifying Relationship : A relationship that serves to uniquely identify the
weak entity. (* Must be N : M relationship cardinality)
2017 © M
️ ike Dane
For each regular entity type, create a relation(table) that includes all simple attributes of
that entity.
> The primary key of the new relation should be the partial key of the weak entity plus
the primary key of its owner.
Include one side of the relationship as a foreign key in the other. Also, let’s favor total
participation.
Include one side’s primary key as a foreign key on the N side relation(table).
# Result
2017 © M
️ ike Dane
2017 © M
️ ike Dane
Thanks for reading! 🎵 If you like this blog post, please clap👏