【正文】
declare c cursor for select * from EMP where city = ?Parise? for update ? To update tuple at the current location of cursor update EMP set SAL = SAL + 100 where current of c 動態(tài) SQL ? Allows programs to construct and submit SQL queries at run time. ? The dynamic SQL program contains a ?, which is a place holder for a value that is provided when the SQL program is executed. 動態(tài) SQL續(xù) ? Example of the use of dynamic SQL from within a C program. char * sqlprog = “update EMP set SAL = SAL * where d = ?” EXEC SQL prepare dynprog from :sqlprog。 char account [10] = “A101”。 EXEC SQL execute dynprog using :account。 ODBC ? Open DataBase Connectivity(ODBC) standard – standard for application program to municate with a database server. – application program interface (API) to ? open a connection with a database, ? send queries and updates, ? get back results. ? Applications such as GUI, spreadsheets, etc. can use ODBC ODBC 續(xù) ? Each database system supporting ODBC provides a driver library that must be linked with the client program. ? When client program makes an ODBC API call, the code in the library municates with the server to carry out the requested action, and fetch results. ? ODBC program first allocates an SQL environment, then a database connection handle. ODBC 續(xù) ? Opens database connection using SQLConnect(). ? Parameters for SQLConnect: – connection handle, – the server to which to connect – the user identifier, – password ? Must also specify types of arguments: – SQL_NTS denotes previous argument is a nullterminated string. ODBC 編程 ? int ODBCexample() { RETCODE error。 HENV env。 /* environment */ HDBC conn。 /* database connection */ SQLAllocEnv(amp。env)。 SQLAllocConnect(env, amp。conn)。 SQLConnect(conn, , SQL_NTS, avi, SQL_NTS, avipasswd, SQL_NTS)。 { … . Do actual work … } SQLDisconnect(conn)。 SQLFreeConnect(conn)。 SQLFreeEnv(env)。 } Exercise Write the following queries, based on the following database example Movie(title, year, length, inColor, studioName, producerC) StarsIn(movieTitle, movieYear, strName) MovieStar(name, address, gender, birthdate) MovieExec(name, address, cert, Worth) Studio(name, address, presC) Classes(class, type, country, numGuns, bore, displacement) Ships(name, class, launched) Battles(name, date) Outes(ship, battle,result) In SQL. 1. Find Sandra Bullock’s birthdate 2. Find all executives worth at least $10,000,000 3. Find all the stars who either are male or live in Malibu stars appeared in movies produced by MGM in 1995? 5. Who is the president of MGM studio? 6. Find the countries whose ships had the largest number of guns. 7. Find the names of the ship with 16inch bore. 8. Find the average number of guns of battleship classes.