Sunteți pe pagina 1din 18

IS222 Lab 7

Introduction to Structured Query Language


Using MySQL

Approximate Time required: 2 hours

Objectives:

 To learn basic SQL commands 


To practice writing SQL commands in MySQL 

Pre-Lab Preparation:

Before attending this lab, it is a requirement to have done the following:

You should have completed all previous labs.


You should have read the lab (this document).
You should have read the Chapter on Introduction to Structured Query Language (SQL) from
the textbook and looked at the lecture notes relating to SQL from prior weeks.

You should have looked at the following reference links to familiarize yourself with the
basics of the SQL syntax (for DML/DDL) when working with MySql. You also can use these
during the lab for reference:
1) http://www3.ntu.edu.sg/home/ehchua/programming/sql/MySQL_Beginner.html
(provides a good summary of pivotal SQL commands and its syntax)
2) https://dev.mysql.com/doc/refman/5.1/en/sql-syntax.html (this link provides the entire
reference manual for describing the syntax for the SQL statements supported by MySQL.
This is the complete guide for you to locate details regarding the syntax of any type of
DDL/DML SQL query supported in MySQL.

Note that the reference manual is for MySql 5.1 as that is what we have installed in our
SCIMS based server

Note:

After the lab and the lectures from the previous week, you should be aware of the following:
- Connecting to your MySql account
- Issuing basic commands in MySql (over the Shell), using your database, seeing table
structures/data and the tables in your db, etc
- The fundamental syntax for creating tables and DML queries such as select, insert etc
including making selections based on certain conditions, and the concat function etc (as
discussed in the lecture)
- All other query related elements discussed in the lectures from the previous week

1
- How to access your database via phpMyAdmin

What you will be doing?

In this lab you are going to practice writing SQL statements. For better practice, you should also
attempt the Review Questions from the Introduction to Structured Query Language (SQL) chapter of
the text.

Overall Goal of this Tutorial

By the end of this lab, you should know how write a variety of queries using MySQL.

Saving Queries

You cannot save your queries in MySQL. Create a text file (using Notepad or Notepad++) and then
type your queries in the text file. Notepad is recommended.

Save the file with the extension, sql. You can then copy and paste the queries from your text file
into the putty window.

Schema for the Lab

The following database structure should result after you create all your tables in your student
database. Please note that you cannot see the following schema in MySQL.

2
Task 1: Mapping onto your Personal Share on the given SCIMS server
In the next task you will be creating SQL script/Batch files that will need to be placed on the SCIMS
share that has been created for you, so that you can execute it later.
In order to do this, you will need to know how to map onto your given network share.
A guide of how you can map to any particular share on a Windows machines is given below that you
can use for this task.

Mapping Network Drives Guide

To map network drives in Windows, do the following:

1. Do one of the following:


o Windows XP: Right-click on My Computer and select Map Network Drive....
o Windows 7/Vista: Click Start. Right-click on Computer and select Map Network
Drive....
o Windows 8: On the Start screen, type the word computer. Right-click on the Computer
icon. Then click Map network drive.
2. The Map Network Drive window will appear.
3. In the Drive: box, it does not matter which letter you choose for the drive. However, if a letter is
usually associated with the drive that you are mapping, you could select that letter for simplicity.
4. Enter a network path in the Folder: box.
o Example network share: \\computername\sharename
5. Click Finish.
6. If you are prompted to enter a user name and password, do so with the relevant credentials.
7. Click OK.

Complete all the Task 1 instructions below:


1. Map on to the share \\scims.usp.ac.fj\Sxxxxxxxx where Sxxxxxxxx represents your student
ID. You should already have the basic skills of knowing how to map onto some Network
Share. If not, then use the guide above.
Note: If a different networking mapping is to be used, it will be announced on Moodle
where this lab’s link is
2. If user credentials are sought with any window popups (like the one below), specify your
details as follows and Click OK:
a. Username: student\ Sxxxxxxxx (where Sxxxxxxxx represents your student ID)
b. Password: your email password should be used

3
3. After connecting successfully, you should see your share show in a Window like the one
below:

4. Inside the public_html folder, create a new folder called lab7 (this is where you will place
your script files from the tasks that follow). Make sure that lab7 is one word without any
spaces and the same case as spelt here.
5. Now you can start Task 2 and create your batch files. Your batch files will need to be copied
to the lab7 folder you created earlier so that you can execute them from the MySql shell.

Task 2: Creating SQL Script/Batch Files

In this task you will create 2 batch files that you will later execute to create the schema for the lab
given earlier. Read the important notes below before you proceed with the task instructions for
Part A and B.

4
Important Notes:
a) It is possible to place comments/remarks that can explain the purpose of the SQL statements in
your script. You use -- before beginning your comment. These are similar to comments that you
have in programming with languages like Java or C++, C Sharp etc which use // instead of --. These
comments/remarks are NOT executed in the script.
For instance, the snippet below gives an example of comments/remarks in a batch file which will
not be executed.
-- phpMyAdmin SQL Dump
-- version 2.11.1.2
-- http://www.phpmyadmin.net
b) Each SQL statement that you place in the script file should end with a ; (semi-colon) to indicate
the end of that statement.
c) Try to leave a blank line between each SQL statement. Note that one SQL statement can have
multiple lines and its end is indicated with the semi-colon.
d) Note that the double/single quotation symbol from Microsoft Word may not necessarily be
compatible with the shell environment for MySql. It’s best to use the quotation symbols in Notepad.
e) Also note that when you have spaces in your table names eg. My Table, you can specify them as
`My Table`. Be wary of the use of `, ' and " symbols. However, avoid using spaces for separators and
use _ (underscore) instead.
Finally:
- Be sure to note the syntax of the statements in the scripts that you will work with for the
next tasks. Try to briefly understand the purpose of each statement as you do each task.
- Note that the ‘DROP TABLE’ statement is used to remove the existing table so that it can be
recreated.
- The ‘CREATE TABLE IF NOT EXISTS’ syntax only creates a table if it does not exist.
- When you write your scripts (such as for your assignments), they should create your tables
from scratch and remove any existing tables of the same name in your script. This is useful
in testing your scripts. This is NOT compulsory though unless specified so in the
assignment.

Complete all the instructions for Part A and Part B

Part A: Instructions

1. Open Notepad/Notepad++ to start a new text file.


2. Save the new file as "DBPart1.sql" (with the quotes and the same case) in the lab7
folder on your given SCIMS-based network share. When saving, you can change the
Save As Type option to All Files.
3. Now copy all the code snippet from the script given below and paste it into your file
on the share. You can just copy the entire statements directly from here and paste it
into your Notepad script file.

Note:
a) It is important to note that when you write your scripts, do not include any

5
connect or use statements at the top to connect to your database. Just write the
SQL statements required. This is important for your assignment.
b) Be careful not to copy the page numbers on the bottom right of each page. The
entire script spans several pages.

4. You can modify the remark in the script below to include your student ID in between
the quotes:

-- Database: `place your database or student id here in the remark`

5. Save your file so that it reflects the changes.

SQL script - DBPart1.sql

-- phpMyAdmin SQL Dump


-- version 2.11.1.2
-- http://www.phpmyadmin.net
--
-- Host: localhost
-- Generation Time: Nov 13, 2010 at 05:23 PM
-- Server version: 5.0.77
-- PHP Version: 5.1.6

--
-- Database: `place your database or student id here in the remark`
--

-- --------------------------------------------------------

--
-- Table structure for table `customer`
--

DROP TABLE IF EXISTS `customer`;


CREATE TABLE IF NOT EXISTS `customer` (
`cus_code` int(11) NOT NULL default '0',
`cus_lname` varchar(15) default NULL,
`cus_fname` varchar(15) default NULL,
`cus_initial` varchar(1) default NULL,
`cus_areacode` varchar(3) default NULL,
`cus_phone` varchar(8) default NULL,
`cus_balance` decimal(19,4) default '0.0000',
CONSTRAINT customer_pk_cuscode PRIMARY KEY (`cus_code`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

6
--
-- Dumping data for table `customer`
--

INSERT INTO `customer` (`cus_code`, `cus_lname`, `cus_fname`, `cus_initial`,


`cus_areacode`, `cus_phone`, `cus_balance`) VALUES
(10010, 'Ramas', 'Alfred', 'A', '615', '844-2573', 0.0000),
(10011, 'Dunne', 'Leona', 'K', '713', '894-1238', 0.0000),
(10012, 'Smith', 'Kathy', 'W', '615', '894-2285', 345.8600),
(10013, 'Olowski', 'Paul', 'F', '615', '894-2180', 536.7500),
(10014, 'Orlando', 'Myron', NULL, '615', '222-1672', 0.0000),
(10015, 'O''Brian', 'Amy', 'B', '713', '442-3381', 0.0000),
(10016, 'Brown', 'James', 'G', '615', '297-1228', 221.1900),
(10017, 'Williams', 'George', NULL, '615', '290-2556', 768.9300),
(10018, 'Farriss', 'Anne', 'G', '713', '382-7185', 216.5500),
(10019, 'Smith', 'Olette', 'K', '615', '297-3809', 0.0000);

-- --------------------------------------------------------

--
-- Table structure for table `employee`
--

DROP TABLE IF EXISTS `employee`;


CREATE TABLE IF NOT EXISTS `employee` (
`emp_num` int(11) NOT NULL default '0',
`emp_title` varchar(4) default NULL,
`emp_lname` varchar(15) default NULL,
`emp_fname` varchar(15) default NULL,
`emp_initial` varchar(1) default NULL,
`emp_dob` datetime default NULL,
`emp_hire_date` datetime default NULL,
`emp_years` int(11) default '0',
`emp_areacode` varchar(3) default NULL,
`emp_phone` varchar(8) default NULL,
CONSTRAINT employee_pk_empnum PRIMARY KEY (`emp_num`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

--
-- Dumping data for table `employee`
--

INSERT INTO `employee` (`emp_num`, `emp_title`, `emp_lname`,


`emp_fname`, `emp_initial`, `emp_dob`, `emp_hire_date`, `emp_years`,
`emp_areacode`, `emp_phone`) VALUES
(100, 'Mr.', 'Kolmycz', 'George', 'D', '1942-06-15 00:00:00', '1985-03-15

7
00:00:00', 18, '615', '324-5456'),
(101, 'Ms.', 'Lewis', 'Rhonda', 'G', '1965-03-19 00:00:00', '1986-04-25
00:00:00', 16, '615', '324-4472'),
(102, 'Mr.', 'VanDam', 'Rhett', NULL, '1958-11-14 00:00:00', '1990-12-20
00:00:00', 12, '901', '675-8993'),
(103, 'Ms.', 'Jones', 'Anne', 'M', '1974-10-16 00:00:00', '1994-08-28 00:00:00',
8, '615', '898-3456'),
(104, 'Mr.', 'Lange', 'John', 'P', '1971-11-08 00:00:00', '1994-10-20 00:00:00', 8,
'901', '504-4430'),
(105, 'Mr.', 'Williams', 'Robert', 'D', '1975-03-14 00:00:00', '1998-11-08
00:00:00', 4, '615', '890-3220'),
(106, 'Mrs.', 'Smith', 'Jeanine', 'K', '1968-02-12 00:00:00', '1989-01-05
00:00:00', 14, '615', '324-7883'),
(107, 'Mr.', 'Diante', 'Jorge', 'D', '1974-08-21 00:00:00', '1994-07-02 00:00:00',
8, '615', '890-4567'),
(108, 'Mr.', 'Wiesenbach', 'Paul', 'R', '1966-02-14 00:00:00', '1992-11-18
00:00:00', 10, '615', '897-4358'),
(109, 'Mr.', 'Smith', 'George', 'K', '1961-06-18 00:00:00', '1989-04-14 00:00:00',
13, '901', '504-3339'),
(110, 'Mrs.', 'Genkazi', 'Leighla', 'W', '1970-05-19 00:00:00', '1990-12-01
00:00:00', 12, '901', '569-0093'),
(111, 'Mr.', 'Washington', 'Rupert', 'E', '1966-01-03 00:00:00', '1993-06-21
00:00:00', 9, '615', '890-4925'),
(112, 'Mr.', 'Johnson', 'Edward', 'E', '1961-05-14 00:00:00', '1983-12-01
00:00:00', 19, '615', '898-4387'),
(113, 'Ms.', 'Smythe', 'Melanie', 'P', '1970-09-15 00:00:00', '1999-05-11
00:00:00', 3, '615', '324-9006'),
(114, 'Ms.', 'Brandon', 'Marie', 'G', '1956-11-02 00:00:00', '1979-11-15
00:00:00', 23, '901', '882-0845'),
(115, 'Mrs.', 'Saranda', 'Hermine', 'R', '1972-07-25 00:00:00', '1993-04-23
00:00:00', 9, '615', '324-5505'),
(116, 'Mr.', 'Smith', 'George', 'A', '1965-11-08 00:00:00', '1988-12-10 00:00:00',
14, '615', '890-2984');

-- --------------------------------------------------------

--
-- Table structure for table `vendor`
--

DROP TABLE IF EXISTS `vendor`;


CREATE TABLE IF NOT EXISTS `vendor` (
`v_code` int(11) NOT NULL default '0',
`v_name` varchar(15) default NULL,
`v_contact` varchar(50) default NULL,
`v_areacode` varchar(3) default NULL,

8
`v_phone` varchar(8) default NULL,
`v_state` varchar(2) default NULL,
`v_order` varchar(1) default NULL,
CONSTRAINT vendor_pk_vcode PRIMARY KEY (`v_code`),
UNIQUE INDEX vendor_uidx_vname (`v_name`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

--
-- Dumping data for table `vendor`
--

INSERT INTO `vendor` (`v_code`, `v_name`, `v_contact`, `v_areacode`,


`v_phone`, `v_state`, `v_order`) VALUES
(21225, 'Bryson, Inc.', 'Smithson', '615', '223-3234', 'TN', 'Y'),
(21226, 'SuperLoo, Inc.', 'Flushing', '904', '215-8995', 'FL', 'N'),
(21231, 'D&E Supply', 'Singh', '615', '228-3245', 'TN', 'Y'),
(21344, 'Gomez Bros.', 'Ortega', '615', '889-2546', 'KY', 'N'),
(22567, 'Dome Supply', 'Smith', '901', '678-1419', 'GA', 'N'),
(23119, 'Randsets Ltd.', 'Anderson', '901', '678-3998', 'GA', 'Y'),
(24004, 'Brackman Bros.', 'Browning', '615', '228-1410', 'TN', 'N'),
(24288, 'ORDVA, Inc.', 'Hakford', '615', '898-1234', 'TN', 'Y'),
(25443, 'B&K, Inc.', 'Smith', '904', '227-0093', 'FL', 'N'),
(25501, 'Damal Supplies', 'Smythe', '615', '890-3529', 'TN', 'N'),
(25595, 'Rubicon Systems', 'Orton', '904', '456-0092', 'FL', 'Y');

Part B: Instructions

You will follow the same instructions as in Part A to create the second batch file for its given
script below in the lab7 folder on your share. The new file will be called "DBPart2.sql" using
the same case

SQL script – DBPart2.sql

-- phpMyAdmin SQL Dump


-- version 2.11.1.2
-- http://www.phpmyadmin.net
--
-- Host: localhost
-- Generation Time: Nov 13, 2010 at 05:23 PM
-- Server version: 5.0.77
-- PHP Version: 5.1.6

--
-- Database: `place your database or student id here`

9
--

-- --------------------------------------------------------

--
-- Table structure for table `product`
--

DROP TABLE IF EXISTS `product`;


CREATE TABLE IF NOT EXISTS `product` (
`p_code` varchar(10) NOT NULL,
`p_descript` varchar(35) default NULL,
`p_indate` datetime default NULL,
`p_onhand` int(11) default '0',
`p_min` int(11) default '0',
`p_price` decimal(19,4) default '0.0000',
`p_discount` double default '0',
`v_code` int(11) default '0',
CONSTRAINT product_pk_pcode PRIMARY KEY (`p_code`),
CONSTRAINT product_fk_vcode FOREIGN KEY (`v_code`) REFERENCES vendor
(`v_code`),
INDEX product_idx_pdescript (`p_descript`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

--
-- Dumping data for table `product`
--
INSERT INTO `product` VALUES
('11QER/31', 'Power painter, 15 psi., 3-nozzle', '2003-11-03 00:00:00', 8, 5, 109.99, 0,
25595),
('13-Q2/P2', '7.25-in. pwr. saw blade', '2003-12-13 00:00:00', 32, 15, 14.99, .05, 21344),
('14-Q1/L3', '9.00-in. pwr. saw blade', '2003-11-13 00:00:00', 18, 12, 17.49, 0, 21344),
('1546-QQ2', 'Hrd. cloth, 1/4-in., 2x50', '2004-01-15 00:00:00', 15, 8, 39.95, 0, 23119),
('1558-QW1', 'Hrd. cloth, 1/2-in., 3x50', '2004-01-15 00:00:00', 23, 5, 43.99, 0, 23119),
('2232/QTY', 'B&D jigsaw, 12-in. blade', '2003-12-30 00:00:00', 8, 5, 109.92, .05, 24288),
('2232/QWE', 'B&D jigsaw, 8-in. blade', '2003-12-24 00:00:00', 6, 5, 99.87, .05, 24288),
('2238/QPD', 'B&D cordless drill, 1/2-in.', '2004-01-20 00:00:00', 12, 5, 38.95, .05,
25595),
('23109-HB', 'Claw hammer', '2004-01-20 00:00:00', 23, 10, 9.95, .1, 21225),
('23114-AA', 'Sledge hammer, 12 lb.', '2004-01-02 00:00:00', 8, 5, 14.4, .05, NULL),
('54778-2T', 'Rat-tail file, 1/8-in. fine', '2003-12-15 00:00:00', 43, 20, 4.99, 0, 21344),
('89-WRE-Q', 'Hicut chain saw, 16 in.', '2004-02-07 00:00:00', 11, 5, 256.99, .05, 24288),

10
('PVC23DRT', 'PVC pipe, 3.5-in., 8-ft', '2004-02-20 00:00:00', 188, 75, 5.87, 0, NULL),
('SM-18277', '1.25-in. metal screw, 25', '2004-03-01 00:00:00', 172, 75, 6.99, 0, 21225),
('SW-23116', '2.5-in. wd. screw, 50', '2004-02-24 00:00:00', 237, 100, 8.45, 0, 21231),
('WR3/TT3', 'Steel matting, 4\'x8\'x1/6\", .5\" mesh', '2004-01-17 00:00:00', 18, 5,
119.95, .1, 25595);

-- --------------------------------------------------------

--
-- Table structure for table `invoice`
--

DROP TABLE IF EXISTS `invoice`;


CREATE TABLE IF NOT EXISTS `invoice` (
`inv_number` int(11) NOT NULL default '0',
`cus_code` int(11) default '0',
`inv_date` datetime default NULL,
CONSTRAINT invoice_pk_invnumber PRIMARY KEY (`inv_number`),
CONSTRAINT invoice_fk_cuscode FOREIGN KEY (`cus_code`) REFERENCES customer
(`cus_code`),
INDEX invoice_idx_invdate (`inv_date`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

--
-- Dumping data for table `invoice`
--

INSERT INTO `invoice` (`inv_number`, `cus_code`, `inv_date`) VALUES


(1001, 10014, '2004-01-16 00:00:00'),
(1002, 10011, '2004-01-16 00:00:00'),
(1003, 10012, '2004-01-16 00:00:00'),
(1004, 10011, '2004-01-17 00:00:00'),
(1005, 10018, '2004-01-17 00:00:00'),
(1006, 10014, '2004-01-17 00:00:00'),
(1007, 10015, '2004-01-17 00:00:00'),
(1008, 10011, '2004-01-17 00:00:00');

-- --------------------------------------------------------

--
-- Table structure for table `line`
--

11
DROP TABLE IF EXISTS `line`;
CREATE TABLE IF NOT EXISTS `line` (
`inv_number` int(11) NOT NULL default '0',
`line_number` int(11) NOT NULL default '0',
`p_code` varchar(10) default NULL,
`line_units` double default '0',
`line_price` decimal(19,4) default '0.0000',
CONSTRAINT line_pk_invnum_linenum PRIMARY KEY (`inv_number`,`line_number`),
CONSTRAINT line_fk_invnumber FOREIGN KEY (`inv_number`) REFERENCES invoice
(`inv_number`),
CONSTRAINT line_fk_pcode FOREIGN KEY (`p_code`) REFERENCES product (`p_code`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

--
-- Dumping data for table `line`
--

INSERT INTO `line` (`inv_number`, `line_number`, `p_code`, `line_units`, `line_price`)


VALUES
(1001, 1, '13-Q2/P2', 1, 14.9900),
(1001, 2, '23109-HB', 1, 9.9500),
(1002, 1, '54778-2T', 2, 4.9900),
(1003, 1, '2238/QPD', 1, 38.9500),
(1003, 2, '1546-QQ2', 1, 39.9500),
(1003, 3, '13-Q2/P2', 5, 14.9900),
(1004, 1, '54778-2T', 3, 4.9900),
(1004, 2, '23109-HB', 2, 9.9500),
(1005, 1, 'PVC23DRT', 12, 5.8700),
(1006, 1, 'SM-18277', 3, 6.9900),
(1006, 2, '2232/QTY', 1, 109.9200),
(1006, 3, '23109-HB', 1, 9.9500),
(1006, 4, '89-WRE-Q', 1, 256.9900),
(1007, 1, '13-Q2/P2', 2, 14.9900),
(1007, 2, '54778-2T', 1, 4.9900),
(1008, 1, 'PVC23DRT', 5, 5.8700),
(1008, 2, 'WR3/TT3', 3, 119.9500),
(1008, 3, '23109-HB', 1, 9.9500);

-- --------------------------------------------------------

12
Task 3: Create your database schema via Script Execution

Now that you have created your batch files on your given network shares, you can generate your
tables to create the database schema required for the lab.
Follow the instructions below:

1. Before beginning with Step 2, be sure you have the batch files on your network shares from
the previous tasks in the lab7 folder.
2. Login to the given SCIMS server from the previous lab using the putty software (look at your
previous lab if you have forgotten how to do this)
3. Now login to your MySQL database. Issue the connect or use statement to connect to your
student database before you execute your script.

For example, issue the statement below at the MySQL prompt after you have logged in with
your MySql account:

connect Sxxxxxxxx;

where Sxxxxxxxx represents your student id and your database.

Note:
 When you execute your script, the shell will execute the script for the
database that it is already connected to. It is pivotal to connect to your db
before running the script using the source command.
 The general syntax for executing a batch file via the shell is:
source filename_with_path

4. The command to execute your DBPart1.sql script at the MySQL prompt (in the same case)
is as follows, (after you have connected to your database):

source ~/public_html/lab7/DBPart1.sql

If your script was created correctly, everything should go just fine without any errors. If you
are unsure about anything, your tutor will assist and check.

A sample output as follows may show:

13
5. The command to execute your DBPart2.sql script at the MySQL prompt (in the same case)
is as follows, (after you have connected to your database):

source ~/public_html/lab7/DBPart2.sql

If your script was created correctly, everything should go just fine without any errors. If you
are unsure about anything, your tutor will assist and check.

A sample output as follows may show:

14
6. If steps (4) and (5) were successful and you managed to run both the scripts, go straight to
step 7.

If you were unable to use the source command from steps 4 & 5, take the following steps
below to execute your scripts:

a) Open the DBPart1.sql file in Notepad or Notepad++.


b) Copy all the SQL commands (use Crtl + A and Ctrl + C keys on the keyboard or Click on
Edit menu and choose Select All and click on Edit again and select Copy).
c) Go to the putty window with your MySQL prompt (assuming you are already logged in
and have issued the connect/use statement for your db)
d) Right click on the screen to paste. Your SQL commands that you copied from the file
should be pasted and executed at the same time.
e) Press the Enter key a couple of times after pasting the commands just to make sure that
file SQL commands get executed.
f) Now follow all the steps from (a) for DBPart2.sql file.

7. Type in the command show tables;. Do you see all the tables?

A sample output as follows should show:

8. To see the data in the tables, use the select statement for your tables, For eg. select * from
customer; should yield the output as follows:

15
Task 4: Writing Queries

- Now write the SQL commands in MySQL for the following.


- Your tutor will need to see your queries so these should be saved in a text file.
- Call this file Lab7_Task4.sql.
- Save this file anywhere you can have access to it after the lab.

Note:
You cannot save your queries in MySQL. You do not need to run a script for these questions
below.

Questions

1. Write the SQL statement that would be used to create a table the same as the Customer table.
The name of the new table will be Customer2. However, the number of characters allowed
should be 30 for the first and last name attributes. Use the InnoDB engine.

Also do not include the clause (If Not Exists) for creating this table.

Those will be the only differences from the original customer table.

Note: You can look up the Create Table Syntax from the MySQL Reference manual links given in
the lab and the scripts that you used for this lab from the initial tasks.

2. Write the SQL statement that would be used to create a table the same as the Product table.
The name of the new table will be Product2. Add into the create table syntax the clause to only
create the table if it does not exist. Also set the default p_min value to 2. Use the MyISAM
engine. There will be no other difference from the original product table.

To check if your engine is correct, you will issue the command: SHOW TABLE STATUS WHERE
Name = 'product2';

3. Create a table called customer2_product which will have p_code and cus_code from the
product (not product 2 table) and customer2 tables respectively, making up the foreign key in
this new table. Also, both attributes will make up the Primary key in this new table. These are
the only attributes.

- Note also that your foreign key constraint for the customer2 table with cus_code should
have no referential action on delete and update.
- Note also that your foreign key constraint for the product table with p_code should have
allow the referential actions: on delete cascade and on update cascade.

16
Use the InnoDb engine.

Hint: See the syntax for creating tables in the manual.

You can also issue the command

select * from information_schema.table_constraints where table_schema =


schema()and table_name = 'customer2_product';

to see its constraints after creating it. A sample output is below:

Additional Question: What is the effect of the on delete cascade and on update cascade on the
foreign key constraint relating to the product table? Explain in relation to referential integrity
and with some examples of data in your database.

4. Insert the following data in the Customer2 table.

Cus_code Cus_Lname Cus_Fname Cus_Initial Cus_AreaCode Cus_Phone Cus_Balance


10010 Ramas Alfred A 615 844-2573 0.00

5. Write only one SQL statement that can insert multiple values in the customer2 table as follows:

Cus_code Cus_Lname Cus_Fname Cus_Initial Cus_AreaCode Cus_Phone Cus_Balance


777 Di Stefano Alfredo I 617 844-2173 9.80
778 Puskas Ferenc K 619 844-2273 45.40

6. Write a query to delete the customer with the customer code of 777 in the customer 2 table.

NOTE:
In the remaining exercises you are going to use the CUSTOMER table and NOT the CUSTOMER2
table

7. Write query to display the list of all customers. You should display all the columns of the
customer table. You can either list all the columns individually or use an alternative approach.
Attempt both possible solutions.

17
Note that the cus_code should be the first column displayed in both your solutions.

8. Write a query to select the customer code and the customer’s full name from the customer
table. See the sample result below. What is peculiar about the output below?

Hint: Use MySQL’s CONCAT and Concat_WS functions. Write one solution using each function and
compare with the output above.

Additional Question: What is the difference in the solutions?

9. Write a query to select the customer code and the customer’s full name from the customer
table just like the previous query. This time, attempt two solutions using the & and + operators
to concatenate separately.

Additional Question: What is the output for each case?

10. Generate a listing of all invoices that were created for the customer with the customer code,
10011. A sample result is shown in the figure below:

HINT: Use the WHERE clause.

18

S-ar putea să vă placă și