• CONSTRAINT的用法举例


    当我们在建表时,我们会考虑的表的一些约束,下面简述几种约束的用法:
    SQL> select * from v$version where rownum<2;


    BANNER
    --------------------------------------------------------------------------------
    Oracle Database 11g Enterprise Edition Release 11.2.0.1.0 - Production
    primary key:
    SQL> create table diy_os(id number,name varchar2(10));
    表已创建。
    SQL> alter table diy_os add constraint pk0 primary key(id);
    表已更改。
    SQL> show user;
    USER 为 "HR"
    SQL> select owner,constraint_name,table_name from user_constraints where constraint_name='PK0';
    OWNER
    ----------------------------------------------------------------------------------------------------
    CONSTRAINT_NAME                TABLE_NAME
    ------------------------------ ------------------------------
    HR
    PK0                            DIY_OS

    foreign key:
    我在另一个用户wang上创建表wang(f_id number,f_name varchar2(10)).
    在hr用户下:
    SQL>grant reference on wang to hr;
    在wang用户下:
    SQL> alter table wang  add constraint fk7 foreign key(id) references hr.diy_os(id);
    unique:
    alter table diy_os  add constraint unq_1 unique (id);

    check :
    alter table t add constraint chk_1 check (id in(1,2,3)); ==》id后面跟的是表达式

    not null:(这个约束是列级约束,和上面的不同,上面的是表级)
    alter table diy_os  add constraint not_null check(id is not null);

    read only:(view)基于视图的约束
    create or replace view v as select * from test.id with read only;

    check option:(view)基于视图的约束
    create or replace view v as select * from diy_os where id<100 with check option constraint check_1; 
  • 相关阅读:
    Jmeter_Beanshell_使用Java处理JSON块
    BeanShell Processor_使用Java处理脚本
    MySQL_explain关键字分析查询语句
    LoadRunner11_录制脚本时的浏览器版本
    Jmeter_实现Excel文件导出到本地
    Jmeter_录制HTTPS
    性能测试常用sql语句
    LoadRunner11_MySQL数据库脚本
    LoadRunner11_录制Oracle数据库脚本
    实现liunx之间无密码访问——ssh密匙
  • 原文地址:https://www.cnblogs.com/diy-os/p/5083366.html
Copyright © 2020-2023  润新知