﻿<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/"><channel><title>BlogJava-3278 7840-随笔分类-SQL</title><link>http://www.blogjava.net/oracle/category/42225.html</link><description>1100111010001110 1001010010001010</description><language>zh-cn</language><lastBuildDate>Sat, 17 Oct 2009 15:41:14 GMT</lastBuildDate><pubDate>Sat, 17 Oct 2009 15:41:14 GMT</pubDate><ttl>60</ttl><item><title>数据库范式实例</title><link>http://www.blogjava.net/oracle/archive/2009/10/17/298711.html</link><dc:creator>逍遥晨空</dc:creator><author>逍遥晨空</author><pubDate>Sat, 17 Oct 2009 14:45:00 GMT</pubDate><guid>http://www.blogjava.net/oracle/archive/2009/10/17/298711.html</guid><wfw:comment>http://www.blogjava.net/oracle/comments/298711.html</wfw:comment><comments>http://www.blogjava.net/oracle/archive/2009/10/17/298711.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/oracle/comments/commentRss/298711.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/oracle/services/trackbacks/298711.html</trackback:ping><description><![CDATA[<font color="#000000"><strong>第一范式<br />
</strong>定义：如果关系R 中所有属性的值域都是单纯域，那么关系模式R是第一范式的<br />
那么符合第一模式的特点就有<br />
1)有主关键字<br />
2)主键不能为空，<br />
3)主键不能重复,<br />
4)字段不可以再分<br />
例如：<br />
<strong>StudyNo | </strong>Name | Sex | Contact<br />
20040901 &nbsp;&nbsp; john &nbsp;&nbsp; &nbsp;&nbsp; Male &nbsp;&nbsp; Email:kkkk@ee.net,phone:222456<br />
20040901 &nbsp;&nbsp; mary &nbsp;&nbsp; &nbsp;&nbsp; famale email:kkk@fff.net phone:123455<br />
以上的表就不符合，第一范式：主键重复(实际中数据库不允许重复的)，而且Contact字段可以再分<br />
所以变更为正确的是<br />
<strong>StudyNo | </strong>Name | Sex | &nbsp;&nbsp; Email &nbsp;&nbsp; &nbsp;&nbsp; | &nbsp;&nbsp; Phone<br />
20040901 &nbsp;&nbsp; &nbsp;&nbsp; john &nbsp;&nbsp; &nbsp;&nbsp; Male &nbsp;&nbsp; </font><a href="&#109;&#97;&#105;&#108;&#116;&#111;&#58;&#107;&#107;&#107;&#107;&#64;&#101;&#101;&#46;&#110;&#101;&#116;"><font color="#000000">kkkk@ee.net</font></a><font color="#000000">&nbsp;&nbsp; 222456<br />
20040902 &nbsp;&nbsp;&nbsp; mary &nbsp;&nbsp; &nbsp;&nbsp; famale </font><a href="&#109;&#97;&#105;&#108;&#116;&#111;&#58;&#107;&#107;&#107;&#64;&#102;&#102;&#102;&#46;&#110;&#101;&#116;"><font color="#000000">kkk@fff.net</font></a><font color="#000000"> 123455<br />
<br />
<strong>第二范式：</strong><br />
定义：如果关系模式R是第一范式的，而且关系中每一个非主属性不部分依赖于主键，称R是第二范式的。<br />
所以第二范式的主要任务就是<br />
满足第一范式的前提下，消除部分函数依赖。<br />
<strong>StudyNo | </strong>Name | Sex | &nbsp;&nbsp; &nbsp;&nbsp; Email &nbsp;&nbsp; &nbsp;&nbsp; | &nbsp;&nbsp; Phone | <strong>ClassNo</strong>&nbsp;&nbsp; | ClassAddress<br />
01 &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; john &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; Male &nbsp;&nbsp; </font><a href="&#109;&#97;&#105;&#108;&#116;&#111;&#58;&#107;&#107;&#107;&#107;&#64;&#101;&#101;&#46;&#110;&#101;&#116;"><font color="#000000">kkkk@ee.net</font></a><font color="#000000"> &nbsp;&nbsp;&nbsp; 222456 &nbsp;&nbsp; 200401 &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; A楼2<br />
02&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; mary &nbsp;&nbsp; famale </font><a href="&#109;&#97;&#105;&#108;&#116;&#111;&#58;&#107;&#107;&#107;&#64;&#102;&#102;&#102;&#46;&#110;&#101;&#116;"><font color="#000000">kkk@fff.net</font></a><font color="#000000"> &nbsp;&nbsp; 123455 &nbsp;&nbsp; &nbsp;&nbsp; 200402 &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; A楼3<br />
这个表完全满足于第一范式,<br />
主键由StudyNo和ClassNo组成，这样才能定位到指定行<br />
但是,ClassAddress部分依赖于关键字(ClassNo-〉ClassAddress),<br />
所以要变为两个表<br />
表一<br />
<strong>StudyNo | </strong>Name | Sex | &nbsp;&nbsp; Email &nbsp;&nbsp; &nbsp;&nbsp; | &nbsp;&nbsp; Phone | <em>ClassNo</em> <br />
&nbsp;&nbsp; 01 &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; john &nbsp;&nbsp; &nbsp;&nbsp; Male &nbsp;&nbsp; </font><a href="&#109;&#97;&#105;&#108;&#116;&#111;&#58;&#107;&#107;&#107;&#107;&#64;&#101;&#101;&#46;&#110;&#101;&#116;"><font color="#000000">kkkk@ee.net</font></a><font color="#000000">&nbsp;&nbsp; 222456 200401 &nbsp;&nbsp; <br />
&nbsp;&nbsp; 02 &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; mary &nbsp;&nbsp; &nbsp;&nbsp; famale </font><a href="&#109;&#97;&#105;&#108;&#116;&#111;&#58;&#107;&#107;&#107;&#64;&#102;&#102;&#102;&#46;&#110;&#101;&#116;"><font color="#000000">kkk@fff.net</font></a><font color="#000000"> 123455 &nbsp;&nbsp; 200402 &nbsp;&nbsp;&nbsp;<br />
表二<br />
<strong>ClassNo</strong>&nbsp;&nbsp; | ClassAddress<br />
200401 &nbsp;&nbsp; A楼2<br />
200402 &nbsp;&nbsp; A楼3<br />
<br />
<br />
<font size="4"><strong>第三范式：</strong></font><br />
<font size="4">满足第二范式的前提下，消除传递依赖。</font><br />
例:<br />
<strong>StudyNo | </strong>Name | Sex | &nbsp;&nbsp; Email &nbsp;&nbsp; &nbsp;&nbsp; | &nbsp;&nbsp; bounsLevel | bouns<br />
20040901 &nbsp;&nbsp; john &nbsp;&nbsp; &nbsp;&nbsp; Male &nbsp;&nbsp; </font><a href="&#109;&#97;&#105;&#108;&#116;&#111;&#58;&#107;&#107;&#107;&#107;&#64;&#101;&#101;&#46;&#110;&#101;&#116;"><font color="#000000">kkkk@ee.net</font></a><font color="#000000"> 优秀 &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; $1000<br />
20040902 &nbsp;&nbsp;&nbsp; mary &nbsp;&nbsp; &nbsp;&nbsp; famale </font><a href="&#109;&#97;&#105;&#108;&#116;&#111;&#58;&#107;&#107;&#107;&#64;&#102;&#102;&#102;&#46;&#110;&#101;&#116;"><font color="#000000">kkk@fff.net</font></a><font color="#000000"> &nbsp;&nbsp; 良&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; $600<br />
这个完全满足了第二范式,但是bounsLevel和bouns存在传递依赖<br />
更改为：<br />
<strong>StudyNo | </strong>Name | Sex | &nbsp;&nbsp; Email &nbsp;&nbsp; &nbsp;&nbsp; | &nbsp;&nbsp; <em>bouunsNo</em><br />
20040901 &nbsp;&nbsp; john &nbsp;&nbsp; &nbsp;&nbsp; Male &nbsp;&nbsp; </font><a href="&#109;&#97;&#105;&#108;&#116;&#111;&#58;&#107;&#107;&#107;&#107;&#64;&#101;&#101;&#46;&#110;&#101;&#116;"><font color="#000000">kkkk@ee.net</font></a><font color="#000000">&nbsp;&nbsp;&nbsp; 1<br />
20040902 &nbsp;&nbsp;&nbsp; mary &nbsp;&nbsp; &nbsp;&nbsp; famale </font><a href="&#109;&#97;&#105;&#108;&#116;&#111;&#58;&#107;&#107;&#107;&#64;&#102;&#102;&#102;&#46;&#110;&#101;&#116;"><font color="#000000">kkk@fff.net</font></a><font color="#000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 2<br />
<strong>bounsNo</strong> | bounsLevel | bouns<br />
1 &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; 优秀 &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; $1000<br />
2 &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; 良 &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; $600<br />
这里我比较喜欢用bounsNo作为主键，<br />
基于两个原因<br />
1）不要用字符作为主键。可能有人说：如果我的等级一开始就用数值就代替呢？<br />
2）但是如果等级名称更改了，不叫 1，2 ，3或优、良，这样就可以方便更改，所以我一般优先使用与业务无关的字段作为关键字。<br />
<br />
一般满足前三个范式就可以避免数据冗余。<br />
<br />
<strong>第四范式：</strong><br />
主要任务：满足第三范式的前提下，消除多值依赖<br />
product | <strong>agent</strong> |&nbsp;&nbsp;<strong>factory</strong><br />
Car &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; A1 &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; F1<br />
Bus &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; A1 &nbsp;&nbsp; &nbsp;&nbsp; F2<br />
Car&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; A2 &nbsp;&nbsp; &nbsp;&nbsp; F2<br />
在这里，Car的定位，必须由 agent 和 Factory才能得到（所以主键由agent和factory组成）,可以通过 product依赖了agent和factory两个属性<br />
所以正确的是<br />
表1 &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 表2：<br />
product | agent &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; factory&nbsp;&nbsp; | product<br />
Car &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; A1 &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; F1 &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; Car<br />
Bus &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; A1 &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; F2 &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; Car<br />
Car &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; A2 &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; F2 &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; Bus<br />
<br />
<strong>第五范式：</strong><br />
<strong>定义:</strong> 如果关系模式R中的每一个连接依赖, 都是由R的候选键所蕴含, 称R是第五范式的<br />
看到定义，就知道是要消除连接依赖,并且必须保证数据完整<br />
例子<br />
<strong>A</strong> | <strong>B</strong>&nbsp;&nbsp; | <strong>C</strong><br />
a1 &nbsp;&nbsp; b1 c1<br />
a2 &nbsp;&nbsp; b1 c2<br />
a1 &nbsp;&nbsp; b2 c1<br />
a2 &nbsp;&nbsp; b2 c2<br />
如果要定位到特定行，必须三个属性都为关键字。<br />
所以关系要变为 三个关系，分别是A 和B,B和C ，C和A<br />
如下：<br />
表1 &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; 表2 &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; 表3<br />
A | B &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; B | C &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp; C | A<br />
a1 &nbsp;&nbsp; b1 &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; b1 &nbsp;&nbsp; c1 &nbsp;&nbsp; &nbsp;&nbsp; c1 &nbsp;&nbsp; a1 &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; <br />
a1 &nbsp;&nbsp; b2 &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; b1 &nbsp;&nbsp; c2 &nbsp;&nbsp; &nbsp;&nbsp; c1 &nbsp;&nbsp; a2<br />
</font>
<img src ="http://www.blogjava.net/oracle/aggbug/298711.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/oracle/" target="_blank">逍遥晨空</a> 2009-10-17 22:45 <a href="http://www.blogjava.net/oracle/archive/2009/10/17/298711.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>数据库范式</title><link>http://www.blogjava.net/oracle/archive/2009/10/17/298710.html</link><dc:creator>逍遥晨空</dc:creator><author>逍遥晨空</author><pubDate>Sat, 17 Oct 2009 14:42:00 GMT</pubDate><guid>http://www.blogjava.net/oracle/archive/2009/10/17/298710.html</guid><wfw:comment>http://www.blogjava.net/oracle/comments/298710.html</wfw:comment><comments>http://www.blogjava.net/oracle/archive/2009/10/17/298710.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/oracle/comments/commentRss/298710.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/oracle/services/trackbacks/298710.html</trackback:ping><description><![CDATA[数据库范式是数据库设计中必不可少的知识，没有对范式的理解，就无法设计出高效率、优雅的数据库。甚至设计出错误的数据库。而想要理解并掌握范式却并不是那 么容易。教科书中一般以关系代数的方法来解释数据库范式。这样做虽然能够十分准确的表达数据库范式，但比较抽象，不太直观，不便于理解，更难以记忆。<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />
<strong>一、基础概念</strong><br />
<ul>
    <li><font color="#0000ff">实体：</font>现实世界中客观存在并可以被区别的事物。比如&#8220;一个学生&#8221;、&#8220;一本书&#8221;、&#8220;一门课&#8221;等等。值得强调的是这里所说的&#8220;事物&#8221;不仅仅是看得见摸得着的&#8220;东西&#8221;，它也可以是虚拟的，不如说&#8220;老师与学校的关系&#8221;。
    <li><font color="#0000ff">属性：</font>教科书上解释为：&#8220;实体所具有的某一特性&#8221;，由此可见，属性一开始是个逻辑概念，比如说，&#8220;性别&#8221;是&#8220;人&#8221;的一个属性。在关系数据库中，属性又是个物理概念，属性可以看作是&#8220;表的一列&#8221;。
    <li><font color="#0000ff">元组</font>：表中的一行就是一个元组。
    <li><font color="#0000ff">分量：</font>元组的某个属性值。在一个关系数据库中，它是一个操作原子，即关系数据库在做任何操作的时候，属性是&#8220;不可分的&#8221;。否则就不是关系数据库了。
    <li><font color="#0000ff">码：</font>表中可以唯一确定一个元组的某个属性（或者<font color="#0000ff">属性组</font>），如果这样的码有不止一个，那么大家都叫<font color="#0000ff">候选码，</font>我们从候选码中挑一个出来做老大，它就叫<font color="#0000ff">主码。</font>
    <li><font color="#0000ff">全码：</font>如果一个码包含了所有的属性，这个码就是全码。
    <li><font color="#0000ff">主属性：</font>一个属性只要在任何一个候选码中出现过，这个属性就是主属性。
    <li><font color="#0000ff">非主属性：</font>与上面相反，没有在任何候选码中出现过，这个属性就是非主属性。
    <li><font color="#0000ff">外码：</font>一个属性（或属性组），它不是码，但是它别的表的码，它就是外码。 </li>
</ul>
<ol></ol>
    <br />
    <strong>二、6个范式</strong><br />
    好了，上面已经介绍了我们掌握范式所需要的全部基础概念，下面我们就来讲范式。首先要明白，范式的包含关系。一个数据库设计如果符合第二范式，一定也符合第一范式。如果符合第三范式，一定也符合第二范式&#8230;<br />
    <br />
    <br />
    <font color="#0000ff">第一范式（</font><font color="#0000ff">1NF</font><font color="#0000ff">）：属性不可分。</font><br />
    在前面我们已经介绍了<font color="#0000ff">属性值</font>的概念，我们说，它是&#8220;不可分的&#8221;。而第一范式要求属性也不可分。那么它和属性值不可分有什么区别呢？给一个例子：
    <table cellspacing="2" cellpadding="2" align="center" border="1">
        <tbody>
            <tr>
                <td width="95">name</td>
                <td width="197" colspan="2">tel</td>
                <td width="69">age</td>
            </tr>
            <tr>
                <td width="95">大宝</td>
                <td width="197" colspan="2">13612345678</td>
                <td width="69">22</td>
            </tr>
            <tr>
                <td width="95">小明</td>
                <td width="98">13988776655</td>
                <td width="98">010－1234567</td>
                <td width="69">21</td>
            </tr>
        </tbody>
    </table>
    Ps：这个表中，属性值&#8220;分&#8221;了。
    <table cellspacing="2" cellpadding="2" align="center" border="1">
        <tbody>
            <tr>
                <td width="95" rowspan="2">name</td>
                <td width="197" colspan="2">tel</td>
                <td width="69" rowspan="2">age</td>
            </tr>
            <tr>
                <td width="98">手机</td>
                <td width="98">座机</td>
            </tr>
            <tr>
                <td width="95">大宝</td>
                <td width="98">13612345678</td>
                <td width="98">021－9876543</td>
                <td width="69">22</td>
            </tr>
            <tr>
                <td width="95">小明</td>
                <td width="98">13988776655</td>
                <td width="98">010－1234567</td>
                <td width="69">21</td>
            </tr>
        </tbody>
    </table>
    Ps：这个表中，属性 &#8220;分&#8221;了。<br />
    这两种情况都不满足第一范式。不满足第一范式的数据库，不是关系数据库！所以，我们在任何关系数据库管理系统中，做不出这样的&#8220;表&#8221;来。<br />
    <br />
    <br />
    <br />
    <font color="#0000ff">第二范式（</font><font color="#0000ff">2NF</font><font color="#0000ff">）</font>：符合1NF，并且，<font color="#0000ff">非主属性完全依赖于码。</font><br />
    听起来好像很神秘，其实真的没什么。<br />
    一 个候选码中的主属性也可能是好几个。如果一个主属性，它不能单独做为一个候选码，那么它也不能确定任何一个非主属性。给一个反例：我们考虑一个小学的教务 管理系统，学生上课指定一个老师，一本教材，一个教室，一个时间，大家都上课去吧，没有问题。那么数据库怎么设计？（学生上课表）
    <table cellspacing="2" cellpadding="2" width="541" align="center" border="1">
        <tbody>
            <tr>
                <td width="54"><font color="#0000ff">学生</font></td>
                <td width="113"><font color="#0000ff">课程</font></td>
                <td width="43">老师</td>
                <td width="91">老师职称</td>
                <td width="101">教材</td>
                <td width="49">教室</td>
                <td width="91">上课时间</td>
            </tr>
            <tr>
                <td width="54">小明</td>
                <td width="113">一年级语文（上）</td>
                <td width="43">大宝</td>
                <td width="91">副教授</td>
                <td width="101">《小学语文1》</td>
                <td width="49">101</td>
                <td width="91">14：30</td>
            </tr>
        </tbody>
    </table>
    一个学生上一门课，一定在特定某个教室。所以有（学生，课程）－&gt;教室<br />
    一个学生上一门课，一定是特定某个老师教。所以有（学生，课程）－&gt;老师<br />
    一个学生上一门课，他老师的职称可以确定。所以有（学生，课程）－&gt;老师职称<br />
    一个学生上一门课，一定是特定某个教材。所以有（学生，课程）－&gt;教材<br />
    一个学生上一门课，一定在特定时间。所以有（学生，课程）－&gt;上课时间<br />
    因此（学生，课程）是一个码。<br />
    然而，一个课程，一定指定了某个教材，一年级语文肯定用的是《小学语文1》，那么就有课程－&gt;教材。（学生，课程）是个码，课程却决定了教材，这就叫做不完全依赖，或者说部分依赖。出现这样的情况，就不满足第二范式！<br />
    有什么不好吗？你可以想想：<br />
    1、校长要新增加一门课程叫&#8220;微积分&#8221;，教材是《大学数学》，怎么办？学生还没选课，而学生又是主属性，主属性不能空，课程怎么记录呢，教材记到哪呢? &#8230;&#8230;郁闷了吧?<font color="#0000ff">(</font><font color="#0000ff">插入异常</font><font color="#0000ff">)</font><br />
    2、下学期没学生学一年级语文（上）了，学一年级语文（下）去了，那么表中将不存在一年级语文（上），也就没了《小学语文1》。这时候，校长问：一年级语文（上）用的什么教材啊？&#8230;&#8230;郁闷了吧?<font color="#0000ff">(</font><font color="#0000ff">删除异常</font><font color="#0000ff">)</font><br />
    3、校长说：一年级语文（上）换教材，换成《大学语文》。有10000个学生选了这么课，改动好大啊！改累死了&#8230;&#8230;郁闷了吧？<font color="#0000ff">（修改异常）</font><br />
    那应该怎么解决呢？投影分解，将一个表分解成两个或若干个表
    <table cellspacing="2" cellpadding="2" width="500" align="center" border="1">
        <tbody>
            <tr>
                <td width="74"><font color="#0000ff">学生</font></td>
                <td width="133"><font color="#0000ff">课程</font></td>
                <td width="53">老师</td>
                <td width="73">老师职称</td>
                <td width="54">教室</td>
                <td width="113">上课时间</td>
            </tr>
            <tr>
                <td width="74">小明</td>
                <td width="133">一年级语文（上）</td>
                <td width="53">大宝</td>
                <td width="73">副教授</td>
                <td width="54">101</td>
                <td width="113">14：30</td>
            </tr>
        </tbody>
    </table>
    学生上课表新
    <table cellspacing="2" cellpadding="2" width="319" align="center" border="1">
        <tbody>
            <tr>
                <td width="160"><font color="#0000ff">课程</font></td>
                <td width="160">教材</td>
            </tr>
            <tr>
                <td width="160">一年级语文（上）</td>
                <td width="160">《小学语文1》</td>
            </tr>
        </tbody>
    </table>
    课程的表&nbsp;&nbsp;<font color="#0000ff">第三范式（</font><font color="#0000ff">3NF</font><font color="#0000ff">）：</font>符合2NF，并且，<font color="#0000ff">消除传递依赖</font><br />
    上面的&#8220;学生上课表新&#8221;符合2NF，可以这样验证：两个主属性单独使用，不用确定其它四个非主属性的任何一个。但是它有传递依赖！<br />
    在哪呢？问题就出在&#8220;老师&#8221;和&#8220;老师职称&#8221;这里。一个老师一定能确定一个老师职称。<br />
    有什么问题吗？想想：<br />
    1、老师升级了，变教授了，要改数据库，表中有N条，改了N次&#8230;&#8230;<font color="#0000ff">（修改异常）</font><br />
    2、没人选这个老师的课了，老师的职称也没了记录&#8230;&#8230;<font color="#0000ff">（删除异常）</font><br />
    3、新来一个老师，还没分配教什么课，他的职称记到哪？&#8230;&#8230;<font color="#0000ff">（插入异常）</font><br />
    那应该怎么解决呢？和上面一样，投影分解：
    <table cellspacing="2" cellpadding="2" width="428" align="center" border="1">
        <tbody>
            <tr>
                <td width="74"><font color="#0000ff">学生</font></td>
                <td width="133"><font color="#0000ff">课程</font></td>
                <td width="53">老师</td>
                <td width="54">教室</td>
                <td width="113">上课时间</td>
            </tr>
            <tr>
                <td width="74">小明</td>
                <td width="133">一年级语文（上）</td>
                <td width="53">大宝</td>
                <td width="54">101</td>
                <td width="113">14：30</td>
            </tr>
        </tbody>
    </table>
    <table cellspacing="2" cellpadding="2" width="251" align="center" border="1">
        <tbody>
            <tr>
                <td width="106"><font color="#0000ff">老师</font></td>
                <td width="144">老师职称</td>
            </tr>
            <tr>
                <td width="106">大宝</td>
                <td width="144">副教授</td>
            </tr>
        </tbody>
    </table>
    <br />
    <br />
    <font color="#0000ff">BC</font><font color="#0000ff">范式（</font><font color="#0000ff">BCNF</font><font color="#0000ff">）：</font>符合3NF，并且，<font color="#0000ff">主属性不依赖于主属性</font><br />
    若关系模式属于第一范式，且每个属性都不传递依赖于键码，则R属于BC范式。 <br />
    <br />
    通常<br />
    BC范式的条件有多种等价的表述：每个非平凡依赖的左边必须包含键码；每个决定因素必须包含键码。 <br />
    <br />
    BC范式既检查非主属性，又检查主属性。当只检查非主属性时，就成了第三范式。满足BC范式的关系都必然满足第三范式。 <br />
    还可以这么说：<font color="#0000ff">若一个关系达到了第三范式，并且它只有一个候选码，或者它的每个候选码都是单属性，则该关系自然达到</font><font color="#0000ff">BC</font><font color="#0000ff">范式。</font><br />
    <br />
    一般，一个数据库设计符合3NF或BCNF就可以了。在BC范式以上还有第四范式、第五范式。<br />
    <br />
    <font color="#0000ff">第四范式：</font>要求把同一表内的多对多关系删除。<br />
    <br />
    <font color="#0000ff">第五范式：</font>从最终结构重新建立原始结构。<br />
<img src ="http://www.blogjava.net/oracle/aggbug/298710.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/oracle/" target="_blank">逍遥晨空</a> 2009-10-17 22:42 <a href="http://www.blogjava.net/oracle/archive/2009/10/17/298710.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>