Oracle的insert插入语句的功能很是强大,我们可以实现在插入的过程中仅允许插入指定的数据记录,功能展示在此,供参考。1.环境准备1)创建T表sec@ora10g> create table t (x number, y number);Table created.2)初始化两条数据,用于后续插入语句比对sec@ora10g> insert into t values (1,null);1 row created.sec@ora10g> insert into t values (2,2000);1 row created.sec@ora10g> select * from t;X Y---------- ----------12 20002.尝试使用带限制条件的插入语句1)以下两条SQL插入语句是符合条件的例子这里我们使用的是“with check option”选项限制插入T表时Y列值只允许是3000或4000。sec@ora10g> insert into (select * from t where y in (3000,4000) with check option) values(3,3000);1 row created.sec@ora10g> insert into (select * from t where y in (3000,4000) with check option) values(4,4000);1 row created.sec@ora10g> select * from t;X Y---------- ----------12 20003 30004 4000上面两条数据符合插入条件,插入成功。2)尝试插入不符合条件的数据sec@ora10g> insert into (select * from t where y in (3000,4000) with check option) values(5,5000);insert into (select * from t where y in (3000,4000) with check option) values(5,5000)*ERROR at line 1:ORA-01402: view WITH CHECK OPTION where-clause violationsec@ora10g> select * from t;X Y---------- ----------12 20003 30004 4000显然,在这种约束条件下,我们是无法插入Y值不等于3000和4000的数据的。3)去掉“with check option”选项再次尝试数据插入sec@ora10g> insert into (select * from t where y in (3000,4000)) values(5,5000);1 row created.sec@ora10g> select * from t;X Y---------- ----------12 20003 30004 40005 5000此时插入数据的约束已取消。数据可以成功插入到T表中。3.小结本文给出了一种插入数据的特殊用法,目的是提醒大家,我们在使用常规插入技术的同时还有很多新奇的方法值得探索。Good luck.secooler11.09.05-- The End --
oracle中insert语句加条件,【INSERT】在INSERT插入语句中引入条件限制选项实现数据插入控制...
最新推荐文章于 2024-01-09 13:00:00 发布