Notice
Recent Posts
Recent Comments
ยซ   2024/12   ยป
์ผ ์›” ํ™” ์ˆ˜ ๋ชฉ ๊ธˆ ํ† 
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30 31
Archives
Today
In Total
๊ด€๋ฆฌ ๋ฉ”๋‰ด

A Joyful AI Research Journey๐ŸŒณ๐Ÿ˜Š

[54~55] 230316, 230317 SQL ๋ฌธ์ œ ํ’€์ด (member, board ํ…Œ์ด๋ธ”) [K-๋””์ง€ํ„ธ ํŠธ๋ ˆ์ด๋‹ 54~55์ผ] ๋ณธ๋ฌธ

๐ŸŒณBootcamp Revision 2023โœจ/Python, Flask, SQL

[54~55] 230316, 230317 SQL ๋ฌธ์ œ ํ’€์ด (member, board ํ…Œ์ด๋ธ”) [K-๋””์ง€ํ„ธ ํŠธ๋ ˆ์ด๋‹ 54~55์ผ]

yjyuwisely 2023. 3. 17. 11:26

230316 Thu 54th class
230317 Fri 55th class


๋ฌธ์ œ ํ’€์ด)

1. naver ์‚ฌ์ดํŠธ์˜ ํšŒ์›๊ฐ€์ž… ํ™”๋ฉด์„ ๋ณด๊ณ , member ํ…Œ์ด๋ธ”์„ ์ƒ์„ฑํ•ด๋ณด์ž

use example; 
create table member (
	id varchar(20) primary key,
    password varchar(30) not null,
    name varchar(10) not null,
    birthday date not null, -- date ํƒ€์ž…์œผ๋กœ ํ•˜๋ฉด 2/30์„ ๋ฑ‰์–ด๋‚ธ๋‹ค.
    gender varchar(5) not null,
    email varchar(10),
    phone varchar(15)
);
desc member;

ALTER TABLE member
MODIFY COLUMN email varchar(30);

insert into member
values('abcd','1234','์ •์ž๋ฐ”','2023-03-17','๋‚จ','abcd@naver.com','01011112222');

insert into member
values('efgh','1234','๊น€์ž๋ฐ”','2023-03-17','์—ฌ','efgh@naver.com','01012345678');

select * from member;

์ฐธ๊ณ : https://reeme.tistory.com/28

<input type="text" id="year">
<select id="month">
	<option value="1">1</option>
	<option value="2">2</option>
    <option value="3">3</option>
</select>
<input type="text id="day">



jquery
var year=$("#year").val(); /*๋…„๋„๊ฐ’*/
var month=$("#month").val(); /*์›”๊ฐ’*/
var day=$("#day").val(); /*์ผ๊ฐ’*/

var birthday = concat(year, month, day) /*3๊ฐœ์˜ ๋ฐ์ดํ„ฐ๋ฅผ ํ•ฉ์ณ์ค€ ํ›„์— DB์— ๋ณด๋‚ธ๋‹ค*/

HTML, Javascript์˜ JQuery ์ด์šฉ์œผ๋กœ ๋ฌถ์–ด์„œ ๋ณด๋‚ธ๋‹ค. (ํฐ ๋ฒˆํ˜ธ๋„ ๋งˆ์ฐฌ๊ฐ€์ง€๋‹ค.) 
DB์—์„œ ์—ฌ๋Ÿฌ ๊ฐœ ๋งŒ๋“ค ํ•„์š”๊ฐ€ ์—†๋‹ค.


ALTER TABLE - ALTER/MODIFY DATATYPE

To change the data type of a column in a table, use the following syntax:

My SQL / Oracle (prior version 10G):

ALTER TABLE table_name
MODIFY COLUMN column_name datatype;

์ฐธ๊ณ : https://www.w3schools.com/sql/sql_alter.asp


The Data Access Object (or DAO) pattern: separates a data resource's client interface from its data access mechanisms. adapts a specific data resource's access API to a generic client interface.

์ฐธ๊ณ : https://blog.naver.com/jjk123456789/222294651268


2. naver ์‚ฌ์ดํŠธ์˜ ๊ฒŒ์‹œํŒ ํ™”๋ฉด์„ ๋ณด๊ณ , board ํ…Œ์ด๋ธ”์„ ์ƒ์„ฑํ•ด๋ณด์ž

-- ๊ฒŒ์‹œํŒ
create table board(
	no int primary key auto_increment COMMENT '๋ฒˆํ˜ธ', -- ์ž๋™์œผ๋กœ 1์”ฉ ์ฆ๊ฐ€์‹œํ‚ค๋Š” ์—ด์„ ๋งŒ๋“ ๋‹ค. (insert์— ์–ธ๊ธ‰ํ•  ํ•„์š”X)
    title varchar(10) not null COMMENT '์ œ๋ชฉ',
    content varchar(20) COMMENT '๋‚ด์šฉ',
    id varchar(10) COMMENT '์ž‘์„ฑ์ž', -- ์ƒํ™ฉ์— ๋”ฐ๋ผ์„œ๋Š” ์™ธ๋ž˜ํ‚ค(Foreign Key)๋ฅผ ์„ค์ •ํ•ด์•ผ ํ•˜๋Š” ๊ฒฝ์šฐ๋„ ์žˆ๋‹ค.
    regdate datetime default now() COMMENT '์ž‘์„ฑ์ผ', 
    -- default now()๊ฐ€ ์—†์œผ๋ฉด ํ˜„์žฌ ๋‚ ์งœ๋ฅผ insertํ•  ๋•Œ ์ถ”๊ฐ€ํ•ด๋„ ๋œ๋‹ค. INSERT INTO board(regdate) VALUES(now());
    count int COMMENT '์กฐํšŒ์ˆ˜', -- insert๊ฐ€ ์•„๋‹Œ update์ด๋‹ค. 
    good int COMMENT '์ข‹์•„์š”์ˆ˜', -- insert๊ฐ€ ์•„๋‹Œ update์ด๋‹ค. 
    -- member ํ…Œ์ด๋ธ”์˜ id์—ด์ด ๋ถ€๋ชจ์—ด, boardํ…Œ์ด๋ธ”์˜ id์—ด์ด ์ž์‹์—ด 
    foreign key(id) references member(id)
);
-- ๋งŒ์•ฝ ์™ธ๋ž˜ํ‚ค ์ถ”๊ฐ€ ์•ˆ ํ–ˆ์„ ๊ฒฝ์šฐ
-- alter table board add constraint foreign key(id) references member(id);
desc board;

insert into board (title, content) 
values ('์•ˆ๋…•ํ•˜์„ธ์š”', '์˜ค๋Š˜ ์ •๋ชจ ์žˆ์Šต๋‹ˆ๋‹ค.','bbb'
);

select * from board;

alter table board add constraint foreign key(id) references member(id); -- ๋ถ€๋ชจํ…Œ์ด๋ธ”(๋ถ€๋ชจ์—ด) 
-- ๋ถ€๋ชจ์—†๋Š” ์ž์‹์€ ์—†๋‹ค. 

insert into member
values('abcd','1234','์ •์ž๋ฐ”','2023-03-17','๋‚จ','abcd@naver.com','01011112222');
--		๋ถ€๋ชจ์—ด

insert into member
values('efgh','1234','๊น€์ž๋ฐ”','2023-03-17','์—ฌ','efgh@naver.com','01012345678');
--		๋ถ€๋ชจ์—ด

insert into board (title, content, id) 
values ('์•ˆ๋…•ํ•˜์„ธ์š”', '์˜ค๋Š˜ ์ •๋ชจ ์žˆ์Šต๋‹ˆ๋‹ค.','efgh');
--					์ž์‹์—ด

๋กœ๊ทธ์ธ์„ ํ•ด์•ผ ๊ฒŒ์‹œํŒ์— ๊ธ€์„ ์“ธ ์ˆ˜ ์žˆ๋‹ค. (์™ธ๋ž˜ํ‚ค์˜ ์—ญํ• ) 

์™ธ๋ž˜ํ‚ค 
๋ถ€๋ชจ ํ…Œ์ด๋ธ”์˜ id๊ฐ€ ๊ธฐ๋ณธํ‚ค์—ฌ์•ผํ•œ๋‹ค. 

์ „์ฒด ์ฝ”๋“œ)

-- 230317 FRI ์ˆ˜์—…
-- ### ์ •๋‹ต: ํšŒ์›๊ฐ€์ž… ###
use sample; 
create table member (
	id varchar(20) primary key,
    password varchar(30) not null,
    name varchar(10) not null,
    birthday date not null, -- date ํƒ€์ž…์œผ๋กœ ํ•˜๋ฉด 2์›” 30์ผ์„ ๋ฑ‰์–ด๋‚ธ๋‹ค.
    gender varchar(5) not null,
    email varchar(10),
    phone varchar(15)
);
desc member;

ALTER TABLE member
MODIFY COLUMN email varchar(30);

insert into member
values('abcd','1234','์ •์ž๋ฐ”','2023-03-17','๋‚จ','abcd@naver.com','01011112222');

insert into member
values('efgh','1234','๊น€์ž๋ฐ”','2023-03-17','์—ฌ','efgh@naver.com','01012345678');

insert into member
values('abcd1234','1234','์ตœ์ž๋ฐ”','2023-03-17','๋‚จ','ijkh@naver.com','01033334444');

select * from member;

-- ### ์ •๋‹ต: ๊ฒŒ์‹œํŒ ###
create table board(
	no int primary key auto_increment COMMENT '๋ฒˆํ˜ธ', -- ์ž๋™์œผ๋กœ 1์”ฉ ์ฆ๊ฐ€์‹œํ‚ค๋Š” ์—ด์„ ๋งŒ๋“ ๋‹ค. (insert์— ์–ธ๊ธ‰ํ•  ํ•„์š”X)
    title varchar(10) not null COMMENT '์ œ๋ชฉ',
    content varchar(20) COMMENT '๋‚ด์šฉ',
    id varchar(10) COMMENT '์ž‘์„ฑ์ž', -- ์ƒํ™ฉ์— ๋”ฐ๋ผ์„œ๋Š” ์™ธ๋ž˜ํ‚ค(Foreign Key)๋ฅผ ์„ค์ •ํ•ด์•ผ ํ•˜๋Š” ๊ฒฝ์šฐ๋„ ์žˆ๋‹ค.
    regdate datetime default now() COMMENT '์ž‘์„ฑ์ผ', 
    -- default now()๊ฐ€ ์—†์œผ๋ฉด ํ˜„์žฌ ๋‚ ์งœ๋ฅผ insertํ•  ๋•Œ ์ถ”๊ฐ€ํ•ด๋„ ๋œ๋‹ค. INSERT INTO board(regdate) VALUES(now());
    count int COMMENT '์กฐํšŒ์ˆ˜', -- insert๊ฐ€ ์•„๋‹Œ update์ด๋‹ค. 
    good int COMMENT '์ข‹์•„์š”์ˆ˜', -- insert๊ฐ€ ์•„๋‹Œ update์ด๋‹ค. 
    -- member ํ…Œ์ด๋ธ”์˜ id์—ด์ด ๋ถ€๋ชจ์—ด, boardํ…Œ์ด๋ธ”์˜ id์—ด์ด ์ž์‹์—ด 
    foreign key(id) references member(id)
);
-- ๋งŒ์•ฝ ์™ธ๋ž˜ํ‚ค ์ถ”๊ฐ€ ์•ˆ ํ–ˆ์„ ๊ฒฝ์šฐ
-- alter table board add constraint foreign key(id) references member(id); -- ๋ถ€๋ชจํ…Œ์ด๋ธ”(๋ถ€๋ชจ์—ด) 
-- ๋ถ€๋ชจ์—†๋Š” ์ž์‹์€ ์—†๋‹ค. 
desc board;

insert into board (title, content, id) 
values ('์•ˆ๋…•ํ•˜์„ธ์š”', '์˜ค๋Š˜ ์ •๋ชจ ์žˆ์Šต๋‹ˆ๋‹ค.','abcd');

insert into board (title, content) 
values ('์•ˆ๋…•ํ•˜์„ธ์š”', '์˜ค๋Š˜ ์ •๋ชจ ์žˆ์Šต๋‹ˆ๋‹ค.');

select * from board;

JSP ์„ธ์…˜(Session)์— id๋ฅผ ์ €์žฅํ•˜๊ณ  ์„ธ์…˜์—์„œ getํ•ด์„œ ๊ฐ€์ ธ์˜จ๋‹ค.

regdate์˜ Default๋Š” CURRENT_TIMESTAMP์ด๋‹ค.

If Key is MUL, the column is the first column of a nonunique index in which multiple occurrences of a given value are permitted within the column.
์ฐธ๊ณ :
https://torbjorn.tistory.com/557

์ฐธ๊ณ :
 https://m.blog.naver.com/PostView.naver?isHttpsRedirect=true&blogId=kim-stone&logNo=60194596843

์ฐธ๊ณ : https://tychejin.tistory.com/16

ํ…Œ์ด๋ธ”์— COMMENT ๋ถ€์—ฌ
์ฐธ๊ณ : https://jhnyang.tistory.com/309


์ฐธ๊ณ : https://develop-writing.tistory.com/15

 

Servlet, Jsp, Spring์€ ๋ฌด์—‡์ผ๊นŒ?? ๊ณต๋ถ€ ์ˆœ์„œ๋Š”??

๊ณต๋ถ€ ์ˆœ์„œ 1. Servlet ์ž๋ฐ”๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์›นํŽ˜์ด์ง€๋ฅผ ๋™์ ์œผ๋กœ ์ƒ์„ฑํ•˜๋Š” ์„œ๋ฒ„ ์ธก ํ”„๋กœ๊ทธ๋žจ์ด๋‹ค. -> ์„œ๋ธ”๋ฆฟ์ด๋ผ๋Š” ๊ธฐ์ˆ  ๋งŒ์œผ๋กœ๋„ ์ž๋ฐ” ์›น ํ”„๋กœ๊ทธ๋žจ์„ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋‹ค. ํ•˜์ง€๋งŒ ์„œ๋ธ”๋ฆฟ๋งŒ ์‚ฌ์šฉํ•  ๊ฒฝ์šฐ HTML

develop-writing.tistory.com


๊ธ€ ํด๋ฆญํ–ˆ์„ ๋•Œ ์กฐํšŒ์ˆ˜ ์ฆ๊ฐ€

use example;
alter table board modify count int default 0;
desc board;
select * from board where no = 5; -- title ํด๋ฆญํ•˜๋ฉด,
-- update board set count=0;
update board set count=count+1 where no=5;

start transaction;
select * from board where no = 5; -- title ํด๋ฆญํ•˜๋ฉด,
update board set count = count+1 where no=5;
-- ์ •์ƒ์ ์œผ๋กœ select๋ž‘ update๊ฐ€ ๋จ: ์ •์ƒ์ฒ˜๋ฆฌ ํ•ด์ค˜์•ผ ํ•จ.(commit) 
commit;
-- update,๋Š” ์ •์ƒ์ ์œผ๋กœ ์ˆ˜ํ–‰๋˜์—ˆ์œผ๋‚˜, select๊ฐ€ ์ œ๋Œ€๋กœ ์ž‘๋™ํ•˜์ง€ ์•Š์•˜์Œ.: count๊ฐ’์„ ์›๋ž˜๋Œ€๋กœ ๋˜๋Œ๋ ค ์ค˜์•ผ ํ•จ.
rollback;

์ฒ˜์Œ์— no = 3์œผ๋กœ ํ–ˆ์—ˆ๋Š”๋ฐ ์ฝ”๋“œ ์‹คํ–‰ํ•˜๋ฉด์„œ ์ง€์›Œ์กŒ๋‹ค.


728x90
๋ฐ˜์‘ํ˜•
Comments