oracle数据库的完整性约束规则详解

<h1>
    <span class="link_title"><a href="/haiross/article/details/11772667">
    &#65279;&#65279;&#65279;&#65279;oracle数据库的完整性约束规则详解            
    </a></span>
</h1>
    <div class="article_manage clearfix">
    <div class="article_r">
        <span class="link_postdate">2013-09-17 15:07</span>
        <span class="link_view" title="阅读次数">842人阅读</span>
        <span class="link_comments" title="评论次数"> <a href="#comments" onclick="_gaq.push(['_trackEvent','function', 'onclick', 'blog_articles_pinglun'])">评论</a>(0)</span>
        <span class="link_collect tracking-ad" data-mod="popu_171"> <a href="javascript:void(0);" onclick="javascript:collectArticle('%ef%bb%bf%ef%bb%bf%ef%bb%bf%ef%bb%bforacle%e6%95%b0%e6%8d%ae%e5%ba%93%e7%9a%84%e5%ae%8c%e6%95%b4%e6%80%a7%e7%ba%a6%e6%9d%9f%e8%a7%84%e5%88%99%e8%af%a6%e8%a7%a3','11772667');return false;" title="收藏">收藏</a></span>
         <span class="link_report"> <a href="#report" onclick="javascript:report(11772667,2);return false;" title="举报">举报</a></span>

    </div>
</div>
<div class="embody" style="display:none" id="embody">
    <span class="embody_t">本文章已收录于:</span>
    <div class="embody_c" id="lib" value="{&quot;err&quot;:0,&quot;msg&quot;:&quot;ok&quot;,&quot;data&quot;:[]}"></div>
</div>
<style type="text/css">        
        .embody{
            padding:10px 10px 10px;
            margin:0 -20px;
            border-bottom:solid 1px #ededed;                
        }
        .embody_b{
            margin:0 ;
            padding:10px 0;
        }
        .embody .embody_t,.embody .embody_c{
            display: inline-block;
            margin-right:10px;
        }
        .embody_t{
            font-size: 12px;
            color:#999;
        }
        .embody_c{
            font-size: 12px;
        }
        .embody_c img,.embody_c em{
            display: inline-block;
            vertical-align: middle;               
        }
         .embody_c img{               
            width:30px;
            height:30px;
        }
        .embody_c em{
            margin: 0 20px 0 10px;
            color:#333;
            font-style: normal;
        }
</style>
<script type="text/javascript">
    $(function () {
        try
        {
            var lib = eval("("+$("#lib").attr("value")+")");
            var html = "";
            if (lib.err == 0) {
                $.each(lib.data, function (i) {
                    var obj = lib.data[i];
                    //html += '<img src="' + obj.logo + '"/>' + obj.name + "&nbsp;&nbsp;";
                    html += ' <a href="' + obj.url + '" target="_blank">';
                    html += ' <img src="' + obj.logo + '">';
                    html += ' <em><b>' + obj.name + '</b></em>';
                    html += ' </a>';
                });
                if (html != "") {
                    setTimeout(function () {
                        $("#lib").html(html);                      
                        $("#embody").show();
                    }, 100);
                }
            }      
        } catch (err)
        { }
        
    });
</script>
  <div class="category clearfix">
    <div class="category_l">
       <img src="http://static.blog.csdn.net/images/category_icon.jpg">
        <span>分类:</span>
    </div>
    <div class="category_r">
                <label onclick="GetCategoryArticles('1600815','haiross','top','11772667');">
                    <span onclick="_gaq.push(['_trackEvent','function', 'onclick', 'blog_articles_fenlei']);">oracle开发SQL语句<em>(273)</em></span>
                  <img class="arrow-down" src="http://static.blog.csdn.net/images/arrow_triangle _down.jpg" style="display:inline;">
                  <img class="arrow-up" src="http://static.blog.csdn.net/images/arrow_triangle_up.jpg" style="display:none;">
                    <div class="subItem">
                        <div class="subItem_t"><a href="http://blog.csdn.net/haiross/article/category/1600815" target="_blank">作者同类文章</a><i class="J_close">X</i></div>
                        <ul class="subItem_l" id="top_1600815">                            
                        </ul>
                    </div>
                </label>                    
    </div>
</div>
<script type="text/javascript" src="http://static.blog.csdn.net/scripts/category.js"></script>  

oracle数据库的完整性约束规则详解

完整性约束用于增强数据的完整性,Oracle提供了5种完整性约束: 

    Check
    NOT NULL
    Unique
    Primary
    Foreign key
完整性约束是一种规则,不占用任何数据库空间。完整性约束存在数据字典中,在执行SQL或PL/SQL期间使用。用户可以指明约束是启用的还是禁用的,当约束启用时,他增强了数据的完整性,否则,则反之,但约束始终存在于数据字典中。禁用约束,使用ALTER语句: 

ALTER TABLE table_name DISABLE CONSTRAINT constraint_name;
 

ALTER TABLE policies DISABLE CONSTRAINT chk_gender
如果要重新启用约束: 

ALTER TABLE policies ENABLE CONSTRAINT chk_gender
删除约束: 

ALTER TABLE table_name DROP CONSTRAINT constraint_name
 

ALTER TABLE policies DROP CONSTRAINT chk_gender;
Check 约束 
在数据列上Check 约束需要一个特殊的布尔条件或者将数据列设置成TRUE,至少一个数据列的值是NULL,Check约束用于增强表中数据内容的简单的商业规则。用户使用 Check约束保证数据规则的一致性。Check约束可以涉及该行同属Check约束的其他数据列但不能涉及其他行或其他表,或调用函数SYSDATE, UID,USER,USERENV。如果用户的商业规则需要这类的数据检查,那么可以使用触发器。Check约束不保护LOB数据类型的数据列和对象、嵌套表、VARRY、ref等。单一数据列可以有多个Check约束保护,一个Check约束可以保护多个数据列。创建表的Check约束使用CREATE TABLE语句,更改表的约束使用ALTER TABLE语句。 
语法: 

CONSTRAINT [constraint_name] CHECK (condition);
Check约束可以被创建或增加为一个表约束,当Check约束保护多个数据列时,必须使用表约束语法。约束名是可选的并且如果这个名字不存在,那么oracle将产生一个以SYS_开始的唯一的名字。例: 

CREATE TABLE policies
(policy_id NUMBER,
holder_name VARCHAR2(40),
gender VARCHAR2(1) constraint chk_gender CHECK (gender in ('M','F'),
marital_status VARCHAR2(1),
date_of_birth DATE,
constraint chk_marital CHECK (marital_status in('S','M','D','W'))
);
NOT NULL约束 
NOT NULL约束应用在单一的数据列上,并且他保护的数据列必须要有数据值。缺省状况下,ORACLE允许任何列都可以有NULL值。某些商业规则要求某数据列必须要有值,NOT NULL约束将确保该列的所有数据行都有值。例: 

CREATE TABLE policies
(policy_id NUMBER,
holder_name VARCHAR2(40) NOT NULL,
gender VARCHAR2(1),
marital_status VARCHAR2(1),
date_of_birth DATE NOT NULL
);
对于NOT NULL的ALTER TABLE语句与其他约束稍微有点不同。 

ALTER TABLE policies MODIFY holder_name NOT NULL
唯一性约束(Unique constraint) 
唯一性约束可以保护表中多个数据列,保证在保护的数据列中任何两行的数据都不相同。唯一性约束与表一起创建,在唯一性约束创建后,可以使用ALTER TABLE语句修改。语法: 

column_name data_type CONSTRAINT constraint_name UNIQUE
如果唯一性约束保护多个数据列,那么唯一性约束要作为表约束增加。语法如下: 

CONSTRAINT constraint_name (column) 
UNIQUE USING INDEX TABLESPACE 
(tablespace_name) STORAGE (stored clause)
    <div id="digg" articleid="11772667">
        <dl id="btnDigg" class="digg digg_disable" onclick="btndigga();">
           
             <dt>顶</dt>
            <dd>0</dd>
        </dl>
       
          
        <dl id="btnBury" class="digg digg_disable" onclick="btnburya();">
          
              <dt>踩</dt>
            <dd>0</dd>               
        </dl>
        
    </div>
 <div class="tracking-ad" data-mod="popu_222"><a href="javascript:void(0);">&nbsp;</a>   </div>
<div class="tracking-ad" data-mod="popu_223"> <a href="javascript:void(0);">&nbsp;</a></div>
<script type="text/javascript">
            function btndigga() {
                $(".tracking-ad[data-mod='popu_222'] a").click();
            }
            function btnburya() {
                $(".tracking-ad[data-mod='popu_223'] a").click();
            }
        </script>
<div style="clear:both; height:10px;"></div>


    <div class="similar_article" style="">
            <h4>我的同类文章</h4>
            <div class="similar_c" style="margin:20px 0px 0px 0px">
                <div class="similar_c_t">
                            <label class="similar_cur">
                                <span style="cursor:pointer" onclick="GetCategoryArticles('1600815','haiross','foot','11772667');">oracle开发SQL语句<em>(273)</em></span>
                            </label>
                </div>
               
                <div class="similar_wrap tracking-ad" data-mod="popu_141" style="max-height:195px;">
                    <a href="http://blog.csdn.net" style="display:none">http://blog.csdn.net</a>
                    <ul class="similar_list fl"><li><em>•</em><a href="http://blog.csdn.net/haiross/article/details/52703419" id="foot_aritcle_52703419undefined5920718691798312" target="_blank" title="ORACLE关于锁和V$LOCK的分析">ORACLE关于锁和V$LOCK的分析</a><span>2016-09-29</span><label><i>阅读</i><b>340</b></label></li> <li><em>•</em><a href="http://blog.csdn.net/haiross/article/details/51718854" id="foot_aritcle_51718854undefined17365905042862329" target="_blank" title="Oracle级联操作详解">Oracle级联操作详解</a><span>2016-06-20</span><label><i>阅读</i><b>659</b></label></li> <li><em>•</em><a href="http://blog.csdn.net/haiross/article/details/51579198" id="foot_aritcle_51579198undefined5330477749926683" target="_blank" title="execute、executeUpdate、executeQuery三者的区别(及返回值)">execute、executeUpdate、executeQuery三者的区别(及返回值)</a><span>2016-06-03</span><label><i>阅读</i><b>692</b></label></li> <li><em>•</em><a href="http://blog.csdn.net/haiross/article/details/51003405" id="foot_aritcle_51003405undefined9644343531090429" target="_blank" title="oracle :time_t 转换timestamp">oracle :time_t 转换timestamp</a><span>2016-03-29</span><label><i>阅读</i><b>226</b></label></li> <li><em>•</em><a href="http://blog.csdn.net/haiross/article/details/50456134" id="foot_aritcle_50456134undefined9406513311769233" target="_blank" title="MySQL模拟Oracle的Sequence两种方法">MySQL模拟Oracle的Sequence两种方法</a><span>2016-01-04</span><label><i>阅读</i><b>2246</b></label></li> <li><em>•</em><a href="http://blog.csdn.net/haiross/article/details/50456069" id="foot_aritcle_50456069undefined6367427497429241" target="_blank" title="MySQL 序列使用AUTO_INCREMENT">MySQL 序列使用AUTO_INCREMENT</a><span>2016-01-04</span><label><i>阅读</i><b>176</b></label></li> </ul>

                    <ul class="similar_list fr"><li><em>•</em><a href="http://blog.csdn.net/haiross/article/details/52596119" id="foot_aritcle_52596119undefined1992347617741137" target="_blank" title="外键列上是否需要索引">外键列上是否需要索引</a><span>2016-09-20</span><label><i>阅读</i><b>125</b></label></li> <li><em>•</em><a href="http://blog.csdn.net/haiross/article/details/51579943" id="foot_aritcle_51579943undefined032563079543873386" target="_blank" title="SQL异常">SQL异常</a><span>2016-06-03</span><label><i>阅读</i><b>143</b></label></li> <li><em>•</em><a href="http://blog.csdn.net/haiross/article/details/51003696" id="foot_aritcle_51003696undefined3918251031155908" target="_blank" title="Oracle中的TIMESTAMP类型解读(TIMESTAMP存储格式)">Oracle中的TIMESTAMP类型解读(TIMESTAMP存储格式)</a><span>2016-03-29</span><label><i>阅读</i><b>1417</b></label></li> <li><em>•</em><a href="http://blog.csdn.net/haiross/article/details/50946302" id="foot_aritcle_50946302undefined374999555736963" target="_blank" title="oracle 查看表被哪个使用(oracle 查看表被锁)百度">oracle 查看表被哪个使用(oracle 查看表被锁)百度</a><span>2016-03-21</span><label><i>阅读</i><b>910</b></label></li> <li><em>•</em><a href="http://blog.csdn.net/haiross/article/details/50456112" id="foot_aritcle_50456112undefined30455331382106365" target="_blank" title="MYSQL获取自增ID的四种方法">MYSQL获取自增ID的四种方法</a><span>2016-01-04</span><label><i>阅读</i><b>2143</b></label></li> </ul>
                <a href="http://blog.csdn.net/haiross/article/category/1600815" class="MoreArticle">更多文章</a></div>
            </div>
        </div>    
<script type="text/javascript">
    $(function () {
        GetCategoryArticles('1600815', 'haiross','foot','11772667');
    });
</script>
<div>
        <ins data-revive-zoneid="205" data-revive-id="8c38e720de1c90a6f6ff52f3f89c4d57"></ins> 
 </div>

参考知识库

img

Oracle知识库

img

MySQL知识库

 <dt><span>猜你在找</span></dt>    





<div id="adCollege" style="width: 42%;float: left;"> 
    <script src="http://csdnimg.cn/jobreco/job_reco.js" type="text/javascript"></script> 
    <script type="text/javascript">
        csdn.position.showEdu({
            sourceType: "blog",
            searchType: "detail",
            searchKey: "11772667",
            username: "",
            recordcount: "5",
            containerId: "adCollege" //容器DIV的id。 
        });
    </script> 
<div class="tracking-ad" data-mod="popu_84"><dd style="background:url(http://static.blog.csdn.net/skin/default/images/blog-dot-red3.gif) no-repeat 0 10px; white-space: nowrap;"><a href="http://edu.csdn.net/course/detail/3137" title="Oracle数据库开发之PL/SQL基础实战视频课程" strategy="v4:content" target="_blank">Oracle数据库开发之PL/SQL基础实战视频课程</a></dd><dd style="background:url(http://static.blog.csdn.net/skin/default/images/blog-dot-red3.gif) no-repeat 0 10px; white-space: nowrap;"><a href="http://edu.csdn.net/course/detail/3574" title="Oracle数据库" strategy="v4:content" target="_blank">Oracle数据库</a></dd><dd style="background:url(http://static.blog.csdn.net/skin/default/images/blog-dot-red3.gif) no-repeat 0 10px; white-space: nowrap;"><a href="http://edu.csdn.net/course/detail/458" title="Oracle数据库从入门到精通" strategy="v4:content" target="_blank">Oracle数据库从入门到精通</a></dd><dd style="background:url(http://static.blog.csdn.net/skin/default/images/blog-dot-red3.gif) no-repeat 0 10px; white-space: nowrap;"><a href="http://edu.csdn.net/course/detail/3984" title="从零开始学习Oracle数据库" strategy="v4:content" target="_blank">从零开始学习Oracle数据库</a></dd><dd style="background:url(http://static.blog.csdn.net/skin/default/images/blog-dot-red3.gif) no-repeat 0 10px; white-space: nowrap;"><a href="http://edu.csdn.net/course/detail/3739" title="Oracle数据库基础入门培训课程视频" strategy="v4:content" target="_blank">Oracle数据库基础入门培训课程视频</a></dd></div></div>  


 <div id="res" data-mod="popu_36" class="tracking-ad" style="width: 42%; float: left; margin-right: 30px; display: block;"><dd style="background:url(http://static.blog.csdn.net/skin/default/images/blog-dot-red3.gif) no-repeat 0 10px;"><a href="http://blog.csdn.net/chatliang/article/details/4059994" title="&#65279;USB控制传输在EZ-USB68013总结" strategy="SearchAlgorithm">&#65279;USB控制传输在EZ-USB68013总结</a></dd><dd style="background:url(http://static.blog.csdn.net/skin/default/images/blog-dot-red3.gif) no-repeat 0 10px;"><a href="http://blog.csdn.net/charlv/article/details/6794585" title="&#65279;Linux命令行学习笔记" strategy="SearchAlgorithm">&#65279;Linux命令行学习笔记</a></dd><dd style="background:url(http://static.blog.csdn.net/skin/default/images/blog-dot-red3.gif) no-repeat 0 10px;"><a href="http://blog.csdn.net/SunCherryDream/article/details/8269641" title="&#65279;C#里WinForm开发中如何实现控件随窗体大小的改变而自动适应其改变" strategy="SearchAlgorithm">&#65279;C#里WinForm开发中如何实现控件随窗体大小的改变而自动适应其改变</a></dd><dd style="background:url(http://static.blog.csdn.net/skin/default/images/blog-dot-red3.gif) no-repeat 0 10px;"><a href="http://blog.csdn.net/wgembed/article/details/21184367" title="ubuntu shell脚本line 1 &#65279;#binbash No such file or directory" strategy="SearchAlgorithm">ubuntu shell脚本line 1 &#65279;#binbash No such file or directory</a></dd><dd style="background:url(http://static.blog.csdn.net/skin/default/images/blog-dot-red3.gif) no-repeat 0 10px;"><a href="http://blog.csdn.net/Ocean2006/article/details/6995393" title="&#65279;OpenGL学习四帧缓存" strategy="SearchAlgorithm">&#65279;OpenGL学习四帧缓存</a></dd></div>
<div id="ad_cen">        

<!-- 广告位开始 -->
<ins data-revive-zoneid="72" data-revive-id="8c38e720de1c90a6f6ff52f3f89c4d57"></ins>
<!-- 广告位结束 -->
查看评论

  暂无评论

* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
<div id="ad_bot">
</div>
    <a id="quick-reply" class="btn btn-top q-reply" title="快速回复" style="display:none;">
        <img src="http://static.blog.csdn.net/images/blog-icon-reply.png" alt="快速回复">
    </a>    
<a id="d-top-a" class="btn btn-top backtop" style="display: none;" title="返回顶部" onclick="_gaq.push(['_trackEvent','function', 'onclick', 'blog_articles_huidaodingbu'])">         
     <img src="http://static.blog.csdn.net/images/top.png" alt="TOP">
</a>

                    <div class="clear">
                    </div>
                </div>                   
            
        </div>
posted @ 2017-03-07 21:53  jobs-lgy  阅读(762)  评论(0编辑  收藏  举报