๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๐Ÿ–ฅ๏ธ/DBMS

DB์„ค๊ณ„) ํ•™์ƒ ํ…Œ์ด๋ธ”๊ณผ ์„ฑ์  ํ…Œ์ด๋ธ” ์„ค๊ณ„ํ•˜๊ธฐ

by HanaV 2023. 4. 9.
728x90

1. ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ์„ค๊ณ„ํ•˜๊ธฐ

db ์ด๋ฆ„: student_db
1) ํ•™์ƒ ํ…Œ์ด๋ธ”

์—ด ์ด๋ฆ„ student_info ๋ฐ์ดํ„ฐํƒ€์ž… ์ตœ๋Œ€๊ธธ์ด null ํ—ˆ์šฉ ์—ฌ๋ถ€
์ด๋ฆ„  student_name char 5 x
ํ•™๋…„ student_grade tinyint 1 o
ํ•™๋ฒˆ (PK) student_number int 8 x
์„ฑ๋ณ„ student_gender char 6(Male/Female) x
์ƒ๋…„์›”์ผ student_birth date YYYY-MM-DD x
ํ•™๊ณผ์ฝ”๋“œ (FK) major_code smallint 3 x

student_grade๋Š” 1~4๊นŒ์ง€๋งŒ ํ‘œ๊ธฐ๋˜๋ฏ€๋กœ tinyint ํƒ€์ž…์„ ์„ ํƒํ–ˆ๊ณ , ํ•™๊ณผ์ฝ”๋“œ๋Š” ์„ธ ์ž๋ฆฌ ์ •์ˆ˜๋กœ ์„ค์ •ํ•ด์„œ smallint ํƒ€์ž…์„ ์„ ํƒํ•˜์˜€๋‹ค.
2) ๊ณผ๋ชฉ ํ…Œ์ด๋ธ”

์—ด ์ด๋ฆ„ subject_info ๋ฐ์ดํ„ฐํƒ€์ž… ์ตœ๋Œ€๊ธธ์ด null ํ—ˆ์šฉ ์—ฌ๋ถ€
ํ•™๊ณผ์ฝ”๋“œ (FK) (PK) major_code smallint 3 (111) x
๊ณผ๋ชฉ์ฝ”๋“œ (PK) subject_code int 5 (11111) x
๊ณผ๋ชฉ์ด๋ฆ„ subject_name char 20 x
๊ณผ๋ชฉํ•™์  subject_credit tinyint 1 x

๊ณผ๋ชฉ์€ ํ•™๊ณผ์ฝ”๋“œ-๊ณผ๋ชฉ์ฝ”๋“œ๋กœ ํ‘œ๊ธฐ๋˜๋ฏ€๋กœ ํ•™๊ณผ์ฝ”๋“œ์™€ ๊ณผ๋ชฉ์ฝ”๋“œ๋ฅผ ๊ฐ™์ด PK๋กœ  ์„ค์ •ํ•˜์˜€๋‹ค.
3) ํ•™๊ณผ ํ…Œ์ด๋ธ”

์—ด ์ด๋ฆ„ major_info ๋ฐ์ดํ„ฐํƒ€์ž… ์ตœ๋Œ€๊ธธ์ด null ํ—ˆ์šฉ์—ฌ๋ถ€
ํ•™๊ณผ์ฝ”๋“œ (PK) major_code smallint 3 x
ํ•™๊ณผ์ด๋ฆ„ major_name char 20 x

4) ์ˆ˜๊ฐ• ํ…Œ์ด๋ธ”

์—ด ์ด๋ฆ„ sign_info ๋ฐ์ดํ„ฐํƒ€์ž… ์ตœ๋Œ€๊ธธ์ด null ํ—ˆ์šฉ ์—ฌ๋ถ€
ํ•™๊ณผ์ฝ”๋“œ (FK) (PK) major_code smallint 3 x
๊ณผ๋ชฉ์ฝ”๋“œ (FK) (PK) subject_code int 5 x
ํ•™๋ฒˆ (FK) (PK) student_number int 8 x
์ˆ˜๊ฐ•ํ•™๊ธฐ sign_semester char 3 (4/1) x
๊ณผ๋ชฉ์„ฑ์  subject_score char 4 (PASS / FAIL ๋„ ํฌํ•จ) o

ํ•œ ๊ณผ๋ชฉ์€ ์—ฌ๋Ÿฌ ํ•™์ƒ์ด ๋“ค์œผ๋ฏ€๋กœ ํ•œ ๊ณผ๋ชฉ๋‹น ํ•œ ํ•™์ƒ์ด ๋“ฃ๋Š” ๊ฒƒ์„ PK๋กœ ์„ค์ •ํ•ด์•ผํ•  ๊ฒƒ ๊ฐ™์•„์„œ ํ•™๊ณผ์ฝ”๋“œ, ๊ณผ๋ชฉ์ฝ”๋“œ, ํ•™๋ฒˆ์„ PK๋กœ ์ง€์ •ํ•˜์˜€๋‹ค.
์ตœ์ข…์ ์œผ๋กœ ์ถœ๋ ฅํ•  ํ…Œ์ด๋ธ”์€ ํ•œ ํ•™์ƒ์˜ ์ „์ฒด  ์„ฑ์  ํ…Œ์ด๋ธ”์ด๋‹ค.

์„ฑ์ ํ…Œ์ด๋ธ”
ํ•™๊ณผ ์ด๋ฆ„ major_name
ํ•™๋ฒˆ student_number
์ด๋ฆ„ student_name
ํ•™๋…„ student_grade
๊ณผ๋ชฉ ์ด๋ฆ„ subject_name
๊ณผ๋ชฉ ์„ฑ์  subject_score
๊ณผ๋ชฉ ํ•™์  subject_credit

 

2. ํ…Œ์ด๋ธ” ๋งŒ๋“ค ์ˆœ์„œ ์ •ํ•˜๊ธฐ

FK๊ฐ€ ์—†๋Š” major_info ํ…Œ์ด๋ธ”์„ ๋จผ์ € ๋งŒ๋“ค๊ณ , sign_info ํ…Œ์ด๋ธ”์€ ๋ชจ๋“  ํ…Œ์ด๋ธ”์˜ PK๋ฅผ FK๋กœ ๋ฐ›์œผ๋ฏ€๋กœ ์ œ์ผ ๋งˆ์ง€๋ง‰์— ๋งŒ๋“ ๋‹ค.
 

3. major_info ํ…Œ์ด๋ธ” ๋งŒ๋“ค๊ณ  ๋ฐ์ดํ„ฐ ๋„ฃ๊ธฐ

create table major_info (
	major_code smallint not null primary key,
    major_name char(20) not null
);

show columns from major_info;

insert into major_info values ('111', '๊ธฐ๊ณ„๊ณตํ•™๊ณผ');
insert into major_info values ('222', '์ปดํ“จํ„ฐ๊ณตํ•™๊ณผ');
insert into major_info values ('333', '์ˆ˜ํ•™๊ณผ');

select * from major_info;

 

4. student_info ํ…Œ์ด๋ธ” ๋งŒ๋“ค๊ณ  ๋ฐ์ดํ„ฐ ๋„ฃ๊ธฐ

FK๋ฅผ ์„ค์ •ํ•ด ์ฃผ๊ธฐ ์œ„ํ•ด ๋งˆ์ง€๋ง‰ ์ค„์—
foreign key (FK์ด๋ฆ„) references ๋งˆ๋”ํ…Œ์ด๋ธ”์ด๋ฆ„(PK์ด๋ฆ„)
๋ฅผ ์ถ”๊ฐ€ํ•ด ์ฃผ๋ฉด FK๊ฐ€ ๋งˆ๋”ํ…Œ์ด๋ธ”์˜ PK๋ฅผ ์ฐธ์กฐํ•˜๊ฒŒ ๋œ๋‹ค.

create database student_db;
use student_db;

create table student_info (
	student_name char(5) not null,
    student_grade tinyint,
    student_number int not null primary key,
    student_gender char(6) not null,
    student_birth date not null,
    major_code smallint not null
    foreign key (major_code) references major_info(major_code)
);

show columns from student_info;

insert into student_info values ('๋‚˜๋Š”์•ผ', '4', 19111222, 'Female', '20000309', '111');
insert into student_info values ('์กธ์—…๋ฐ˜', '4', 19111223, 'Female', '20001010', '222');
insert into student_info values ('์ƒˆ๋‚ด๊ธฐ', '1', 22111333, 'Male', '20030712', '111');
insert into student_info values ('๋ณตํ•™์ƒ', '2', 19111333, 'Male', '20000929', '333');

select * from student_info;

 

5. subject_info ํ…Œ์ด๋ธ” ๋งŒ๋“ค๊ณ  ๋ฐ์ดํ„ฐ ๋„ฃ๊ธฐ

๋งˆ์ฐฌ๊ฐ€์ง€๋กœ FK์„ค์ •์„ ํ•˜๊ณ , ์—ฌ๋Ÿฌ ๊ฐœ์˜ PK ๊ฐ’์„ ์„ค์ •ํ•  ๋•Œ๋Š” ๋ฐ‘์— 
primary key (PK์ด๋ฆ„, PK์ด๋ฆ„, ...)
์œผ๋กœ ํ•˜๋ฉด ๋œ๋‹ค.

create table subject_info (
	major_code smallint not null,
    subject_code int not null,
    subject_name char(20) not null,
    subject_credit tinyint not null,
    primary key (major_code, subject_code),
    foreign key (major_code) references major_info(major_code)
);

show columns from subject_info;

insert into subject_info values (111, 11111, '๊ณต๊ธฐ์—ญํ•™', 3);
insert into subject_info values (111, 11122, '์ง„๋™ํ•™', 3);
insert into subject_info values (111, 11133, '์ œ์–ด๊ณตํ•™', 3);
insert into subject_info values (111, 11144, '์ „์‚ฐ์—ด์œ ์ฒด', 3);
insert into subject_info values (222, 22111, '์ปดํ“จํ„ฐ ํ”„๋กœ๊ทธ๋ž˜๋ฐ', 3);
insert into subject_info values (333, 33155, '์•”ํ˜ธ์˜ ์ˆ˜ํ•™์  ์ดํ•ด', 3);
insert into subject_info values (333, 33100, '๋ฏธ๋ถ„์ ๋ถ„ํ•™1', 2);
delete from subject_info where student_number='22300242';

select * from subject_info;

 

6. sign_info ํ…Œ์ด๋ธ” ๋งŒ๋“ค๊ธฐ

create table sign_info (
	major_code smallint not null,
    subject_code int not null,
    student_number int not null,
    sign_semester char(4) not null,
    subject_score char(5),
    primary key (major_code, subject_code, student_number),
    foreign key (student_number) references student_info(student_number),
    foreign key (major_code) references major_info(major_code)
);

insert into sign_info values (111, 11111, 19111222, '3/2', 'B+');
insert into sign_info values (111, 11122, 19111222, '4/1', 'A0');
insert into sign_info values (111, 11133, 19111222, '4/1', 'A+');
insert into sign_info values (111, 11144, 19111222, '4/2', 'PASS');
insert into sign_info values (333, 33155, 19111222, '4/2', 'A+');
insert into sign_info values (111, 11122, 19111223, '4/2', 'A+');

select * from sign_info;

 

7. ์กฐ์ธํ•ด์„œ ์„ฑ์  ํ…Œ์ด๋ธ” ๋ฐ์ดํ„ฐ ๋ณด๊ธฐ

select major_info.major_name, student_info.student_number, student_info.student_name,
		student_info.student_grade, subject_info.subject_name, sign_info.subject_score, subject_info.subject_credit
	from sign_info
    left join student_info
		on student_info.student_number = sign_info.student_number
    left join subject_info
		on subject_info.subject_code = sign_info.subject_code
	left join major_info
		on major_info.major_code = sign_info.major_code
	where student_info.student_number=19111222;

๋งŒ์•ฝ Error Code: 1052. Column 'student_number' in field list is ambiguous ๊ฐ€ ๋œฌ๋‹ค๋ฉด,  value ์ด๋ฆ„์ด ๊ฒน์ณ์„œ ์–ด๋Š ํ…Œ์ด๋ธ”์˜ value๋ฅผ ๊ฐ€์ ธ์˜ค๋Š” ๊ฑด์ง€ ํ™•์‹คํ•˜๊ฒŒ ํ‘œ์‹œํ•ด ์ฃผ๋ฉด ๋œ๋‹ค.

 

7-1. view๋กœ ๋งŒ๋“ค๊ธฐ

์„ฑ์  ํ…Œ์ด๋ธ”์„ ๋” ์‰ฝ๊ฒŒ ๋ณด๊ธฐ ์œ„ํ•ด์„œ ์ด ํ…Œ์ด๋ธ”์„ view๋กœ ์ง€์ •ํ•ด๋„ ๋œ๋‹ค.

create view student_grade
as
select major_info.major_name, student_info.student_number, student_info.student_name,
		student_info.student_grade, subject_info.subject_name, sign_info.subject_score, subject_info.subject_credit
	from sign_info
    left join student_info
		on student_info.student_number = sign_info.student_number
    left join subject_info
		on subject_info.subject_code = sign_info.subject_code
	left join major_info
		on major_info.major_code = sign_info.major_code
    
select * from student_grade
	where student_number=19111222;

select * from student_grade
	where student_number=19111223;

 

8. ๊ฐœ์„ ์‚ฌํ•ญ

์•„์‰ฌ์› ๋˜ ์ ์€ ์žฌ์ˆ˜๊ฐ•๊ณผ ๋ณต์ˆ˜์ „๊ณต์„ ๊ณ ๋ คํ•˜์ง€ ์•Š์•˜๋‹ค๋Š” ์ ์ด๋‹ค. 
ํ•™์ƒ ํ•˜๋‚˜ํ•˜๋‚˜๋งˆ๋‹ค ํ…Œ์ด๋ธ”์„ ๋งŒ๋“œ๋Š” ๊ฑด ์˜ค๋ฐ”์ด๊ธฐ ๋•Œ๋ฌธ์— ๊ณผ๋ชฉ์„ ์ˆ˜๊ฐ•ํ•œ ํ•™์ƒ์œผ๋กœ ์„ฑ์ ์„ ๋ƒˆ๋Š”๋ฐ, ์ด๋•Œ ์ˆ˜๊ฐ• ํ…Œ์ด๋ธ”์˜ PK๊ฐ’์„ ๋ฌด์—‡์œผ๋กœ ์žก์„์ง€์— ๋Œ€ํ•œ ๊ณ ๋ฏผ์ด ์žˆ์—ˆ๋‹ค. ํ•˜์ง€๋งŒ ์ง€๊ธˆ ํ…Œ์ด๋ธ”์€ ๊ฐ™์€ ํ•™์ƒ์ด ๊ฐ™์€ ์ „๊ณต, ๊ณผ๋ชฉ์ฝ”๋“œ์˜ ์ˆ˜์—…์„ ๋“ค์„ ์ˆ˜๊ฐ€ ์—†๋‹ค(PK๊ฐ’์ด ๊ฒน์น˜๊ธฐ ๋•Œ๋ฌธ์—). ==> ์ˆ˜๊ฐ•ํ…Œ์ด๋ธ”์— ์ผ๋ จ๋ฒˆํ˜ธ๋ฅผ ์ถ”๊ฐ€ํ•ด์„œ ๊ทธ๊ฑธ PK๊ฐ’์œผ๋กœ ์žก์œผ๋ฉด ํ•œ ํ•™์ƒ์ด ๊ฐ™์€ ๊ณผ๋ชฉ์„ ์ˆ˜๊ฐ•ํ•ด๋„ ๊ณ„์† ๋ฐ์ดํ„ฐ๊ฐ€ ์Œ“์ด๊ธฐ ๋•Œ๋ฌธ์— ๋‚˜์ค‘์— ์ถœ๋ ฅํ•  ๋•Œ ๊ฐ™์€ ๋ฐ์ดํ„ฐ๊ฐ€ ์žˆ์œผ๋ฉด ์‹ ๊ทœ ๋ฐ์ดํ„ฐ๋ฅผ ์ถœ๋ ฅํ•˜๋„๋ก ํ•˜๋ฉด ๋  ๊ฒƒ ๊ฐ™๋‹ค.
์ฐพ์•„๋ณด๋‹ˆ ์šฐ๋ฆฌ ํ•™๊ต๋Š” ์•ฝ 11,000๋ช…์˜ ์žฌํ•™์ƒ๊ณผ ์กธ์—…์ƒ๋“ค์˜ ์„ฑ์ ๊นŒ์ง€ ์žˆ์„ ํ…๋ฐ, ์ด๋ ‡๊ฒŒ ๋งŽ์€ ๋ฐ์ดํ„ฐ๋ฅผ ์–ด๋–ป๊ฒŒ ์„ค๊ณ„ํ•ด์„œ ๋ณด๊ด€ํ•˜๋Š” ๊ฑธ๊นŒ..
๊ทธ๋ž˜๋„ ๋ณต์ˆ˜์ „๊ณต์€ ์ƒ๊ฐ๋ณด๋‹ค ์‰ฝ๊ฒŒ ๋  ๊ฒƒ ๊ฐ™๋‹ค.
๋ณต์ˆ˜์ „๊ณต์€ ์†Œ์†์—์„œ 1:m์œผ๋กœ ์„ค์ •ํ•ด์„œ ๋ณต์ˆ˜์ „๊ณต์€ ๋ถˆ๊ฐ€๋Šฅํ•˜์ง€๋งŒ, student_info์— ๋ณต์ˆ˜์ „๊ณต ํ–‰์„ ๋งŒ๋“ค๊ณ  ๋ณต์ˆ˜์ „๊ณต์ด ์—†๋Š” ํ•™์ƒ๋“ค์„ ์œ„ํ•ด null ๊ฐ’์„ ํ—ˆ์šฉํ•˜๋ฉด ๋˜์ง€ ์•Š์„๊นŒ ์‹ถ๋‹ค.
์ด์— ๋Œ€ํ•œ ์˜๊ฒฌ์ด๋‚˜ ์กฐ์–ธ์ด ์žˆ์œผ๋ฉด ์–ธ์ œ๋‚˜ ๋Œ“๊ธ€ ๋‚จ๊ฒจ์ฃผ์‹œ๋ฉด ๊ฐ์‚ฌํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค..
 
์ „์ฒด์ฝ”๋“œ

create database student_db;
use student_db;

-- major_info table
	create table major_info (
	major_code smallint not null primary key,
    major_name char(20) not null
);

insert into major_info values ('111', '๊ธฐ๊ณ„๊ณตํ•™๊ณผ');
insert into major_info values ('222', '์ปดํ“จํ„ฐ๊ณตํ•™๊ณผ');
insert into major_info values ('333', '์ˆ˜ํ•™๊ณผ');


-- student_info table
create table student_info (
	student_name char(5) not null,
    student_grade tinyint,
    student_number int not null primary key,
    student_gender char(6) not null,
    student_birth date not null,
    major_code smallint not null,
    foreign key (major_code) references major_info(major_code)
);

insert into student_info values ('๋‚˜๋Š”์•ผ', '4', 19111222, 'Female', '20000309', '111');
insert into student_info values ('์กธ์—…๋ฐ˜', '4', 19111223, 'Female', '20001010', '222');
insert into student_info values ('์ƒˆ๋‚ด๊ธฐ', '1', 22111333, 'Male', '20030712', '111');
insert into student_info values ('๋ณตํ•™์ƒ', '2', 19111333, 'Male', '20000929', '333');


-- subject_info table
create table subject_info (
	major_code smallint not null,
    subject_code int not null,
    subject_name char(20) not null,
    subject_credit tinyint not null,
    primary key (major_code, subject_code),
    foreign key (major_code) references major_info(major_code)
);

insert into subject_info values (111, 11111, '๊ณต๊ธฐ์—ญํ•™', 3);
insert into subject_info values (111, 11122, '์ง„๋™ํ•™', 3);
insert into subject_info values (111, 11133, '์ œ์–ด๊ณตํ•™', 3);
insert into subject_info values (111, 11144, '์ „์‚ฐ์—ด์œ ์ฒด', 3);
insert into subject_info values (222, 22111, '์ปดํ“จํ„ฐ ํ”„๋กœ๊ทธ๋ž˜๋ฐ', 3);
insert into subject_info values (333, 33155, '์•”ํ˜ธ์˜ ์ˆ˜ํ•™์  ์ดํ•ด', 3);
insert into subject_info values (333, 33100, '๋ฏธ๋ถ„์ ๋ถ„ํ•™1', 2);


-- sign_info table
create table sign_info (
	major_code smallint not null,
    subject_code int not null,
    student_number int not null,
    sign_semester char(4) not null,
    subject_score char(5),
    primary key (major_code, subject_code, student_number),
    foreign key (student_number) references student_info(student_number),
    foreign key (major_code) references major_info(major_code)
);

insert into sign_info values (111, 11111, 19111222, '3/2', 'B+');
insert into sign_info values (111, 11122, 19111222, '4/1', 'A0');
insert into sign_info values (111, 11133, 19111222, '4/1', 'A+');
insert into sign_info values (111, 11144, 19111222, '4/2', 'PASS');
insert into sign_info values (333, 33155, 19111222, '4/2', 'A+');
insert into sign_info values (111, 11122, 19111223, '4/2', 'A+');


-- ํ…Œ์ด๋ธ” ์กฐ์ธํ•ด์„œ ์„ฑ์  ๋ฐ์ดํ„ฐ ์กฐํšŒํ•˜๊ธฐ
select major_info.major_name, student_info.student_number, student_info.student_name,
		student_info.student_grade, subject_info.subject_name, sign_info.subject_score, subject_info.subject_credit
	from sign_info
    left join student_info
		on student_info.student_number = sign_info.student_number
    left join subject_info
		on subject_info.subject_code = sign_info.subject_code
	left join major_info
		on major_info.major_code = sign_info.major_code
	where student_info.student_number=19111222;
    
-- view๋กœ ๋งŒ๋“ค๊ธฐ
create view student_grade
as
select major_info.major_name, student_info.student_number, student_info.student_name,
		student_info.student_grade, subject_info.subject_name, sign_info.subject_score, subject_info.subject_credit
	from sign_info
    left join student_info
		on student_info.student_number = sign_info.student_number
    left join subject_info
		on subject_info.subject_code = sign_info.subject_code
	left join major_info
		on major_info.major_code = sign_info.major_code;
    
select * from student_grade
	where student_number=19111222;
select * from student_grade
	where student_number=19111223;

 

728x90

"); wcs_do();