DataBase/SQL

[DB][SQL] 레코드 생성, 수정, 삭제 2

bay07 2024. 4. 2. 14:02

1. update 수정

articles 테이블 1번 레코드의 title 필드 값을 'update title'로 변경한다 

UPDATE 
  articles
SET
  title = 'update Title'
WHERE
  id = 1;

 

 

articles 테이블 2번 레코드의 title, content 필드의 값을 

각각 'update Title', 'update Content'로 변경한다

UPDATE 
  articles
SET
  title = 'update Title',
  content = 'update Content'
WHERE
  id = 2;

 

 

2. Delete 삭제

artitcles 테이블의 1번 레코드 삭제 

DELETE FROM 
  articles
WHERE 
  id = 1;

 

articles 테이블에서 작성일이 오래된 순서대로 

레코드를 2개 삭제한다. 

DELETE FROM 
  articles
WHERE id IN (
  SELECT id FROM articles
  ORDER BY createdAt
  LIMIT 2
);

 

 

만약  WHERE절을 작성하지 않으면 모든 레코드를 삭제해 버린다