Beruflich Dokumente
Kultur Dokumente
Objectives:
Pre-Lab Preparation:
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
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.
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.
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.
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.
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.
Part A: Instructions
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`
--
-- --------------------------------------------------------
--
-- Table structure for table `customer`
--
6
--
-- Dumping data for table `customer`
--
-- --------------------------------------------------------
--
-- Table structure for table `employee`
--
--
-- Dumping data for table `employee`
--
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`
--
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`
--
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
--
-- Database: `place your database or student id here`
9
--
-- --------------------------------------------------------
--
-- Table structure for table `product`
--
--
-- 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`
--
--
-- Dumping data for table `invoice`
--
-- --------------------------------------------------------
--
-- 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`
--
-- --------------------------------------------------------
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;
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.
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.
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:
7. Type in the command show tables;. Do you see all the tables?
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
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.
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.
5. Write only one SQL statement that can insert multiple values in the customer2 table as follows:
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.
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.
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:
18