Oracle中怎樣查詢數(shù)據(jù)表的哪個字段是主鍵 |
發(fā)布時間: 2012/9/20 16:44:35 |
工作中要用到 Oracle 10g,經(jīng)常要向其中的某張表插入事件發(fā)生的日期及時間。專門就 Oracle 的日期及時間顯示方式和插入方式記一筆。
例如,我們創(chuàng)建了一個表 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 是我們指定的日期時間格式,如果不顯式指定就用 Oracle 的默認值。 fmt 里常用的跟日期時間有關(guān)的占位符如下: MM 用數(shù)字表示的月份(例如,07) 跟 Oracle 顯示日期時間時會隱性調(diào)用 TO_CHAR 函數(shù)一樣,當(dāng) Oracle 期望一個 Date 類型的值時,它會隱性調(diào)用 TO_DATE 函數(shù)來轉(zhuǎn)換輸入的字符串,依據(jù)的默認格式是“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 是表示日期和時間的字符串。fmt 的表示方法和 TO_CHAR 函數(shù)一樣。 我們前面一直提到 Oracle 默認的日期時間格式是“DD-MON-YY”,其實,我們還可以修改這個默認格式,把它改成我們需要的格式。在 SQL*plus 里面輸入下面的命令: alter session set NLS_DATE_FORMAT='<my_format>'; ——這個改變只對當(dāng)前的會話(session)有用。 例如: SQL> alter session set nls_date_format='yyyy-mm-dd';會話已更改。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.
本文出自:億恩科技【www.allwellnessguide.com】 服務(wù)器租用/服務(wù)器托管中國五強!虛擬主機域名注冊頂級提供商!15年品質(zhì)保障!--億恩科技[ENKJ.COM] |