Oracle中怎樣查詢數(shù)據(jù)表的哪個(gè)字段是主鍵 |
發(fā)布時(shí)間: 2012/9/20 16:44:35 |
工作中要用到 Oracle 10g,經(jīng)常要向其中的某張表插入事件發(fā)生的日期及時(shí)間。專門就 Oracle 的日期及時(shí)間顯示方式和插入方式記一筆。
例如,我們創(chuàng)建了一個(gè)表 x: SELECT TO_CHAR(b, 'YYYY/MM/DD') AS b FROM abc; 返回的結(jié)果是: B------------ 2010/09/01 TO_CHAR(d [, fmt [, 'nlsparams'] ]) d 是 Date 類型的變量,fmt 是我們指定的日期時(shí)間格式,如果不顯式指定就用 Oracle 的默認(rèn)值。 fmt 里常用的跟日期時(shí)間有關(guān)的占位符如下: MM 用數(shù)字表示的月份(例如,07) 跟 Oracle 顯示日期時(shí)間時(shí)會(huì)隱性調(diào)用 TO_CHAR 函數(shù)一樣,當(dāng) Oracle 期望一個(gè) Date 類型的值時(shí),它會(huì)隱性調(diào)用 TO_DATE 函數(shù)來(lái)轉(zhuǎn)換輸入的字符串,依據(jù)的默認(rèn)格式是“DD-MON-YY”。 還是以我們的 x 表為例,我們可以直接輸入: insert into abc values(99, '31-may-08'); insert into abc values(99, to_date('2008/05/31:12:00:00AM', 'yyyy/mm/dd:hh:mi:ssam')); TO_DATE(char [, fmt [, 'nlsparams'] ]) char 是表示日期和時(shí)間的字符串。fmt 的表示方法和 TO_CHAR 函數(shù)一樣。 我們前面一直提到 Oracle 默認(rèn)的日期時(shí)間格式是“DD-MON-YY”,其實(shí),我們還可以修改這個(gè)默認(rèn)格式,把它改成我們需要的格式。在 SQL*plus 里面輸入下面的命令: alter session set NLS_DATE_FORMAT='<my_format>'; ——這個(gè)改變只對(duì)當(dāng)前的會(huì)話(session)有用。 例如: SQL> alter session set nls_date_format='yyyy-mm-dd';會(huì)話已更改。SQL> insert into abc (b) values('2004-08-26');已創(chuàng)建1行。 -------------------------------------------------------------------------------- You can use double quotes to make names case sensitive (by default, SQL is case insensitive), or to force spaces into names. Oracle will treat everything inside the double quotes literally as a single name. In this example, if "Current Time" is not quoted, it would have been interpreted as two case insensitive names CURRENT and TIME, which would actually cause a syntax error. DUAL is built-in relation in Oracle which serves as a dummy relation to put in the FROM clause when nothing else is appropriate. For example, try "select 1+2 from dual;". Another name for the built-in function SYSDATE is CURRENT_DATE. Be aware of these special names to avoid name conflicts.
You can add and subtract constants to and from a DATE value, and these numbers will be interpreted as numbers of days. For example, SYSDATE+1 will be tomorrow. You cannot multiply or divide DATE values. With the help of TO_CHAR, string operations can be used on DATE values as well. For example, to_char(<date>, 'DD-MON-YY') like '%JUN%' evaluates to true if <date> is in June.
本文出自:億恩科技【mszdt.com】 服務(wù)器租用/服務(wù)器托管中國(guó)五強(qiáng)!虛擬主機(jī)域名注冊(cè)頂級(jí)提供商!15年品質(zhì)保障!--億恩科技[ENKJ.COM] |