Oracle Supplements Series (1)

 

Oracle Supplements Series (1)

 

In view of the difficulty of content supervision, the lack of profit model, and the huge cost pressure, the large and small "network disks and cloud disks" on the market are either transformed or shut down. For this reason, I already had two 1T hard drives lying in my shopping cart on Double Eleven (just waited for 12 o’clock to grab it), and Evernote also paid to upgrade to an advanced user, so I almost didn’t build its own cloud service. Some network services, especially free services, such as the previous google reader, yahoo mail, and Baidu space... all stop when they say it is stopped, so that you can be caught off guard. Today, I read a lot of articles posted in Baidu space before. Fortunately, there are such elegant products as "WeChat", so I just moved an old article to mark it.


1. About add_months of oracle

Let's take a look at the results of the following select statements:

select add_months(to_date('2010-02-28','yyyy-mm-dd'),1) from dual;
ADD_MONTHS(TO_DATE('2010-02-28
------------------------------
2010-3-31

select add_months(to_date('2012-02-28','yyyy-mm-dd'),1) from dual;
ADD_MONTHS(TO_DATE('2010-02-28
------------------------------
2010-3-31

select add_months(to_date('2010-03-31','yyyy-mm-dd'),1) from dual;
ADD_MONTHS(TO_DATE('2010-03-31
------------------------------
2010-4-30

ADD_MONTHS returns the date d plus n months. The argument n can be any integer. If d is the last day of the month or if the resulting month has fewer days than the day component of d, then the result is the last day of the resulting month. Otherwise, the result has the same day component as d.

Oh, it turns out that ADD_MONTHS will first determine whether it is the last day of the current month. If it is the last day of the current month, the result will be the last day after N months; if not, it will be the D day after N months. But when there is no D day after N months (for example, there is no 31st in April), it will also set it as the last day.

2. About not in of oracle

IN is a member condition that compares each member value for a given set or subquery. IN is functionally equivalent to the operation of =ANY, and NOT IN is functionally equivalent to the operation of !=ALL. Logically, IN is actually an item-by-item determination of a given member set or sub-query result set.

SELECT name FROM employee A WHERE A.name not in ('gallen','tomoya',NULL)

What will be the result? The result is nothing, the result is an empty set. why? In fact, the above sql statement is equivalent to:

SELECT name FROM employee A WHERE A.name!='gallen' AND A.name!='tomoya' AND A.name!=NULL

根据NULL的运算特性和真值表,该语句无论前两个判定条件是否为真,其结果一定是NULL或者FALSE。故绝对没有任何记录可以返回。

其实平时的时候并没有上面的语句那么明显,集合往往会是一个子查询,比如:

SELECT name FROM employee A WHERE A.name not in (select name FROM employee A WHERE A.salary>8000)

这时候,你要预先在子查询里把NULL去掉,比如用nvl。

3、Oracle修改字段的类型、精度、范围

可能修改字段的精度和范围比修改字段类型来得更常见,比如我们需要把某个字段的数据类型从原来的number(10)变为number(10,2)型的实际操作。要是没有数据的话直接用以下语句即可

alter table tbl_test modify cityCode number(10,2);

但是有数据的话就不能用上面方法了,为什么呢?因为oracle只允许在类型相同的情况下,增加精度或者放大范围,"column to be modified must be empty to decrease precision or scale" 。

有人会问number(10)变为number(10,2)精度不是变大了吗?其实不然,number()类型的默认精度是38位,你不指定精度,oracle无法辨别具体位数,所以无论是number(10)变为number(10,2)还是number(10,2)变为number(10),在该字段非空的情况下都是不允许的。

alter table tbl_test add cityCode_temp number(5,2)
update tbl_test set cityCode_temp=permile;
alter table tbl_test drop column cityCode;
alter table tbl_test rename column cityCode_temp to cityCode;

这种方法会使列名发生变化,而且字段顺序增加 有可能发生行迁移,对应用程序会产生影响 以下方法是比较好的方法 不用使列名发生变化 也不会发生表迁移,但这个有个缺点是表要更新两次 如果数据量较大的话 产生的undo和redo更多 ,前提也是要停机做 要是不停机的话 ,也可以采用在线重定义方式来做 以下是脚本:

alter table tbl_test add cityCode_temp number;
Add/modify columns
alter table tbl_test modify cityCode null;
update tbl_test set cityCode_temp=cityCode,cityCode=null;
commit;
alter table tbl_test modify cityCode number(10,2);
update tbl_test set cityCode=cityCode_temp,cityCode_temp=null;
commit;
alter table tbl_test drop column cityCode_temp;
alter table tbl_test modify cityCode not null;
select * from tbl_test ;

上述的相关内容就是对Oracle修改字段类型的方法的描述,希望会给你带来一些帮助在此方面。

4、未完待续...


欢迎关注我的个人微信公众号:能叔

能叔

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326433935&siteId=291194637