Database Design and Introduction To MySQL Day - 3
Database Design and Introduction To MySQL Day - 3
AI-ML
1
Module Name: Database
Design and Introduction to
MySQL
Course : DBMS
Edit Master text styles
Lecture On : Database
Design and Introduction to
MySQL - Day - 3
Instructor :
2
Today’s Agenda
● Revision
● DDL Statements
● DML Statements
● SQL Basic Statements and Operators
● Aggregate and Inbuilt Functions
● String functions
● Key Takeaways
● Semantic Constraints
● Understanding ERD
● Basics of SQL
DDL Statements
● We continue learning SQL with the basic syntaxes, starting with DDL statements. It
is important to understand the two primary subcategories of SQL commands before
learning these syntaxes:
● DDL is responsible for defining the structure of the data, creating and modifying
database objects like types and attributes.
● You can refer to the documentation provided on the platform to install MySQL on
your laptop before proceeding to learn how to write DDL commands using SQL.
DDL Statements
● Now, we will learn how to use the CREATE command and execute the
following operations using this syntax:
● After creating the table, we can use the ALTER command to make the
following modifications to the table:
○ Make the ‘ID’ column in the ‘customers’ table as the primary key.
○ Add a new column named ‘employer’ to the ‘customers’ table with
VARCHAR(32) as the data type.
○ Drop (delete) the ‘employer’ column from the ‘customers’ table.
● Let’s also look at how to use the DROP command to execute the following
operations:
● To better remember the differences between DDL and DML, please refer to the
comparison table below:
DML Statements
DML Statements
● Now, let's proceed to the implementation of each of these commands through code.
For demonstration purposes, we have already created a table called 'transportation'
which is shown below.
DML Statements
● The INSERT command was used to add the following records to the table:
○ ('DELIVERY TRUCK', 'Ashok Leyland', false)
○ ('REGULAR AIR', ‘Air India', false)
● We can use the UPDATE command to modify the ‘toll_required’ attribute to ‘True’
wherever the ‘ship_mode’ value was ‘DELIVERY TRUCK’.
● In this demonstration, we have used the WHERE command, which filters out
specific rows of a table.
● The DELETE command was used to delete all rows where the value of the
‘vehicle_company’ attribute was ‘AIR INDIA’.
● If you execute this command without the WHERE clause, it will delete all rows of the
table.
DML Statements
● Using the ALTER command, we add a column named ‘vehicle_number’ and set its
data type to varchar(32).
● Following this, we use the UPDATE command to update all records in this column
to ‘MH-05-R1234’.
● Besides DDL and DML, there may be additional subcategories in SQL. You can
refer to this link to explore the different commands in SQL.
● If you want to view your table at any point, use this command:
○ SELECT * FROM table_name
● This case study is about 'market_data', where you are given a total of 5 tables. The
description of the tables is as follows:
● cust_dimen: This table contains information about customers. It defines their customer
ID, customer name, city, state and the nature of their business.
● shipping_dimen: This table stores the data of the shipping modes for each order ID
placed by the customers. Its attributes are order_ID, ship_mode and ship_date.
● orders_dimen: This table contains information about the orders placed by customers. It
includes the Order_Number, order_ID, order_date and the order_priority.
● The order ID denotes the ID of the order placed by the customer. A customer may order
multiple products together, where the ID of each product is given by the Order_Number.
SQL Basic Statements and Operators
● prod_dimen: This table contains information about the products of a company that
are going to be sold to the customers. It includes information about product types
and product subcategories.
● Market_fact_full: This table contains the details of each order, the customer who
placed the order, the shipping details and the product details. It contains the foreign
keys of all four tables described above.
● Download the workbench given below, which includes the query for table creation
and a few comments. Further on in the module, you are expected to use this
workbench to practice.
● The LIKE operator checks whether a string contains a specified pattern using two
wildcards:
● Aggregate and built-in functions are essential for data analysis and extracting
insights. The fundamental functions include:
○ COUNT() counts the total number of records that meet the specified condition.
○ SUM() provides the total number of non-null values.
○ AVG() returns the average of non-null values.
○ MIN() and MAX() return the minimum and maximum values, respectively,
excluding null values.
● The GROUP BY clause is used to group data by a specified condition and perform
operations on individual groups.
● In addition, there are inbuilt functions such as UPPER and LOWER that convert
strings to upper and lower cases, respectively.
Aggregate and Inbuilt Functions
● Let's examine the usage of the GROUP BY and HAVING clauses..
Aggregate and Inbuilt Functions
● Each of the code snippets is explained in the table below.
String Functions
● Several other string functions can also provide useful insights. Let's explore some of
these functions.
● There are numerous additional functions that may be helpful. For more information, refer
to the following resources:
○ String functions
Poll Question
Q. Which type of SQL statements modify the data stored in a database?
A) DDL Statements
B) DML Statements
C) SQL Basic Statements and Operators
D) Aggregate and Inbuilt Functions
Key Takeaway
● DDL Statements
● DML Statements
● SQL Basic Statements and Operators
● Aggregate and Inbuilt Functions
● String functions
Thank You!
25