Error Code: 1175. You are using safe update mode and you tried to update a table without a WHERE that uses a KEY column.

leeheejee·2025년 1월 22일
0

Error_note

목록 보기
4/4

에러를 마주하게된 계기.

UPDATE STUDENT SET MAJOR = 01 WHERE MAJOR = '컴퓨터공학과' 

STUDENT 라는 테이블에서 MAJOR 컬럼이 '컴퓨터공학과'라는 문자열로 되어있는데,
이를 숫자로 값을 변경하고 싶었습니다.

Error Code: 1175. You are using safe update mode and you tried to update a table without a WHERE that uses a KEY column. To disable safe mode, toggle the option in Preferences -> SQL Editor and reconnect.

에러 코드를 살펴보면 safe update mode 를 사용하면서 발생된 에러임을 알 수 있습니다.

  • safe update mode 관련 my sql 공식문서

    Using Safe-Updates Mode (--safe-updates)
    For beginners, a useful startup option is --safe-updates (or --i-am-a-dummy, which has the same effect). Safe-updates mode is helpful for cases when you might have issued an UPDATE or DELETE statement but forgotten the WHERE clause indicating which rows to modify. Normally, such statements update or delete all rows in the table. With --safe-updates, you can modify rows only by specifying the key values that identify them, or a LIMIT clause, or both. This helps prevent accidents. Safe-updates mode also restricts SELECT statements that produce (or are estimated to produce) very large result sets.

My SQL 공식문서 확인하기
내용을 살펴보니, UPDATE, DELETE를 할 때 WHERE을 잊고 사용해서 전체 테이블에 영향을 주는것을 막기 위해서 safe mode가 사용됨을 확인하였습니다.

safe update mode 에서는 specifying the key 를 사용해서만 row 를 수정할 수 있는데,
저는 식별 가능한 컬럼이 아닌, 일반 컬럼인 MAJOR를 수정하려해서 에러가 발생되었습니다.

가장 원초적인 방법인 My SQL의 설정을 변경하여서 에러를 해결하였는데, 이 방법을 간략하게 설명하면 다음과 같습니다.

에러문의 하단에 친절하게 설명되어있는 부분을 다시 잘 살펴보면
To disable safe mode, toggle the option in Preferences -> SQL Editor and reconnect.
safe mode를 해제하기 위해서 SQL Editor에서 설정하고 재연결하라고 되어있습니다.

설정에서 SQL Editor을 클릭하고 맨 하단의
"Safe Updates (rejects UPDATEs and DELETEs with no restrictions)" 에 체크된 박스를 체크 해제하고 재연결하여서 SQL문을 실행하니 문제없이 처리가 되었습니다.

이렇게 설정을 변경하므로써 좋은 점은,

UPDATE STUDENT SET MAJOR = 01 WHERE MAJOR = '컴퓨터공학과' 

에서 MAJOR가 '컴퓨터공학과'인 row가 여러개 있다면 한번에 MAJOR_CODE 개념으로 숫자로 변경 가능하게 됩니다.

하지만,
실수로라도 WHERE를 잊은 경우 전체 테이블의 값에 영향을 끼칠 위험성을 가지게 됩니다.
My SQL 공식문서에서는 아래와 같은 방법도 제안합니다.

UPDATE tbl_name SET not_key_column=val WHERE key_column=val;

not_key_column의 값을 update 하되, 그 위치는 key_column의 값으로 지정하는 것 입니다.

필요에따라 적절한 방법을 선택하거나, 다른 방법을 추가로 학습해봐야겠습니다!

profile
🫥 😊

0개의 댓글