Module1 MySQL
Module1 MySQL
Learning Outcomes
At the end of the lesson, the students are able to:
Learn what is SQL
Enumerate popular queries in managing databases
Understand Data Types
Create a basic database using MySQL
Introduction
SQL is a standard language for accessing and manipulating databases. Structured Query Language
(SQL) is a special-purpose programming language designed for managing data held in a Relational Database
Management System (RDBMS). SQL-like languages can also be used in Relational Data Stream Management
Systems (RDSMS), or in "not-only SQL" (NoSQL) databases.
What is SQL?
What is MySQL?
Some database systems require a semicolon at the end of each SQL statement. Semicolon is the standard way
to separate each SQL statement in database systems that allow more than one SQL statement to be executed in
the same call to the server.
Note: SQL keywords are NOT case sensitive: select is the same as SELECT
We all know that databases are important in designing and development of a program. To begin with the
discussion let us first discuss the SQL Database. So how do we create, delete, update, and retrieve a database
using MySQL.
Syntax:
Example:
Return value:
Query OK, 1 row affected (0.05 sec)
Using the created database
The USE statement is use to proceed with the usage of database
Syntax:
Use databasename;
Example
USE imdbaseone;
Return value:
Database Changed
Syntax:
DROP DATABASE databasename;
Example:
DROP DATABASE imdbaseone;
Note: Be careful before dropping a database. Deleting a database will result in loss of complete
information stored in the database! Make sure you have admin privilege before dropping any database.
Once a database is dropped, you can check it in the list of databases with the following SQL
command: SHOW DATABASES;
The BACKUP DATABASE statement is used in SQL Server to create a full back up of an existing SQL
database.
Syntax
BACKUP DATABASE databasename
TO DISK = 'filepath';
A differential back up only backs up the parts of the database that have changed since the last full
database backup.
Syntax
BACKUP DATABASE databasename
TO DISK = 'filepath'
WITH DIFFERENTIAL;
The following SQL statement creates a differential back up of the database "testDB":
Note: A differential back up reduces the back up time (since only the changes are backed up).
The column parameters specify the names of the columns of the table.
The datatype parameter specifies the type of data the column can hold (e.g. varchar, integer,
date, etc.).
SQL CREATE TABLE Example
CREATE TABLE Student (
Studentno int,
LastName varchar(255),
FirstName varchar(255),
Address varchar(255),
City varchar(255)
);
The SQL INSERT INTO Statement
2. If you are adding values for all the columns of the table, you do not need to specify the column
names in the SQL query. However, make sure the order of the values is in the same order as the
columns in the table. Here, the INSERT INTO syntax would be as follows:
INSERT INTO table_name
VALUES (value1, value2, value3, ...);
Demo Database
Student Lastname Firstname Address City
Number
89 Lopez Margarita Santo Tomas Pampanga
90 Jones Esmeralda Guagua Pampanga
91 Gonzales Beatrice San Fernando Pampanga
92 Luvusia Janno Manila Metro Manila
93 Lukarit Maldita Arayat Pampanga
MySQL CREATE TABLE Example
CREATE TABLE Student (
Studentno int,
LastName varchar(255),
FirstName varchar(255),
Address varchar(255),
City varchar(255)
);
INSERT INTO Student (Lastname, Firstname, Address, City,)
VALUES (‘Lopez’, ‘Margarita’, ‘Santo Tomas’, ‘Pampanga’)
The SELECT DISTINCT statement is used to return only distinct (different) values.
Inside a table, a column often contains many duplicate values; and sometimes you only want to list
the different (distinct) values.
The following SQL statement selects all (including the duplicates) values from the "Address"
column in the "Student" table:
4. Create a video demonstration while you do the activity and upload it to youtube.
Syntax
DROP TABLE table_name;
The TRUNCATE TABLE statement is used to delete the data inside a table, but not the table itself.
Syntax
TRUNCATE TABLE table_name;
MySQL ALTER TABLE Statement
The ALTER TABLE statement is used to add, delete, or modify columns in an existing table.
The ALTER TABLE statement is also used to add and drop various constraints on an existing table.
To change the data type of a column in a table, use the following syntax:
ALTER TABLE table_name
MODIFY COLUMN column_name datatype;
SQL is a standard language for accessing and manipulating databases. Structured Query Language
(SQL) is a special-purpose programming language designed for managing data held in a Relational
Database Management System (RDBMS).
The important queries such as Create, select, insert, delete, drop, truncate alter were discuss in the
module.
The creation of table was applied by the use of demo tables.
Module Assessment
1. Log-in to your Neo LMS account using your student login credentials.
2. Online Assessment will be announced via the learning management system calendar
References:
Database Systems Design, Implementation and Management 9th Edition Coronel, Moris, Rob
MySQL™ Notes for Professional
https://www.w3schools.com/
https://www.javatpoint.com/
Learning MySQL by Seyed M.M. “Saied” Tahaghoghi and Hugh E. Williams
https://riptutorial.com/mysql