1. ๋ฐ์ดํฐ ์ถ๊ฐ
insert into ํ ์ด๋ธ
values ๊ฐ
์ธ์ธ๋ฒ
1) ๊ฐ๊ฐ ๋ฐ์ดํฐ ์ถ๊ฐํ๊ธฐ
[ ์์ ]
insert into member
values('TWC', 'ํธ์์ด์ค', 9, '์์ธ', '02', '11111111', 167, '2015.10.19')
insert into buy
values(null, 'TWC', '์ง๊ฐ', null, 30, 2)
# member ํ
์ด๋ธ
insert into `member`
values('TWC','ํธ์์ด์ค', 9, '์์ธ', '02', '11111111', 167, '2015.10.19');
insert into `member`
values('BLK','๋ธ๋ํํฌ', 4, '๊ฒฝ๋จ', '055', '22222222', 163, '2016.08.08');
insert into `member`
values('WMN','์ฌ์์น๊ตฌ', 6, '๊ฒฝ๊ธฐ', '031', '33333333', 166, '2015.01.15');
insert into `member`
values('OMG','์ค๋ง์ด๊ฑธ', 7, '์์ธ', '', '', 160, '2015.04.21');
insert into `member`
values('GRL','์๋
์๋', 8, '์์ธ', '02', '44444444', 168, '2007.08.02');
insert into `member`
values('ITZ','์์ง', 5, '๊ฒฝ๋จ', '', '', 167, '2019.02.12');
insert into `member`
values('RED','๋ ๋๋ฒจ๋ฒณ', 4, '๊ฒฝ๋ถ', '054', '55555555', 161, '2014.08.01');
insert into `member`
values('APN','์์ดํํฌ', 6, '๊ฒฝ๊ธฐ', '031', '77777777', 164, '2011.02.10');
insert into `member`
values('SPC','์ฐ์ฃผ์๋
', 13, '์์ธ', '02', '88888888', 162, '2016.02.25');
insert into `member`
values('MMU','๋ง๋ง๋ฌด', 4, '์ ๋จ', '061', '99999999', 165, '2014.06.19');
insert into buy
values(null, 'BLK', '์ง๊ฐ', null, 30, 2),
(null, 'BLK', '๋งฅ๋ถํ๋ก', '๋์งํธ', 1000, 1),
(null, 'APN', '์์ดํฐ', '๋์งํธ', 200, 1),
(null, 'MMU', '์์ดํฐ', '๋์งํธ', 200, 5),
(null, 'BLK', '์ฒญ๋ฐ์ง', 'ํจ์
', 50, 3),
(null, 'MMU', '์์ดํ', '๋์งํธ', 80, 10),
(null, 'GRL', 'ํผ๊ณตSQL', '์์ ', 15, 5),
(null, 'APN', 'ํผ๊ณตSQL', '์์ ', 15, 2),
(null, 'APN', '์ฒญ๋ฐ์ง', 'ํจ์
', 50, 1),
(null, 'MMU', '์ง๊ฐ', null, 30, 1),
(null, 'APN', 'ํผ๊ณตSQL', '์์ ', 15, 1),
(null, 'MMU', '์ง๊ฐ', null, 30, 4)
1) auto_increment ์์ฑ์ ๊ฐ์ง๊ณ ์๋ num์ ๋ฐ์ดํฐ๋ฅผ ์ฝ์ ํ ๋ ๊ทธ ์๋ฆฌ์๋ null์ ๋ฃ์ด์ฃผ์๋ค
2) ๋ง์ฝ insert into buy์์
'TWC'๊ฐ ์๋๋ผ ๋ค๋ฅธ ๊ฐ์ ๋ฃ๋๋ค๋ฉด
buy ํ ์ด๋ธ์์์ mem_id๋ member ํ ์ด๋ธ์์์ mem_id๋ฅผ ์ฐธ์กฐํ๋ ์ธ๋ ํค ์ ์ฝ์กฐ๊ฑด์ด ์์ง๋ง
member ํ ์ด๋ธ์์์ mem_id๊ฐ์ด ์๋ ๊ฐ์ ๋ฃ๋๋ค๋ฉด ์กฐ๊ฑด์ ์๋ฐฐ๋๋ค
3)
ALTER TABLE ํ
์ด๋ธ์ด๋ฆ AUTO_INCREMENT = 1;
๋ฅผ ํ๋ค๋ฉด ๋ค์ 1๋ถํฐ ์ฆ๊ฐ๋๋ค
# ์๋์ผ๋ก ์ฆ๊ฐํ๋ auto_increment
1) auto_increment๋ก ์ง์ ํ๋ ์ด์ ๊ผญ ๊ธฐ๋ณธํค๋ก ์ง์ ํด์ค์ผ ํ๋ค
2) ๋ฐ์ดํฐ ์ถ๊ฐํ ๋ ์๋ ์ฆ๊ฐํ๋ ๋ถ๋ถ์ null๊ฐ์ผ๋ก ์ฑ์ ๋์ผ๋ฉด ๋๋ค
create table hongong3 (
toy_id int auto_increment primary key,
toy_name char(4),
age int
);
alter table hongong3 auto_increment = 1000;
set @@auto_increment_increment = 3;
insert into hongong3 values (null, 'ํ ๋ง์ค', 20);
insert into hongong3 values (null, '์ ์์ค', 23);
insert into hongong3 values (null, '์กด์
', 25);
select *
from hongong3

3) select last_insert_id() ๋ ํ์ฌ ์ด๋ ์ซ์๊น์ง ์ฆ๊ฐํ๋์ง ํ์ธํ ์ ์๋ค
select last_insert_id()

2) ๋ค๋ฅธ ํ ์ด๋ธ์์ ๋ฐ์ดํฐ ์กฐํํด์ ์ถ๊ฐํ๊ธฐ
๋จผ์ ํ ์ด๋ธ์ ๋ง๋ค์ด ์ค๋ค
create table city_popul (
city_name char(35),
population int
)
์๋ก ์์ฑํ ํ ์ด๋ธ์ ๊ธฐ์กด์ ์๋ ํ ์ด๋ธ์ ๋ด์ฉ์ ๋ฃ์ด์ค๋ค
insert into city_popul
select name, population
from world.city

2. ๋ฐ์ดํฐ ์์
update ํ ์ด๋ธ
set ์ด = ๊ฐ
where ์กฐ๊ฑด
์ ์ ์
1) ์ปฌ๋ผ 1๊ฐ๋ง ๋ฐ๊พธ๊ธฐ
update city_popul
set city_name = '์์ธ'
where city_name = 'Seoul'
select *
from city_popul
where city_name = '์์ธ'

2) ์ปฌ๋ผ ์ฌ๋ฌ๊ฐ ๋ฐ๊พธ๊ธฐ
์ฝค๋ง , ๋ก ๋ถ๋ฆฌํด์ ์ฌ๋ฌ๊ฐ์ ์ด ๋ณ๊ฒฝ
update city_popul
set city_name = '๋ด์', population = 0
where city_name = 'New York'
select *
from city_popul cp
where city_name = '๋ด์'

โป where์ ์ฌ์ฉํ์ง ์๋๋ค๋ฉด ํ ์ด๋ธ์ ๋ชจ๋ ํ ๊ฐ์ด ๋ณ๊ฒฝ๋๋ค
update city_popul
set population = population / 10000
3. ๋ฐ์ดํฐ ์ญ์
delete
from ํ ์ด๋ธ
where ์กฐ๊ฑด
๋ธํ์
delete
from city_popul
where city_name like 'New%'
โป where์ ์ฌ์ฉํ์ง ์๋๋ค๋ฉด ํ ์ด๋ธ์ ๋ชจ๋ ํ ๊ฐ์ด ๋ณ๊ฒฝ๋๋ค
# ๋์ฉ๋ ํ ์ด๋ธ์ ์ญ์
๋์ฉ๋ ํ ์ด๋ธ big_table1,2,3 ๋ง๋ค๊ธฐ
create table big_table1(select * from world.city, test.buy);
create table big_table2(select * from world.city, test.buy);
create table big_table3(select * from world.city, test.buy);
select count(*) from big_table1;
1) delete
delete from ํ ์ด๋ธ
๋น ํ ์ด๋ธ์ด ๋จ๋๋ค
์ญ์ ํ๋ ์๋๊ฐ ๋๋ฆฌ๋ค
โป where ๋ฌธ์ ์ฌ์ฉํ ์ ์๋ค
delete from big_table1
2) drop
drop table ํ ์ด๋ธ
ํ ์ด๋ธ์ด ์์ ์์ด์ง๋ค
์ญ์ ํ๋ ์๋๊ฐ ๋น ๋ฅด๋ค
drop table big_table2
3) truncate
truncate table ํ ์ด๋ธ
๋น ํ ์ด๋ธ์ด ๋จ๋๋ค
์ญ์ ํ๋ ์๋๊ฐ ๋น ๋ฅด๋ค
โป where ๋ฌธ์ ์ฌ์ฉํ ์ ์๋ค.
๋ฐ๋ผ์ ํ ์ด๋ธ์ ์ ์ฒด ํ์ ์ญ์ ํ ๋ ์ฌ์ฉํด์ผ ํ๋ค
truncate table big_table3'Knowledge๐ฆข > SQL' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
| [SQL 2ํ์ฐจ] where๊ณผ having์ ์ฐจ์ด์ , ์๋ธ์ฟผ๋ฆฌ, ์ ์์ฌํญ (1) | 2024.04.26 |
|---|---|
| [SQL] WHERE 1=1์ด ๋ญ์ผ? ๋๋์ฒด ์ ์ฐ๋ ๊ฑฐ์ผ? (0) | 2024.04.23 |
| [MySQL] ์๊ฐ/๋ ์ง ๊ด๋ จ ํจ์ ์์๋ณด๊ธฐ (1) | 2024.04.20 |
| [SQL๊ธฐ์ด] ๋ฐ์ดํฐ๋ฒ ์ด์ค, ํ ์ด๋ธ ์์ฑ ๋ฐ ์ญ์ (0) | 2024.04.19 |
| [SQL] ํ ์ด๋ธ ์๋ฃํ ์ข ๋ฅ (0) | 2024.04.19 |