테이블의 데이터 업데이트

Python으로 배우는 데이터베이스 입문

Jason Myers

Co-Author of Essential SQLAlchemy and Software Engineer

테이블의 데이터 업데이트

  • update() 문으로 수행합니다
  • insert()와 유사하나, 어떤 레코드를 수정할지 결정하는 where 절이 포함됩니다
  • 변경할 값을 values() 절에 column=value 쌍으로 추가합니다
Python으로 배우는 데이터베이스 입문

한 행 업데이트

from sqlalchemy import update

stmt = update(employees) stmt = stmt.where(employees.columns.id == 3) stmt = stmt.values(active=True)
result_proxy = connection.execute(stmt) print(result_proxy.rowcount)
1
Python으로 배우는 데이터베이스 입문

여러 행 업데이트

  • 업데이트할 모든 레코드를 선택하는 where 절을 작성합니다
Python으로 배우는 데이터베이스 입문

여러 행 삽입

stmt = update(employees)
stmt = stmt.where(employees.columns.active == True)

stmt = stmt.values(active=False, salary=0.00)
result_proxy = connection.execute(stmt) print(result_proxy.rowcount)
3
Python으로 배우는 데이터베이스 입문

상관 업데이트

new_salary = select([employees.columns.salary])
new_salary = new_salary.order_by(
    desc(employees.columns.salary))
new_salary = new_salary.limit(1)

stmt = update(employees)
stmt = stmt.values(salary=new_salary)
result_proxy = connection.execute(stmt)
print(result_proxy.rowcount)
3
Python으로 배우는 데이터베이스 입문

상관 업데이트

  • 업데이트할 열의 값을 찾기 위해 select() 문을 사용합니다
  • 최대값으로 갱신하거나 다른 테이블의 약어와 일치하도록 문자열을 바꿀 때 자주 사용합니다
Python으로 배우는 데이터베이스 입문

연습해 봅시다!

Python으로 배우는 데이터베이스 입문

Preparing Video For Download...