MySQL Basics
Overview
I.1.1 Introduction
MySQL is an Open Source Structured Query Language (SQL) database management system.
MySQL is a trademark of Oracle Corporation which also distributes and supports MySQL.
This document will discuss the basics of using MySQL once it is installed. These include basic administrative commands such as setting passwords and creating new users. Very basic database management is also covered. The operations in database management of interest
including creating and editing databases, tables in databases and structures in tables.
I.1.2 Prerequisites
If MySQL has not been installed, installation is covered by: "Setting up Localhost".
I.1.3 Contents
I.2.1 Start and Stop
MySQL provides its own built in tool to administer the databases, namely the MySQL Monitor. This section will cover how to enter and exit the monitor
The following starts up MySQL:
To start the MySQL monitor and use it with root privileges, log into the MySQL monitor as root:
To stop the monitor and MySQL itself the following sequence of commands can be used.
To exit the MySQL monitor enter the command \q. To stop MySQL:
I.2.2 Non-root Login
For security reasons, and to prevent accendential changes it can be of utility to log into either MySQL or the MySQL Monitor as a user other than "root". .
To switch to using the monitor as the user "arbprimary" instead of root, quit the monitor if necessary and log in again using the command:
This will permit the use of the monitor with the privlieges associated with the user "arbprimary" instead of "root". Creating new users such as "arbprimary" is discussed in the next section, "I.3 User Management".
I.3.1 Intro
Basic user management includes displaying users, creating users, deleting users, changing their passwords, and granting them privileges. This section will briefly cover the basics of these tasks.
I.3.2 Change Password for root
When MySQL was installed the root user was given a randomly generated password.
To change the password for root from the random password originally generated during the installation of
MySQL to a chosen password, enter the MySQL Monitor. Then use the following command:
and follow the prompts.
This command can also be used to change the password for users other than root;
the string, "'root'@'localhost'" must be replaced by a string appropriate to the user for whom the password is being changed.
I.3.3 Create and Delete Users
To create a new user for MySQL use the command:
This creates a new user named “arbprimary” with login rights from any host. The field that comes after the “@” symbol is the host allowed for the user to log in from and the “%” symbol is a wild card which specifies any.
Users can be deleted with the following command:
This line drops, or deletes, the user that the previous chapter created.
I.3.4 View Users
To view the current users use the command:
The ouput from this should show several users including "root" and the new user, "arbprimary".
To show the permissions for the user "arbprimary" use the following command:
This will show that "arbprimary" is granted usage on "*.*". However currently "arbprimary" does not have any privileges on any databases. Adding privileges for the new user will be covered in "I.4 Database Basics", the next section.
I.4.1 Intro
This section will covers the basics of database management.This includes, creating databases, deleting databases, granting user permissions on databases and viewing the results.
I.4.2 Creation and Removal
If not already logged in, log into the MySQL monitor as root.
Create a new database:
This command creates a new database, "arbordb".
A database can be deleted with the command “DROP DATABASE”. For example to delete the newly created “arbordb”:
I.4.3 Inspection
List the current databases for the system:
This should show the newly created “arbordb” along with some system databases that are automatically created when MySQL is installed.
I.4.4 Managing User Privlieges
Grant the user “arbprimary” full privileges for the newly created database “arbordb”.
This grants all privileges including modification privileges to the user “arbprimary” for the new database. To check that the privileges were granted:
The line “GRANT ALL PRIVILEGES...” now appears in the grants for “arbprimary”.
The following line removes the privileges to operate on the database "arbdb" from the user "arbprimary":
Tables
I.5.1 Intro
The data storage units within a MySQL database are called "tables". These tables each have a name and a format. They store sequences of data, as records, organized in a series of fields. Typically when the table is visualized the series of fields correspond to the columns of the table, and the series of data values stored for each column for a given record corresponds to a row in the table.
This section will cover creating, deleting and displaying tables. The commands necessary to edit and search tables will also be covered briefly.
The MySQL monitor will be used at this stage to operate on the database and manipulate the tables.
For the commands in this section to work a user named "arbprimary" with privileges for the database "arbdb" must be created.
This is covered in the previous sections; "User Management" covers creating new users,
and "Database Basics" covers creating databases and granting user permissions for them.
After the user "arbprimary" has been created, open the "MySQL Monitor" as this user:
This will allow the database "arbdb" to be manipulated in the monitor.
I.5.2 Table Creation and Removal
To begin with a database in which the tables will be manipulated must be chosen.
As "arbprimary" choose the example database, "arbdb":
This creates a new table, "trees" in the current database, "arbordb". This table has two string columns (VARCHAR) which accept strings of variable length up to 40 chars: species and name. It has three columns that accept integer data (INT) and three columns that accept decimal data (DECIMAL). The number in parentheses behind the type specifies the length of the field. For instance, "evergreen" is an "INT" of length 1.The "id" column which is an integer data type is set to be an automatically incrementing primary key.
The "id" does not need to be set it will increment with each new record added; it is never null.
The table can be deleted with the command:
I.5.3 Viewing the Tables
To view which tables exist in a database use the following command:
To show the rows and the columns of the table "trees" use the command:
Since there are currently no rows in the table "trees" this command will only show the the column names.
I.5.4 Add a Row
To fill one row in the table, “trees” use the command:
If the resulting table is displayed now, it is printed as:
To add another line execute:
The command "SELECT * FROM trees;" should now display both lines:
I.5.5 Delete a Row
To delete the second line added to the table trees use the command:
Displaying the table again should show that the second row has in fact been deleted.
I.5.6 Edit an Existing Row
To change an existing line in a table use the command:
This changes the height column from 70 to 65.
To alter the format of the table itself see:
"alter-table" from mysql.com.
I.5.7 An Example Table
An example table will be needed for later exercises. For instance, the following example table can be created using the commands covered in this section:
I.5.8 Searching Tables
Search queries operating on tables is of great utility in retrieving records from the database. To search the table returning those entries that match a wild card use the command:
This command uses the wildcard "%" to return from the table "trees" those rows which have a species name which begins with "picea".
For a more complete description on using wildcards to search for table entries see:
"pattern-matching" from mysql.com.
To search the table and only see certain columns returned use the command:
If this command is used on the example tables described in section I.5.7, then the result is:
This search command returns the values in the columns: species, name and height; for all rows that have a name ending in "spruce".