--查询约束
select dbms_metadata.get_ddl('TABLE','EMP') from dual;
-- 建表的约束,与在第一列写上primary key 一样,约束可以在后面定义,
--在结尾统一定义的约束,都有别名,不是在尾部定义的约束,系统会自动取别名;
create table student (
stu_id number(10), --primary key ,与结尾第一个约束意义相同;
stu_name varchar2(20), --unique, 与结尾第二个约束意义相同
stu_sex varchar2(2) default '男', --check(stu_sex in('男','女')), 与结尾第三个约束意义相同:
--在表定义的结尾写明约束条件;
constraint student_id_pk primary key(stu_id),
constraint student_name_uk unique(stu_name),
constraint student_sex_ck check(stu_sex in('男','女'))
);
--主外键关联(可以保证两张表的内容一致性);
--将上面的学生表的 id 与book 表中的 id 建立关联;
--注意:与子表中的外键对应的必须是主表中的主键;
-- 删除的时候,必须先删除子表,再删除主表中的内容,
-- 或者使用 cascade 级联删除;
create table book(
b_id number(10) primary key,
b_name varchar2(50),
stu_id number(10),
constraint student_book_sid_fk foreign key(stu_id) references student(stu_id)
--若想要提供允许级联删除的操作,则加上 on delete cascade;
on delete cascade
);
-- 主外键的删除,要考虑级联删除的问题;
--若有主外键的关联,则不能单独删除主表的内容,
--要想删除主表的内容,则要考虑级联删除的操作;
delete from student where stu_id=0825121016 on delete cascade;
--修改约束;
--语法:( 增加约束 )alter table 表名 add constraint 约束名 约束类型(约束字段)
alter table book add constraint book_name_uk unique(b_name);
--语法:( 删除约束 )alter table 表名 drop constraint 约束名;
alter table book drop constraint book_name_uk;
--创建视图 ,
create or replace view myemp as
select * from emp where deptno=20;
--限制视图不能更改判断的条件,则使用 with check option ;
create or replace view myemp as
select * from emp where deptno=20 with check option;
--限制只能进行查询操作,不能更改视图内容 则使用 with read only ;
create or replace view myemp as
select * from emp where deptno=20 with read only;
来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/24465008/viewspace-692332/,如需转载,请注明出处,否则将追究法律责任。
转载于:http://blog.itpub.net/24465008/viewspace-692332/