Creating a database
In this section of the MySQL primer we will learn how to create a database.
The commands for creating a database in Windows and Linux are the same. However, the prelimnary commands in Linux are slightly more complex. Since this tutorial is meant for the complete newbie, I’ll discuss the Windows and Linux systems separately.
We’ll create a database called employees that contains details of employees of our company . The details we plan to store would be names, salaries, age, addresses, emails, birth dates, hobbies, phone numbers etc.
create database employees;
(Note: The command ends with a semi-colon).
Query OK, 1 row affected (0.00 sec)
show databases;
The server responds with the list of databases.
+----------------+ | Database | +----------------+ | employees | | mysql | | test | +----------------+ 3 rows in set (0.00 sec)
Here we have three databases, two created by MySQL during installation and our employees database.
mysql -u root -p
The system prompts for the MySQL root password that you set up in Installing MySQL on Linux. (Note: This is not the Linux root password but the MySQL root password). Enter the password, which is not displayed for security reasons.
Once you are successfully logged in, the system prints a welcome message and displays the mysql prompt … something like
Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 1 to server version: 3.22.32 Type 'help' for help. mysql>
create database employees;
(Note: The command ends with a semi-colon)
GRANT ALL ON employees.* TO manish@localhost IDENTIFIED BY "eagle"
The above command grants my account (manish@localhost) all the permissions on employees database and sets my password to eagle. You should replace manish with your user name and choose an appropriate password.
mysql -u user_name -p
Type in the password when prompted. (This password was set by the GRANTS ALL… command above) . The system displays the welcome message once you have successfully logged on to MySQL. Here is how your session should look like:
[manish@localhost manish]$ mysql -u manish -p Enter password: Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 3 to server version: 3.22.32 Type 'help' for help. mysql>
mysql> SHOW DATABASES; +----------------+ | Database | +----------------+ | employees | | mysql | | test | +----------------+ 3 rows in set (0.00 sec)
|
« Previous
|
Next »
|
Installing MySQL on Linux It's simple to install MySQL on Linux using the RPM file. Become the superuser if you are working in your account. ...
Installing MySQL on Windows Once you have successfully downloaded the Windows version, installing it is a breeze... trust me! (The installation steps below have ...
Creating tables In this section of the mysql training course we will explore the MySQL commands to create database tables and selecting the database. Databases ...
MySQL database introduction The MySQL database package consists of the following: The MySQL server: This is the heart of MySQL. You can consider it ...
MySQL tables Now that we've created our employee_data table, let's check its listing. Type SHOW TABLES; at the mysql prompt. This should present you with ...