Oracle的表约束

来源:互联网 发布:重庆少儿编程 编辑:程序博客网 时间:2024/06/06 02:07

约束是操作表数据的强制规定

有五种约束:not null,unique,primary key,foreign key,check。

在什么时候创建约束:建表的同时,建表之后

可以在表级和列极定义约束

可以通过数据字典视图查看约束


举例:create table employee(

employee_id number(6),

first_name varchar2(20),

...

email varchar2(25),

salary number(8,2),

job_id varchar2(10) not null,

hire_date date not null,

constraint emp_emp_id_pk primary key(employee_id));

constraint emp_email_uk unique(email));


添加约束的语法:

使用alter table语句:

添加或删除约束,但是不能修改约束

有效化或无效化约束

添加not null约束要使用modify

alter table employee add constraint emp_manager_fk foreign key(manager_id) references employee(employee_id);


删除约束

alter table employee drop constraint emp_manager_fk;

alter table department drop primary key cascade;


无效化约束



静态数据字典:

User_*:当前用户拥有的对象信息;

All_*:当前用户能访问的对象信息;

Dba_*:数据库中所有的对象信息;


常用的静态数据字典

*_users,*_tables,*_objects。

例: select * from user_user --当前用户信息

select * from user_tables --当前用户表信息

select * from user_objects --当前用户的对象信息


常用的动态数据字典

数据库运行,不断更新,提供关于内存,磁盘运行的情况,只能读取不能修改。
以V$开头
V$session:当前会话信息。
V$sql:当前查询过的sql语句,访问过的资源与对象。
V$tablespace 表空间信息

常用约束

查询数据字典视图 user_constraints

select constraint_name,constraint_type,serarch_condition from user_constraints where table_name='EMPLOYEES';

查询数据字典视图 user_cons_columns

select constraint_name,column_name from user_cons_columns where table_name='EMPLOYEES';