Unit-2-MySQL-Basics
Unit-2-MySQL-Basics
Introduction to MySQL 2
UNIT
.
Introduction
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).
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:
Presentation of Content
What is SQL?
1
Learning Guide in Advanced Database System
Introduction to MySQL
To build a web site that shows data from a database, you will need:
What is RDBMS?
RDBMS is the basis for all modern database systems such as MySQL, Microsoft SQL
Server, Oracle, and Microsoft Access.
A table is a collection of related data entries, and it consists of columns and rows.
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.
2
Learning Guide in Advanced Database System
Introduction to MySQL
prompt
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
Create a Database
Naming conventions
Database dbReceiver
Table tblReceiver
4
Learning Guide in Advanced Database System
Introduction to MySQL
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:
USE dbReceiver;
Create a Table
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));
DESCRIBE receiver;
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:
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:
Presentation of Content
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.
Example:
8
Learning Guide in Advanced Database System
Introduction to MySQL
To delete a column in a table, use the following syntax (notice that some database
systems don't allow deleting a column):
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;
To change the data type of a column in a table, use the following syntax:
Example:
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:
10
Learning Guide in Advanced Database System
Introduction to MySQL
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;
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”;
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
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.
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:
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:
We can use the ALTER TABLE statement to change the table name.
Syntax:
13
Learning Guide in Advanced Database System
Introduction to MySQL
Also, the RENAME TABLE statement is used to change the table name.
Syntax:
Example
The WHERE clause is optional, but you'll usually want it, unless you really want to delete
every row from the table.
Example
14
Learning Guide in Advanced Database System
Introduction to MySQL
Syntax
Example
Syntax
15
Learning Guide in Advanced Database System
Introduction to MySQL
Example
16
Learning Guide in Advanced Database System
Introduction to MySQL
Summary Unit
References
17
Learning Guide in Advanced Database System