Data
Java E-Commerce © Martin Cooke, 2003 Plan Issues in data-handling Relational database and SQL refresher XML vs relational databases Next lecture: Java database connectivity API (JDBC) 14/04/2019 Java E-Commerce © Martin Cooke, 2003
Issues in data handling on the web
Java E-Commerce © Martin Cooke, 2003 persistent? structure? portable? available? concurrent access? secure? transactionally safe? 14/04/2019 Java E-Commerce © Martin Cooke, 2003
Java E-Commerce © Martin Cooke, 2003 persistent? Granting immortality to your data instances Persistence = data living beyond the immediate lifecycle of the processes manipulating it A relational database is the primary tool for persistence, and has been for decades Can be handled automatically using an appropriate ‘container’ (see EJBs) structure? portable? available? concurrent access? secure? transactionally safe? 14/04/2019 Java E-Commerce © Martin Cooke, 2003
Java E-Commerce © Martin Cooke, 2003 persistent? Free text Most of the web Tabular Vast amounts of existing data (forced into tabular form?) Tree-structured Marked up content eg XML Tedious to handle relationally Arbitrary object Images, sound files Tricky to handle relationally Object-relational or object database structure? portable? available? concurrent access? secure? transactionally safe? 14/04/2019 Java E-Commerce © Martin Cooke, 2003
Java E-Commerce © Martin Cooke, 2003 persistent? Free text Most of the web Tabular Vast amounts of existing data (forced into tabular form?) Tree-structured Marked up content eg XML Tedious to handle relationally Arbitrary object Images, sound files Tricky to handle relationally Object-relational or object database structure? portable? available? concurrent access? secure? transactionally safe? Our focus in this lecture is on tabular data 14/04/2019 Java E-Commerce © Martin Cooke, 2003
Java E-Commerce © Martin Cooke, 2003 persistent? Vast volumes of data must be … searched rapidly … available 24x7 (eg credit card checking) … accessible by many concurrent users Rely on optimised database engines which have been honed over the years structure? portable? available? concurrent access? secure? transactionally safe? 14/04/2019 Java E-Commerce © Martin Cooke, 2003
Java E-Commerce © Martin Cooke, 2003 persistent? Modification Dirty reads and writes Pessimistic vs optimistic concurrency Efficiency How to manage multiple connections? Expensive to create Limited in number for typical databases structure? portable? available? concurrent access? secure? transactionally safe? 14/04/2019 Java E-Commerce © Martin Cooke, 2003
Java E-Commerce © Martin Cooke, 2003 persistent? To maintain consistency, it often makes sense to carry out a group of changes (a transaction) to data credit card db and order db If one statement in the transaction fails, data can be left in an inconsistent state must be able to unwind (rollback) the transaction structure? portable? available? concurrent access? secure? transactionally safe? 14/04/2019 Java E-Commerce © Martin Cooke, 2003
Java E-Commerce © Martin Cooke, 2003 persistent? Just how secure are files left lying around the place? Again, rely on existing secure solution in relational database Introduces its own layer of authentication structure? portable? available? concurrent access? secure? transactionally safe? 14/04/2019 Java E-Commerce © Martin Cooke, 2003
Java E-Commerce © Martin Cooke, 2003 persistent? Java may be portable, but what about its dependence on existing databases? Ideally, no vendor tie-in avoid legacy data problems in future Tricky in practice since industry standard (SQL) is not as standard as it seems structure? portable? available? concurrent access? secure? transactionally safe? 14/04/2019 Java E-Commerce © Martin Cooke, 2003
Relational database and SQL refresher Nobody fails the databases course
(Relational) data model student module lecturer 14/04/2019 Java E-Commerce © Martin Cooke, 2003
Java E-Commerce © Martin Cooke, 2003 id name 1 Java 2 Databases … student stud/mod module sid mid 1 2 id name SSID 1 B Black 12456541 2 J Brown 97625516 … lecturer 14/04/2019 Java E-Commerce © Martin Cooke, 2003
Java E-Commerce © Martin Cooke, 2003 SQL CREATE command student stud/mod module id name SSID 1 B Black 12456541 2 J Brown 97625516 … lecturer CREATE TABLE student ( id INT NOT NULL PRIMARY KEY AUTO_INCREMENT, name VARCHAR(4), SSID INT DEFAULT 0 ) CREATE TABLE student ( id INT, name TEXT, SSID INT ) 14/04/2019 Java E-Commerce © Martin Cooke, 2003
Java E-Commerce © Martin Cooke, 2003 SELECT statement id name 1 Java 2 Databases … student stud/mod module sid mid 1 2 id name SSID 1 B Black 12456541 2 J Brown 97625516 … lecturer SELECT module.name FROM module,student,studMod WHERE module.id = mid AND student.name = ‘B Black’ AND student.id = studMod.sid; SELECT name FROM student WHERE id = 2; 14/04/2019 Java E-Commerce © Martin Cooke, 2003
Java E-Commerce © Martin Cooke, 2003 CREATE and INSERT CREATE TABLE student ( id INT NOT NULL PRIMARY KEY AUTO_INCREMENT, name VARCHAR(50) NOT NULL, SSID INT DEFAULT 0 ); INSERT INTO student VALUES(1,'Joe Bloggs',12345678); INSERT INTO student VALUES(20,'Bob Black',null); INSERT INTO student VALUES(4,'Gill Brown',23267656); 14/04/2019 Java E-Commerce © Martin Cooke, 2003
Java E-Commerce © Martin Cooke, 2003 SELECT SELECT * FROM student; 14/04/2019 Java E-Commerce © Martin Cooke, 2003
Java E-Commerce © Martin Cooke, 2003 UPDATE UPDATE student SET name = ‘Gill Bloggs’ WHERE id = 4; 14/04/2019 Java E-Commerce © Martin Cooke, 2003
Java E-Commerce © Martin Cooke, 2003 JOIN Find all modules taken by Bob Black 14/04/2019 Java E-Commerce © Martin Cooke, 2003
Java E-Commerce © Martin Cooke, 2003 Other SQL DELETE Deletes rows from a table DELETE FROM student WHERE name=‘fred’; 14/04/2019 Java E-Commerce © Martin Cooke, 2003
Java E-Commerce © Martin Cooke, 2003 Other SQL DELETE Deletes rows from a table DELETE FROM student WHERE name=‘fred’; DROP Permanently remove database or table from the system DROP DATABASE studs; DROP TABLE student; 14/04/2019 Java E-Commerce © Martin Cooke, 2003
Java E-Commerce © Martin Cooke, 2003 Other SQL DELETE Deletes rows from a table DELETE FROM student WHERE name=‘fred’; DROP Permanently remove database or table from the system DROP DATABASE studs; DROP TABLE student; DESCRIBE Give information about a table or column DESCRIBE student; 14/04/2019 Java E-Commerce © Martin Cooke, 2003
Java E-Commerce © Martin Cooke, 2003 Other SQL DELETE Deletes rows from a table DELETE FROM student WHERE name=‘fred’; DROP Permanently remove database or table from the system DROP DATABASE studs; DROP TABLE student; DESCRIBE Give information about a table or column DESCRIBE student; SHOW Display information about system SHOW DATABASES SHOW TABLES [FROM db] 14/04/2019 Java E-Commerce © Martin Cooke, 2003
Java E-Commerce © Martin Cooke, 2003 Datatypes SQL2 BIT, TINYINT, SMALLINT, INTEGER, BIGINT, REAL, FLOAT, DOUBLE, DECIMAL, NUMERIC, CHAR, VARCHAR, LONGVARCHAR, DATE, TIME, TIMESTAMP, BINARY, VARBINARY, LONGVARBINARY emphasis on Primitive types Storage size SQL3 BLOB, CLOB, ARRAY, REF, STRUCT 14/04/2019 Java E-Commerce © Martin Cooke, 2003
Java E-Commerce © Martin Cooke, 2003 More on datatypes Different database engines support their own types internally and loosely translate to an SQL2 type Big mismatch between rich Java type possibilities and SQL datatypes Care needed, though JDBC helps (later) 14/04/2019 Java E-Commerce © Martin Cooke, 2003
Choices and integrations Relational data vs XML Choices and integrations
Persistence is not the issue Need to consider questions of data representation and persistence separately Can store/retrieve XML as a string on a relational database 14/04/2019 Java E-Commerce © Martin Cooke, 2003
Java E-Commerce © Martin Cooke, 2003 Which to use? pros cons XML Good for complex data Reasonably good fit to object-based software Storage-hungry Slow to build and search documents Relational data Good for tabular data Optimised for memory and speed, especially for multi-factor searches Tricky for tree-structured data Very tricky for arbitrary objects: needs transformation 14/04/2019 Java E-Commerce © Martin Cooke, 2003
Complementary aspects Store XML together with some search terms Convert SQL to XML Represent SQL query in XML 14/04/2019 Java E-Commerce © Martin Cooke, 2003
Resources
Java E-Commerce © Martin Cooke, 2003 Book Yarger, Reese and King (1999) MySQL and mSQL, O’Reilly, 1-56592-434-7 Check if later version 14/04/2019 Java E-Commerce © Martin Cooke, 2003
Java E-Commerce © Martin Cooke, 2003 Online documents An online SQL interpreter http://www.dcs.napier.ac.uk/~andrew/sql/ 14/04/2019 Java E-Commerce © Martin Cooke, 2003