안녕하세요

프로그램 과정에서 막혔던 문제들에 대한 해결책 정리


페이지 목록

2013년 10월 22일 화요일

[MySql] begin transaction, rollback, commit

MySql 사용 시 백업과 비슷한 방식이다.

Insert, Update, Delete 등과 같이 잘못 사용하면 큰 문제가 일어나는 Query문을 사용할 경우에

자신이 수행한 결과를 다시 Rollback 할 수 있고

제대로 동작했다면 Commit하는 기능을 나타낸다.

Example을 보면 쉽게 이해 할 수 있다.

【예제】
mysql> create table test2 (a int) type=innodb;
Query OK, 0 rows affected (0.01 sec)

mysql> set autocommit=1;  ☜ autocommit의 디폴트값이 '1'이므로  실행하지 
않아도 됨
Query OK, 0 rows affected (0.00 sec)

mysql> insert into test2 values(10);
Query OK, 1 row affected (0.00 sec)

mysql> begin;
Query OK, 0 rows affected (0.00 sec)

mysql> update test2 set a=20 where a=10;
Query OK, 1 row affected (0.00 sec)
Rows matched: 1  Changed: 1  Warnings: 0

mysql> select * from test2;
+------+
| a    |
+------+
|   20 |
+------+
1 row in set (0.00 sec)

mysql> rollback;
Query OK, 0 rows affected (0.00 sec)

mysql> select * from test2;
+------+
| a    |
+------+
|   10 |
+------+
1 row in set (0.00 sec)

mysql> begin;
Query OK, 0 rows affected (0.00 sec)

mysql> update test2 set a=20 where a=10;
Query OK, 1 row affected (0.00 sec)
Rows matched: 1  Changed: 1  Warnings: 0

mysql> commit;
Query OK, 0 rows affected (0.00 sec)

mysql> select * from test2;
+------+
| a    |
+------+
|   20 |
+------+
1 row in set (0.00 sec)

mysql>


현재의 autocommit 상태를 확인하는 방법은 다음 예제와 같다. 【예제】 mysql> select @@session.autocommit; +----------------------+ | @@session.autocommit | +----------------------+ | 1 | +----------------------+ 1 row in set (0.03 sec) mysql> set autocommit=0; Query OK, 0 rows affected (0.00 sec) mysql> select @@session.autocommit; +----------------------+ | @@session.autocommit | +----------------------+ | 0 | +----------------------+ 1 row in set (0.01 sec) mysql>

출처 : http://radiocom.kunsan.ac.kr/lecture/mysql/begin_commit_rollback.html

2013년 7월 25일 목요일

VC6.0 단축키

Shift + Tab : 선택된 줄을 탭 단위로 앞으로 이동

바로 내가 찾던 단축키

그외 단축키는 아래 블로그로 가세요.

http://blog.naver.com/yousilver23?Redirect=Log&logNo=50942552

2013년 7월 23일 화요일

[MySQL] You can't specify target table 'Customize' for update in FROM clause

insert 구문에서 에러가 났다.

에러메시지는 뭔소리인가..
구글링하니 한방에 나옴ㅋ

이 에러는 insert나 update를 하는 경우 서브쿼리의 FROM절에서 target 테이블과 동일한 테이블을 사용할 수 없다는 내용이다.

ㅋㅋ




UPDATE mytable_name
SET a='kim'
WHERE Exists ( SELECT* FROM ( SELECT id
FROM mytable_name
WHERE b=0
LIMIT 2 ) as temp

위와 같이  한 번 더 감싸줘야 한다.

[MySQL] Insert where not exists

Insert into Table1 (col1, col2)
select 'val1', 'val2' from Table2
where not exists
(
      select col1, col2
      from Table
      where col1 = 'val1' and col2 = 'val2'
 )

where not exists 문 아래 select 문에 값이 없을 때
Insert 처리 한다.
do0park.blog.me/40134359319


INSERT INTO propval (tocid, prop_id, pos, str_val)
VALUES (tocid, 1, 0, 'No')
WHERE NOT EXISTS (SELECT * FROM propval WHERE prop_id = 1)

Insert 시 select 쓰지 않는 소스

http://www.dbforums.com/microsoft-sql-server/1640697-insert-where-not-exists.html

위 두 방법이 되지 않는 다면...

INSERT INTO Table(col1, col2, col3) SELECT col1, col2,col3  FROM DUAL WHERE NOT EXISTS ( SELECT col FROM Table WHERE col = val1)

value 대신 Select 문을 사용하여 Insert 진행하고 From Dual 즉 더미 Table 을 적어 주고,

Where not exists 문에 Select 문을 넣으면 동작한다.

2013년 7월 16일 화요일

[MySQL] Insert 와 select 문 같이 써서 Insert 하기

Insert Into Select 를 사용하면 서, ColA 는 1로 넣고 싶을 때
아래와 같이 사용하면 된다.
INSERT INTO MyTable
           
(ColA,
             ColB
,
             ColC
)
SELECT 1,
       colBB
,
       colCC
FROM   MyTable2 

응용하여서, 하나의 칼럼에 Constant 값을 넣고 싶으면
Select 문에 넣고 싶은 칼럼에 Constant를 넣으면 됨

출처 : http://stackoverflow.com/questions/4989583/insert-combine-value-and-select

2013년 6월 26일 수요일

[MFC] MySQL 함수 정리

CString query;
query.Format(" blur blur");

쿼리 실행
ado.ExecuteSQL(query, 0)

검색결과 개수

int count = ado.GetRecordCount();

컬럼 값 구하기
CString temp;
ado.GetFieldValue( column number, temp, _T(""));

다음 값 가져오기
ado.m_ptrRecordset->Moveext();

쿼리값 마지막 찾기
while(!ado.m_ptrRecordset->adoEOF)
{

다음 값 가져오기
ado.m_ptrRecordset->Moveext();
}

[DB] Query 정리

Select

 Select Column From Table where Col1 = xx and Col2 = yy or Col3 = zz

Insert

 Insert Into Table (Col1, Col2, Col3) VALUES (Co1, Col2, Col3)

String의 경우 ' ' 혹은 " " 로 감싸준다. "이순신" '이순신'

Update
밋
Update tabe set col1 = val1 , co2 = val2 where some_col = som_val;

Add a Column

Alter Table contacts Add email VARCHAR(60);

Column 가져오기

Show columns from Table;