MCIT DB SQL
MCIT DB SQL
2- Introduction to SQL:
▪ What is SQL.
▪ Preparing MySQL Environment.
▪ Data Definition Language (DDL)
▪ Data Control Language (DCL)
▪ Data Manipulation Language (DML)
File Based System Limitations:
3 Non-functional testing.
(Example: Stress testing).
Entity Relationship Diagram (ERD):
It identifies information required by the business by displaying the relevant
Entities and Relationships between them.
Entity: it’s a thing in the real world with independent existence and can
be described with a set of attributes.
Entity Relationship Diagram (ERD):
There should be always relationships between entities and there shouldn’t
be an entity without any relationship.
Attributes:
1Single or simple Attributes. (Ex: ID, SSN).
Weak Entity:
It doesn’t have a key attribute.
It must be fully dependent on another entity.
It’s represented in a double rectangle.
Relationships:
For each relationship, We need to identify:
1- Degree of relationship
2- Cardinality
3- Participation
Degree of relationship:
It’s the number of participating entities.
Binary Relationship Unary Relationship Ternary Relationship
(Two Entities) (Entity and itself) (Three Entities)
Cardinality
It specifies the maximum number of relationships.
Must May
It means that every student must have It means that courses may have
enrolled at least in one course students enrolled in it
Relationships
For each relationship, We need to identify:
Every Table should have a primary key (may be one column or more than one column).
Primary Key:
A column which values are unique.
Can’t contain null values.
Database Constraints:
1 Primary Key Constraints:
Not Null.
Unique.
4 Check Constraints. (Check constraints on a salary column Minimum 1000, Max 2000).
Mapping ERD To Tables
1- Mapping Strong Entity:
1Single or Separate attributes are represented by separate columns.
2Composite Attributes (Each sub part of Composite attribute is represented by a column (first
name, Last name).
3Primary key is chosen from the candidate key (Shortest Unique combination in storage) if we
have more than one candidate key, we choose the less in storage (Numbers is chosen over text –
less storage-
1- Mapping Strong Entity:
4Multi valued attribute is made in a new table person-Hobbies (Primary key of Person as a
foreign Key & Hobbies Column), and the combination of the FK and the Hobbies are the
primary key of this table.
5Derived Attribute usually we don’t store it in our DB, however each time we need to deal with
it, it will be called and the calculation will be processed which may affect our performance in
the DB system. So we would store it in our DB if we use it frequently.
1- Mapping Strong Entity:
Person (SSN, Address, First_Name, Last_Name, BirthDate)
Dependents(employee_id, dependent_name)
3- Mapping One to Many Relationship:
▪ Binary: Adding PK of one side as a FK in Many side (PK of customer as FK in order).
May-Must:
▪ PK of May side as a FK in the Must side.
▪ License(license_id, license_date, person_id)
Must-Must:
▪ We merge both tables in a new table and we can
choose any PK of them as a new PK of the new
Table.
SQL is a query language, whereas MySQL is a relational DBMS that uses SQL to query a
database.
You can use SQL to access, update, and manipulate the data stored in a database. However,
MySQL is a database that stores the existing data in a database in an organized manner.
SQL is used for writing queries for databases, MySQL facilitates data storing, modifying, and
management in a tabular format.
SQL Syntax
▪ SQL follows some unique set of rules and guidelines called syntax. Here, we are providing
all the basic SQL syntax.
▪ SQL is not case sensitive. Generally SQL keywords are written in uppercases.
▪ You can perform most of the action in a database with SQL statements.
SQL Commands:
Data Definition Language (DDL):
▪ It’s used to create and modify the structure of database objects in a database.
❑ CREATE
❑ ALTER
❑ DROP
❑ TRUNCATE
❑ RENAME
CREATE
It’s used to create Tables Or Database
Col Data
Constraint
Name Type(size)
ALTER TABLE
It is used to add, delete, modify or rename columns in an existing table.
DROP COLUMN
It’s used to delete a column in the table.
MODIFY COLUMN
It’s used to change the data type of a column.
RENAME COLUMN
It’s used to RENAME an existing COLUMN in a database.
DROP TABLE
used to delete an existing table in a database.
TRUNCATE TABLE
used to delete the data inside a table, but not the table itself.
Data Control Language (DCL):
It’s used to give access privileges to the data.
GRANT
▪ It gives DML privileges to certain users.
REVOKE
▪ It removes the privileges given to certain users.
For More Info and Examples about Revoke:
https://www.mysqltutorial.org/mysql-revoke.aspx
Data Manipulation Language(DML):
It’s used to access and manipulate data in existing tables.
INSERT
It’s used to insert a record in the table.
Note:
If WHERE Clause isn’t written, All records in the table will be updated.
DELETE
It’s used to delete existing records in a table.
Note:
If WHERE Clause isn’t written, All records in the table will be deleted.
Difference between DELETE and TRUNCATE
DELETE TRUNCATE
Syntax: Syntax:
DELETE FROM Table_Name; TRUNCATE TABLE Table_Name;
or
DELETE FROM Table_Name
WHERE Condition;
Delete all the data and keeps the structure of the Delete all the data and keeps the structure of the
table (If WHERE Clause isn’t used) table.
Removes the rows one by one so it’s slower. It actually Deletes the table at once and creates a
new one. So it’s faster.
▪ When two or more columns are mentioned after DISTINCT, it returns values that aren’t
duplicated for both of the columns together.
Assignment:
1. From products table: Return all the products with these columns
(name, unit price, new price [unit price*1.1]).
JOINS
SQL JOINs
▪ A JOIN clause is used to combine rows from two or more tables, based on a related column
between them.
JOIN Types:
INNER JOIN
▪ It returns records that have matching values in both tables.
▪ We can get these common records using two methods as mentioned below.
USING Clause
INNER JOIN
▪ INNER JOIN 3 tables:
INNER JOIN
▪ INNER JOIN 3 tables:
INNER JOIN
▪ INNER JOIN 3 tables:
OUTER JOINs
▪ OUTER JOIN is classified into 3 main types.
LEFT JOIN
▪ It returns all records from the left table (table1), and the matching records from the right table (table2).
The result is 0 records from the right side, if there is no match.
LEFT JOIN (Inclusive)
LEFT JOIN (Exclusive)
RIGHT JOIN
▪ It returns all records from the right table (table2), and the matching records from the left table (table1).
The result is 0 records from the left side, if there is no match.
RIGHT JOIN (Inclusive)
RIGHT JOIN (Exclusive)
SELF JOIN
▪ A self join is a regular join, but the table is joined with itself.
SELF JOIN
▪ We handle them as two separate tables, the first one for the employees and the other is for the managers.
SELF JOIN
▪ A self join is a regular join, but the table is joined with itself.
CROSS JOIN
▪ It JOINS tables by matching every row from one table with every row from another table. If there are
X rows in the first table, and Y rows in the second table, the result set will have exactly X times Y rows.
UNION
▪ It JOINS tables by matching every row from one table with every row from another table. If there are
X rows in the first table, and Y rows in the second table, the result set will have exactly X times Y rows.
UNION
▪ It JOINS tables by matching every row from one table with every row from another table. If there are
X rows in the first table, and Y rows in the second table, the result set will have exactly X times Y rows.
FULL JOIN
▪ It returns all records when there is a match in left (table1) or right (table2) table records.
▪ Office_id 2 employees:
ANY (TRUE if any of the subquery values meet the
condition)
▪ ANY is a multi row operator, we should use it when a
subQuery will provide more than one result.
▪ Office_id 5 employees :
AGGREGATE functions
▪ COUNT() >> It doesn’t count NULL values
▪ MAX()
▪ MIN()
▪ AVG() >> It doesn’t take into consideration the NULL values.
▪ SUM()
GROUP BY
▪ It groups rows that have the same values in a certain column.
▪ It is often used with aggregate functions to group the result-set
by one or more columns.
Exercise:
▪ Display customer_id, first_name and number of orders for each customer and order them by number of
orders.
Exercise:
▪ Display customer_id, first_name and number of orders for each customer and order them by number of
orders.
HAVING
▪ It is a condition used with Aggregate functions instead of WHERE.
Exercise:
▪ Display the office_id, maximum salary, avg salary and the city for each department, given that its average
should be greater than 90000, and sort them by office_id.
Exercise:
▪ Display the office_id, maximum salary, avg salary and the city for each department, given that its average
should be greater than 90000, and sort them by office_id.
Order of operations:
THANK YOU!