레이블이 Database인 게시물을 표시합니다. 모든 게시물 표시
레이블이 Database인 게시물을 표시합니다. 모든 게시물 표시

2009년 2월 4일 수요일

MySQL의 auto_increment 적용

Mysql을 사용할때, 인덱스와 같이 필요에 의해 자동으로 숫자가 증가되는 기능이 필요할 경우
auto_increment를 사용한다.

적용 방법은 다음과 같다.

mysql> create table info_contents(c_count int not null auto_increment primary key, c_type varchar(10), c_name varchar(20));

auto_increment기능을 적용할 때 초기값을 지정해서, 지정된 숫자 부터 증가값을 적용하려면 다음과 같이 초기 값을 지정한다.

mysql> create table info_contents (c_count int not null auto_increment primary key, c_type varchar(10), c_name varchar(20)) auto_increment=200;

필요에 따라 증가된 값을 초기화 하고 싶을 경우에는  alter table 명령을 사용하여 증가된 값을 변경하여 줄수가 있다.

mysql> alter table info_contents auto_increment = 0;

2008년 10월 21일 화요일

Oracle Sequence 와 Trigger 생성

Sequence 생성
create sequence autonum6 increment by 1 start with 1 nomaxvalue nocycle nocache;

Triger  생성
create trigger auto_trigger6
before insert on Reservation
for each row
begin
select autonum6.nextval into :new.id from dual;
end;
/

시퀀스를 만들고 트리거를 실행하면 자동으로 증가한다.

auto_increment를 사용해도 된다.
예를들어  시퀀스만을 만들고(트리거는 안씀)
insert into test (num)  values (autonum.nextval); 도 가능하다.

2008년 10월 1일 수요일

Transaction Control Language (TCL)

Transaction Control Language (TCL)

Transaction control statements manage changes made by DML statements.

What is a Transaction?

A transaction is a set of SQL statements which Oracle treats as a Single Unit. i.e. all the statements should execute successfully or none of the statements should execute.

To control transactions Oracle does not made permanent any DML statements unless you commit it. If you don’t commit the transaction and power goes off or system crashes then the transaction is roll backed.

TCL Statements available in Oracle are

COMMIT       :Make changes done in  transaction permanent.

ROLLBACK  :Rollbacks the state of database to the last commit point.

SAVEPOINT :Use to specify a point in transaction to which later you can rollback.

COMMIT

To make the changes done in a transaction permanent issue the COMMIT statement.

The syntax of COMMIT Statement is

COMMIT  [WORK]  [COMMENT ‘your comment’];

WORK is optional.

COMMENT is also optional, specify this if you want to identify this transaction in data dictionary DBA_2PC_PENDING.

Example

insert into emp (empno,ename,sal) values (101,’Abid’,2300);

commit;

 

ROLLBACK

 

To rollback the changes done in a transaction give rollback statement. Rollback restore the state of the database to the last commit point.

 

 

Example :

delete from emp;

rollback;          /* undo the changes */

 

 

SAVEPOINT

 

Specify a point in a transaction to which later you can roll back.

 

Example

 

insert into emp (empno,ename,sal) values (109,’Sami’,3000);

savepoint a;

insert into dept values (10,’Sales’,’Hyd’);

savepoint b;

insert into salgrade values (‘III’,9000,12000);

 

Now if you give

 

rollback to a;

 

Then  row from salgrade table and dept will be roll backed. Now you can commit the row inserted into emp table or rollback the transaction.

 

If you give

 

rollback to b;

 

Then row inserted into salgrade table will be roll backed. Now you can commit the row inserted into dept table and emp table or rollback to savepoint a or completely roll backed the transaction.

 

If you give

 

rollback;

 

Then the whole transactions is roll backed.

 

If you give

 

commit;

 

Then the whole transaction is committed and all savepoints are removed.


Oracle Isolation Level

Transaction Isolation Level(격리수준)을 이해하기
 
이번 기사에서는 지난 Deadlock 발생을 감소시키는 방법 기사에서 언급되었던 한 세션 상의 모든 SELECT 문장에 대한 디폴트 트랜잭션 잠금 동작을 제어하는 Transaction Isolation Level에 대하여 알아보자.
 
트랜잭션에서 일관성이 없는 데이터를 허용하도록 하는 수준을 Isolation Level(격리수준)이라고 한다. 예를 들어, 한 사용자가 어떠한 데이터를 수정하고 있는 경우 다른 사용자들이 그 데이터에 접근하는 것을 차단함으로써 완전한 데이터만을 사용자들에게 제공하게 된다. 또한, 많은 사용자들의 수정 작업으로 인하여 통계 자료를 작성할 수 없는 사용자를 위하여 읽기 작업을 수행할 수 있도록 Isolation Level을 변경할 수 있다.
 
ANSI에서 작성된 SQL-92 표준은 네 종류의 Isolation Level을 정의하고 있으며 SQL Server 7.0, 2000은 이 표준을 준수한다. Isolation Level을 조정하는 경우 동시성이 증가되는데 반해 데이터의 무결성에 문제가 발생할 수 있거나, 데이터의 무결성을 완벽하게 유지하는 데 반하여 동시성이 떨어질 수 있으므로 그 기능을 완벽하게 이해한 후에 사용해야 한다.
 
현재 설정된 Isolation Level을 보고자 하는 경우 DBCC USEROPTIONS 를 이용한다.
 
다음은 SQL Server에서 지원하는 네 종류의 Transaction Isolation Level이다.
l        Read Uncommitted
l        Read Committed
l        Repeatable Read
l        Serializable
 
Read Uncommitted Isolation Level
SELECT 문장을 수행하는 경우 해당 데이터에 Shared Lock이 걸리지 않는 Level이다. 따라서, 어떤 사용자가 A라는 데이터를 B라는 데이터로 변경하는 동안 다른 사용자는 B라는 아직 완료되지 않은(Uncommitted 혹은 Dirty) 데이터 B를 읽을 수 있다.
 
Read Committed Isolation Level
SQL Server가 Default로 사용하는 Isolation Level이다. 이 Level에선 SELECT 문장이 수행되는 동안 해당 데이터에 Shared Lock이 걸린다. 그러므로, 어떠한 사용자가 A라는 데이터를 B라는 데이터로 변경하는 동안 다른 사용자는 해당 데이터에 접근할 수 없다.
 
Repeatable Read Isolation Level
트랜잭션이 완료될 때까지 SELECT 문장이 사용하는 모든 데이터에 Shared Lock이 걸리므로 다른 사용자는 그 영역에 해당되는 데이터에 대한 수정이 불가능하다. 가령, Select col1 from A where col1 between 1 and 10을 수행하였고 이 범위에 해당하는 데이터가 2건이 있는 경우(col1=1과 5) 다른 사용자가 col1이 1이나 5인 Row에 대한 UPDATE가 불가능하다. 하지만, col1이 1과 5를 제외한 나머지 이 범위에 해당하는 Row를 INSERT하는 것이 가능하다.
 
Serializable Isolation Level
트랜잭션이 완료될 때까지 SELECT 문장이 사용하는 모든 데이터에 Shared Lock이 걸리므로 다른 사용자는 그 영역에 해당되는 데이터에 대한 수정 및 입력이 불가능하다. 예를 들어, Repeatable Read의 경우 1에서 10 사이에 해당되는 데이터에 대한 UPADTE가 가능하였다. 하지만 이 Level에서는 UPDATE 작업도 허용하지 않는다.
 
사용 방법
SET TRANSACTION ISOLATION LEVEL
{
READ COMMITTED
| READ UNCOMMITTED
| REPEATABLE READ
| SERIALIZABLE
}
즉, SET 문장을 이용하여 Transaction Isolation Level을 정의하게 되므로 해당 Session에서만 Isolation Level이 적용된다.
 
사용 예제
다음 예제는 세션에 대해 TRANSACTION ISOLATION LEVEL을 설정한다. 뒤따르는 각 Transact-SQL 문에 대해 SQL Server는 트랜잭션이 끝날 때까지 모든 공유 잠금을 유지한다.
SET TRANSACTION ISOLATION LEVEL REPEATABLE READ
GO
BEGIN TRANSACTION
SELECT * FROM publishers
SELECT * FROM authors
...
COMMIT TRANSACTION