0% found this document useful (0 votes)
39 views

Introduction To Mysql Solutions 2

ajhhajhsdasdjkhahkhkhdasd

Uploaded by

Mantily Holmes
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
39 views

Introduction To Mysql Solutions 2

ajhhajhsdasdjkhahkhkhdasd

Uploaded by

Mantily Holmes
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Introduction to MySQL: Solutions

W. H. Bell

October 31, 2022

1 Typesetting and spaces

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.

• 14. “What has happened to the input values?”

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.

Introduction to MySQL: Solutions Page 1 of 2


Listing 2: The file create-tables-1.sql.
1 DROP TABLE IF EXISTS Customers ;
2 CREATE TABLE Customers (
3 CustomerId INT ,
4 CustomerName VARCHAR (255)
5 );

Listing 3: Attempting to insert bad values


insert into Customers ( CustomerId , CustomerName )
values ( ’A text string ’ , ’A bad value ’) ;
insert into Customers ( CustomerId , CustomerName )
values ( ’ 15 ’ ,999) ;

• 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.

Listing 4: The file insert-data-2.sql.


1 INSERT INTO Customers ( CustomerName )
2 VALUES ( ’ Barry Rayner ’) ;
3 INSERT INTO Customers ( CustomerName )
4 VALUES ( ’ Zaynab Rowley ’) ;
5 INSERT INTO Customers ( CustomerName )
6 VALUES ( ’ Asiya Sheridan ’) ;
7 INSERT INTO Customers ( CustomerName )
8 VALUES ( Null ) ;
9 INSERT INTO Customers ( CustomerName )
10 VALUES ( ’ Jak May ’) ;

• 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.

Introduction to MySQL: Solutions Page 2 of 2

You might also like