MARIADB 10.0.9 버젼 사용중입니다.
현재 개발중인 시스템이라 중간중간 데이터 넣고 빼고 테스트중인데요.
DB 엔진 타입은 innodb (xtradb) 사용중입니다.
id에 auto_increment를 주고
초기화 시키고 싶어서
데이터 삭제후에
ALTER TABLE `member` AUTO_INCREMENT=1;
명령을 주는데 AUTO_INCREMENT 초기화가 안되네요...
그래서 편법으로 데이터 삭제후에 DB를 재시작 시키긴하는데.
(AUTO_INCREMENT 값의 경우 메모리에 저장이 되어서 디비가 종료될 경우 날라감...)
이거 초기화 쿼리 왜 안먹는지 원인을 모르겠네요.
InnoDB에서는 MyISAM과는 다르게 Auto_Increment 값이 변경될 때마다 기록하지 않습니다. “메모리 상에서 Auto_Increment 값을 관리”하는 것이죠. DB가 처움 구동되면 다음과 같이 Auto_Increment 속성이 있는 테이블은 모두 초기화됩니다.
1
SELECT MAX(ai_col) FROM t for UPDATE
만약 결과 값이 NULL이면 Auto_Increment_Offset으로 대체되거나, 1로 초기화됩니다. 그리고 Auto_Increment_Increment만큼 증가되어 Auto_Increment 가 관리되는 것이죠. 이런 상황에서 어떤 문제가 발생할 수 있을까요?
자답입니다.
http://viralpatel.net/blogs/reseting-mysql-autoincrement-column/
보니 작거나 같은 값으로는 SET 할수 없다네요.
Note that you cannot reset the counter to a value less than or equal to any that have already been used. For MyISAM, if the value is less than or equal to the maximum value currently in the AUTO_INCREMENT column, the value is reset to the current maximum plus one. For InnoDB, if the value is less than the current maximum value in the column, no error occurs and the current sequence value is not changed.
데이터 지운후에 초기화 방법으로는
TRUNCATE
TABLE
table_name;
쓰면 되지만 문제는 foreign key 설정으로 이 명령어가 안먹으니 결론은
DELETE FROM 으로 데이터 날리고 DB 재시작 밖에는 방법이 없겠네요...