Perl Programming Creating a Database with Perl and DBI |
The first step in creating a database is to start the MySQL server and then issue a MySQL command to create the database—let’s call the database musicians_db,since it will contain information about our favorite musicians. First, let’s log into the MySQL command line interface (CLI): $ mysql -u root -p A few lines of information about the MySQL server will be printed, and then we will see the MySQL prompt: mysql> SQL CASE SENSITIVITY
The CREATE DATABASE Command The first step in working with a MySQL database is to create one. Creating a database is as simple as executing theCREATE DATABASEcommand: mysql> CREATE DATABASE musicians_db; Query OK, 1 row affected (0.01 sec) The USE Command Now that the database namedmusicians_dbis created, we need to tell MySQL that we want to work with it. That is done with theUSEcommand. mysql> USE musicians_db; The CREATE TABLE Command Now that the database has been created and we have selected it as the one we are using, we need to create some tables. The first table to create ismusicians. Recall that it has three fields:player_id, an integer that is the key;name, a character string; andphone, a character string. The command to create a table is, not unsurprisingly,CREATE TABLE.4 The syntax for this command will resemble this: CREATE TABLE table_name (field_definition, field_definition...) The value oftable_name is up to us—we are usingmusicians. The field definitions within the parentheses is a comma-separated list of information that follows this basic form: field_name type We get to choose the field names. Thetype will be one of many different types that MySQL supports includingINT. Strings are specified asCHAR(n)wheren is the number of characters in the string. Here is the command to create our table of musicians: mysql> CREATE TABLE musicians ( The player_id field is an integer that will be the key into the table. Both nameandphoneare strings. Note There are many different SQL data types and ways in which we can create keys. For all the
blog comments powered by Disqus |
|
|
|
|
|
|
|