Creation and Use of Database in MySQL
The prerequisites for this document are that MySQL is installed and the
MySQL daemon is running. The following guidelines are for creating a
simple database, creating tables in that databse and then populating
these tables in MySQL.
Step1: Connecting to the MySQL Server
In order to connect to the server, you will have to provide a hostname, username and password. Of these, hostname and password may not be needed. Contact
your MySQL administrator to know what parameters are needed.
For example, the following can be the steps performed to connect to
MySQL Server:
[purshah@gordon
purshah]$ cd mysql-standard-4.0.14-pc-linux-i686/
[purshah@gordon
mysql-standard-4.0.14-pc-linux-i686]$ ./bin/mysql -u purshah -p
Enter
password:
The following is generally displayed:
Welcome to
the MySQL monitor. Commands end with ; or \g.
Your MySQL
connection id is 9 to server version: 4.0.14-standard
Type 'help;'
or '\h' for help. Type '\c' to clear the buffer.
mysql>
Now you are connected to the MySQL Server.
Step 2: Creating and Using a Database
In this step, we shall have a look at how to
- Create a database
- Create a table
- Load data into the table
- Retrieve data from the table in various ways
To create a database, the user should have the corresponding
privileges. In case the user is not allowed to create a database, the
MySQL administrator must be contacted. Also, every user is generally
allowed to use the test
database within MySQL. One can check the databases that can be used by
using the command show
databases;.
mysql>
show databases;
+----------+
| Database |
+----------+
|
test |
+----------+
1 rows
in set (0.01 sec)
In order to make use of a database, the use
<database_name> command must be used.
mysql> use
test;
Database
changed
The next thing to do is to check what tables are already present. this
is done using the show
tables; command.
mysql>
show tables;
Empty set
(0.00 sec)
A table can be created by using create table
statement.
mysql>
create table temp (id int, firstname varchar(20), lastname varchar(20));
Query OK, 0
rows affected (0.00 sec)
mysql>
show tables;
+----------------+
|
Tables_in_test |
+----------------+
|
temp |
+----------------+
1 row in set
(0.00 sec)
The table can be populated in a number of ways using the insert statement.
mysql>
insert into temp values (1, 'Bimal', 'Shah');
Query OK, 1
row affected (0.00 sec)
mysql>
insert into temp values (2, 'Kaustubh', 'Nagarkar');
Query OK, 1
row affected (0.00 sec)
mysql>
insert into temp values (3, 'Kiran', 'Annaiah');
Query OK, 1
row affected (0.00 sec)
The entries of a table can be displayed using a select - from - where
statement.
mysql>
select * from temp;
+------+-----------+----------+
|
id | firstname | lastname |
+------+-----------+----------+
|
1 | Bimal | Shah |
|
2 | Kaustubh | Nagarkar |
|
3 | Kiran | Annaiah |
+------+-----------+----------+
3 rows in set
(0.00 sec)
Step 3: Disconnecting from the MySQL Server
The user can disconnect from the MySQL Server by using the quit command.
mysql> quit
Bye
For more details regarding the use of MySQL please refer the online
manual at http://www.mysql.com/doc/en/index.html