์ผ | ์ | ํ | ์ | ๋ชฉ | ๊ธ | ํ |
---|---|---|---|---|---|---|
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 |
- Absolute
- AGI
- ai
- AI agents
- AI engineer
- AI researcher
- ajax
- algorithm
- Algorithms
- aliases
- Array ๊ฐ์ฒด
- ASI
- bayes' theorem
- Bit
- Blur
- BOM
- bootstrap
- canva
- challenges
- ChatGPT
- Today
- In Total
A Joyful AI Research Journey๐ณ๐
[54~55] 230316, 230317 SQL ๋ฌธ์ ํ์ด (member, board ํ ์ด๋ธ) [K-๋์งํธ ํธ๋ ์ด๋ 54~55์ผ] ๋ณธ๋ฌธ
[54~55] 230316, 230317 SQL ๋ฌธ์ ํ์ด (member, board ํ ์ด๋ธ) [K-๋์งํธ ํธ๋ ์ด๋ 54~55์ผ]
yjyuwisely 2023. 3. 17. 11:26230316 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
๊ธ ํด๋ฆญํ์ ๋ ์กฐํ์ ์ฆ๊ฐ
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์ผ๋ก ํ์๋๋ฐ ์ฝ๋ ์คํํ๋ฉด์ ์ง์์ก๋ค.