View
2
Download
0
Category
Preview:
Citation preview
Phil Smith
Learning outcomes LO2
LO2 Develop a fully functional relational database system, based on an existing system design.
The design for the assessment is of your own choosing.
Implementation of security elements in databases.
Relational databases with controls like data validation using; input masks,
drop down lists, option buttons.
User interface for requirements, functionality, reliability, consistency and
performance.
Implementation – DQL1. DQL: Data Query Language
Sample activities:
Demonstration of query language and application by
students to apply to own applications.
Implementation – DQL1. TASK
Firstly we need another example of the MWS
database.
On your local xampp.
Create a new database name emws.
Import emws.sql on studshare/HND
We shall use emws for the web based interface.
SQL - Select
The SELECT clause is use to make queries against
data held in one or more tables in your database.
The basic syntax of the select clause -
SELECT column-name1, column-name2, column-name3, column-nameN
from table-name;
SQL: SELECT Statement
A basic SELECT statement includes 3 clauses
SELECT <attribute name> FROM <tables> WHERE <condition>
SELECT
Specifies the
attributes that are
part of the resulting
relation
FROM
Specifies the tables
that serve as the
input to the
statement
WHERE
Specifies the
selection condition,
including the join
condition.
Note: that you don't need to use WHERE
Using a “*” in a select statement indicates that
every attribute of the input table is to be selected.
Example: SELECT * FROM … WHERE …;
To get unique rows, type the keyword DISTINCT
after SELECT.
Example: SELECT DISTINCT * FROM …
WHERE …;
SQL: SELECT Statement (cont.)
8
SELECT Statement Clauses of the SELECT statement:
SELECT
List the columns (and expressions) that should be returned from the query
FROM
Indicate the table(s) or view(s) from which data will be obtained
WHERE
Indicate the conditions under which a row will be included in the result
GROUP BY
Indicate columns to group the results
HAVING
Indicate the conditions under which a group will be included
ORDER BY
Sorts the result according to specified columns
9
Figure 7-8: SQL statement
processing order
SELECT Example 1 List Customers
We have already done this single table query.
SELECT `Title`,`FirstName`,`Surname`,`DateOfBirth`
FROM `customer`
SELECT Example 2 List Customers
We have already done this multi table query.
SELECT `Title`,`FirstName`,`Surname`,`DateOfBirth`,
orders.DateOfOrder
FROM `customer`
Inner Join orders on orders.CustomerID =
customer.CustomerID
SELECT Example 3 List Customers
We have already done this multi table query. Only Customers who have placed orders whose first name is Jean?
SELECT `Title`,`FirstName`,`Surname`,`DateOfBirth`, orders.DateOfOrder
FROM `customer`
Inner Join orders on orders.CustomerID = customer.CustomerID
Where Firstname = “Jean”
13
SELECT Example using Alias Alias is an alternative column or table name
SELECT CONCAT(CUST.Firstname, ' ',
CUST.FirstName) AS NAME, CUST.City
FROM CUSTOMER CUST WHERE
`Surname` = "Jones"
Using a Function Using the COUNT aggregate function to find totals
Aggregate functions: SUM(), MIN(), MAX(), AVG(),
COUNT()
How many customers do we have?
SELECT Count(*) FROM `CUSTOMER`
Answer = 44 in the MWS example data
16
SELECT Example – Boolean Operators
AND, OR, and NOT Operators for customizing
conditions in WHERE clause
SELECT phones.Model, designs.DesignType, designs.Cost
FROM `products`
Inner join phones on phones.id = products.PhoneID and
phones.Model = 'iPhone 7'
Inner join designs on designs.id = products.DesignID
17
SELECT Example – Like
SELECT phones.Model, designs.DesignType, designs.Cost
FROM `products`
Inner join phones on phones.id = products.PhoneID
Inner join designs on designs.id = products.DesignID
Where phones.Model LIKE "%iP%"
Note: the LIKE operator allows you to compare strings using
wildcards. For example, the % wildcard in ‘%Desk’ indicates that all
strings that have any number of characters preceding the word “Desk”
will be allowed
SELECT Example –Sorting Results with the ORDER BY Clause
Sort the results first by Design
SELECT phones.Model, designs.DesignType, designs.Cost
FROM `products`
Inner join phones on phones.id = products.PhoneID
Inner join designs on designs.id = products.DesignID
Where phones.Model LIKE "%iP%"
Order by designs.DesignType
SELECT Example –with the in Clause
Inclide results where city is in a provided list
SELECT * FROM `customer` WHERE `City` in ("Hereford",
"Molden")
Note: the IN City value is either Hereford or Molden. It is more efficient
than separate OR conditions
SELECT Example –Categorizing Results Using the GROUP BY Clause
SELECT City, Count(*) FROM `customer`
Group By City
Note: you can use single-value fields with aggregate
functions if they are included in the GROUP BY clause
SELECT Example –Qualifying Results by Categories Using the HAVING Clause
For use generally with GROUP BY
SELECT phones.Model, designs.DesignType,
designs.Cost FROM `products`
Inner join phones on phones.id = products.PhoneID
Inner join designs on designs.id = products.DesignID
Having designs.Cost > 3.50
Like a WHERE clause, but it operates on groups (categories),
not on individual rows. Here, only those groups with total
numbers greater than 1 will be included in final result
Using and Defining Views
Views provide users controlled access to tables
Base Table – table containing the raw data
Dynamic View A “virtual table” created dynamically upon request by a user
No data actually stored; instead data from base table made available to user
Based on SQL SELECT statement on base tables or other views
Sample CREATE VIEWCREATE VIEW `viewshowusers` AS
select `users`.`userid` AS `userid`,`users`.`userlogin` AS `userlogin`,`users`.`firstnames` AS `firstnames`,`users`.`surname` AS `surname`,`users`.`adminuser` AS `adminuser`
from `users` ;
▪View has a name
▪View is based on a SELECT statement
24
Advantages of Views
Simplify query commands
Assist with data security (but don't rely on views for security, there are more important security measures)
Enhance programming productivity
Contain most current base table data
Use little storage space
Provide customized view for user
25
Disadvantages of Views
Use processing time each time view is referenced
May or may not be directly updateable
SQL: Join operation
A join can be specified in the FROM clause
which list the two input relations and the
WHERE clause which lists the join condition.
Example:
Biotech1003
Sales1002
IT1001
DivisionID
TN1002
MA1001
CA1000
StateID
Emp Dept
SQL: Join operation (cont.)
Sales1002
IT1001
Dept.DivisionDept.ID
TN1002
MA1001
Emp.StateEmp.ID
inner join = join
SELECT *
FROM emp join dept (or FROM emp, dept)
on emp.id = dept.id;
SQL: Join operation (cont.)
IT1001
Sales1002
nullnull
Dept.DivisionDept.ID
CA1000
TN1002
MA1001
Emp.StateEmp.ID
left outer join = left join
SELECT *
FROM emp left join dept
on emp.id = dept.id;
SQL: Join operation (cont.)
Sales1002
Biotech1003
IT1001
Dept.DivisionDept.ID
MA1001
nullnull
TN1002
Emp.StateEmp.ID
right outer join = right join
SELECT *
FROM emp right join dept
on emp.id = dept.id;
SQL Union1. You can combine the output from two or more
queries/views by using the union clause.
2. The general form is -
Select col1, col2 from table1 where col1 = ‘xxx’
Union
Select col3, col4 from table2 where col3 = ‘yyy’
Rules, the selected columns from each query must
match in type, the output column must be the same
type.
SQL Correlated sub queryConsider this query - we want to list all customers who have placed no orders.
We could try and do this with left outer joins.
But we can to a correlated subquery.
Select `FirstName`,`Surname` From Customer C
Where not exists (select "a" from orders CO where CO.Customerid = C.Customerid)
The sub query returns a true/false condition.
Select ‘a’ is faster then returning a column.
Implementation – TestingLO3. Test the system against user and system
requirements.
Identify elements of the system that need to be tested.
Consider data that should be used to fully test the
system.
Match tests against user and system requirements.
Test procedures to be used: test plans, test models e.g.
white box, black box;
testing documentation.
Functional and system testing and testing the robustness
of the system,
including help menus, pop-ups, hot-spots, data validation
checks.
Implementation – TestingTesting and test data.
Database Testing is checking –
1. the schema (physical against logical)
2. Tables (structure – keys, validation, defaults etc)
3. Triggers (if any)
4. It may involve creating complex queries to
load/stress test the database
5. check its responsiveness to 5 above.
6. Checks data integrity
7. Check consistency
8. Check insert/update anomalies.
Implementation – Testing
Implementation – Testing
The 3 types of Database Testing are
Structural Testing
Functional Testing
Non-functional Testing
Structural database testingThe structural data testing involves the
validation of all those elements inside the
data repository that are used primarily for
storage of data and which are not allowed to
be directly manipulated by the end users.
The validation of the database servers is
also a very important consideration in these
types of testing.
The successful completion of this phase by
the testers involves mastery in SQL queries.
Schema testingThe structural data testing involves the
validation of all those elements inside the
data repository that are used primarily for
storage of data and which are not allowed to
be directly manipulated by the end users.
The validation of the database servers is
also a very important consideration in these
types of testing.
The successful completion of this phase by
the testers involves mastery in SQL queries.
What have we learnt today?
Applying techniques to your own problem.
You need to create your own database from your
own design.
Recommended