SlideShare a Scribd company logo
IELM 511 Information Systems Design
                        Labs 5 and 6. DB creation and Population

In this lab, your objective is to learn the basics of creating and managing a DB system. One
way to interact with the DBMS (MySQL) is to use the GUI provided by MySQL. However,
we shall use a separate interface, phpMyAdmin for most tasks in this lab and the next one.

Objectives of lab 5.
(a) Learn to create a database
(b) Learn to create tables in the database.

Objectives of lab 6.
(a) Learn to populate you DB tables.
(b) Learn the basics of developing a DB API using PHP CGI's.


As before, if you need to look up the syntax of some SQL command, you can use a good
online tutorial site, such as: http://www.w3schools.com/sql

Step 5.1. Create a DB using phpMyAdmin.
Before running this task, note that you should create a user account in MySQL, and assign
a password to the user. When you start phpMyAdmin, you will be presented with a screen
something like:




Use the link for "Create new database" to create a database called "bank".
After creating the database, click on the DB name, and then you can start creating tables.


Step 5.2. Create tables using phpMyAdmin.
You will learn three ways to create tables (two methods in lab 5, one in lab 6)
1. Using the GUI of phpMyAdmin program.
2. Using a SQL “CREATE TABLE …” command issued from phpMyAdmin GUI.
3. Using a CGI program to connect to your MySQL DB, and sending the command from
your CGI program

Before you create any table(s), please do your planning:

   - Names of all the tables.
   - All the attributes for each table
   - The domain constraint(s) for each attribute
   - The primary key for each table
   - The referential constraints (Foreign keys)

Conventions: Try to use a consistent convention for all names that you will assign.
For example:
All table names: First letter capitalized with no underscores: e.g. Loan.
All attribute names: lower case with underscores: name, ssn, start_date.
All constraint names: lower case, underscored; for example, a foreign key
constraints from in table Loan fk_loan_c1, etc.


1. How to use phpMyAdmin to create your own table?
1. Login in to phpMyAdmin, click on the database name on the left-hand-side
2. Enter the table name and number of fields, then click 'Go'.
3. For each attribute (field), enter the name and specify the type, length, set the
primary key, etc.; Click on “Save” to create the table.

Conventions: I suggest use only three data types: VARCHAR, DOUBLE and DATE.
Task: Use this method to create the 'Branch' table from your lecture notes.

2. How to use SQL command to create tables using phpMyAdmin
1. Click on the SQL tab (blue color) from any page of phpMyAdmin
2. Type your SQL command in a TXT file. Load the TXT file as below, and click 'Go'.


                                       bank                   localhost

       bank (-)




                  Type your SQL “create table ..” command in a TXT file.
                      Use this interface to load the query in this box




Task: Use this method to create all the tables of the 'bank' database from the lecture notes.



Step 6.1. Populate the tables using phpMyAdmin.
There are several ways to insert rows of data into a table:
1. Using the GUI provided by phpMyAdmin:
    - select the table
    - click on the "Insert" button, and
    - enter the data.
b_name char(30)
 city     char(60)

 assets double


                enter values here




2. The preferred way for this lab: Use the "insert into …" command of SQL, and type all
the store data commands into a TXT file3. Then load the file into the SQL interface and run
it.

3. It may be possible to import the data from some previously exported DB file (or even
from text or Excel files – however, you should not use that method for this lab.

Task: Use this method to populate all the tables you have created. For the tables in the lecture
notes, please use the same data as given (this will be later useful to test your homework
solutions!). For other tables, please use data consistent with the notes and the design.



Step 6.2. Learn the basics of developing a DB API using PHP CGI's.
In this lab, we will first learn the simplest method for a PHP program to connect to the
MySQL database. For now, you can use a modified version of the following PHP script,
and execute it directly from the phpDesigner (or compile it and run it from your local host.

NOTE: modify the portions that are in bold black.

    <?php
    $hostname = 'localhost';
    $username = 'root';
    $password = "password";

    $link = mysql_connect( $hostname, $username, $password ) or die("Could not
    connect : " . mysql_error());
    echo "Connected successfully!";

    $mydb = "dbname";
$db = mysql_select_db( $mydb ) or die("Could not select database");
   echo "Database " . $mydb . " selected.";

   // Note: values that are of type char( ) must be quoted; numbers are not in quotes.
   $query = "insert into mytable values( 'v1', 'v2', v3)";

   $result = mysql_query($query) or die("Query failed: " . mysql_error());

   /* $result is a special data structure; the function mysql_fetch_assoc is used to
      convert the returned table into a PHP associative array. */
   while ($row = mysql_fetch_assoc($result)) {
      echo $row['column_name_1'];
      echo $row['column_name_2'];
      echo $row['column_name_3'];
   }
   mysql_close($link);

   ?>


References:
1. HTML quick reference file.
2. PHP tutorials and function references: (from www.php.net)
3. PHP tutorial site: http://www.w3pop.com/learn/view/p/3/o/0/doc/php_ref_string/
4. Another PHP tutorial/reference: http://www.w3schools.com/PHP/php_ref_array.asp
5. MySQL tutorial/reference: http://www.w3schools.com/sql

Lab materials for IELM 511 prepared by WEI Xiangzhi, Dept of IELM, HKUST.

More Related Content

What's hot (19)

PDF
Introduction to php database connectivity
baabtra.com - No. 1 supplier of quality freshers
 
PPT
New: Two Methods of Installing Drupal on Windows XP with XAMPP
Rupesh Kumar
 
PPT
MYSQL - PHP Database Connectivity
V.V.Vanniaperumal College for Women
 
PPT
PHP and MySQL
webhostingguy
 
PDF
phptut4
tutorialsruby
 
PDF
PHP and Mysql
Sankhadeep Roy
 
PDF
2006 Esug Omnibrowser
bergel
 
PPT
Database presentation
webhostingguy
 
TXT
Conexcion java mysql
jbersosa
 
PPTX
Database Connectivity in PHP
Taha Malampatti
 
PDF
4.3 MySQL + PHP
Jalpesh Vasa
 
PPT
Blocks In Drupal
guest6fb0ef
 
PDF
Capability Driven Design - Rapid Talks - November 2020
Andrzej Jóźwiak
 
DOC
Creating a Simple PHP and MySQL-Based Login System
Azharul Haque Shohan
 
PPTX
System performance tuning
Menandro Oba
 
PDF
Php summary
Michelle Darling
 
ODP
My sql
Nadhi ya
 
PDF
extending-php
tutorialsruby
 
Introduction to php database connectivity
baabtra.com - No. 1 supplier of quality freshers
 
New: Two Methods of Installing Drupal on Windows XP with XAMPP
Rupesh Kumar
 
MYSQL - PHP Database Connectivity
V.V.Vanniaperumal College for Women
 
PHP and MySQL
webhostingguy
 
phptut4
tutorialsruby
 
PHP and Mysql
Sankhadeep Roy
 
2006 Esug Omnibrowser
bergel
 
Database presentation
webhostingguy
 
Conexcion java mysql
jbersosa
 
Database Connectivity in PHP
Taha Malampatti
 
4.3 MySQL + PHP
Jalpesh Vasa
 
Blocks In Drupal
guest6fb0ef
 
Capability Driven Design - Rapid Talks - November 2020
Andrzej Jóźwiak
 
Creating a Simple PHP and MySQL-Based Login System
Azharul Haque Shohan
 
System performance tuning
Menandro Oba
 
Php summary
Michelle Darling
 
My sql
Nadhi ya
 
extending-php
tutorialsruby
 

Viewers also liked (7)

PDF
wtst3_pettichord3
tutorialsruby
 
PDF
has_many_and_belongs_to_many
tutorialsruby
 
PDF
rails.html
tutorialsruby
 
PDF
Devoxx%202008%20Tutorial
tutorialsruby
 
PDF
Accessing_MySQL_from_Ruby
tutorialsruby
 
PDF
ruby-efl-tutorial-hsyl20
tutorialsruby
 
PDF
rubyonrails
tutorialsruby
 
wtst3_pettichord3
tutorialsruby
 
has_many_and_belongs_to_many
tutorialsruby
 
rails.html
tutorialsruby
 
Devoxx%202008%20Tutorial
tutorialsruby
 
Accessing_MySQL_from_Ruby
tutorialsruby
 
ruby-efl-tutorial-hsyl20
tutorialsruby
 
rubyonrails
tutorialsruby
 
Ad

Similar to lab56_db (20)

PPTX
UNIT V (5).pptx
DrDhivyaaCRAssistant
 
PPTX
Database Connectivity MYSQL by Dr.C.R.Dhivyaa Kongu Engineering College
Dhivyaa C.R
 
PDF
Php 2
tnngo2
 
PPTX
CHAPTER six DataBase Driven Websites.pptx
KelemAlebachew
 
PPTX
Learn PHP Lacture2
ADARSH BHATT
 
PPTX
PHP DATABASE MANAGEMENT.pptx
CynthiaKendi1
 
PPT
Php Mysql
Mudasir Syed
 
PPT
Download It
webhostingguy
 
ODP
Php modul-3
Kristophorus Hadiono
 
PPT
SQL -PHP Tutorial
Information Technology
 
PPTX
This slide show will brief about database handling
averynight005
 
PPTX
Chapter 3.1.pptx
mebratu9
 
PDF
MySQL for beginners
Saeid Zebardast
 
PDF
phptut4
tutorialsruby
 
PPTX
Web Application Development using PHP Chapter 6
Mohd Harris Ahmad Jaal
 
PPTX
Php basics
Egerton University
 
PPTX
chapter_Seven Database manipulation using php.pptx
Getawu
 
PPT
Php MySql For Beginners
Priti Solanki
 
PPT
php databse handling
kunj desai
 
UNIT V (5).pptx
DrDhivyaaCRAssistant
 
Database Connectivity MYSQL by Dr.C.R.Dhivyaa Kongu Engineering College
Dhivyaa C.R
 
Php 2
tnngo2
 
CHAPTER six DataBase Driven Websites.pptx
KelemAlebachew
 
Learn PHP Lacture2
ADARSH BHATT
 
PHP DATABASE MANAGEMENT.pptx
CynthiaKendi1
 
Php Mysql
Mudasir Syed
 
Download It
webhostingguy
 
SQL -PHP Tutorial
Information Technology
 
This slide show will brief about database handling
averynight005
 
Chapter 3.1.pptx
mebratu9
 
MySQL for beginners
Saeid Zebardast
 
phptut4
tutorialsruby
 
Web Application Development using PHP Chapter 6
Mohd Harris Ahmad Jaal
 
Php basics
Egerton University
 
chapter_Seven Database manipulation using php.pptx
Getawu
 
Php MySql For Beginners
Priti Solanki
 
php databse handling
kunj desai
 
Ad

More from tutorialsruby (20)

PDF
&lt;img src="../i/r_14.png" />
tutorialsruby
 
PDF
TopStyle Help &amp; &lt;b>Tutorial&lt;/b>
tutorialsruby
 
PDF
The Art Institute of Atlanta IMD 210 Fundamentals of Scripting &lt;b>...&lt;/b>
tutorialsruby
 
PDF
&lt;img src="../i/r_14.png" />
tutorialsruby
 
PDF
&lt;img src="../i/r_14.png" />
tutorialsruby
 
PDF
Standardization and Knowledge Transfer – INS0
tutorialsruby
 
PDF
xhtml_basics
tutorialsruby
 
PDF
xhtml_basics
tutorialsruby
 
PDF
xhtml-documentation
tutorialsruby
 
PDF
xhtml-documentation
tutorialsruby
 
PDF
0047ecaa6ea3e9ac0a13a2fe96f4de3bfd515c88f5d90c1fae79b956363d7f02c7fa060269
tutorialsruby
 
PDF
HowTo_CSS
tutorialsruby
 
PDF
HowTo_CSS
tutorialsruby
 
PDF
BloggingWithStyle_2008
tutorialsruby
 
PDF
BloggingWithStyle_2008
tutorialsruby
 
PDF
cascadingstylesheets
tutorialsruby
 
PDF
cascadingstylesheets
tutorialsruby
 
PDF
Winter%200405%20-%20Advanced%20Javascript
tutorialsruby
 
&lt;img src="../i/r_14.png" />
tutorialsruby
 
TopStyle Help &amp; &lt;b>Tutorial&lt;/b>
tutorialsruby
 
The Art Institute of Atlanta IMD 210 Fundamentals of Scripting &lt;b>...&lt;/b>
tutorialsruby
 
&lt;img src="../i/r_14.png" />
tutorialsruby
 
&lt;img src="../i/r_14.png" />
tutorialsruby
 
Standardization and Knowledge Transfer – INS0
tutorialsruby
 
xhtml_basics
tutorialsruby
 
xhtml_basics
tutorialsruby
 
xhtml-documentation
tutorialsruby
 
xhtml-documentation
tutorialsruby
 
0047ecaa6ea3e9ac0a13a2fe96f4de3bfd515c88f5d90c1fae79b956363d7f02c7fa060269
tutorialsruby
 
HowTo_CSS
tutorialsruby
 
HowTo_CSS
tutorialsruby
 
BloggingWithStyle_2008
tutorialsruby
 
BloggingWithStyle_2008
tutorialsruby
 
cascadingstylesheets
tutorialsruby
 
cascadingstylesheets
tutorialsruby
 
Winter%200405%20-%20Advanced%20Javascript
tutorialsruby
 

Recently uploaded (20)

DOCX
Python coding for beginners !! Start now!#
Rajni Bhardwaj Grover
 
PDF
Kit-Works Team Study_20250627_한달만에만든사내서비스키링(양다윗).pdf
Wonjun Hwang
 
PDF
Staying Human in a Machine- Accelerated World
Catalin Jora
 
PDF
The 2025 InfraRed Report - Redpoint Ventures
Razin Mustafiz
 
PPTX
Designing_the_Future_AI_Driven_Product_Experiences_Across_Devices.pptx
presentifyai
 
PDF
“NPU IP Hardware Shaped Through Software and Use-case Analysis,” a Presentati...
Edge AI and Vision Alliance
 
PDF
UiPath DevConnect 2025: Agentic Automation Community User Group Meeting
DianaGray10
 
PDF
Newgen 2022-Forrester Newgen TEI_13 05 2022-The-Total-Economic-Impact-Newgen-...
darshakparmar
 
PDF
How do you fast track Agentic automation use cases discovery?
DianaGray10
 
PDF
NLJUG Speaker academy 2025 - first session
Bert Jan Schrijver
 
PPTX
Digital Circuits, important subject in CS
contactparinay1
 
PDF
Agentic AI lifecycle for Enterprise Hyper-Automation
Debmalya Biswas
 
PDF
NASA A Researcher’s Guide to International Space Station : Physical Sciences ...
Dr. PANKAJ DHUSSA
 
PDF
Book industry state of the nation 2025 - Tech Forum 2025
BookNet Canada
 
PPTX
Agentforce World Tour Toronto '25 - Supercharge MuleSoft Development with Mod...
Alexandra N. Martinez
 
PDF
ICONIQ State of AI Report 2025 - The Builder's Playbook
Razin Mustafiz
 
PDF
LOOPS in C Programming Language - Technology
RishabhDwivedi43
 
PDF
Go Concurrency Real-World Patterns, Pitfalls, and Playground Battles.pdf
Emily Achieng
 
PPTX
COMPARISON OF RASTER ANALYSIS TOOLS OF QGIS AND ARCGIS
Sharanya Sarkar
 
PDF
Automating Feature Enrichment and Station Creation in Natural Gas Utility Net...
Safe Software
 
Python coding for beginners !! Start now!#
Rajni Bhardwaj Grover
 
Kit-Works Team Study_20250627_한달만에만든사내서비스키링(양다윗).pdf
Wonjun Hwang
 
Staying Human in a Machine- Accelerated World
Catalin Jora
 
The 2025 InfraRed Report - Redpoint Ventures
Razin Mustafiz
 
Designing_the_Future_AI_Driven_Product_Experiences_Across_Devices.pptx
presentifyai
 
“NPU IP Hardware Shaped Through Software and Use-case Analysis,” a Presentati...
Edge AI and Vision Alliance
 
UiPath DevConnect 2025: Agentic Automation Community User Group Meeting
DianaGray10
 
Newgen 2022-Forrester Newgen TEI_13 05 2022-The-Total-Economic-Impact-Newgen-...
darshakparmar
 
How do you fast track Agentic automation use cases discovery?
DianaGray10
 
NLJUG Speaker academy 2025 - first session
Bert Jan Schrijver
 
Digital Circuits, important subject in CS
contactparinay1
 
Agentic AI lifecycle for Enterprise Hyper-Automation
Debmalya Biswas
 
NASA A Researcher’s Guide to International Space Station : Physical Sciences ...
Dr. PANKAJ DHUSSA
 
Book industry state of the nation 2025 - Tech Forum 2025
BookNet Canada
 
Agentforce World Tour Toronto '25 - Supercharge MuleSoft Development with Mod...
Alexandra N. Martinez
 
ICONIQ State of AI Report 2025 - The Builder's Playbook
Razin Mustafiz
 
LOOPS in C Programming Language - Technology
RishabhDwivedi43
 
Go Concurrency Real-World Patterns, Pitfalls, and Playground Battles.pdf
Emily Achieng
 
COMPARISON OF RASTER ANALYSIS TOOLS OF QGIS AND ARCGIS
Sharanya Sarkar
 
Automating Feature Enrichment and Station Creation in Natural Gas Utility Net...
Safe Software
 

lab56_db

  • 1. IELM 511 Information Systems Design Labs 5 and 6. DB creation and Population In this lab, your objective is to learn the basics of creating and managing a DB system. One way to interact with the DBMS (MySQL) is to use the GUI provided by MySQL. However, we shall use a separate interface, phpMyAdmin for most tasks in this lab and the next one. Objectives of lab 5. (a) Learn to create a database (b) Learn to create tables in the database. Objectives of lab 6. (a) Learn to populate you DB tables. (b) Learn the basics of developing a DB API using PHP CGI's. As before, if you need to look up the syntax of some SQL command, you can use a good online tutorial site, such as: http://www.w3schools.com/sql Step 5.1. Create a DB using phpMyAdmin. Before running this task, note that you should create a user account in MySQL, and assign a password to the user. When you start phpMyAdmin, you will be presented with a screen something like: Use the link for "Create new database" to create a database called "bank". After creating the database, click on the DB name, and then you can start creating tables. Step 5.2. Create tables using phpMyAdmin. You will learn three ways to create tables (two methods in lab 5, one in lab 6)
  • 2. 1. Using the GUI of phpMyAdmin program. 2. Using a SQL “CREATE TABLE …” command issued from phpMyAdmin GUI. 3. Using a CGI program to connect to your MySQL DB, and sending the command from your CGI program Before you create any table(s), please do your planning: - Names of all the tables. - All the attributes for each table - The domain constraint(s) for each attribute - The primary key for each table - The referential constraints (Foreign keys) Conventions: Try to use a consistent convention for all names that you will assign. For example: All table names: First letter capitalized with no underscores: e.g. Loan. All attribute names: lower case with underscores: name, ssn, start_date. All constraint names: lower case, underscored; for example, a foreign key constraints from in table Loan fk_loan_c1, etc. 1. How to use phpMyAdmin to create your own table? 1. Login in to phpMyAdmin, click on the database name on the left-hand-side 2. Enter the table name and number of fields, then click 'Go'.
  • 3. 3. For each attribute (field), enter the name and specify the type, length, set the primary key, etc.; Click on “Save” to create the table. Conventions: I suggest use only three data types: VARCHAR, DOUBLE and DATE. Task: Use this method to create the 'Branch' table from your lecture notes. 2. How to use SQL command to create tables using phpMyAdmin 1. Click on the SQL tab (blue color) from any page of phpMyAdmin 2. Type your SQL command in a TXT file. Load the TXT file as below, and click 'Go'. bank localhost bank (-) Type your SQL “create table ..” command in a TXT file. Use this interface to load the query in this box Task: Use this method to create all the tables of the 'bank' database from the lecture notes. Step 6.1. Populate the tables using phpMyAdmin. There are several ways to insert rows of data into a table: 1. Using the GUI provided by phpMyAdmin: - select the table - click on the "Insert" button, and - enter the data.
  • 4. b_name char(30) city char(60) assets double enter values here 2. The preferred way for this lab: Use the "insert into …" command of SQL, and type all the store data commands into a TXT file3. Then load the file into the SQL interface and run it. 3. It may be possible to import the data from some previously exported DB file (or even from text or Excel files – however, you should not use that method for this lab. Task: Use this method to populate all the tables you have created. For the tables in the lecture notes, please use the same data as given (this will be later useful to test your homework solutions!). For other tables, please use data consistent with the notes and the design. Step 6.2. Learn the basics of developing a DB API using PHP CGI's. In this lab, we will first learn the simplest method for a PHP program to connect to the MySQL database. For now, you can use a modified version of the following PHP script, and execute it directly from the phpDesigner (or compile it and run it from your local host. NOTE: modify the portions that are in bold black. <?php $hostname = 'localhost'; $username = 'root'; $password = "password"; $link = mysql_connect( $hostname, $username, $password ) or die("Could not connect : " . mysql_error()); echo "Connected successfully!"; $mydb = "dbname";
  • 5. $db = mysql_select_db( $mydb ) or die("Could not select database"); echo "Database " . $mydb . " selected."; // Note: values that are of type char( ) must be quoted; numbers are not in quotes. $query = "insert into mytable values( 'v1', 'v2', v3)"; $result = mysql_query($query) or die("Query failed: " . mysql_error()); /* $result is a special data structure; the function mysql_fetch_assoc is used to convert the returned table into a PHP associative array. */ while ($row = mysql_fetch_assoc($result)) { echo $row['column_name_1']; echo $row['column_name_2']; echo $row['column_name_3']; } mysql_close($link); ?> References: 1. HTML quick reference file. 2. PHP tutorials and function references: (from www.php.net) 3. PHP tutorial site: http://www.w3pop.com/learn/view/p/3/o/0/doc/php_ref_string/ 4. Another PHP tutorial/reference: http://www.w3schools.com/PHP/php_ref_array.asp 5. MySQL tutorial/reference: http://www.w3schools.com/sql Lab materials for IELM 511 prepared by WEI Xiangzhi, Dept of IELM, HKUST.