Home arrow Perl Programming arrow Page 2 - Creating a Database with Perl and DBI

The DESCRIBE Command - Perl

In this second part of a four-part series on Perl and the DBI, you'll learn how to create a database and how to use a collection of very important commands. This article is excerpted from chapter 15 of the book Beginning Perl by James Lee (Apress; ISBN: 159059391X).

TABLE OF CONTENTS:
  1. Creating a Database with Perl and DBI
  2. The DESCRIBE Command
  3. The INSERT Command
  4. The SELECT Command
By: Apress Publishing
Rating: starstarstarstarstar / 8
March 20, 2008

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

TheDESCRIBEcommand shows all the fields in the table and their types. This will show us if themusicianstable was created correctly:

mysql> DESCRIBE musicians;

 Field

 Type

 Null

 Key

 Default

 Extra

 player_id

 int(11)

 

PRI

 0

 

 name

 char(50)

 YES

 

 NULL

 

 phone

 char(12)

 YES

 

 NULL

 

3 rows in set (0.00 sec)

This looks OK so far. Let’s create the other two tables:what_they_playandinstruments:

mysql> CREATE what_they_play (
    ->   player_id INT,
   
->   
inst_id INT);
Query OK, 0 rows affected (0.01 sec)

mysql> CREATE TABLE instruments (
   
->  inst_id INT PRIMARY KEY,
   
->  instrument CHAR(40),
   
->  type CHAR(20),
   
->  difficulty INT);
Query OK, 0 rows affected (0.00 sec)

Creating a Non-root User with the GRANT Command

It is important to create a non-root user to access the database—performing normal non-MySQL-admin activities using the root user is a bad idea for security reasons. So let’s create a user that will be allowed to perform basic queries on themusicians_dbdatabase:

mysql> GRANT SELECT, INSERT, UPDATE, DELETE
    ->   ON musicians_db.*
   
->  TO musicfan@localhost
   
->  IDENTIFIED BY "CrimsonKing";
Query OK, 0 rows affected (0.03 sec)

You can trust us when we say that this command creates a user namedmusicfan with a password “CrimsonKing”5 and grants this user permission to select, insert, update, and delete records from the database. Or, you can check out the documentation and read all about theGRANTcommand.

We are going to start inserting data into ourmusicians_dbdatabase, so we need to log out as therootuser and log back into MySQL as the newly createdmusicfanuser:

mysql> quit
Bye
$ mysql -u musicfan -p
Enter password: CrimsonKing

mysql>



 
 
>>> More Perl Programming Articles          >>> More By Apress Publishing
 

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

PERL PROGRAMMING ARTICLES

- Perl Turns 25
- Lists and Arguments in Perl
- Variables and Arguments in Perl
- Understanding Scope and Packages in Perl
- Arguments and Return Values in Perl
- Invoking Perl Subroutines and Functions
- Subroutines and Functions in Perl
- Perl Basics: Writing and Debugging Programs
- Structure and Statements in Perl
- First Steps in Perl
- Completing Regular Expression Basics
- Modifiers, Boundaries, and Regular Expressio...
- Quantifiers and Other Regular Expression Bas...
- Parsing and Regular Expression Basics
- Hash Functions

Developer Shed Affiliates

 


Dev Shed Tutorial Topics: