A Relational Database Overview
A database is a means of storing information in such a way that information can be retrieved from it. In simplest terms, a relational database is one that presents information in tables with rows and columns. A table is referred to as a relation in the sense that it is a collection of objects of the same type (rows). Data in a table can be related according to common keys or concepts, and the ability to retrieve related data from a table is the basis for the term relational database. A Database Management System (DBMS) handles the way data is stored, maintained, and retrieved. In the case of a relational database, a Relational Database Management System (RDBMS) performs these tasks. DBMS as used in this book is a general term that includes RDBMS.
Integrity Rules
完整性约束
Relational tables follow certain integrity rules to ensure that the data they contain stay accurate and are always accessible. First, the rows in a relational table should all be distinct. If there are duplicate rows, there can be problems resolving which of two possible selections is the correct one. For most DBMSs, the user can specify that duplicate rows are not allowed, and if that is done, the DBMS will prevent the addition of any rows that duplicate an existing row.
唯一性
A second integrity rule of the traditional relational model is that column values must not be repeating groups or arrays. A third aspect of data integrity involves the concept of a null value. A database takes care of situations where data may not be available by using a null value to indicate that a value is missing. It does not equate to a blank or zero. A blank is considered equal to another blank, a zero is equal to another zero, but two null values are not considered equal.
原子性
When each row in a table is different, it is possible to use one or more columns to identify a particular row. This unique column or group of columns is called a primary key. Any column that is part of a primary key cannot be null; if it were, the primary key containing it would no longer be a complete identifier. This rule is referred to as entity integrity.
主键
Table 1.2 illustrates some of these relational database concepts. It has five columns and six rows, with each row representing a different employee.
Table 1.2: Employees
Employee_Number First_name Last_Name Date_of_Birth Car_Number
10001 Axel Washington 28-Aug-43 5
10083 Arvid Sharma 24-Nov-54 null
10120 Jonas Ginsberg 01-Jan-69 null
10005 Florence Wojokowski 04-Jul-71 12
10099 Sean Washington 21-Sep-66 null
10035 Elizabeth Yamaguchi 24-Dec-59 null
The primary key for this table would generally be the employee number because each one is guaranteed to be different. (A number is also more efficient than a string for making comparisons.) It would also be possible to use First_Name and Last_Name because the combination of the two also identifies just one row in our sample database. Using the last name alone would not work because there are two employees with the last name of "Washington." In this particular case the first names are all different, so one could conceivably use that column as a primary key, but it is best to avoid using a column where duplicates could occur. If Elizabeth Taylor gets a job at this company and the primary key is First_Name, the RDBMS will not allow her name to be added (if it has been specified that no duplicates are permitted). Because there is already an Elizabeth in the table, adding a second one would make the primary key useless as a way of identifying just one row. Note that although using First_Name and Last_Name is a unique composite key for this example, it might not be unique in a larger database. Note also that Table 1.2 assumes that there can be only one car per employee.
(主键用数字的话会更快捷一些。也可以用姓和名做组合主键。其实也不是很合理啦)
SELECT Statements
SQL is a language designed to be used with relational databases. There is a set of basic SQL commands that is considered standard and is used by all RDBMSs. For example, all RDBMSs use the SELECT statement.
A SELECT statement, also called a query, is used to get information from a table. It specifies one or more column headings, one or more tables from which to select, and some criteria for selection. The RDBMS returns rows of the column entries that satisfy the stated requirements. A SELECT statement such as the following will fetch the first and last names of employees who have company cars:
(此处省略...)
A special type of WHERE clause involves a join, which is explained in the next section.
Joins
(比较罗嗦,再省)
Common SQL Commands
SQL commands are divided into categories, the two main ones being Data Manipulation Language (DML) commands and Data Definition Language (DDL) commands. DML commands deal with data, either retrieving it or modifying it to keep it up-to-date. DDL commands create or change tables and other database objects such as views and indexes.
A list of the more common DML commands follows:
SELECT — used to query and display data from a database. The SELECT statement specifies which columns to include in the result set. The vast majority of the SQL commands used in applications are SELECT statements.
INSERT — adds new rows to a table. INSERT is used to populate a newly created table or to add a new row (or rows) to an already-existing table.
DELETE — removes a specified row or set of rows from a table
UPDATE — changes an existing value in a column or group of columns in a table
The more common DDL commands follow:
CREATE TABLE — creates a table with the column names the user provides. The user also needs to specify a type for the data in each column. Data types vary from one RDBMS to another, so a user might need to use metadata to establish the data types used by a particular database. CREATE TABLE is normally used less often than the data manipulation commands because a table is created only once, whereas adding or deleting rows or changing individual values generally occurs more frequently.
DROP TABLE — deletes all rows and removes the table definition from the database. A JDBC API implementation is required to support the DROP TABLE command as specified by SQL92, Transitional Level. However, support for the CASCADE and RESTRICT options of DROP TABLE is optional. In addition, the behavior of DROP TABLE is implementation-defined when there are views or integrity constraints defined that reference the table being dropped.
ALTER TABLE — adds or removes a column from a table. It also adds or drops table constraints and alters column attributes
(DML和DDL)
Result Sets and Cursors
(结果集和光标)
The rows that satisfy the conditions of a query are called the result set. The number of rows returned in a result set can be zero, one, or many. A user can access the data in a result set one row at a time, and a cursor provides the means to do that. A cursor can be thought of as a pointer into a file that contains the rows of the result set, and that pointer has the ability to keep track of which row is currently being accessed. A cursor allows a user to process each row of a result set from top to bottom and consequently may be used for iterative processing. Most DBMSs create a cursor automatically when a result set is generated.
Earlier JDBC API versions added new capabilities for a result set's cursor, allowing it to move both forward and backward and also allowing it to move to a specified row or to a row whose position is relative to another row.
Transactions
(事务)
When one user is accessing data in a database, another user may be accessing the same data at the same time. If, for instance, the first user is updating some columns in a table at the same time the second user is selecting columns from that same table, it is possible for the second user to get partly old data and partly updated data. For this reason, DBMSs use transactions to maintain data in a consistent state (data consistency) while allowing more than one user to access a database at the same time (data concurrency).
(同时获取或更改相同的数据,具有不可确定性)
A transaction is a set of one or more SQL statements that make up a logical unit of work. A transaction ends with either a commit or a rollback, depending on whether there are any problems with data consistency or data concurrency. The commit statement makes permanent the changes resulting from the SQL statements in the transaction, and the rollback statement undoes all changes resulting from the SQL statements in the transaction.
(事务就是一组sql语句,或者被提交或者被回滚)
A lock is a mechanism that prohibits two transactions from manipulating the same data at the same time. For example, a table lock prevents a table from being dropped if there is an uncommitted transaction on that table. In some DBMSs, a table lock also locks all of the rows in a table. A row lock prevents two transactions from modifying the same row, or it prevents one transaction from selecting a row while another transaction is still modifying it.
(锁是防止两个事物同时操作相同数据的机制。行锁和表锁)
Stored Procedures
(存储过程)
A stored procedure is a group of SQL statements that can be called by name. In other words, it is executable code, a mini-program, that performs a particular task that can be invoked the same way one can call a function or method. Traditionally, stored procedures have been written in a DBMS-specific programming language. The latest generation of database products allows stored procedures to be written using the Java programming language and the JDBC API. Stored procedures written in the Java programming language are bytecode portable between DBMSs. Once a stored procedure is written, it can be used and reused because a DBMS that supports stored procedures will, as its name implies, store it in the database.
(居然把prepareStatement叫做java编码的存储过程。疯掉了。记得sqlserver2005开始可以用真正的编程语言来写存储过程了。人家微软一整套方案自然可以达到这个效果,oracle又不是sun的。怎么可能有java语言的存储过程呢)
The following code is an example of how to create a very simple stored procedure using the Java programming language. Note that the stored procedure is just a static Java method that contains normal JDBC code. It accepts two input parameters and uses them to change an employee's car number.
Do not worry if you do not understand the example at this point. The code example below is presented only to illustrate what a stored procedure looks like. You will learn how to write the code in this example in the tutorials that follow.
import java.sql.*;
public class UpdateCar {
public static void UpdateCarNum(int carNo, int empNo)
throws SQLException {
Connection con = null;
PreparedStatement pstmt = null;
try {
con = DriverManager.getConnection("jdbc:default:connection");
pstmt = con.prepareStatement(
"UPDATE EMPLOYEES SET CAR_NUMBER = ? " +
"WHERE EMPLOYEE_NUMBER = ?");
pstmt.setInt(1, carNo);
pstmt.setInt(2, empNo);
pstmt.executeUpdate();
}
finally {
if (pstmt != null) pstmt.close();
}
}
}
Metadata
(元数据)
Databases store user data, and they also store information about the database itself. Most DBMSs have a set of system tables, which list tables in the database, column names in each table, primary keys, foreign keys, stored procedures, and so forth. Each DBMS has its own functions for getting information about table layouts and database features. JDBC provides the interface DatabaseMetaData, which a driver writer must implement so that its methods return information about the driver and/or DBMS for which the driver is written. For example, a large number of methods return whether or not the driver supports a particular functionality. This interface gives users and tools a standardized way to get metadata. In general, developers writing tools and drivers are the ones most likely to be concerned with metadata.
分享到:
相关推荐
JDBC(Java Database Connectivity)是Java平台上的一个标准接口,由Sun Microsystems开发并纳入Java Development Kit (JDK) 中,它为Java程序员提供了一种统一的方法来访问各种数据库。《JDBC API教程与参考》是由...
《JDBC API Tutorial and Reference 3rd Edition》是数据库编程领域一本重要的参考资料,它深入浅出地介绍了Java数据库连接(JDBC)API的使用方法。这本书由Addison Wesley出版,旨在帮助开发者理解和掌握如何在Java...
JDBC™ API Tutorial and Reference, Third Edition By Maydene Fisher, Jon Ellis, Jonathan Bruce Publisher : Addison Wesley Pub Date : June 13, 2003 ISBN : 0-321-17384-8
实在对不起大家,之前传的这个有问题。是我的疏忽,不是我有意骗大家分。我重新上传了。这个就不要再下了。 #非常棒的一本入门教程,配合java api doc 中文看,黄金搭档。
ResultSet rs = stmt.executeQuery("SELECT * FROM users"); // 处理结果集 while (rs.next()) { System.out.println(rs.getString("name") + ", " + rs.getString("email")); } // 关闭资源 rs.close(); ...
在这个"SUN Java certificate tutorial.rar"压缩包中,很显然包含了一个关于如何理解和使用Java证书的教学资源。 首先,Java证书是公钥基础设施(PKI)的一部分,它由可信的证书颁发机构(CA)签署,以确认一个实体...
通过一个实例代码完全描述了决策树的构造过程。具有很好的学习和借鉴意义。(PDF文档)
- JDBC教程:[http://java.sun.com/docs/books/tutorial/jdbc/](http://java.sun.com/docs/books/tutorial/jdbc/) - 可用JDBC驱动列表:[http://industry.java.sun.com/products/jdbc/drivers/]...
在这个"FMC-IMAGEON - Building a Video Design from Scratch Tutorial"中,我们将会深入探讨如何从零开始构建一个视频设计项目。教程的标题暗示了这是一次全面的学习过程,旨在帮助用户掌握视频设计的基础知识和...
Sun权威教程--《J2EE Tutorial中文版》 作者:Stephanie Bodoff,Dale Green,Kim Haase,Eric Jendrock,Monica Pawlan,Beth Stearns 翻译参与人员:sharetop,worldheart,zhaoy,bruce等 出版商:铁道出版社...
JDBC API知道和开发引用
- JDBC教程:[http://java.sun.com/docs/books/tutorial/jdbc](http://java.sun.com/docs/books/tutorial/jdbc) - 多线程教程:[http://java.sun.com/docs/books/tutorial/essential/threads]...
《The J2EETM Tutorial 中文版》的作者是Sun Microsystem公司Java开发小组资深的开发人员,与另一部取得空前成功的《The JavaTM Tutorial》一样,建立了与读者之间最为有效的交互途径。同时,在本书的编写过程中,也...
Sun Directory Server 是一款基于 Lightweight Directory Access Protocol (LDAP) 的目录服务软件,由 Sun Microsystems 开发。在进行 Sun Directory Server 的安装教程中,有几个关键的知识点是必须了解的,这些...
JDBC API教程与参考手册(第三版)其余部分 PART 3
"j3d_tutorial\ j3d教程sun官方版" 提供的是Sun官方的Java 3D教程,这是一份非常宝贵的资源,对于学习和理解Java 3D编程至关重要。 Java 3D API允许开发者通过对象模型来构建3D世界,这些对象包括几何形状、变换、...