DB/SQL

SQL update, delete function 정리

ej503 2022. 6. 28. 15:35
delete from insert_test2 where seq in (select seq from insert_test);​
create table insert_test
(
    seq        int(10) primary key ,
    cont       text,
    name       varchar(15),
    tel_num    int(11),
    input_date datetime
);

insert into insert_test
values (13, '대한민국은 코로나를 잘 극복 하고 있습니다.', '홍길동', 01012345678, now()),
       (14, '대한민국은 코로나를 잘 극복 하고 있습니다.', '홍길동', 01012345678, now()),
       (15, '대한민국은 코로나를 잘 극복 하고 있습니다.', '홍길동', 01012345678, now()),
       (16, '대한민국은 코로나를 잘 극복 하고 있습니다.', '홍길동', 01012345678, now()),
       (17, '대한민국은 코로나를 잘 극복 하고 있습니다.', '홍길동', 01012345678, now())
;
create table insert_test2 as select * from insert_test;

//create table (table명) (as select * from (table명)); * where 1=2

insert into insert_test2
values (21, '대한민국은 코로나를 잘 극복 하고 있습니다.', '홍길동', 01012345678, now()),
       (22, '대한민국은 코로나를 잘 극복 하고 있습니다.', '홍길동', 01012345678, now()),
       (23, '대한민국은 코로나를 잘 극복 하고 있습니다.', '홍길동', 01012345678, now()),
       (24, '대한민국은 코로나를 잘 극복 하고 있습니다.', '홍길동', 01012345678, now()),
       (25, '대한민국은 코로나를 잘 극복 하고 있습니다.', '홍길동', 01012345678, now())
;

update insert_test2 set name = "손흥민", cont = "업데이트 되었습니다." where seq in (13, 14, 15);

//update table 명 set 컬럼명 = ~  where ~