Creating a Database in MySQL
Implementing Database in MySQL – Part 2
Division 4
Forward: In this part of the series, we see how to create a database in MySQL.
By: Chrysanthus Date Published: 6 Aug 2012
Introduction
Note: If you cannot see the code or if you think anything is missing (broken link, image absent), just contact me at forchatrans@yahoo.com. That is, contact me for the slightest problem you have about what you are reading.
Working with a Database
To work with a MySQL database you have to follow the following steps:
- Create the database (directory).
- Start the database server (in memory) that will run the database.
- Start the database (put it in memory).
- Connect user to the database (in memory).
- Use (create tables, save data, retrieve data, etc) the database.
- Disconnect from the database.
- Stop the database (remove from memory).
- Stop the database server.
Note: when you complete anything you do with the database such as create table or send data, the result is automatically saved to disk; there is no command or statement to save changes. However, in some cases you can have the changes in memory and then flush the changes to the database in disk.
In this series, steps 6, 7 and 8 will be combined, as we saw in the previous part of the series.
Consider the database of a wholesale company that orders products from manufacturers and sells to shops in the cities. Let the database name be, wholesale. We shall create yhe database for this company called, wholesale. This database is to track the ordering and sales of products for shops (supermarkets) by the wholesale company.
Creating a Database
- Open the command prompt window and type, cd c: Enter, to go to the root directory of your computer.
- Start and connect to the server with the following command; type the password (sql) immediately after:
"C:Program FilesMySQLMySQL Server 5.1binmysql" -u root -p
You should have the mysql prompt. From now onward, you will be typing SQL statements until you type, QUIT, to quit.
- To create the database, type the following SQL statement (ending with a semicolon) and press Enter.
CREATE DATABASE wholesale;
A confirmation message should appear. It is not very explicit. Now, CREATE and DATABASE are reserved words for MySQL. Reserved words are words that you do not use arbitrarily. wholesale is the name of the database. A database in MySQL is implemented as a directory containing files that correspond to tables in the database. The database directory is created in the MySQL data directory.
In MySQL you cannot use a database after you have just created it. You have to state explicitly that you want to use the database. The USE SQL statement is used for this.
- Execute (type and press Enter) the following statement which will allow you to use the database you have just created:
USE wholesale;
You should see the feedback, “Database changed” displayed. It means you have changed from whatever database you were with to this one.
Let us leave the database and server and so disconnecting from the server.
Type the following command without the semicolon and press Enter:
QUIT
You should see, Bye.
We have learned how to use a database and leave it with the server. Let us stop here and continue in the next part of the series.
Chrys
Related Links
Major in Website DesignWeb Development Course
HTML Course
CSS Course
ECMAScript Course
NEXT