Home Database Mysql Tutorial Oracle中dual表

Oracle中dual表

Jun 07, 2016 pm 03:32 PM
oracle s constitute virtual

dual是一个虚拟表,用来构成select的语法规则,oracle保证dual里面永远只有一条记录。我们可以用它来做很多事情,如下: 1、查看当前用户,可以在 SQL Plus中执行下面语句 select user from dual; 2、用来调用系统函数 select to_char(sysdate,'yyyy-mm-dd h

dual是一个虚拟表,用来构成select的语法规则,oracle保证dual里面永远只有一条记录。我们可以用它来做很多事情,如下:

  1、查看当前用户,可以在 SQL Plus中执行下面语句 select user from dual;

  2、用来调用系统函数

  select to_char(sysdate,'yyyy-mm-dd hh24:mi:ss') from dual;--获得当前系统时间

  select SYS_CONTEXT('USERENV','TERMINAL') from dual;--获得主机名

  select SYS_CONTEXT('USERENV','language') from dual;--获得当前 locale

  select dbms_random.random from dual;--获得一个随机数

  3、得到序列的下一个值或当前值,用下面语句

  select your_sequence.nextval from dual;--获得序列your_sequence的下一个值

  select your_sequence.currval from dual;--获得序列your_sequence的当前值

  4、可以用做计算器 select 7*9 from dual;

  ------

  Oracle系统中dual表是一个“神秘”的表,网上有很多网友都对该表进行了测试,该表只有一行一列,其实该表和系统中的其他表一样,一样可以执行插入、更新、删除操作,还可以执行drop操作。但是不要去执行drop表的操作,否则会使系统不能用,数据库起不了,会报Database startup crashes with ORA-1092错误。此时也不要慌乱,可以通过执行以下步骤来进行恢复。可以用sys用户登陆。

  SQL> create pfile=’d:pfile.bak’ from spfile

  SQL> shutdown immediate

  在d:pfile.bak文件中最后加入一条:

  replication_dependency_tracking = FALSE

  重新启动数据库:

  SQL> startup pfile=’d:pfile.bak’

  SQL> create table “sys”.”DUAL”

  [an error occurred while processing this directive]

  =====

  DUAL ? 有什么神秘的? 当你想得到ORACLE系统时间, 简简单单敲一行SQL 不就得了吗? 故弄玄虚….

  SQL> select sysdate from dual;

  SYSDATE

  ---------

  28-SEP-03

  哈哈, 确实DUAL的使用很方便. 但是大家知道DUAL倒底是什么OBJECT, 它有什么特殊的行为吗? 来,我们一起看一看. 首先搞清楚DUAL是什么OBJECT :

  SQL> connect system/manager

  Connected.

  SQL> select owner, object_name , object_type from dba_objects where object_name like '%DUAL%'; OWNER OBJECT_NAME OBJECT_TYPE

  --------------- --------------- -------------

  SYS DUAL TABLE PUBLIC DUAL SYNONYM

  原来DUAL是属于SYS schema的一个表,然后以PUBLIC SYNONYM的方式供其他数据库USER使用.

  再看看它的结构:

  SQL> desc dual Name Null? Type

  ----------------------------------------- -------- ----------------------------

  DUMMY VARCHAR2(1)

  SQL>

  只有一个名字叫DUMMY的字符型COLUMN .

  然后查询一下表里的数据:

  SQL> select dummy from dual;

  DUMMY

  ----------

  X

  哦, 只有一条记录, DUMMY的值是’X’ .很正常啊,没什么奇怪嘛. 好,下面就有奇妙的东西出现了!

  插入一条记录:

  SQL> connect sys as sysdba

  Connected.

  SQL> insert into dual values ( 'Y');

  1 row created.

  SQL> commit;

  Commit complete.

  SQL> select count(*) from dual;

  COUNT(*)

  ----------

  2

  迄今为止,一切正常. 然而当我们再次查询记录时,奇怪的事情发生了

  SQL> select * from dual;

  DUMMY

  ----------

  X

  刚才插入的那条记录并没有显示出来 ! 明明DUAL表中有两条记录, 可就是只显示一条!

  再试一下删除 ,狠一点,全删光 !

  SQL> delete from dual; /*注意没有限定条件,试图删除全部记录*/

  1 row deleted.

  SQL> commit;

  Commit complete.

  哈哈,也只有一条记录被删掉,

  SQL> select * from dual;

  DUMMY

  ----------

  Y

  为什么会这样呢? 难道SQL的语法对DUAL不起作用吗?带着这个疑问,

  我查询了一些ORACLE官方的资料. 原来ORACLE对DUAL表的操作做了一些内部处理,尽量保证DUAL表中只返回一条记录.当然这写内部操作是不可见的 . 看来ORACLE真是蕴藏着无穷的奥妙啊!

转:http://www.cnblogs.com/qiangqiang/archive/2010/10/15/1852229.html

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
Two Point Museum: All Exhibits And Where To Find Them
1 months ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How long will Oracle database logs be kept? How long will Oracle database logs be kept? May 10, 2024 am 03:27 AM

The retention period of Oracle database logs depends on the log type and configuration, including: Redo logs: determined by the maximum size configured with the "LOG_ARCHIVE_DEST" parameter. Archived redo logs: Determined by the maximum size configured by the "DB_RECOVERY_FILE_DEST_SIZE" parameter. Online redo logs: not archived, lost when the database is restarted, and the retention period is consistent with the instance running time. Audit log: Configured by the "AUDIT_TRAIL" parameter, retained for 30 days by default.

The order of the oracle database startup steps is The order of the oracle database startup steps is May 10, 2024 am 01:48 AM

The Oracle database startup sequence is: 1. Check the preconditions; 2. Start the listener; 3. Start the database instance; 4. Wait for the database to open; 5. Connect to the database; 6. Verify the database status; 7. Enable the service (if necessary ); 8. Test the connection.

How much memory does oracle require? How much memory does oracle require? May 10, 2024 am 04:12 AM

The amount of memory required by Oracle depends on database size, activity level, and required performance level: for storing data buffers, index buffers, executing SQL statements, and managing the data dictionary cache. The exact amount is affected by database size, activity level, and required performance level. Best practices include setting the appropriate SGA size, sizing SGA components, using AMM, and monitoring memory usage.

How to see the number of occurrences of a certain character in Oracle How to see the number of occurrences of a certain character in Oracle May 09, 2024 pm 09:33 PM

To find the number of occurrences of a character in Oracle, perform the following steps: Get the total length of a string; Get the length of the substring in which a character occurs; Count the number of occurrences of a character by subtracting the substring length from the total length.

Oracle database server hardware configuration requirements Oracle database server hardware configuration requirements May 10, 2024 am 04:00 AM

Oracle database server hardware configuration requirements: Processor: multi-core, with a main frequency of at least 2.5 GHz. For large databases, 32 cores or more are recommended. Memory: At least 8GB for small databases, 16-64GB for medium sizes, up to 512GB or more for large databases or heavy workloads. Storage: SSD or NVMe disks, RAID arrays for redundancy and performance. Network: High-speed network (10GbE or higher), dedicated network card, low-latency network. Others: Stable power supply, redundant components, compatible operating system and software, heat dissipation and cooling system.

How to read dbf file in oracle How to read dbf file in oracle May 10, 2024 am 01:27 AM

Oracle can read dbf files through the following steps: create an external table and reference the dbf file; query the external table to retrieve data; import the data into the Oracle table.

How much memory is needed to use oracle database How much memory is needed to use oracle database May 10, 2024 am 03:42 AM

The amount of memory required for an Oracle database depends on the database size, workload type, and number of concurrent users. General recommendations: Small databases: 16-32 GB, Medium databases: 32-64 GB, Large databases: 64 GB or more. Other factors to consider include database version, memory optimization options, virtualization, and best practices (monitor memory usage, adjust allocations).

Oracle scheduled tasks execute the creation step once a day Oracle scheduled tasks execute the creation step once a day May 10, 2024 am 03:03 AM

To create a scheduled task in Oracle that executes once a day, you need to perform the following three steps: Create a job. Add a subjob to the job and set its schedule expression to "INTERVAL 1 DAY". Enable the job.

See all articles