Download presentation
Presentation is loading. Please wait.
Published byMitchell Ford Modified over 9 years ago
1
1 COP 4710 Databases Fall, 2000 Today’s Topic Chapter 7: SQL David A. Gaitros October 9th, 2000 Department of Computer Science
2
2 Modifying Content with SQL n Insert queries –insert into Customer values (555, 'Yu', 'Jia','540 Magnolia Hall','Tallahassee', 'FL', '32306') –insert into Customer (firstName, lastName, accountId) values ('Jia', 'Yu', 555) n Update queries –update TimeCard set paid = true where paid = false –update HourlyEmployee set hourlyRate = hourlyRate *1.1 where ssn = '145-09-0967' n Samples in Access
3
3 Creating Pay Statements with SQL n Find the number of hours worked for each employee entry –select TimeCard.ssn, sum((endTime- startTime)*24) as hoursWorked from TimeCard where paid=false group by ssn n Create the Pay Statement entries for each Employee –select ssn, hourlyRate, hoursWorked, hoursWorked * hourlyRate as amountPaid, today from … n Insert into the PayStatement table –Insert into PayStatement select … n Look at the Access example in BigHit.mdb
4
4 Defining queries for the PayStatement n A view is a named query n create view EmployeeHours as –select TimeCard.ssn, sum((endTime- startTime)*24) as hoursWorked from TimeCard where paid=false group by ssn n create view EmployeePay as –select ssn, hourlyRate, hoursWorked, hoursWorked * hourlyRate as amountPaid, today from EmployeeHours h, HourlyEmployee e where h.ssn=e.ssn n insert into PayStatement select * from EmployeePay
5
5 Marking TimeCards as paid n update TimeCard set paid = true n update TimeCard set paid=true where paid=false n updateTimeCard set paid=true where ssn in (select ssn from EmployeePay) n What happens if time cards added while pay statements are being created?
6
6 Delete Statements n Delete all time cards for non-hourly employees –delete from Timecard where not exists (select * from HourlyEmployee where TimeCard.ssn = HourlyEmployee.ssn) n More examples in BigHit Video Access database
7
7 Create Table Statement n create table Customer ( accountId int, lastName varchar(32), firstName varchar(32), street varchar(100), city varchar(32), state char(2), zipcode varchar(9) ) n Note that SQL has specific types
8
8 Data types in SQL
9
9 Key Constraints in SQL n Key declarations are part of create table –create table Store ( storeId int primary key, –create table Movie ( movieId varchar(10) primary key, –create table Rental ( accountId int, videoId varchar(10), primary key (accountId, videoId)
10
10 Referential Integrity Constraints n A relationship is implemented by attributes that reference the primary key of the related table –Enforcing referential integrity requires guaranteeing that there is a referenced object –An attempt to modify the relationship (insert, update or delete) is potential violation n Declare foreign key constraints –create table Store ( manager int references Employee –create table Rental ( foreign key (accountId) references Customer(accountId)
11
11 Maintaining Referential Integrity n What happens when an update violates referential integrity –update foreign key attribute change catalog id of a video –insert new object add a new video –delete related object delete catalog entry –update primary key attribute change catalog id of a video title n Alternatives –propagate changes –set to null
12
12 Constraints on Values of Attributes n Not null constraints –create table PreviousRental ( accountId int not null references Customer, videoId int not null references Videotape, dateRented datetime not null, dateReturned datetime, cost real, primary key (accountId, videoId, dateRented)) n Check constraints –check (checkOut < dueDate) –check (answer in (‘T’,’F’)) –check (questionId in (select questionId from questions where quizId=…))
13
13 Strategies for Enforcing Constraints n Enforce always –Never allow a violation of constraint –Suppose 2 rentals are recorded wrong change the customerId of 2 records –some violation will result n Enforce at end of transaction –Allow violations during updates, but check and enforce at the end of the process n Leads us to consider –Chapter 14 Transactions in SQL –Allow cancellation of updates –Support concurrent access
14
14 COP 4710 Databases Fall, 2000 Today’s Topic Chapter 9: Java and Web Sites David A. Gaitros Department of Computer Science
15
15 Java Objects and variables n Objects are dynamically allocated –Figures A.1 and A.2 show String variables Assignment (=) and equality (==)
16
16 Differences from C++ n C++ has three kinds of object variables –ObjClass fixedObj, & refObj, * ptrObj –Java has only one –Hence, no dereferencing, no address calculation, no pointer or reference types n C++ methods, by default, are not virtual –Java methods are virtual by default n C++ virtual method hierarchies follow class hierarchy –Java virtual methods can be based on interfaces n C++ has preprocessor, compiler and linker –Java has compiler and RTE
17
17 Browser-Web server-DB architecture n Figure 8.2: Architecture of a Web site supported by databases
18
18 Java DB Connectivity (JDBC) n Figure 8.4 Strategies for implementing JDBC packages
19
19 Connecting to databases with Java n java.sql.Driver –no methods for users –DriverManager.Connect method create connection n java.sql.Connection –createStatement n java.sql.Statement –executeQuery returns table as ResultSet –executeUpdate returns integer update count n Examples in class
20
20 Details on JDBC Connections n Loading driver classes –Class.forName(“sun.jdbc.odbc.JdbcOdbcDriver”); // Driver for JDBC-ODBC Bridge. Supports the bridge between the ODBC Microsoft Driver and the Java Database Driver. –Class.forName(“oracle.thin.Driver”); // This is the driver needed to connect directly to Oracle. –Class.forName(“jdbc:z1MySQL:”); // This is the Driver to connect to a database using MySQL.
21
21 n n Database connection URL – –jdbc: : – –protocol example jdbc:odbc:mydatabase – –subname example //hostname:port/databasename //enp01.enp.fsu.edu:3306/gsim – –CS Oracle URL jdbc:oracle:thin:@oracle.cs.fsu.edu:1521:cop45 40 Details on JDBC Connections
22
22 Examples of JDBC Applications n See SqlFilter.java n See Web sites –http://enp01.enp.fsu.edu n See code in examples directory
23
23 Executing Insert and Update Statements n Create new customer, using String + int rowcount = stmt.executeUpdate( ”insert into Customer ” +”(accountId,lastName,firstName) ” +”values (1239,’Brown’,’Mary’)”); if (rowcount == 0) // insert failed n Update –String updateSQL = “update TimeCard set “ +”TimeCard.paid = 'yes’ where “ +”paid<>'yes’”; int count = stmt.execute(updateSQL); // count is number of rows affected
24
24 Executing unknown SQL n Arbitrary SQL may return table (ResultSet) or row count (int) n Statement.execute method stmt.execute(sqlStatement); result = stmt.getResultSet(); while (true) {// loop through all results if (result != null) // process result else {// result is not a ResultSet rowcount = stmt.getUpdateCount(); if (rowcount == -1) break // no more results else // process row count } result = stmt.getMoreResults()) }
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.