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;
2009년 2월 4일 수요일
MySQL의 auto_increment 적용
2008년 10월 21일 화요일
Oracle Sequence 와 Trigger 생성
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
|
SET TRANSACTION ISOLATION LEVEL
{
READ COMMITTED
| READ UNCOMMITTED
| REPEATABLE READ
| SERIALIZABLE
} |
|
SET TRANSACTION ISOLATION LEVEL REPEATABLE READ
GO
BEGIN TRANSACTION
SELECT * FROM publishers
SELECT * FROM authors
...
COMMIT TRANSACTION |