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

Unit-2-MySQL-Basics

This document serves as an introduction to MySQL and Structured Query Language (SQL), detailing its usage for managing relational databases. It covers essential SQL commands for creating, updating, and deleting databases and tables, along with practical examples of how to execute these commands. The document also outlines learning objectives and outcomes for users seeking to understand and apply MySQL effectively.

Uploaded by

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

Unit-2-MySQL-Basics

This document serves as an introduction to MySQL and Structured Query Language (SQL), detailing its usage for managing relational databases. It covers essential SQL commands for creating, updating, and deleting databases and tables, along with practical examples of how to execute these commands. The document also outlines learning objectives and outcomes for users seeking to understand and apply MySQL effectively.

Uploaded by

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

Introduction to MySQL

Introduction to MySQL 2
UNIT
.

Introduction

Structured Query Language (SQL) is a standard language for accessing and


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

Using SQL, you will be able to execute queries, retrieve data, insert records, update
records, delete records, create new databases, create new tables, create stored
procedures, create views in a database, and can set permissions on tables, procedures,
and views (w3school.com).

Unit Learning Outcomes

At the end of the unit, you will be able to:

a. Use the Command prompt and XAMPP control panel to manage the
database;
b. Understand and explain the MySQL statements, causes and operators;
and
c. Apply the appropriate MySQL statements, clauses, and operators in
database management.

Topic
The Command Prompt and XAMPP Control
1 Panel
Time Allotment: 5 hours

Learning Objectives

At the end of the session, you will be able to use and execute:

a. Launch the command prompt;


b. Manage the XAMPP control panel
c. Create databases and tables;
d. Use databases and describe tables; and
e. Insert records in the table
.

Presentation of Content

What is SQL?

1
Learning Guide in Advanced Database System
Introduction to MySQL

 SQL is the standard language for dealing with Relational Databases.


 SQL is used to insert, search, update, and delete database records.
 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 very popular open-source relational database management system


(RDBMS).

Who Uses MySQL?

 Huge websites like Facebook, Twitter, Airbnb, Booking.com, Uber, GitHub,


YouTube, etc.
 Content Management Systems like WordPress, Drupal, Joomla!, Contao, etc.
 A very large number of web developers around the world.

Show Data On Your Web Site

To build a web site that shows data from a database, you will need:

 An RDBMS database program (like MySQL)


 A server-side scripting language, like PHP
 To use SQL to get the data you want
 To use HTML / CSS to style the page

What is RDBMS?

RDBMS stands for Relational Database Management System.

RDBMS is a program used to maintain a relational database.

RDBMS is the basis for all modern database systems such as MySQL, Microsoft SQL
Server, Oracle, and Microsoft Access.

RDBMS uses SQL queries to access the data in the database.

What is a Database Table?

A table is a collection of related data entries, and it consists of columns and rows.

A column holds specific information about every record in the table.

A record (or row) is each individual entry that exists in a table.

Keep in Mind That...

SQL keywords are NOT case sensitive: select is the same as SELECT. We will write all
SQL keywords in upper-case to differentiate variables and keywords.

A 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.

Some of the most important SQL commands are:

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

2
Learning Guide in Advanced Database System
Introduction to MySQL

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

Log into the MySQL Shell

1. Start XAMPP Control Panel

2. Open the command

prompt

3. In the command prompt, type the following:

 C:\Users\ACER PC>cd\

 C:\>cd xampp

3
Learning Guide in Advanced Database System
Introduction to MySQL

 C:\xampp>cd mysql

 C:\xampp\mysql> cd bin

 C:\xampp\mysql\bin>mysql -uroot

 Note that the command line is now in mysql>.


 You are now ready to create your database.

Create a Database

Let’s create a reciever database.

Naming conventions
Database  dbReceiver
Table  tblReceiver

1. Create a database using the CREATE statement:

4
Learning Guide in Advanced Database System
Introduction to MySQL

CREATE DATABASE receiver;

2. Next, verify that the database was created by showing a list of all databases. Use
the SHOW statement:

SHOW DATABASES;

The terminal prints out a list of databases and information about the time it took to
perform the query:

3. Select the database to make changes to it by using the USE statement:

USE dbReceiver;

Create a Table

We’ll create a table containing information about receivers of parcel:

5
Learning Guide in Advanced Database System
Introduction to MySQL

In the process of creating a table, you need to specify the following information:

 Column names – We are creating the PostalID, Name, House Number, Street,
Barangay, City, Distance, Type columns for our table.
 Varchar of the columns containing characters – Specifies the maximum
number of characters stored in the column.
 The integer of the columns containing numbers – Defines numeric variables
holding whole numbers.
 Not null rule – Indicates that each new record must contain information for the
column.
 Primary key – Sets a column that defines a record.

1. Create a table using the CREATE command. Using the information from
our receiver example, the command is:

CREATE TABLE receiver (PostalID int(6) NOT NULL, Name varchar (200) NOT NULL,
HouseNumber int(5), Street varchar (50), Barangay varchar(50), City varchar(50),
Distance int(5), Type varchar(20), PRIMARY KEY (PostalID));

2. Verify that the table is created using the DESCRIBE command:

DESCRIBE receiver;

The terminal prints out information about the table:


 Field – Indicates column name.
 Type – Specifies data type for the column (varchar for characters, int for
numbers).
 Null – Indicates whether the column can remain with null values.
 Key – Displays the primary column.
 Default – Displays the column’s default value.
 Extra – Indicates additional information about the columns.

6
Learning Guide in Advanced Database System
Introduction to MySQL

3. Insert movie information in column order – title, genre, director, and release year.
Use the INSERT command:

INSERT INTO receiver VALUE (2011, "Charles Babbage", 34, "Bassig St.",
"Ugac Sur", "Tuguegarao City", 8, "Owned");

4. Repeat the previous step with the second record. Use the SELECT command to
display the table:

SELECT * FROM receiver;

The terminal prints out the receiver table:

7
Learning Guide in Advanced Database System
Introduction to MySQL

Topic
The Alter Table, Update, and Drop Statements
4
Time Allotment: 5 hours

Learning Objectives

At the end of the session, you will be able to use and execute:

a. alter statements to change column properties;


b. update statement to modify records;
c. rename tables and databases; and
d. drop statement to delete tables and databases.

Presentation of Content

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

The following SQL adds an "ContactNumber" column to the "receiver" table:

Example:

ALTER TABLE receiver


ADD ContactNumber int (25);

To check effects, describe the table:

8
Learning Guide in Advanced Database System
Introduction to MySQL

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;

The following SQL deletes the "ContactNumber" column from the "receiver" table:

The following SQL deletes the "Email" column from the "Customers" table:

Example:
ALTER TABLE receiver
DROP COLUMN ContactNumber;

ALTER TABLE - ALTER/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;

Example:

ALTER TABLE receiver


MODIFY COLUMN HouseNumber varchar (10);

9
Learning Guide in Advanced Database System
Introduction to MySQL

Let us bring back the original data type of HouseNumber to int (5):

NOT NULL

To create a NOT NULL constraint on the "Name" column in the tblreceiver tables, use the
following SQL:

ALTER TABLE Name


MODIFY Name varchar NOT NULL;

10
Learning Guide in Advanced Database System
Introduction to MySQL

The SQL UPDATE Statement

The UPDATE statement is used to modify the existing records in a table.

UPDATE Syntax

UPDATE table_name
SET column1 = value1, column2 = value2, ...
WHERE condition;

Note: Be careful when updating records in a table! Notice the WHERE clause in
the UPDATE statement. The WHERE clause specifies which record(s) that should be
updated. If you omit the WHERE clause, all records in the table will be updated!

UPDATE Table

The following SQL statement updates the first parcel receiver (PostalID = 2011) with a
new HouseNumber and a new Street.

Example

UPDATE Receiver
SET HouseNumber = 101, Street = “Reyes St.”
WHERE PostalID = 2011;

UPDATE Multiple Records

It is the WHERE clause that determines how many records will be updated.

The following SQL statement will update the Type to "OWNED" for all records where city
is "Tuguegarao City":

11
Learning Guide in Advanced Database System
Introduction to MySQL

Demo table:

UPDATE Receiver
SET Type = “OWNED”
WHERE City = ”Tuguegarao City”;

Update Warning!

Be careful when updating records. If you omit the WHERE clause, ALL records will be
updated!

UPDATE Receiver
SET Barangay = “Centro 1”;

Rename MySQL Column with the RENAME Statement

The simplest way to rename a column is to use the ALTER TABLE command with the
RENAME COLUMN clause. This clause is available since MySQL version 8.0.

Let’s illustrate its simple syntax. To change a column name, enter the following
statement in your MySQL shell:

The simplest way to rename a column is to use the ALTER TABLE command with the
RENAME COLUMN clause. This clause is available since MySQL version 8.0.

Let’s illustrate its simple syntax. To change a column name, enter the following
statement in your MySQL shell:

12
Learning Guide in Advanced Database System
Introduction to MySQL

ALTER TABLE table_name RENAME COLUMN old_column_name TO


new_column_name;

Replace table_name, old_column_name, and new_column_name with your table and


column names. Keep in mind that you cannot rename a column to a name that already
exists in the table.

For instance, to change the column Name into NameOfReceiver in the table receiver,
you would run:
ALTER TABLE Receiver
RENAME column Name TO NameOfReceiver;

The RENAME COLUMN statement can only be used to rename a column. If you need
additional functions, such as changing the data definition, or position of a column, use
the CHANGE clause instead.

Note: The word COLUMN is obligatory for the ALTER TABLE RENAME COLUMN
command. ALTER TABLE RENAME is the existing syntax to rename the entire table.

Rename MySQL Column with the CHANGE Statement

The CHANGE clause offers important additions to the renaming process. It can be used
to rename a column and change the data type of that column with the same command.

Enter the following command in your MySQL client shell to change the name of the
column and its definition:

ALTER TABLE table_name


CHANGE old_column_name new_col_name Data Type;

You can change the data type of the column or keep the existing one. In both cases you
have to specify the data type as the element is mandatory.

For example, to change the column PostalID into ReceiverID which has the data type
VARCHAR(10) in the table receiver, you would run:

ALTER TABLE receiver


CHANGE PostalID ReceiverID VARCHAR(10);

How to RENAME a Table

We can use the ALTER TABLE statement to change the table name.

Syntax:

13
Learning Guide in Advanced Database System
Introduction to MySQL

ALTER tableName RENAME TO newTableName;

Also, the RENAME TABLE statement is used to change the table name.

Syntax:

RENAME TABLE tableName TO newTableName;

Example

RENAME TABLE receiver TO tblReceiver;

How to Delete in MySQL

To delete rows in a MySQL table, use the DELETE FROM statement:

DELETE FROM table WHERE Condition;

The WHERE clause is optional, but you'll usually want it, unless you really want to delete
every row from the table.

Example

DELETE FROM receiver WHERE ReceiverID = 2019;

14
Learning Guide in Advanced Database System
Introduction to MySQL

The SQL DROP TABLE Statement

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

Syntax

DROP TABLE table_name;

Example

DROP TABLE tblReceiver;

The SQL DROP DATABASE Statement

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

Syntax

DROP DATABASE databasename;

Show existing databases:

15
Learning Guide in Advanced Database System
Introduction to MySQL

Example

DROP DATABASE receiver;

16
Learning Guide in Advanced Database System
Introduction to MySQL

Summary Unit

Use CREATE statement to add new databases and tables.

Use DROP statement to delete the database and tables.

Use UPDATE statement to update data in a database table.

Use INSERT statement to add one or more rows into a table.

Use aggregate functions to filter data.

References

W3school.com (n.d.). SQL Tutorial. Retrieved from https://tinyurl.com/kvbazps

17
Learning Guide in Advanced Database System

You might also like