You are on page 1of 3

CHAPTER THREE

3. MySQL DATABASE
Learning Objectives: After completing this chapter, you will be able to do the following:
• MySQL queries
• Create the new database
• Drop database
• Rename database
• USE database
• Select database function

3.1. MySQL Queries


MySQL supports data definition languages (create table, create view, drop table, truncate table,
alter table etc.), data manipulation languages (select, insert, update and delete), data control
languages (grant, revoke etc.) and transaction control languages (commit, rollback, savepoint). A
list of commonly used MySQL queries to create database, use database, create table, insert record,
update record, delete record, select record, truncate table and drop table are given below.
MySQL Create Database: MySQL create database is used to create database.
Example: Create database WKU;
Use Database: It is used to select database. Example USE WKU;
Create Table: It is used to create a table, view, procedure and function.
Example: CREATE TABLE customer (ID int, FirstName varchar(50), city varchar(50),
PRIMARY KEY (ID));
Alter Query: It is used to add, modify, delete or drop columns of a table. Let's see a query to add
column in customer table:
Example: ALTER TABLE customer ADD age varchar(50);
Insert Query: It is used to insert records into table.
Example: Insert into customer values(12,'Rahel','Muse');
Update Query: It is used to update records of a table.
Example: Update customer set name='Abebe', city='Adama' where id=12;
MySQL Delete Query: It is used to delete records of a table from database. Example: Delete from
customer where id=12;
Select Query: It is used to fetch records from database.
1
Example: SELECT * from customer;
Truncate Table Query: It is used to truncate or remove records of a table. It doesn't remove
structure. Example: Truncate table customer;
Drop Query: It is used to drop a table, view or database. It removes structure and data of a table
if you drop table. For example: Drop table customer;
3.2. Create Database
The SQL CREATE DATABASE statement is used by a developer to create a database. In
MySQL, same command is used to create a database.
Syntax: CREATE DATABASE database-name;
Example: CREATE DATABASE WKU;
You can check the created database by the following query:
Example: SHOW DATABASES;
Note: All the database names, table names and table fields name are unique in the RDBMS. You
must have to use proper and unique names while giving any MySQL command.
Note: Ate the end of each query enter a semicolon to complete the statement, and to executes it.

3.3. Use Database


USE Database is used in MySQL to select a particular database to work with. This query is used
when multiple databases are available with MySQL Server. You can use SQL command USE
to select a particular database.
Syntax: USE database_name;
Let's take an example to use a database name "WKU".
Example: USE WKU;
3.4. Drop Database
You can drop/delete/remove a MySQL database easily with the MySQL command. If you delete
or drop the database, all the tables and views will also be deleted. So be careful while using this
command.
Syntax: DROP DATABASE database_name;
Let's take an example to drop a database name "WKU"
Example: DROP DATABASE WKU;
Now you can check that either your database is removed by executing the following query:
Example: SHOW DATABASES;
2
3.5. Rename Database
MySQL RENAME DATABASE is used when you need to change the name of your database.
Sometimes it is used because you think that the original name is not more relevant to the database
or you want to give a temporary name to that database. Let's see how to rename MySQL databases.
To rename the MySQL database, you need to follow the following syntax:
Syntax: RENAME DATABASE old-db-name TO new-dname; //which is not work after
MySQL server 5.7. version.
3.6. Select Database Function
To find out which database is currently selected, use the DATABASE() function:
Syntax: SELECT DATABASE();
If you have not yet selected any database, the result is NULL.
Exercise: Create a new database its name is Wolkite?
Exercise: Select Wolkite database?
Exercise: Delete Wolkite database?

Summary
You create a database with the CREATE DATABASE database_name command. Each database
name in MySQL server must be unique. Each table name in a database must be unique. You use a
database with the USE database_name command then the database is changed. Also, you can
drop/delete/remove a MySQL database easily with the DROP DATABASE databse_name
command. When you drop a database be careful because all the data in the database is removed.
In addition, you can find out which database is currently selected by using SELECT DATABASE()
function and you can rename the database in MySQL.
Review Questions
1. Discuss the difference between DDL, DML, TCL and DCL?
2. What is database?
3. How do you create, drop, rename and use database in MySQL?
4. What problem will occur when you drop a database?
5. Can you create a table without use a specific database? Why?
6. List and describe MySQL database objects?
7. What is query?

You might also like