user-img
๐Ÿ‘ฉ‍๐Ÿ’ปํ”„๋กœ๊ทธ๋ž˜๋ฐ&DB/mysql 17
๋ฆฌ๋ˆ…์Šค ํ™˜๊ฒฝ์—์„œ mysqlํ•˜๊ธฐ ํŒŒ์ผ
๋ณดํ˜ธ๋˜์–ด ์žˆ๋Š” ๊ธ€์ž…๋‹ˆ๋‹ค.
2023.12.17
๐Ÿ‘ฉ‍๐Ÿ’ปํ”„๋กœ๊ทธ๋ž˜๋ฐ&DB/mysql
mysql๋กœ ๊ตฌํ˜„ํ•œ ๊ฐœ์ธ ๊ณผ์ œ
๋ณดํ˜ธ๋˜์–ด ์žˆ๋Š” ๊ธ€์ž…๋‹ˆ๋‹ค.
2023.12.07
๐Ÿ‘ฉ‍๐Ÿ’ปํ”„๋กœ๊ทธ๋ž˜๋ฐ&DB/mysql
[MySQL] C ํŒŒ์ผ๊ณผ ์—ฐ๋™ํ•ด ์‹คํ–‰์‹œํ‚ค๊ธฐ
๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ๋ฐ ํ—ค๋”ํŒŒ์ผ ๋ณด๊ธฐ mysql_config --libs include์˜ ํ—ค๋” ํŒŒ์ผ๋“ค, ์ปดํŒŒ์ผ๋Ÿฌ ํ”Œ๋ž˜๊ทธ ๋ฐ ์ •์˜๊ฐ’ ํ™•์ธ mysql_config --cflags C ์ปดํŒŒ์ผํ•˜๊ธฐ ์œ„ํ•ด ํ—ค๋” ์ง€์ • gcc dbconn.c –o dbconn $(mysql_config –-cflags) $(mysql_config --libs) ์ ‘์†์„ ์œ„ํ•œ ์ •๋ณด ์ฝ”๋“œ #include #include #include #include int main() { //MYSQL ๋ณ€์ˆ˜ ์ƒ์„ฑ MYSQL mysql; //MYSQL ๋ณ€์ˆ˜ ์ดˆ๊ธฐํ™” mysql_init(&mysql); //MYSQL์— ์ ‘์†: ์„ฑ๊ณตํ•˜๋ฉด 1 ๋ฆฌํ„ด, ์‹คํŒจํ•˜๋ฉด 0 ๋ฆฌํ„ด if(!mysql_real_connect(&mysql,"localhost", user, password, ..
2023.12.07
๐Ÿ‘ฉ‍๐Ÿ’ปํ”„๋กœ๊ทธ๋ž˜๋ฐ&DB/mysql
mysql ๋ทฐ์™€ ์ธ๋ฑ์Šค ์‚ฌ์šฉ๋ฒ•
๋ทฐ ์ž์ฃผ ์‚ฌ์šฉํ•˜๋Š” ํ˜•ํƒœ์˜ ํ…Œ์ด๋ธ”์„ ๊ฐ€์ƒ ํ…Œ์ด๋ธ”๋กœ ๊ตฌ์„ฑํ•ด์„œ ์‚ฌ์šฉํ•˜๊ธฐ ํŽธํ•˜๊ฒŒ ํ•จ! create view cbOrders as select cname, title, odate from cust, orders, book where cust.cid = orders.cid and book.bid = orders.bid; ์ด๋Ÿฐ ์‹์œผ๋กœ ์ˆ˜์ • alter view vw_anyang(cid, cname, addr) as select cid, cname, addr from cust where addr = 'seoul'; as select์—์„œ ๋ฐ์ดํ„ฐ๋ฅผ ๋ณ€๊ฒฝํ•  ๋•Œ๋งŒ ์‚ฌ์šฉ ๊ฐ€๋Šฅ(๋ฐ์ดํ„ฐ๋งŒ ๊ฐ€๋Šฅ!!) ๋ทฐ ์‚ญ์ œํ•˜๊ธฐ drop view ์ด๋ฆ„; ์ธ๋ฑ์Šค B-Tree(์ด์ง„ํŠธ๋ฆฌ)๋กœ ๊ตฌ์„ฑ๋จ. ์ฐพ๊ธฐ ์‰ฝ๊ฒŒ ํ•˜๊ธฐ ์œ„ํ•ด ์ƒ์„ฑํ•จ! create index ์ธ..
2023.12.07
๐Ÿ‘ฉ‍๐Ÿ’ปํ”„๋กœ๊ทธ๋ž˜๋ฐ&DB/mysql
mysql ์ข…ํ•ฉ ๋ถ€์†์งˆ์˜ ์˜ˆ์ œ
1. ๋ชจ๋“  ์ฃผ๋ฌธ ์ •๋ณด๋ฅผ ๋ณด์ด์„ธ์š”(oid,odate,title) - ์Šค์นผ๋ผ ๋ถ€์†์งˆ์˜ select oid, odate, (select title from book where book.bid = orders.bid) as title from orders; 2. ๋ชจ๋“  ์ฃผ๋ฌธ ์ •๋ณด๋ฅผ ์ฑ…๋ณ„๋กœ ๋ณด์ด์„ธ์š”(bid,title,๊ฑด์ˆ˜) - ์Šค์นผ๋ผ ๋ถ€์†์งˆ์˜ select bid, title, (select count(*) from orders where book.bid = orders.bid) as ๊ฑด์ˆ˜ from book; 3. mac ์ถœํŒ์‚ฌ์—์„œ ์ถœ๊ฐ„ํ•œ ์ฑ…์˜ ์ฃผ๋ฌธ์ •๋ณด๋ฅผ ๋ณด์ด์„ธ์š”(title,oid,odate) - ์Šค์นผ๋ผ ๋ถ€์†์งˆ์˜+ ์ค‘์ฒฉ์งˆ์˜ select (select title from book where book.bid = orde..
2023.12.06
๐Ÿ‘ฉ‍๐Ÿ’ปํ”„๋กœ๊ทธ๋ž˜๋ฐ&DB/mysql
mysql ๋ถ€์†์งˆ์˜ (select, from, where)
์Šค์นผ๋ผ ๋ถ€์†์งˆ์˜ - select ๋ถ€์†์งˆ์˜ ๋‹จ์ผ๊ฐ’์„ ๋ฐ˜ํ™˜ ex) ๊ณ ๊ฐ๋ณ„ ํŒ๋งค์•ก์„ ๋ณด์ด์‹œ์˜ค. ๋‹จ, ๊ฒฐ๊ณผ๋Š” ๊ณ ๊ฐ์ด๋ฆ„๊ณผ ๊ณ ๊ฐ๋ณ„ ํŒ๋งค์•ก์„ ์ถœ๋ ฅํ•œ๋‹ค. select (select cname from cust where cust.cid = orders.cid) as cname, sum((select price from book where book.bid = orders.bid)) as tot_price from orders group by cid; ๋จผ์ € ๊ตฌ๋งคํ•œ cust๋ฅผ ๋ถ€์†์งˆ์˜๋กœ ์ฐพ์Œ, ์ดํ›„ sum ์•ˆ์—์„œ ๋ถ€์†์งˆ์˜๋กœ ์ฑ… ๊ฐ€๊ฒฉ์„ ์ฐพ๊ณ  sum์œผ๋กœ ํ•ฉ์‚ฐ sum์—์„œ ๋˜ ๋ถ€์†์งˆ์˜๋ผ ๊ด„ํ˜ธ 2๋ฒˆ์ธ ๋“ฏ/?? ์‚ฌ์šฉํ•˜๋Š” ์ด์œ : cname์„ ์ถœ๋ ฅํ•˜๊ณ  ์‹ถ์€๋ฐ orders์—๋Š” cname์ด ์กด์žฌํ•˜์ง€ ์•Š์Œ, price์˜ ํ•ฉ๊ณ„๋ฅผ ์ถœ๋ ฅํ•˜๊ณ  ์‹ถ์€๋ฐ..
2023.12.06
๐Ÿ‘ฉ‍๐Ÿ’ปํ”„๋กœ๊ทธ๋ž˜๋ฐ&DB/mysql
thumbnail
mysql ๊ธฐ์ดˆ ๋ช…๋ น ์˜ˆ์ œ (์ด์ •๋ฆฌ)
1. ์œ„์™€ ๊ฐ™์€ releation schema๋ฅผ ๊ฐ–๋Š” cust, book, orders ํ…Œ์ด๋ธ”์„ ์ƒ์„ฑํ•˜๋ผ. create table cust{ cid int primary key, name varchar(20), addr varchar(20), hp varchar(20)}; create table book{ bid int primary key, title varchar(20), publisher varchar(20), price int}; create table orders{##ํŠœํ”Œ ์ž…๋ ฅํ•˜๊ณ  ์ƒ์„ฑํ•ด์•ผ ํ•จ oid int primary key, cid int, bid int, odate varchar(20), foreign key(cid) references cust(cid), foreign key(bid) ..
2023.12.06
๐Ÿ‘ฉ‍๐Ÿ’ปํ”„๋กœ๊ทธ๋ž˜๋ฐ&DB/mysql
mysql ๋‚ด์žฅ ํ•จ์ˆ˜ (์ˆ˜ํ•™, ๋ฌธ์ž, ๋‚ ์งœ, NULL)
์ˆ˜ํ•™ ํ•จ์ˆ˜ select abs(์ˆซ์ž) ##์ ˆ๋Œ“๊ฐ’ ๊ตฌํ•˜๊ธฐ. select round(์†Œ์ˆ˜, ๋ช‡์งธ์ž๋ฆฌ) ##๋ฐ˜์˜ฌ๋ฆผ ๊ตฌํ•˜๊ธฐ ๋ฌธ์ž ํ•จ์ˆ˜ select replace(์†์„ฑ, '์ด๊ฑธ', '์ด๋ ‡๊ฒŒ') ##๋ฐ”๊ฟ”์„œ ๋ณด์—ฌ๋‹ฌ๋ผ๋Š” ๊ฑฐ์ž„ select length(์†์„ฑ) ##๊ธ€์ž์ˆ˜ ๋ณด๊ธฐ select substring(์†์„ฑ, x์ถ•, y์ถ•) ##์ด ๊ธธ์ด๋งŒํผ์˜ ๋ฌธ์ž์—ด ๋ฐ˜ํ™˜ ๋‚ ์งœ ํ•จ์ˆ˜ select now(): ํ˜„์žฌ ์‹œ๊ฐ„ day(now())←์ด๋Ÿฐ ํ˜•์‹์œผ๋กœ๋„ ์‚ฌ์šฉ ๊ฐ€๋Šฅ select adddate(์›๋ž˜ ์‹œ๊ฐ„ ์†์„ฑ, ์ˆซ์ž): ์ˆซ์ž ๋งŒํผ ๋‚ ์งœ ์ง€๋‚œ ๊ฑฐ ๋ณด์—ฌ์คŒ select now() ##ํ˜„์žฌ ์‹œ๊ฐ„ day(now())←์ด๋Ÿฐ ํ˜•์‹์œผ๋กœ๋„ ์‚ฌ์šฉ ๊ฐ€๋Šฅ select adddate(์›๋ž˜ ์‹œ๊ฐ„ ์†์„ฑ, ์ˆซ์ž) ##์ˆซ์ž ๋งŒํผ ๋‚ ์งœ ์ง€๋‚œ ๊ฑฐ ๋ณด์—ฌ์คŒ NULL๊ฐ’ ์ฒ˜๋ฆฌ ์—ฐ์‚ฐ..
2023.12.06
๐Ÿ‘ฉ‍๐Ÿ’ปํ”„๋กœ๊ทธ๋ž˜๋ฐ&DB/mysql
mysql ๋ฐ์ดํ„ฐ ์กฐ์ž‘์–ด (insert, update, delete)
Insert๋ฌธ insert into ํ…Œ์ด๋ธ”๋ช…(์†์„ฑ๋ฆฌ์ŠคํŠธ) values(์†์„ฑ๋‚ด์šฉ, ์†์„ฑ๋‚ด์šฉ....) ๋Œ€๋Ÿ‰ ์‚ฝ์ž… insert into ํ…Œ์ด๋ธ”๋ช…(์†์„ฑ๋ฆฌ์ŠคํŠธ) select ์†์„ฑ๋ช…, ์†์„ฑ๋ช… ... from ๊ฐ€์ ธ์˜ค๋Š” ํ…Œ์ด๋ธ”๋ช…; Update๋ฌธ update ํ…Œ์ด๋ธ”๋ช… set ์†์„ฑ์ด๋ฆ„=๊ฐ’ ##์ด๊ฑธ๋กœ ๋ฐ”๊พผ๋‹ค๋Š” ๋œป(๋ถ€์†์งˆ์˜ ๊ฐ€๋Šฅ) where ์กฐ๊ฑด; Delete๋ฌธ delete from ํ…Œ์ด๋ธ”๋ช… where ์กฐ๊ฑด;
2023.12.06
๐Ÿ‘ฉ‍๐Ÿ’ปํ”„๋กœ๊ทธ๋ž˜๋ฐ&DB/mysql
mysql ๋ฐ์ดํ„ฐ ์ •์˜์–ด (create, alter, drop)
create ๋ฌธ create table ํ…Œ์ด๋ธ”์ด๋ฆ„( ์†์„ฑ ์œ ํ˜• ์ œ์•ฝ์กฐ๊ฑด, ... primary key( , )); ํ”„๋ผ์ด๋จธ๋ฆฌ ๊ธฐ๋ณธํ‚ค ์ œ์•ฝ์กฐ๊ฑด์— ๋„ฃ์–ด๋„ ๋˜๊ณ , ์—ฌ๋Ÿฌ๊ฐœ ์„ค์ •ํ•  ๊ฑฐ๋ฉด ์œ„์™€ ๊ฐ™์ด ๋งจ ์•„๋ž˜์— ์ž‘์„ฑ. ์ œ์•ฝ์กฐ๊ฑด์€ (not null, unique, default 10000) ํ…Œ์ด๋ธ”์— ์†์„ฑ ์ถ”๊ฐ€ํ•˜๊ธฐ alter table newbook add; ์†์„ฑ๋ช… ์œ ํ˜• ์ œ์•ฝ์กฐ๊ฑด; ํ…Œ์ด๋ธ” ๊ตฌ์กฐ ์ƒ์„ธํ•˜๊ฒŒ ๋ณด๊ธฐ show create table ํ…Œ์ด๋ธ”๋ช…; ํ…Œ์ด๋ธ” ๋ณต์‚ฌ create table ์ƒˆํ…Œ์ด๋ธ”๋ช… select * from ์›๋ณธํ…Œ์ด๋ธ”๋ช…;##๋‚ด์šฉ ๋ณต์‚ฌ(ํ”„๋ผ์ด๋จธ๋ฆฌ ๊ธฐ๋ณธํ‚ค ์‚ฌ๋ผ์ง) create table ์ƒˆํ…Œ์ด๋ธ”๋ช… like ์›๋ณธํ…Œ์ด๋ธ”๋ช…;##์Šคํ‚ค๋งˆ๋งŒ ๋ณต์‚ฌ๋จ(ํ”„๋ผ์ด๋จธ๋ฆฌ ๊ธฐ๋ณธํ‚ค ๋‚จ์•„์žˆ์Œ) create table ํ…Œ์ด๋ธ”์ด๋ฆ„( ์†์„ฑ ์œ ํ˜•..
2023.12.06
๐Ÿ‘ฉ‍๐Ÿ’ปํ”„๋กœ๊ทธ๋ž˜๋ฐ&DB/mysql
thumbnail
ERROR 1052 (23000): Column 'cid' in field list is ambiguous
์•„๋‹ˆ ์ž˜ ํ•œ ๊ฒƒ ๊ฐ™์€๋ฐ ์™œ ๊ทธ๋Ÿฌ์ง€;; ํ–ˆ๋Š”๋ฐ select์—์„œ cid๊ฐ€ ๋‘ ํ…Œ์ด๋ธ” ํ•จ๊ป˜ ์žˆ๋Š” ๊ฑด๋ฐ ์–ด๋Š ์ชฝ ๊ฑธ ์„ ์–ธํ•œ ๊ฑด์ง€ ๋ชจ๋ฅด๋‹ˆ๊นŒ ์ง€์ •ํ•ด์ค˜์•ผํ•จ!! cust.cid๋กœ cust ํ…Œ์ด๋ธ”์„ ์ง€์ •ํ•ด์ฃผ๋‹ˆ ์™„๋ฃŒ~~
2023.12.06
๐Ÿ‘ฉ‍๐Ÿ’ปํ”„๋กœ๊ทธ๋ž˜๋ฐ&DB/mysql
mysql ๋ถ€์†์งˆ์˜ (where์ ˆ ์ค‘์ฒฉ, ์ƒ๊ด€, exists)
๋ถ€์†์งˆ์˜ where ์ ˆ์— ๋‹ค์‹œ ์ค‘์ฒฉํ•ด ์ž‘์„ฑ(3~4๋ฒˆ๊นŒ์ง€ ์ถ”์ฒœ) ์ตœ์ข…์ ์œผ๋กœ ๋ณด์ด๊ณ  ์‹ถ์€ ๊ฒƒ์„ ๋งจ ์•ž์— ์ž‘์„ฑํ•˜๊ณ , ๋“ค์–ด๊ฐ€๊ธฐ, ๋“ค์–ด๊ฐ€๊ธฐ ํ•ด์„œ ๊ฐ€์žฅ ์ฒซ ์กฐ๊ฑด์ด ๊ด„ํ˜ธ ๊ฐ€์žฅ ์•ˆ์— ์žˆ๋„๋ก. select bookname from book where price = ( select max(price) from book); ์ƒ๊ด€ ๋ถ€์†์งˆ์˜ ๋™์ผํ•œ ํ…Œ์ด๋ธ”์— b1, b2์˜ ์ด๋ฆ„์„ ์ค˜์„œ ๋น„๊ต. select b1.title from book b1 where b1.price > (select avg(b2.price) from book b2 where b1.publisher = b2.publisher); exists ์กด์žฌํ•จ? ๋ชจ๋‘ ์•ˆ ํ•จ?(not exists) where exists (select * from orders where c..
2023.12.06
๐Ÿ‘ฉ‍๐Ÿ’ปํ”„๋กœ๊ทธ๋ž˜๋ฐ&DB/mysql