user-img
๐Ÿ‘ฉ‍๐Ÿ’ปํ”„๋กœ๊ทธ๋ž˜๋ฐ&DB 52
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
[Ubuntu] mysql ๋ฐ์ดํ„ฐ ๊ฒ€์ƒ‰, ์ง‘๊ณ„, ์กฐ์ธ
select๋ฌธ select ์†์„ฑ, ์†์„ฑ... from ํ…Œ์ด๋ธ”๋ช… where ๊ฒ€์ƒ‰ ์กฐ๊ฑด(>= / between(๋ฒ”์œ„) / in, not in(10000, 20000) / like '์–ด์ฉŒ๊ตฌ' / is null, is not null / and, or, not) group by ์†์„ฑ๋ช… having ๊ฒ€์ƒ‰์กฐ๊ฑด order by ์†์„ฑ๋ช… asc/desc (์˜ค๋ฆ„์ฐจ์ˆœ/๋‚ด๋ฆผ์ฐจ์ˆœ) ์‚ฌ์šฉ ์•ˆ ํ•œ๋‹ค๋ฉด ๊ทธ๋ƒฅ ์†์„ฑ๋ช… ์ˆœ์œผ๋กœ ๋‚˜์˜ด. ๊ฒ€์ƒ‰ ์กฐ๊ฑด์€ ๊ด„ํ˜ธ์—์„œ์™€ ๊ฐ™์ด ์ด๋Ÿฐ ์‹์œผ๋กœ ์ž‘์„ฑ ๊ฐ€๋Šฅ where price between 1000 and 2000 price >= 1 and price
2023.12.04
๐Ÿ‘ฉ‍๐Ÿ’ปํ”„๋กœ๊ทธ๋ž˜๋ฐ&DB/mysql
thumbnail
[Ubuntu] mysql ์กฐ์ธ(์ด๋„ˆ/์•„์šฐํ„ฐ) ์˜ˆ์ œ
1. ์ฑ…์„ ์ฃผ๋ฌธํ•œ ๊ณ ๊ฐ์ •๋ณด์™€ ์ฃผ๋ฌธ์ •๋ณด select * from cust, orders where cust.cid = orders.cid; 2. ์ฃผ๋ฌธ๋œ ์ฑ…์˜ ์ •๋ณด์™€ ์ฃผ๋ฌธ์ •๋ณด select * from book, orders whrer book.bid = orders.bid; 3. ์ฃผ๋ฌธ์ •๋ณด๋ฅผ ๋ณด์ด๊ธฐ(oid, cname, title, price) select oid, cname, title, price from cust, orders, book where cust.cid = orders.cid and book.bid = orders.bid; 4. ๊ณ ๊ฐ์˜ ์ฃผ๋ฌธ์ •๋ณด ๋ณด์ด๊ธฐ ๋‹จ, ์ด๋ฆ„์ˆœ์œผ๋กœ ๋‚ด๋ฆผ์ฐจ์ˆœํ•ด์„œ(oid, cname) select oid, cname from cust inner join orders on cus..
2023.12.04
๐Ÿ‘ฉ‍๐Ÿ’ปํ”„๋กœ๊ทธ๋ž˜๋ฐ&DB/mysql
[Ubuntu] ์šฐ๋ถ„ํˆฌ ๊ธฐ์ค€ mysql ๊ธฐ๋ณธ ๋ช…๋ น
๊ธฐ๋ณธ ๋ช…๋ น์— ๊ด€ํ•ด์„œ ์ •๋ฆฌ mysql -p //์‹คํ–‰, ํ›„ ๋น„๋ฒˆ ์ž…๋ ฅ(์„ค์ • ์•ˆํ–ˆ๋‹ค๋ฉด ๊ทธ๋ƒฅ ์—”ํ„ฐ) \q //์ข…๋ฃŒ use ๋””๋น„๋ช…; //๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ์„ ํƒ show databases; //๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ๋ณด๊ธฐ create database ๋””๋น„๋ช…; //๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ์ƒ์„ฑ drop database ๋””๋น„๋ช…; //๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ์‚ญ์ œ create table ํ…Œ์ด๋ธ”๋ช…( //ํ…Œ์ด๋ธ” ์ƒ์„ฑ ์ด๋ฆ„ int primary key, ์ด๋ฆ„ varchar(20), ์ด๋ฆ„ int); show tables; //ํ…Œ์ด๋ธ” ๋ณด๊ธฐ select * from ํ…Œ์ด๋ธ”๋ช…; //ํ…Œ์ด๋ธ”์˜ ๋ชจ๋“  ๊ฒƒ ๋ณด๊ธฐ(*์— ๋ณด๊ณ  ์‹ถ์€ ๊ฑธ ์ž…๋ ฅํ•˜๋ฉด ๊ทธ๊ฒƒ์ด ๋œจ๊ฒ ์ฃ . show create table ํ…Œ์ด๋ธ”๋ช…; //ํ…Œ์ด๋ธ” ์กฐ๊ฑด ๋ณด๊ธฐ explain ํ…Œ์ด๋ธ”๋ช…; //ํ…Œ์ด๋ธ” ๊ตฌ์กฐ ๋ณด๊ธฐ set fore..
2023.12.04
๐Ÿ‘ฉ‍๐Ÿ’ปํ”„๋กœ๊ทธ๋ž˜๋ฐ&DB/mysql
thumbnail
[Ubuntu] ์šฐ๋ถ„ํˆฌ mysql ๊ธฐ๋ณธ ์ดˆ๊ธฐ ์„ค์ •
์ผ๋‹จ ์‹คํ–‰ํ•ด์„œ su๋กœ ๋ฃจํŠธ ๊ณ„์ •์œผ๋กœ ๋ณ€ํ™˜ํ•ฉ์‹œ๋‹ค(๋น„๋ฒˆ ๊ธฐ์–ต ์•ˆ ๋‚œ๋‹ค๋ฉด sudo passwd root ํ•ด์„œ ๋น„๋ฒˆ ์„ค์ •ํ•˜์‹ญ์‹œ์˜ค) mysql ์„ค์น˜ sudo apt-get install mysql-server ํ•œ์ฐธ ๊ธฐ๋‹ค๋ฆฌ๋ฉด ์„ค์น˜๊ฐ€ ๋‹ค ๋ฉ๋‹ˆ๋‹ค ์‹คํ–‰ํ•˜๋Š” ๋ฒ• mysql -p ๋น„๋ฒˆ ์ž…๋ ฅํ•˜๋ผ๋Š”๋ฐ ์ฒ˜์Œ ์‹คํ–‰ํ–ˆ์œผ๋ฉด ๋น„๋ฒˆ์ด ์—†๊ธฐ์— ๊ทธ๋ƒฅ ์—”ํ„ฐ์น˜๋ฉด ๋ฉ๋‹ˆ๋‹ค ์ผ์ผ€ ์‹คํ–‰๋๋‹ค๋ฉด ๋น„๋ฐ€๋ฒˆํ˜ธ๋ฅผ ์„ค์ •ํ•ด๋ด…์‹œ๋‹ค. alter user ์œ ์ €์ด๋ฆ„@localhost identified by ‘๋น„๋ฐ€๋ฒˆํ˜ธ์ž…๋ ฅ’; ์œ ์ €์ด๋ฆ„์€ root๋กœ ์ ‘์†ํ–ˆ๋‹ค๋ฉด root, ๋‹ค๋ฅธ ๊ณ„์ •์œผ๋กœ ์ ‘์†ํ–ˆ๋‹ค๋ฉด ๊ทธ ๊ณ„์ •๋ช… ์ ์–ด์ฃผ์‹ฌ ๋ฉ๋‹ˆ๋‹ค. ์ด๋Ÿฐ ์‹์œผ๋กœ ๊ธฐ๋ณธ ์‚ฌ์šฉ์„ ํ•ด๋ด…์‹œ๋‹ค create database ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค๋ช…;##๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ์ƒ์„ฑ create user ์œ ์ €๋ช…@localhost ident..
2023.12.04
๐Ÿ‘ฉ‍๐Ÿ’ปํ”„๋กœ๊ทธ๋ž˜๋ฐ&DB/mysql