mariadb sql๋ฌธ (+์๊ฐ ๋ ๋ ๋ง๋ค ์ถ๊ฐ ์์ )
2024. 2. 29. 10:55
๊ธฐ๋ณธํค ์์ฑ :
ALTER TABLE ํ
์ด๋ธ๋ช
add primary key (์ด์ด๋ฆ);
๊ธฐ๋ณธํค ์ญ์ :
ALTER TABLE ํ
์ด๋ธ๋ช
DROP PRIMARY KEY;
Auto-increament ์ถ๊ฐ :
ALTER TABEL ํ
์ด๋ธ๋ช
MODIFY num INT NOT NULL AUTO_INCREMENT;
์ธ๋ํค ์ถ๊ฐ :
alter table ์ถ๊ฐํ ํ
์ด๋ธ๋ช
add constraint ์๋ํค ์ด๋ฆ foreign key (์ด ์ด๋ฆ) REFERENCES ๊ฐ์ ธ์ฌ ํ
์ด๋ธ(๊ฐ์ ธ์ฌ์ด);
์ธ๋ํค ์ญ์ :
ALTER TABLE ํ
์ด๋ธ์ด๋ฆ DROP FOREIGN KEY ์ธ๋ํค์ด๋ฆ;
ON DELETE CASCADE :
Alter table ์์ํ
์ด๋ธ
Add foreign key(์์ํ
์ด๋ธ ์ธ๋ํค ์ปฌ๋ผ)
References ๋ถ๋ชจํ
์ด๋ธ(๋ถ๋ชจํ
์ด๋ธ ์ฐธ์กฐํ ์ปฌ๋ผ)
On delete cascade;
๋ฒ์ ํ์ธ :
select version();
ํ ์ด๋ธ ์์ฑ :
create table db๋ช
.table๋ช
(
์ปฌ๋ผ1 int primary key auto_increment, #๊ธฐ๋ณธํค+์๋ ์ฆ๊ฐ ์ค์
์ปฌ๋ผ2 varchar(40) not null,
์ปฌ๋ผ3 date,
primary key(์ปฌ๋ผ1),
foreign key(์ปฌ๋ผ2) references ๋ค๋ฅธํ
์ด๋ธ๋ช
(์ปฌ๋ผa) #๋ค๋ฅธ ํ
์ด๋ธ ์ธ๋ํค ์ฐธ์กฐ
);
ํ ์ด๋ธ ์กฐํ :
select * from ํ
์ด๋ธ๋ช
# *์ ์ ์ฒด๋ฅผ ๋ํ๋ธ๋ค.
where ์กฐ๊ฑด ์ค์ #์กฐ๊ฑด์ ๋ง๋ ๋ฐ์ดํฐ ์ถ๋ ฅ
group by ์ปฌ๋ผ๋ช
#๊ณตํต๋ ๋ฐ์ดํฐ๋ฅผ ๋ฌถ์ด์ค๋ค.
order by ์ปฌ๋ผ๋ช
desc #๋ฐ์ดํฐ ์ ๋ ฌ desc๋ ๋ด๋ฆผ์ฐจ์ ์ ๋ ฌ.
ํ ์ด๋ธ ์ด๋ฆ ๋ณ๊ฒฝ :
alter table ๊ธฐ์กดํ
์ด๋ธ๋ช
rename ๋ฐ๊ฟํ
์ด๋ธ๋ช
;
ํ๋ ์ถ๊ฐ :
alter table ํ
์ด๋ธ๋ช
add ํ๋๋ช
ํ๋ํ์
;
ํ๋ ์ญ์ :
alter table ํ
์ด๋ธ๋ช
drop ํ๋๋ช
;
์กฐ์ธ :
select ์ปฌ๋ผ๋ช
from ํ
์ด๋ธ1 join ํ
์ด๋ธ2 on ํ
์ด๋ธ1.์ปฌ๋ผ = ํ
์ด๋ธ2.์ปฌ๋ผ where ์กฐ๊ฑด
//์์
select r.email,h.hotel_num,h.hotel_title,r.check_in,r.check_out
from hotel_inform h
join reserve_inform r
on r.hotel_num = h.hotel_num
where email="recorder@gmail.com";
'๐ป IT > DB' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
์ผํ๋ฉด์ ๋ฐฐ์ด MYSQL ++๊ณ์ ์ถ๊ฐ (0) | 2024.05.16 |
---|---|
๋ฐ์ดํฐ ๋ชจ๋ธ๋ง (0) | 2024.02.26 |
ํธ๋์ญ์ (Transaction) (0) | 2024.01.22 |