Oracle中怎样查询数据表的哪个字段是主键
工作中要用到 Oracle 10g,经常要向其中的某张表插入事件发生的日期及时间。专门就 Oracle 的日期及时间显示方式和插入方式记一笔
工作中要用到 Oracle 10g,经常要向其中的某张表插入事件发生的日期及时间。专门就 Oracle 的日期及时间显示方式和插入方式记一笔。
像 Number,varchar2 等内置的数据类型一样,Oracle 用 Date 这个内置的数据类型来存储日期和时间。和 MS SQL Server 一样,日期和时间是存储在一个数据类型里的,没有只存储时间或只存储日期的单独的时间和日期数据类型。DATE 数据类型存储年月日和时分秒。
当显示 DATE 类型的数据时,Oracle 先要把存储的值从内部保存格式转化为可输出的字符串。通常,这种转换是通过 TO_CHAR 这个函数来完成的。如果没有用 TO_CHAR 函数来指定特定的日期时间格式,Oracle 默认的显示格式是“DD-MON-YY”。
例如,我们创建了一个表 x:
create table abc(a int, b date);
然后我们执行下面的查询语句:
select b from abc;
我们会看到如下结果(set head on):
B------------ 01-APR-09 (语言是英语) 或 B------------- 01-四月 -09 (语言是中文)
也就是说,每次显示 DATE 类型的数值时,Oracle 会自动调用 TO_CHAR 函数,并以默认日期显示格式为参数。我们可以显式地调用 TO_CHAR 函数,并指定自己想要的格式。例如:
SELECT TO_CHAR(b, 'YYYY/MM/DD') AS b FROM abc;
返回的结果是:
B------------ 2010/09/01
TO_CHAR 是个很强大的函数,可以把 Date,MLSLABEL,Number 类型的数据按指定的格式转换为 VARCHAR2 类型的数据,我们在这里只关心日期时间的转换。语法:
TO_CHAR(d [, fmt [, 'nlsparams'] ])
d 是 Date 类型的变量,fmt 是我们指定的日期时间格式,如果不显式指定就用 Oracle 的默认值。 fmt 里常用的跟日期时间有关的占位符如下:
MM 用数字表示的月份(例如,07)
MON 缩写的月份名称(例如,JUL)
MONTH 完整的月份名称(例如,JULY)
DD 日期(例如, 24)
DY 星期几的缩写(例如,FRI)
YYYY 用4位表示的年份(例如, 2008)
YY 用2位表示的年份,,取年份的后两位(例如,08)
RR 跟 YY 类似,但两位表示的年份被近似到 1950 到 2049 这个范围里的年份,例如 06 被认为是 2006,而不是 1906
AM (或 PM) 上下午指示符
HH 12进制表示的时间(1-12)
HH24 24进制表示的时间(0-23)
MI 分钟(0-59)
SS 秒(0-59)
以上讲的是利用 TO_CHAR 函数来显示 DATE 类型的数据,下面讲讲如何向表里插入一个 Date 类型的数值。这时我们要用到 TO_DATE 这个函数,把字符串表示的日期转换为 Date 类型。
跟 Oracle 显示日期时间时会隐性调用 TO_CHAR 函数一样,当 Oracle 期望一个 Date 类型的值时,它会隐性调用 TO_DATE 函数来转换输入的字符串,依据的默认格式是“DD-MON-YY”。
还是以我们的 x 表为例,我们可以直接输入:
insert into abc values(99, '31-may-08');
但是,在显示时不管用什么格式都不会影响我们实际储存的数据;在插入时,单纯采用 Oracle 的默认格式就不是那么符合工作需要了,我们还是需要显式调用 TO_DATE 函数,例如:
insert into abc values(99, to_date('2008/05/31:12:00:00AM', 'yyyy/mm/dd:hh:mi:ssam'));
TO_DATE 函数的语法:
TO_DATE(char [, fmt [, 'nlsparams'] ])
char 是表示日期和时间的字符串。fmt 的表示方法和 TO_CHAR 函数一样。
我们前面一直提到 Oracle 默认的日期时间格式是“DD-MON-YY”,其实,我们还可以修改这个默认格式,把它改成我们需要的格式。在 SQL*plus 里面输入下面的命令:
alter session set NLS_DATE_FORMAT='
例如:
SQL> alter session set nls_date_format='yyyy-mm-dd';会话已更改。SQL> insert into abc (b) values('2004-08-26');已创建1行。
--------------------------------------------------------------------------------
用 Oracle 内置的函数 SYSDATE 可以返回系统当前的日期和时间,例如:
select to_char(sysdate, 'Dy DD-Mon-YYYY HH24:MI:SS') as "Current Time" from dual;
Two interesting things to note here:
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.
--------------------------------------------------------------------------------
Operations on DATE You can compare DATE values using the standard comparison operators such as =, !=, >, etc.
You can subtract two DATE values, and the result is a FLOAT which is the number of days between the two DATE values. In general, the result may contain a fraction because DATE also has a time component. For obvious reasons, adding, multiplying, and dividing two DATE values are not allowed.
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(

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











Solutions to Oracle cannot be opened include: 1. Start the database service; 2. Start the listener; 3. Check port conflicts; 4. Set environment variables correctly; 5. Make sure the firewall or antivirus software does not block the connection; 6. Check whether the server is closed; 7. Use RMAN to recover corrupt files; 8. Check whether the TNS service name is correct; 9. Check network connection; 10. Reinstall Oracle software.

The method to solve the Oracle cursor closure problem includes: explicitly closing the cursor using the CLOSE statement. Declare the cursor in the FOR UPDATE clause so that it automatically closes after the scope is ended. Declare the cursor in the USING clause so that it automatically closes when the associated PL/SQL variable is closed. Use exception handling to ensure that the cursor is closed in any exception situation. Use the connection pool to automatically close the cursor. Disable automatic submission and delay cursor closing.

In Oracle, the FOR LOOP loop can create cursors dynamically. The steps are: 1. Define the cursor type; 2. Create the loop; 3. Create the cursor dynamically; 4. Execute the cursor; 5. Close the cursor. Example: A cursor can be created cycle-by-circuit to display the names and salaries of the top 10 employees.

Oracle views can be exported through the EXP utility: Log in to the Oracle database. Start the EXP utility, specifying the view name and export directory. Enter export parameters, including target mode, file format, and tablespace. Start exporting. Verify the export using the impdp utility.

When Oracle log files are full, the following solutions can be adopted: 1) Clean old log files; 2) Increase the log file size; 3) Increase the log file group; 4) Set up automatic log management; 5) Reinitialize the database. Before implementing any solution, it is recommended to back up the database to prevent data loss.

Oracle is not only a database company, but also a leader in cloud computing and ERP systems. 1. Oracle provides comprehensive solutions from database to cloud services and ERP systems. 2. OracleCloud challenges AWS and Azure, providing IaaS, PaaS and SaaS services. 3. Oracle's ERP systems such as E-BusinessSuite and FusionApplications help enterprises optimize operations.

Building a Hadoop Distributed File System (HDFS) on a CentOS system requires multiple steps. This article provides a brief configuration guide. 1. Prepare to install JDK in the early stage: Install JavaDevelopmentKit (JDK) on all nodes, and the version must be compatible with Hadoop. The installation package can be downloaded from the Oracle official website. Environment variable configuration: Edit /etc/profile file, set Java and Hadoop environment variables, so that the system can find the installation path of JDK and Hadoop. 2. Security configuration: SSH password-free login to generate SSH key: Use the ssh-keygen command on each node

To stop an Oracle database, perform the following steps: 1. Connect to the database; 2. Shutdown immediately; 3. Shutdown abort completely.
