0% found this document useful (0 votes)
13 views

Module1 MySQL

The document provides information about SQL and MySQL. It discusses that SQL is used to manage data in databases and MySQL is a popular open-source database. It then covers how to create databases and tables in MySQL using SQL statements like CREATE DATABASE, CREATE TABLE, INSERT, SELECT, and DROP TABLE. Examples are given for each statement.

Uploaded by

raveniel00000
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views

Module1 MySQL

The document provides information about SQL and MySQL. It discusses that SQL is used to manage data in databases and MySQL is a popular open-source database. It then covers how to create databases and tables in MySQL using SQL statements like CREATE DATABASE, CREATE TABLE, INSERT, SELECT, and DROP TABLE. Examples are given for each statement.

Uploaded by

raveniel00000
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

Module 1.

Laboratory: Getting Started with 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?

 SQL stands for Structured Query Language


 SQL lets you access and manipulate databases
 SQL became a standard of the American National Standards Institute (ANSI) in 1986, and of the
International Organization for Standardization (ISO) in 1987.

What Can SQL do?

 SQL can execute queries against a database


 SQL can retrieve data from a database
 SQL can insert records in a database
 SQL can update records in a database
 SQL can delete records from a database
 SQL can create new databases
 SQL can create new tables in a database
 SQL can create stored procedures in a database
 SQL can create views in a database
 SQL can set permissions on tables, procedures, and views

What is MySQL?

 MySQL is a widely used relational database management system (RDBMS).


 MySQL is free and open-source.
 MySQL is ideal for both small and large applications.
 MySQL is a relational database management system
 MySQL is open-source
 MySQL is free
 MySQL is ideal for both small and large applications
 MySQL is very fast, reliable, scalable, and easy to use
 MySQL is cross-platform
 MySQL is compliant with the ANSI SQL standard
 MySQL was first released in 1995
 MySQL is developed, distributed, and supported by Oracle Corporation
 MySQL is named after co-founder Monty Widenius's daughter: My

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

The Most Important SQL Commands

SELECT - extracts data from a database


UPDATE - updates data in a database
DELETE - deletes data from a database
INSERT INTO - inserts new data into a database
CREATE DATABASE - creates a new database
ALTER DATABASE - modifies a database
CREATE TABLE - creates a new table
ALTER TABLE - modifies a table
DROP TABLE - deletes a table
CREATE INDEX - creates an index (search key)
DROP INDEX - deletes an index

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.

Creating a database in MySQL

The CREATE DATABASE statement is used to create a new SQL database.

Syntax:

CREATE DATABASE databasename;

Example:

CREATE DATABASE imdbaseone;

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

The SQL DROP DATABASE Statement


The DROP DATABASE statement is used to drop an existing SQL database.

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 SQL BACKUP DATABASE Statement

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';

The SQL BACKUP WITH DIFFERENTIAL Statement

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;

BACKUP DATABASE Example


The following SQL statement creates a full back up of the existing database " imdbaseone;" to the D
disk:

BACKUP DATABASE imdbaseone;


TO DISK = 'D:\backups\ imdbaseone.bak';

BACKUP WITH DIFFERENTIAL Example

The following SQL statement creates a differential back up of the database "testDB":

BACKUP DATABASE testDB


TO DISK = 'D:\backups\ imdbaseone.bak';
WITH DIFFERENTIAL;

Note: A differential back up reduces the back up time (since only the changes are backed up).

Creating a table in MySQL


The CREATE TABLE statement is used to create a new table in a database.
Syntax

CREATE TABLE table_name (


column1 datatype,
column2 datatype,
column3 datatype,
....
);

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

The INSERT INTO statement is used to insert new records in a table.

INSERT INTO Syntax

It is possible to write the INSERT INTO statement in two ways:

1. Specify both the column names and the values to be inserted:

INSERT INTO table_name (column1, column2, column3, ...)


VALUES (value1, value2, value3, ...);

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 MySQL SELECT Statement

The SELECT statement is used to select data from a database.


The data returned is stored in a result table, called the result-set.

SELECT column1, column2, ...


FROM table_name;

SELECT Columns Example (Based from the demo table)


SELECT Lastname, Firstname, Address FROM Student;

The MySQL SELECT DISTINCT Statement

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.

SELECT DISTINCT column1, column2, ...


FROM table_name;

SELECT DistinctExample (Based from the demo table)

SELECT DISTINCT Lastname, Firstname FROM Student;

SELECT Example Without DISTINCT

The following SQL statement selects all (including the duplicates) values from the "Address"
column in the "Student" table:

SELECT Address FROM Student;


Activity 1.1 Database Creation

1. Create a database db_mp2_initial.

2. Create a table employee. (Use the table given)

3. Then use the query Select Distinct.

4. Create a video demonstration while you do the activity and upload it to youtube.

5. Then paste, the youtube link in the given assignment section.

Employee Last First Middle


Number Name Name Name Gender Department Designation Salary
001 Lopez Jennifer Pineda Female Accounting Supervisor 20000
002 Alba Jessica Santos Female Finance Supervisor 25000
003 Bradley Manny De Leon Male Academic Faculty 30000
General
004 Sanchez Joana Tiamzon Female Services Maintenance 15000
Program
005 Lapuz Mary Joy Pecson Female Academic Head 31000
General
006 Moreno Sharon Santos Female Services Electrician 12000
007 Padilla Coco Pascual Male Academic Dean 33000
008 Geronimo Lito Cruz Male Accounting Assistant 21000
009 Salonga Sarah Dizon Female Finance Assistant 26000
010 Pineda Oliver Galang Male Academic Dean 28000

The MySQL DROP TABLE Statement


The DROP TABLE statement is used to drop an existing table in a database.

Syntax
DROP TABLE table_name;

MySQL TRUNCATE TABLE

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.

ALTER TABLE - ADD Column


To add a column in a table, use the following syntax:

ALTER TABLE table_name


ADD column_name datatype;

ALTER TABLE - DROP COLUMN


To delete a column in a table, use the following syntax (notice that some database systems don't
allow deleting a column):
ALTER TABLE table_name
DROP COLUMN column_name;

ALTER TABLE - MODIFY COLUMN

To change the data type of a column in a table, use the following syntax:
ALTER TABLE table_name
MODIFY COLUMN column_name datatype;

Activity 2 Table Creations and alter, drop, update, Queries Usage


1. In your current database “database db_mp2_initial”, create a table order_tbl. (Use the
table given)
2. Create customertbl. (Use the table given)
3. Then, after that use the query “alter, update, etc.” that will modify the second table. See
modified table below.
4. Then lastly, show how to delete the column birthdate.
5. Create a video demonstration while you do the activity and upload it to youtube.
6. Then paste, the youtube link in the given assignment section.
Order_ID CustomerID Employee_Number OrderDate ShipperID
10001 1 1 07-25-2021 2
10002 2 2 01-10-1996 5
10003 3 3 08-10-2018 10
10004 4 4 01-13-2012 1
10005 5 5 09-09-2016 10
10006 6 6 06-11-2017 9
10007 7 7 10-21-2020 8
10008 8 8 08-07-2015 3
10009 9 9 10-06-2014 5
10010 10 10 08-23-2009 1
Order Table

Customer ID Customer Name Address City


1 Jamaima Nucup San Fernando Pampanga
2 Jamaica Lanaza Arayat Pampanga
3 Lyngie Manalang Guagua Pampanga
4 Vivienne Aquino Porac Pampanga
5 Jhoanna Santiago Sampaloc Manila
6 John Haven David Pasong Tamad Quezon City
7 Sharlene Domingo Pasong Tamo Pasay
8 Sherwin Guevarra Dasmarinas Cavite
9 Larry Justine La Juerta Paranaque
Santiago
10 Kenneth Espino Florida Pampanga
Customer Table

Customer Customer Name Address City Contact_no Birthdate


ID
1 Jamaima Nucup San Fernando Pampanga 8850987 03-13-1989
2 Jamaica Lanaza Arayat Pampanga 091598191445 12-06-1992
3 Lyngie Manalang Guagua Pampanga 56782017481 10-31-1994
4 Vivienne Aquino Porac Pampanga 0926479174 09-11-1986
5 Jhoanna Santiago Sampaloc Manila 14562019857
6 John Haven David Pasong Tamad Quezon City 45201984756
7 Sharlene Domingo Pasong Tamo Pasay 14352960491
8 Sherwin Guevarra Dasmarinas Cavite 12657482910
9 Larry Justine La Juerta Paranaque 7689024510
Santiago
10 Kenneth Espino Florida Pampanga 7683128604
Modified Table
Module Recap

 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

You might also like