Introduction To Mysql Solutions 2
Introduction To Mysql Solutions 2
W. H. Bell
A fixed width font is used for commands or source code. To aid the reader, a small bucket character
is used to indicate spaces in commands or source code. This character should be replaced with
a space when the command or source code is entered. An example of the bucket that is used to
indicate spaces is demonstrated in Listing 1, where there is a single space between the command and
argument.
Listing 1: Demonstrating the bucket character used to indicate spaces in source code and commands.
command argument
2 Solutions
Several steps in the lab exercises do not have associated questions. These solutions either address
explicit questions or important observations.
The Customers table is defined in Listing 2 as having an integer CustomerId and a text string
CustomerName. When the SQL given in Listing 3 is run, the value ’A text string’ cannot be
inserted into the integer column. Instead, MySQL inserts the value zero and returns a warning.
When the value ’15’ is inserted, MySQL converts this to an integer and stores it within the table.
Likewise, the value 999, which is an integer, is converted into a string and then stored within the
table. This example demonstrates that MySQL will only allow the correct type of information to
be inserted into a table field.
• 16. “Use the source command to read the insert-data-2.sql file ...”
The table Customers has been defined with a requirement that the CustomerName cannot be
filled with NULL. The SQL script insert-data-2.sql stops at Line 8 of Listing 4 with a warning
message. It does not process Line 9 and 10. This demonstrates that a value can be required
within a table by using the appropriate database schema.
• 17. “Test this by trying to insert a CustomerName that already exists in the table.”
The test can be performed by using source to re-run insert-data-2.sql. The MySQL
database will produce a warning message and refuse to insert a duplicate CustomerName.
This demonstrates that a MySQL database table can be configured to refuse duplicate entries
within a table field.
• 19. “This will fail, since the Customers table has not been filled first.”
The insertion fails, since the value of CustomerId in the Purchases table must be in the
Customers table before it can be used in the Purchases table. Likewise, a CustomerId cannot
be removed from the Customers table while it is also in the Purchases table.