_______________________________________________________________________________________________________________ E-Commerce: Fundamentals and Applications1 Wiley and the book authors 2001 E-Commerce: Fundamentals and Applications Chapter 5 : Server-Side Programming II Database Connectivity
_______________________________________________________________________________________________________________ E-Commerce: Fundamentals and Applications2 Wiley and the book authors 2001 Outline Overview of relational database systems Common SQL commands and their usage
_______________________________________________________________________________________________________________ E-Commerce: Fundamentals and Applications3 Wiley and the book authors 2001 Relational database Database: a shared collection of interrelated data in a structured form. Relational database: uses relations to represent entities and relationships (the E-R relationship). Schema: describes the structure of the database. The relations are essentially "tables". These tables can be manipulated using SQL (Structured Query Language). Fig. 5.1 shows a snapshot of BOOK database which contains three related tables.
_______________________________________________________________________________________________________________ E-Commerce: Fundamentals and Applications4 Wiley and the book authors 2001 Snapshot of BOOK Database (Fig. 5.1)
_______________________________________________________________________________________________________________ E-Commerce: Fundamentals and Applications5 Wiley and the book authors 2001 Basic SQL statements In this chapter, we will provide a brief overview of SQL (The Structured Query Language) using the BOOK database as an example of how to manipulate a relational database based on SQL commands. There are four different basic SQL statements. They are: SELECT statement; INSERT statement; UPDATE statement; and DELETE statement.
_______________________________________________________________________________________________________________ E-Commerce: Fundamentals and Applications6 Wiley and the book authors 2001 SELECT Statement The SELECT command displays the results according to the user’s specified condition(s). A typical form of the SELECT statement is given by: SELECT fields FROM table WHERE criteria Example SELECT ISBN, Name FROM sbook WHERE Publisher='Wiley' It displays all the records with Publisher name "Wiley" and the records will contain only the fields (ISBN, Name) as shown in Fig. 5.2.
_______________________________________________________________________________________________________________ E-Commerce: Fundamentals and Applications7 Wiley and the book authors 2001 Conditional Query on sbook Table (Fig. 5.2) ISBNName Java Electronic Commerce Sourcebook E-commerce Security
_______________________________________________________________________________________________________________ E-Commerce: Fundamentals and Applications8 Wiley and the book authors 2001 Composite SELECT Statement Actually, we can link-up different tables during a query. Let’s see the following example: SELECT Customer.Customer_ID, Customer.Firstname, Transaction.Transaction_No, Transaction.ISBN FROM Customer, Transaction WHERE Customer.Customer_ID = Transaction.Customer_No AND Customer.Customer_ID='JO321' A snapshot of the search result is shown in Fig. 5.3.
_______________________________________________________________________________________________________________ E-Commerce: Fundamentals and Applications9 Wiley and the book authors 2001 A Composite Query on Customer and Transaction Tables (Fig. 5.3) Customer_IDFirstnameTransaction_NOISBN JO321Jones JO321Jones JO321Jones JO321Jones
_______________________________________________________________________________________________________________ E-Commerce: Fundamentals and Applications10 Wiley and the book authors 2001 INSERT Statement The INSERT statement is for adding new rows to a table. INSERT statement comes in two different forms allows the user to add only a single new row allows the user to add a set of rows to a table using information from another table. INSERT statement to add a new row The typical form of this SQL INSERT is: INSERT INTO table (field_name1, field_name2,.., field_nameN) VALUES (field_value1, field_value2, …, field_valueN )
_______________________________________________________________________________________________________________ E-Commerce: Fundamentals and Applications11 Wiley and the book authors 2001 INSERT to Add New Record (Fig. 5.4) Customer_IDLastnameFirstnameTel no CH222SimonChan CL444TimClarke JO321LarryJones LE123LeePeter TO133TommyRaymond Example INSERT INTO Customer (Customer_ID, Lastname, Firstname, “Tel no”) VALUES (’TO133', 'Tommy', ’Raymond', ' ')
_______________________________________________________________________________________________________________ E-Commerce: Fundamentals and Applications12 Wiley and the book authors 2001 UPDATE Statement The UPDATE statement is used to modify the value(s) of the data within a table, and the typical form of it is: UPDATE table SET field_name1 = field_value1, field_name2 = field_value2,.., field_nameN = field_valueN [WHERE ] Using the UPDATE statement, we can also modify the specified record(s) utilising some calculations. Example: Update transaction no ‘0010’ (in transaction table) for buying 2 books instead of 1 book and recalculate the total amount as well: UPDATE Transaction SET Qty = 2, Total = Total * 2 WHERE Transaction_No = ‘0010’
_______________________________________________________________________________________________________________ E-Commerce: Fundamentals and Applications13 Wiley and the book authors 2001 Using UPDATE Statement to Modify Transaction Table (Fig. 5.5) Transaction_NoCustomer_N o DateISBNQtyTotal 0001JO3211/1/ $ JO3211/1/ $ JO3211/1/ $ LE1231/5/ $ LE1231/5/ $ CL4441/5/ $ CL4441/5/ $ CL4441/5/ $ JO3211/10/ $ CH2221/18/ $456.00
_______________________________________________________________________________________________________________ E-Commerce: Fundamentals and Applications14 Wiley and the book authors 2001 DELETE Statement The DELETE statement is used to remove a record or a series of records from a table under specific condition(s). The typical form of it is given by: DELETE FROM table [WHERE ] Using the Customer Table as an example, we can delete all the records with Lastname(s) containing the character “i” by using the following statement: DELETE FROM Customer WHERE Lastname LIKE ‘%i%’
_______________________________________________________________________________________________________________ E-Commerce: Fundamentals and Applications15 Wiley and the book authors 2001 Customer Table After DELETE Operation (Fig. 5.6) Customer_IDLastnameFirstnameTel no CH222ChanSimon CR443JaneCraw JO321LarryJones TO133TommyRaymond LE123LeePeter