Nothing Special   »   [go: up one dir, main page]

Mysql Commands: What Is A Database?

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 6

MySQL COMMANDS

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).

Why Use a Database?


Databases are most useful when it comes to storing information that fits into logical categories.
For example, say that you wanted to store information of all the employees in a company. With a
database you can group different parts of your business into separate tables to help store your
information logically.

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.

 Create a new database


 Create a new user with password
 Assign the user to the database

CAPITALIZATION in MySQL Queries


There are many keywords in MySQL and a good programming habit when using ANY of these
words is to capitalize them. This helps draw them out from the rest of the code and makes them
much easier to read. Below is an example of a MySQL query written in PHP that retrieves all
the data from a MySQL table named "example".

 $result = mysql_query("SELECT * FROM example")

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

Accessing MySQL on the command line

log into your workstation


edit .alias in vi

% vi .alias

enter the following in vi:

alias mysql /users/science/Wilfred/local/mysql/bin/mysql

save and quit vi editor

:wq

(The last three steps need to be performed only once.)

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.

Accessing MySQL from any machine

Your_Shell% mysql -h multivac.sdsc.edu -u wbluhm --password="" wbluhm


(This assumes that you have the client installed on your machine and the PATH set properly.)

Executing MySQL scripts on the command line (outside of MySQL)

% mysql wbluhm < myscript.sql

% mysql -t wbluhm < myscript.sql

2. Creating Tables

Basic commands on mysql command line:

% mysql

mysql> SHOW DATABASES;

mysql> USE wbluhm;

mysql> SHOW TABLES;

mysql> CREATE TABLE dummy ( first_name CHAR(20) , last_name CHAR(20) );

mysql> SHOW TABLES;

mysql> DESCRIBE dummy;

mysql> DROP TABLE dummy;


mysql> SHOW TABLES;

mysql> exit;

Creating tables from .sql scripts

SQL script example: create_books.sql

-- creates the table books in database wbluhm

CREATE TABLE books


(
title CHAR(50) NOT NULL,
author CHAR(30) NOT NULL,
publisher CHAR(30),
topic CHAR(20),
comment CHAR(100),
price FLOAT
)

mysql wbluhm < create_books.sql

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

Complete table of all column types on mysql.com

3. Inserting Data

Basic commands on mysql command line:

% mysql

mysql> USE wbluhm;

mysql> SELECT * FROM books;

mysql> INSERT INTO books


-> VALUES ("My Life", "Mickey Mouse", "Disney",
-> "Biography", "What can I say?", 9.95);

mysql> SELECT * FROM books;


mysql> DELETE FROM books WHERE title="My Life";

mysql> SELECT * FROM books;

mysql>
4. Querying Data

Basic commands on mysql command line:

% mysql

mysql> USE wbluhm;

mysql> SELECT * FROM books;

mysql> SELECT title, price


-> FROM books
-> ;

mysql> SELECT title, author


-> FROM books
-> WHERE topic="Java";

mysql> SELECT title, author, price


-> FROM books
-> WHERE topic="Java"
-> ORDER BY price;

mysql> SELECT title, author, price


-> FROM books
-> WHERE topic="Java"
-> ORDER BY price DESC;

mysql> USE samp_db;

mysql> SHOW TABLES;

mysql> DESCRIBE president;

mysql> SELECT last_name, first_name, birth


-> FROM president
-> WHERE death IS NULL
-> ORDER BY last_name;

mysql> DESCRIBE student;

mysql> DESCRIBE absence;

mysql> SELECT student.name, absence.date


-> FROM student JOIN absence
-> WHERE student.student_id = absence.student_id;
mysql> exit;

Calculation in INSERT clause

/*
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;
           
       

You might also like