Perl Programming Page 3 - Creating a Database with Perl and DBI |
Now comes the time to insert data into the table. We will use the SQL command named INSERT. The basic syntax of this command is INSERT INTO table_name (field1, field2, ...) VALUES (value1, value2, ...); We first tell MySQL into what table we are inserting a row of data. Then, within parentheses, we indicate which fields in the table will be given values. The second set of parentheses after the termVALUEScontains a list of values that are plugged in memberwise into the fields indicated in the first set of parentheses. Roger Waters is deserving of a row of data in our table, so let’s insert him as key 1, including his phone number: mysql> INSERT INTO musicians (player_id, name, phone) TheSELECTcommand can tell us if the row was inserted correctly (more onSELECTlater). mysql> SELECT * FROM musicians; Let’s enter the other musicians: mysql> INSERT INTO musicians (player_id, name, phone) mysql> INSERT INTO musicians (player_id, name, phone) mysql> INSERT INTO musicians (player_id, name, phone) mysql> INSERT INTO musicians (player_id, name, phone) mysql> INSERT INTO musicians (player_id, name, phone) mysql> SELECT * FROM musicians; player_id name phone 1 Roger Waters 555-1212 2 Geddy Lee 555-2323 3 Marshall Mathers III 555-3434 4 Thom Yorke 555-4545 5 Lenny Kravitz 555-5656 6 Mike Diamond 555-6767 6 rows in set (0.00 sec) Excellent! Our musicians are entered. Now for the commands to enter data into the other two tables. Read along and follow the bouncing ball . . . mysql> INSERT INTO what_they_play (player_id, inst_id) mysql> SELECT * FROM what_they_play; Notice that we used an alternative form of theINSERT command to insert multiple rows, in our case all the rows, at the same time. mysql> INSERT INTO instruments mysql> SELECT * FROM instruments;
14 rows in set (0.00 sec) Now that the three tables have been created and populated with data, we can talk about how we can pull information out of the database.
blog comments powered by Disqus |
|
|
|
|
|
|
|