⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 lib0048.html

📁 j2ee架构师手册
💻 HTML
字号:
<html>
<META http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<head>
<title>Key Terms and Concepts</title>
<link rel="STYLESHEET" type="text/css" href="images/xpolecat.css">
<link rel="STYLESHEET" type="text/css" href="images/ie.content.css">
</head>
<body>
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr><td><div STYLE="MARGIN-LEFT: 0.15in;"><a href="toc.html"><img src="images/teamlib.gif" width="62" height="15" border="0" align="absmiddle"  alt="Team LiB"></a></div></td>
<td align="right"><div STYLE="MARGIN-LEFT: 0.15in;">
<a href="LiB0047.html"><img src="images/previous.gif" width="62" height="15" border="0" align="absmiddle" alt="Previous Section"></a>
<a href="LiB0049.html"><img src="images/next.gif" width="41" height="15" border="0" align="absmiddle" alt="Next Section"></a>
</div></td></tr></table>
<br>
<div class="chapter">
<a name="ch07"></a>
<div class="section">
<h2 class="first-section-title"><a name="220"></a><a name="ch07lev1sec1"></a>Key Terms and Concepts</h2><p class="first-para">An <b class="bold">entity</b> is something you want to keep information about and thus represents information that persists (i.e., is written to media). Usually, an entity is a noun. Although most entities are implemented as database tables, the terms <i class="emphasis">entity</i> and <i class="emphasis">table</i> are not synonymous. An entity is purely a conceptual construct, with its closest counterpart in object modeling being a class. Good examples of entities are customer, account, user, customer order, and product.</p>
<p class="para">In a relational database, an entity is implemented as a table. When you implement your data model as an XML DTD or schema, then each entity becomes an element.</p>
<p class="para">An <b class="bold">entity occurrence</b> (sometimes shortened to occurrence) is an instance of an entity. If you're more comfortable with object modeling, you can think of an entity occurrence as similar to instantiating a class. If you can't resist the urge to equate entities and tables, consider an entity occurrence as a row in a table. And for XML users, an entity occurrence is like an individual element in an XML document.</p>
<p class="para">An <b class="bold">attribute</b> is a characteristic of an entity. Although attributes can be nouns, they usually don't make sense outside the context of an entity. For example, attributes of a <span class="fixed">CUSTOMER</span> entity could be <span class="fixed">CUSTOMER_ID</span>, <span class="fixed">FIRST_NAME</span>, <span class="fixed">LAST_NAME</span>, <span class="fixed">STREET_ADDRESS</span>, <span class="fixed">CITY</span>, <span class="fixed">STATE</span>, and <span class="fixed">ZIP_CODE</span>. Attributes should be <b class="bold">atomic</b>&#8212;that is, they should be self-contained and not derived from the values of other attributes.</p>
<p class="para">A <b class="bold">primary key</b> is the one attribute, or combination of attributes, of an entity that uniquely identifies an entity occurrence. For example, <span class="fixed">CUSTOMER_ID</span> would be a good primary key for the <span class="fixed">CUSTOMER</span> entity, and <span class="fixed">ACCOUNT_NUMBER</span> and <span class="fixed">ORDER_NUMBER</span> taken together would be a good primary key for an entity called <span class="fixed">CUSTOMER_ORDER</span>.</p>
<p class="para">Every entry must have a primary key. If, by chance, no combination of attributes uniquely identifies an entity occurrence, make up an attribute to <a name="221"></a><a name="IDX-87"></a>serve as a key. For example, most manufacturers assign unique identifiers (UPC codes) to their products, but when they don't, you might have to make up product IDs to serve as the primary key.</p>
<p class="para">A <b class="bold">relationship</b> is an association between two entities. Out of the many types of relationships that exist, three are commonly used: one-to-many, many-to-many, and supertype/subtype. In a <b class="bold">one-to-many</b> relationship, one occurrence of an entity is associated with possibly multiple occurrences of another entity. For example, a single customer could have multiple accounts or place multiple orders. Often, the entity with a single occurrence is called the <b class="bold">parent</b>, and the entity with multiple occurrences is called the <b class="bold">child</b>. <a class="internaljump" href="#ch07fig01">Figure 7.1</a> illustrates a one-to-many relationship.</p>
<div class="figure">
<a name="222"></a><a name="ch07fig01"></a><span class="figuremediaobject"><a href="images/fig103%5F01%5F0%2Ejpg" NAME="IMG_18" target="_parent"><img src="images/fig103_01.jpg" height="227" width="288" alt="Click To expand" border="0"></a></span>
<br style="line-height: 1">
<span class="figure-title"><span class="figure-titlelabel">Figure 7.1: </span>One-to-Many Relationship</span>
</div>
<p class="para">Notice in the figure that the <span class="fixed">ACCOUNT</span> entity contains the primary key (PK) columns of the <span class="fixed">ACCOUNT_TYPE</span> and <span class="fixed">CUSTOMER</span> entities. Each additional column in <span class="fixed">ACCOUNT</span> is a foreign key (FK). <b class="bold">Foreign keys</b> are the primary keys of related entities that an entity uses for look-up purposes. The existence of a foreign key is an implicit result of creating a one-to-many relationship. For example, given an occurrence of <span class="fixed">ACCOUNT</span>, related <span class="fixed">CUSTOMER</span> and <span class="fixed">ACCOUNT_TYPE</span> information is easy to determine.</p>
<p class="para">Some data-modeling tools provide for many-to-many relationships between entities. In a <b class="bold">many-to-many</b> relationship, each entity has a one-to many relationship with the other. For example, customer orders can contain many products, and each product can be purchased by multiple <a name="223"></a><a name="IDX-88"></a>customers. It is common to rewrite a many-to-many relationship as two separate one-to-many relationships with a new entity defined as a cross-reference. <a class="internaljump" href="#ch07fig02">Figure 7.2</a> is an example of a many-to-many relationship.</p>
<div class="figure">
<a name="224"></a><a name="ch07fig02"></a><span class="figuremediaobject"><a href="images/fig104%5F01%5F0%2Ejpg" NAME="IMG_19" target="_parent"><img src="images/fig104_01.jpg" height="112" width="350" alt="Click To expand" border="0"></a></span>
<br style="line-height: 1">
<span class="figure-title"><span class="figure-titlelabel">Figure 7.2: </span>Many-to-Many Relationship</span>
</div>
<p class="para">In a <b class="bold">supertype/subtype</b> relationship, an entity refines the definition of another entity. For example, in banking, a customer entity might be too generic for a bank that has trust customers, private banking customers, corporate customers, brokerage customers, and so on. As shown in <a class="internaljump" href="#ch07fig03">figure 7.3</a>, the <span class="fixed">CUSTOMER</span> entity is the supertype, and the others are the subtypes.</p>
<div class="figure">
<a name="225"></a><a name="ch07fig03"></a><span class="figuremediaobject"><a href="images/fig104%5F02%5F0%2Ejpg" NAME="IMG_20" target="_parent"><img src="images/fig104_02.jpg" height="274" width="350" alt="Click To expand" border="0"></a></span>
<br style="line-height: 1">
<span class="figure-title"><span class="figure-titlelabel">Figure 7.3: </span>Supertype/Subtype Relationship</span>
</div>
<a name="226"></a><a name="IDX-89"></a>
<p class="last-para">It is possible to have entities related to themselves. This is called a <b class="bold">recursive relationship</b>. For example, consider an <span class="fixed">EMPLOYEE</span> entity with <span class="fixed">EMPLOYEE_ID</span> as the primary key. A recursive one-to-many relationship could be used to indicate the manager of each employee. As a result of the relationship, a foreign key, say <span class="fixed">MANAGER_ID</span>, would be used to cross-reference employees with their managers.</p>
</div>
</div><br>
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr><td><div STYLE="MARGIN-LEFT: 0.15in;"><a href="toc.html"><img src="images/teamlib.gif" width="62" height="15" border="0" align="absmiddle"  alt="Team LiB"></a></div></td>
<td align="right"><div STYLE="MARGIN-LEFT: 0.15in;">
<a href="LiB0047.html"><img src="images/previous.gif" width="62" height="15" border="0" align="absmiddle" alt="Previous Section"></a>
<a href="LiB0049.html"><img src="images/next.gif" width="41" height="15" border="0" align="absmiddle" alt="Next Section"></a>
</div></td></tr></table>
</body></html>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -