Basic SQL _dml statements (2)

Source: Internet
Author: User
Document directory
  • 3. Read-only transactions:

1. Create a table using a query statement

Create Table employe

Select * from EMP;

However, the data in the EMP table is also copied when the table is created. If you only want to copy the table structure but not the data, then

Create Table employe

Select * from EMP where 1 = 2; -- here 1 = 2 can be replaced with any expression, as long as the condition after the where statement returns false. For example, change to 4 = 5.

Note: The table created using the SELECT statement is only the name of each column. The column type is the same as that of the original table. The constraints and indexes in the original table are not copied.

2. Decode and case

3. Read-only transactions:

We know that users may insert or delete data in tables at any time in the database. in this way, different search results will be obtained at different times. if there is such a business requirement, we only need to count the data before on the 30th.

Don't worry about it later. The Finance and Accounting Department may have this kind of requirement. What should we do? We connect to the database at six o'clock and then execute the following command

SET transaction read only;

All query statements in this session will ignore operations after from now on. at least it is unclear how the Oracle background is implemented. it is a bit like copying all the data in the database for you to check. the changes made by others will not affect you. for example.

(1) If you use SQL * Plus to connect to Oracle. -- this is Session 1.

SET transaction read only;

Query table EMP: Select ename, empno from EMP; the result is

Scott 1, 123

(2) another user uses SQL * Plus to connect to Oracle-this is Session 2.

Insert into EMP (ename, empno) values ('arwen', 321 );

Commit;

(3) In session 1, find select ename, empno from EMP; the result is still

Scott 1, 123

Note: In session 1, only search operations are available. No insert, update, or delete operations are required.

4. ordered transactions:

In a read-only transaction, you can only query and cannot perform DML operations. In addition to all functions of read-only transactions, ordered transactions also provide DML operations.

SET transaction isolation level serializable;

-- Note that if DML operations are performed and the transaction is committed in this session, the ordered transaction seems to be invalid. It is the same as the normal operation. This function is a bit strange.

 

Modify Table data using merge

Suppose we want to back up an important table in the database. and regularly update the backup table to keep it synchronized with the original table. oracle has a simple method to implement this function. is to use merge. below is the syntax

Merge into schema. Table alias

Using schema. {table | View | subquery} alias

On (condition)

When matched then

Update set column = value [, column = value]...

When not matched then

Merge_insert_clause/merge_delete_clause;

 

Example:

Create the original table ta first:

Create Table TA (ename char (5), Eno INT );

Insert into ta values ('arwen', 10 );

Insert into ta values ('Alice ', 20 );

Create a backup table ta_copy

Create Table ta_copy (ename char (5), Eno INT );

Insert into ta_copy values ('God', 10 );

 

1. Synchronize two tables:

Merge into ta_copy T_c

Using ta t

On (t_c.eno = T. Eno)

When matched then

Update set t_c.ename = T. ename

-- Where t_c.ename = 'God' -- this row can be added or not

When not matched then

Insert (t_c.ename, t_c.eno)

Values (T. ename, T. Eno)

-- Where t_c.ename = 'God' -- this row can be added or not

;

2. We noticed that the example in 1 uses both update and insert. But we can also use only one of them. Update only or insert only

Update only:

Merge into ta_copy T_c

Using ta t

On (t_c.eno = T. Eno)

When matched then

Update set t_c.ename = T. ename;

Insert only:

Merge into ta_copy T_c
Using ta t
On (t_c.eno = T. Eno)
When not matched then
Insert (t_c.ename, t_c.eno)
Values (T. ename, T. Eno );

 

3. Insert all the data in the original table to the backup table.

Merge into ta_copy T_c
Using ta t
On (1 = 0) -- if the condition is false, all data is inserted no matter whether it is repeated or not.
When not matched then
Insert (t_c.ename, t_c.eno)
Values (T. ename, T. Eno );

 

 

 

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.