1、Database Chapter Four Outline Chapter 4: Advanced SQLSQL Data Types and Schemas EXACT(f7e/d from d) CAST e AS tcreate type Dollars as numeric (12,2) final create domain person_name char(20) not null Types and domains are similar. Domains can have constraints, such as not null, specified on them.
2、 Domains are not strongly typed. Domain Constraints Domain constraints are the most elementary form of integrity constraint. They test values inserted in the database, and test queries to ensure that the comparisons make sense. Large-Object Types Large objects (photos, videos, CAD files, etc.) a
3、re stored as a large object: blob: binary large object - object is a large collection of uninterpreted binary data (whose interpretation is left toan application outside of the database system) clob: character large object - object is a large collection of character data When a query returns a la
4、rge object, a pointer is returned rather than the large object itself. Constraints on a Single Relationnot null primary keyunique check (P), where P is a predicate Candidate keys are permitted to be null unless they have explicitly been declared to be not null (in contrast to primary keys). cre
5、ate table depositor [customer_name char(20), account_number char(lO), primary key (customer_name, account_number), foreign key (account_number) references account, foreign key (customer_name) references customer)Assertions
create assertion
6、 parts of the database: Read - allows reading, but not modification of data. Insert - allows insertion of new data, but not modification of existing data. Update - allows modification, but not deletion of data. Delete - allows deletion of data. Authorization Specification in SQLThe grant statem
7、ent is used to confer authorization
grant
8、 o verdraft-trigger after update on accountnrow nrow referencingnewrowasfor each row when nrow.balance < 0referencing old row as : for deletes and updates referencing new row as : for inserts and updatesSQL Functions create function account_count (customer_name varchar(20))returns integer be
9、gindeclare account integer; select count (*) into accountfrom depositor where depositor.customer_name = customer_namereturn account; endcreate function accounts_of (customer_name char(20) returns table ( account_number char(lO),branch_name charflS), balance numeric(12,2)) SQL Procedurescreate p
10、rocedure account count oroc (in title varchar(20), out a count integer) begin select count(author) into a count from depositor where deoositor.customer name = account count oroc. customer name end declare account integer;call account_count_proc( "Smith", account); ■ Specify the query in SQL an
11、d declare a cursor for itEXEC SQL declare ccursorforselect customer name, customer city from depositor, customer, account where depositor.customer name = customer.customer nameWVW^WVWXAA/W\AAAAAAAAAAAAAAAAAA/WWSAAAAAA/SAAAA/WW VWXAA/SAAAAAAAAAAAAAAAAAA/WSAAA/SAAA/WW'v4AAAAAAAAAAAAA/'v and deposit
12、or account number= accountaccount numberIXAAAAAAAA/VSAAAAAAAAAAAAAAAAAAA/S/V\A/\A/ W\AAAAAAAAAAAAAA/WSZvW\AAAAAAAAAAAAA>yWW\AAAAAAAAAA^ an d account.balance > :amountEND_EXEC ODBCOpen DataBase Connectivity(ODBC) standard standard for application program to communicate with a database server. app
13、lication 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 ODBCJDBC JDBC is a Java API for communicating with database systems supporting SQL JDBC supports a variety of features for que
14、rying and updating data, and for retrieving query results JDBC also supports metadata retrieval, such as querying about relations present in the database and the names and types of relation attributesModel for communicating with the database: Open a connectionCreate a “statement" object Execute q
15、ueries using the Statement object to send queries and fetch resultsException mechanism to handle errors public static void JDBCexamDle(String dbid, String userid, String passwdjtry( Class.forName (lvH);Connection conn = DriverManager.aetConnection( ,vidbc:oracle:thin:@aura.bell-labs :2000:bank
16、dbv\ userid, passwd): Statement stmt = conn .create S tatsm ent();... Do Actual Work .... stmt.close();connxIpseO; )catch (SQLException sale) { System.out.printlnf'SQLException : H + sale);) ■ Execute query and fetch and print resultsResultSet rset = stmt.executeQuery( "select branch_name, avg( balance) from accountgroup by branch-name,1); while (rset.next()){System .out.println( rsetgetStringCbranch-name") +11 M + rset.getFloat(2));






