Professional Documents
Culture Documents
PREFACE
Nowadays, technology has grown up rapidly, it forces programmers to program and
develop sophisticated and useful software for users. Many programming languages that we
can choose according to our necessary, such as Delphi, C++, Java, PHP, Visual Basic, and
many many more.
For building dynamic software {office program, school program, commerce program,
cashier program, etc.}, we need integrate our program with database. Like water in a
container, the water is data, and the container is database. Data which has executed through
an interface will be proceeded to a database for processing. Each database has tables, and
each table has relationships.
In this book, I will show you how to create a relational database using MySQL
through Command Prompt (CMD). As we have known, there are many kinds of database
such as MS. Access, SQL Server, MySQL, Postgre, Oracle, and many many more, but in this
book we use MySQL. Why? Because MySQLs more popular and open source database.
We could create a database through PHPMyAdmin easily, but as a programmer we have to
know how to create a database manually.
Chapter I
Introducing MySQL
MySQL, the most popular Open Source SQL database management system, is developed,
distributed, and supported by Oracle Corporation.
The MySQL Web site (http://www.mysql.com/) provides the latest information about
MySQL software.
The MySQL Database Server is very fast, reliable, scalable, and easy to use.
If that is what you are looking for, you should give it a try. MySQL Server can run
comfortably on a desktop or laptop, alongside your other applications, web servers,
and so on, requiring little or no attention. If you dedicate an entire machine to MySQL,
you can adjust the settings to take advantage of all the memory, CPU power, and I/O
capacity available. MySQL can also scale up to clusters of machines, networked
together.
You can find a performance comparison of MySQL Server with other database
managers on our benchmark page. See Section 7.1.3, The MySQL Benchmark Suite.
MySQL Server was originally developed to handle large databases much faster than
existing solutions and has been successfully used in highly demanding production
environments for several years. Although under constant development, MySQL
Server today offers a rich and useful set of functions. Its connectivity, speed, and
security make MySQL Server highly suited for accessing databases on the Internet.
Chapter II
Relational Database
A relational database is a type of database that organizes data into tables, and links them,
based on defined relationships. These relationships enable you to retrieve and combine data
from one or more tables with a single query.
Chapter III
Practicing Database
Before practice, I want you know that I am using MySQL that bundled in xampp 3. 2. 1.
At first, you must turn on server and MySQL at XAMPP control panel at folder XAMPP
located in C partition. Look at the picture below!
Figure 1: Go to a path where Xampp control panel located to turn on server (Apache) and
database (MySQL)
Figure 2: Xampp control panel appearance then turn on Apache and MySQL
Look at the red rounds on the picture, if MySQL and server (Apache) have turned on, status
change detected running will be appeared .
Figure 3: Detecting whether Apache and MySQL have turned on or not yet
Before create a database, we have to design all things in database that we are going to create.
It called ERD (Entity Relationship Diagram).
I am about to use MS. Visio. In this book, I use MS. Visio 2007. Select the Basic Flowchart
(US units).
As we have seen, the database has three tables, ie student, borrow, and book tables.
Student table has no_id as primary key, name, gender, and address fields.
Borrow has no_id and book_id as foreign key, and date fields.
Book has book_id as primary key, title, and author fields.
Furthermore the database has degree relationship or cardinality ie: 1 -> n, n-> 1 which means
that a student may borrow more than once, and also a book can be borrowed to many student.
Look at the tables below, I used MS. Excel to describe the tables.
Data
Type
Int
Varchar
Enum
Varchar
Size
10
50
1
50
Description
Pk
not null
not null
not null
book
Field Name
book_id
title
author
Data
Type
Size Description
Varchar
5
pk
Varchar 50
not null
Varchar 50
not null
borrow
Field Name
no_id
book_id
date
Data
Type
Size Description
Int
10
Fk
Varchar
5
Fk
Varchar 10
not null
A field can be defined as the part of a record held in a database, containing specific
information. It is a collection of information organized in a way that a computer program can
quickly select desired pieces of data. Data type tells what kind of data that value can have.
Size means how many characters or numbers can be included. Description means whether the
field has pk, fk, or not both of them. PK or primary key is a special relational database table
column (or combination of columns) designated to uniquely identify all table records.
A primary keys main features are:
1. It must contain a unique value for each row of data.
2. It cannot contain null values.
A foreign key is a column or group of columns in a relational database table that provides a
link between data in two tables. It acts as a cross-reference between tables because it
references the primary key of another table, thereby establishing a link between them. NOT
NULL means you must provide a value for the fields.
Well, lets create a database. Open command prompt (CMD) on your Windows!
C:\Users\Dewi>CD..
C:\Users>CD..
C:\>cd xampp
C:\xampp>cd mysql
C:\xampp\mysql>cd bin
C:\xampp\mysql\bin>mysql -u root p
If your MySQL doesnt have a password, just enter when MySQL request you to type a
password.
To check whether our tables have related, we must check it on a browser. Follow these
instructions!
Now lets try to insert a record to aour database, follow these commands.
Commands select *from (table name); used to show data records in the table.
Chapter IV
List of MySQL
Commands
USE database_name
Change to this database. You need to change to some database when you first connect to
MySQL.
SHOW DATABASES
Lists all MySQL databases on the system.
SHOW TABLES [FROM database_name]
Lists all tables from the current database or from the database given in the command.
DESCRIBE table_name
SHOW FIELDS FROM table_name
SHOW COLUMNS FROM table_name
These commands all give a list of all columns (fields) from the given table, along with
column type and other info.
SHOW INDEX FROM table_name
Lists all indexes from this tables.
SET PASSWORD=PASSWORD('new_password')
Allows the user to set his/her own password.
CREATE TABLE table_name (create_clause1, create_clause2, ...)
Creates a table with columns as indicated in the create clauses.
DROP TABLE table_name
Removes the table from the database. Permanently! So be careful with this command!
ALTER TABLE table_name ADD (create_clause1, create_clause2, ...)
Adds the listed columns to the table.
ALTER TABLE table_name DROP column_name
Drops the listed columns from the table.
ALTER TABLE table_name MODIFY create_clause
Changes the type or modifiers to a column. Using MODIFY means that the column keeps
the same name even though its type is altered. MySQL attempts to convert the data to
match the new type: this can cause problems.
ALTER TABLE table_name CHANGE column_name create_clause
Changes the name and type or modifiers of a column. Using CHANGE (instead of
MODIFY) implies that the column is getting a new name.
ALTER TABLE table_name ADD INDEX [index_name] (column_name1,
column_name2, ...)
CREATE INDEX index_name ON table_name (column_name1, column_name2, ...)
Adds an index to this table, based on the listed columns. Note that the order of the
columns is important, because additional indexes are created from all subsets of the listed
columns reading from left to write. The index name is optional if you use ALTER
TABLE, but it is necesary if you use CREATE INDEX. Rarely is the name of an index
useful (in my experience).
INSERT [INTO] table_name VALUES (value1, value2, ...)
Insert a complete row of data, giving a value (or NULL) for every column in the proper
order.
Allows you to move data from a table into an external file. The field and line termination
clauses are the same as for LOAD above. Several tricky features:
1. Note the positions of the table_name and where_clause, after the external file is
given.
2. You must use a complete path, not just a file name. Otherwise MySQL attempts to
write to the directory where the database is stored, where you don't have
permission to write.
3. The user who is writing the file is 'mysql', not you! This means that user 'mysql'
needs permission to write to the directory you specify. The best way to do that is
to creat a new directory under your home directory, then change the directory's
permission to 777, then write to it. For example: mkdir mysql_output, chmod
777 mysql_output.
GRANT USAGE ON *.* TO user_name@localhost [IDENTIFIED BY 'password']
Creates a new user on MySQL, with no rights to do anything. The IDENTIFED BY
clause creates or changes the MySQL password, which is not necessarily the same as the
user's system password. The @localhost after the user name allows usage on the local
system, which is usually what we do; leaving this off allows the user to access the
database from another system. User name NOT in quotes.
GRANT SELECT ON *.* TO user_name@localhost
In general, unless data is supposed to be kept private, all users should be able to view it.
A debatable point, and most databases will only grant SELECT privileges on particular
databases. There is no way to grant privileges on all databses EXCEPT specifically
enumerated ones.
GRANT ALL ON database_name.* TO user_name@localhost
Grants permissions on all tables for a specific database (database_name.*) to a user.
Permissions are for: ALTER, CREATE, DELETE, DROP, INDEX, INSERT, SELECT,
UPDATE.
FLUSH PRIVILEGES
Needed to get updated privileges to work immediately. You need RELOAD privileges to
get this to work.
SET PASSWORD=PASSWORD('new_password')
Allows the user to set his/her own password.
REFERENCES
What is MySQL, http://dev.mysql.com/doc/refman/4.1/en/what-is-mysql.html,
accessed in May 21th 2014.
Relational Databases for Dummies, http://code.tutsplus.com/tutorials/relational-databasesfordummies--net-30244, accessed in May 21th 2014.
What is a Field on a Databse, http://www.ask.com/question/what-is-a-field-on-a-database,
accessed in May 21th 2014.
Data Type, http://www.techopedia.com/definition/3349/data-type,
accessed in May 21th 2014.
Primary Key, http://www.techopedia.com/definition/5547/primary-key,
accessed in May 21th 2014.
Foreign Key, http://www.techopedia.com/definition/7272/foreign-key,
accessed in May 21th 2014.
MySQL Commands, http://www.bios.niu.edu/johns/bioinform/mysql_commands.htm,
accessed in May 21th 2014.