Home > Database > Mysql Tutorial > Oracle SQL细节总结之constraint约束

Oracle SQL细节总结之constraint约束

WBOY
Release: 2016-06-07 17:20:43
Original
1268 people have browsed it

在表创建时,指定约束条件,也可在创建表后,再添加修改;约束条件分为列级或者表级;约束延期是一个对主键、唯一键等约束进行延

1)约束constraints:在表上强加的约束条件,以保证数据的完整性;防止关联表的数据丢失;
常见的约束类型:
        - NOT NULL
        - UNIQUE
        - PRIMARY KEY
        - FOREIGE KEY
        - CHECK

在表创建时,指定约束条件,,也可在创建表后,再添加修改;约束条件分为列级或者表级;
REATE TABLE [schema.]table
        (column datatype [DEFAULT expr]
        [column_constraint],
        ...
        [table_constraint][,...]);

2)添加约束
ALTER TABLE table
              ADD [CONSTRAINT constraint] type (column);
3)删除约束
ALTER TABLE table_name DROP CONSTRAINT constraint_name

4)重名约束
alter table table_name rename constraint old_name to new_name

5)约束有效/无效
alter table table_name enable/disable constraint constraint_name;
约束延期是一个对主键、唯一键等约束进行延迟检查的功能。可以使用该功能将约束检查延迟到事务提交时,而不是在进行DDL操作之后立即进行检查。所以对约束检查也有这两个选项:延迟和立即;
SQL> create table aaa
  2  (
  3   c1 number constraint aaa_pk primary key deferrable initially immediate,
  4   c2 number constraint c2_ck check(c2>10) deferrable initially immediate
  5  );

Table created.

SQL> set constraints c2_ck deferred;

Constraint set.

SQL> insert into aaa value (1,1);
insert into aaa value (1,1)
                       *
ERROR at line 1:
ORA-00928: missing SELECT keyword


SQL> insert into aaa values (1,1);

1 row created.

SQL> commit;
commit
*
ERROR at line 1:
ORA-02091: transaction rolled back
ORA-02290: check constraint (SCOTT.C2_CK) violated

提交时就会约束检查,即延迟检查;


6)查看约束
user_constraints
user_cons_columns

linux

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template