Mysql Commands: What Is A Database?
Mysql Commands: What Is A Database?
MySQL is currently the most popular open source database server in existence. On top of that, it
is very commonly used in conjunction with PHP scripts to create powerful and dynamic server-
side applications.
MySQL has been criticized in the past for not supporting all the features of other popular and
more expensive DataBase Management Systems. However, MySQL continues to improve with
each release (currently version 5), and it has become widely popular with individuals and
businesses of many different sizes.
What is a Database?
A database is a structure that comes in two flavors: a flat database and a relational database. A
relational database is much more oriented to the human mind and is often preferred over the
gabble-de-gook flat database that are just stored on hard drives like a text file. MySQL is a
relational database.
In a relational structured database there are tables that store data. The columns define which
kinds of information will be stored in the table. An individual column must be created for each
type of data you wish to store (i.e. Age, Weight, Height).
On the other hand, a row contains the actual values for these specified columns. Each row will
have 1 value for each and every column. For example a table with columns (Name, Age, Weight-
lbs) could have a row with the values (Bob, 65, 165).
Example tables might be: Employees, Supervisors, and Customers. Each table would then
contain columns specific to these three areas. To help store information related to each
employee, the Employees table might have the following columns: Hire, Date, Position, Age,
and Salary.
Setting Up MySQL in CPanel
There are many different types of control panels that your shared hosting provider may have.
This tutorial assumes that you are using the most popular, CPanel.
First, find the link that allows you to administer MySQL. Within CPanel the icon is labeled
MySQL Databases. Once there, you will need to do the following before you can start using
MySQL.
That line of code is valid PHP, but it also contains valid MySQL. The text that appears
between the quotations "SELECT * FROM example", is the MySQL code.
As you probably can tell "SELECT" and "FROM" are the MySQL keywords used in this
query. Capitalizing them allows you to tell from a quick glance that this query selects data
from a table.
MySQL Commands
Each command has both a long and short form. The long form is not case sensitive; the short
form is. The long form can be followed by an optional semicolon terminator, but the short form
should not.
The use of short-form commands within multi-line /* ... */ a comment is not supported.
1. Accessing MySQL
% vi .alias
:wq
You may create tables, drop tables, etc. inside your own database. samp_db or others which
you may browse may not be deleted, or dropped.
2. Creating Tables
% mysql
mysql> exit;
For more examples, see some of the scripts from the samp_db database in
/users/science/wbluhm/mysql/create_*.sql
Column types
Some basic column types: INTEGER, FLOAT, CHAR, DATE, TIME, BLOB
3. Inserting Data
% mysql
mysql>
4. Querying Data
% mysql
/*
mysql> Drop table Inventory;
Query OK, 0 rows affected (0.02 sec)
mysql> CREATE TABLE Inventory
-> (
-> ID SMALLINT NOT NULL PRIMARY KEY,
-> InStock SMALLINT NOT NULL,
-> OnOrder SMALLINT NOT NULL,
-> Reserved SMALLINT NOT NULL
-> );
Query OK, 0 rows affected (0.05 sec)
mysql> INSERT INTO Inventory VALUES (104, 16, 25-InStock, 0);
Query OK, 1 row affected (0.00 sec)
mysql> select * from Inventory;
+-----+---------+---------+----------+
| ID | InStock | OnOrder | Reserved |
+-----+---------+---------+----------+
| 104 | 16 | 9 | 0 |
+-----+---------+---------+----------+
1 row in set (0.00 sec)
*/
Drop table Inventory;
CREATE TABLE Inventory
(
ID SMALLINT NOT NULL PRIMARY KEY,
InStock SMALLINT NOT NULL,
OnOrder SMALLINT NOT NULL,
Reserved SMALLINT NOT NULL
);
INSERT INTO Inventory VALUES (104, 16, 25-InStock, 0);
select * from Inventory;