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

DB์„ค๊ณ„) ์ง์› ํ…Œ์ด๋ธ”๊ณผ ๊ธ‰์—ฌ ํ…Œ์ด๋ธ” ์„ค๊ณ„ํ•˜๊ธฐ

by HanaV 2023. 4. 7.
728x90

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

 db ์ด๋ฆ„: employee_db
1. ์ง์› ํ…Œ์ด๋ธ”

์—ด ์ด๋ฆ„ employee_info ๋ฐ์ดํ„ฐํƒ€์ž… ์ตœ๋Œ€๊ธธ์ด null ํ—ˆ์šฉ ์—ฌ๋ถ€
์ด๋ฆ„ emp_name char 5 x
์‚ฌ์›๋ฒˆํ˜ธ (PK) emp_code char 10 x
์ƒ๋…„์›”์ผ emp_birth date YYYY-MM-DD o
์„ฑ๋ณ„ emp_gender char 6 (Female) o
์ž…์‚ฌ์ผ emp_start date YYYY-MM-DD x
๋ถ€์„œ๋ฒˆํ˜ธ (FK) team_code char 3 x
์ง๊ธ‰ emp_level char 5 x
๊ธ‰์—ฌ emp_pay bigint   x
์„ฑ๊ณผ๊ธˆ emp_bonus bigint   o

2. ๋ถ€์„œ ํ…Œ์ด๋ธ”

์—ด ์ด๋ฆ„ team_info ๋ฐ์ดํ„ฐํƒ€์ž… ์ตœ๋Œ€๊ธธ์ด null ํ—ˆ์šฉ ์—ฌ๋ถ€
๋ถ€์„œ๋ฒˆํ˜ธ (PK) team_code char 3 x
๋ถ€์„œ์ด๋ฆ„ team_name char 10 x

์ตœ์ข…์ ์œผ๋กœ ์ถœ๋ ฅํ•  ๊ธ‰์—ฌ ํ…Œ์ด๋ธ”

๊ธ‰์—ฌ ํ…Œ์ด๋ธ”
์ง์› ์ด๋ฆ„ emp_name
์ง์› ๋ถ€์„œ team_name
์ง์› ์ง๊ธ‰ emp_level
์ง์› ๊ธ‰์—ฌ emp_pay
์ง์› ๋ณด๋„ˆ์Šค emp_bonus

 

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

employee_info๋Š” team_info์˜ PK๊ฐ’์„ FK๋กœ ๋ฐ›๊ณ  ์žˆ์œผ๋ฏ€๋กœ team_info๋ฅผ ๋จผ์ € ๋งŒ๋“ค์–ด์ค€๋‹ค.

 

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

create database employee_db;
use employee_db;

create table team_info (
	team_code char(5) not null primary key,
    team_name char(20) not null
);

show columns from team_info;

insert into team_info values ('AAA', '์ด๋ฌดํŒ€');
insert into team_info values ('BBB', '์ธ์‚ฌ๊ณผ');
insert into team_info values ('CCC', '๊ธฐํšํŒ€');

select * from team_info;

 

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

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

create table employee_info (
	emp_name char(10) not null,
    emp_code char(20) not null primary key,
    emp_birth date,
    emp_gender char(6),
    emp_start date not null,
    team_code char(5) not null,
    emp_level char(10) not null,
    emp_pay bigint not null,
    emp_bonus bigint,    
    foreign key (team_code) references team_info(team_code)
);

show columns from employee_info;

insert into employee_info values ('๊น€์‚ฌ์›', 'MMMM', 20000309, 'Female', 20230910, 'AAA', '์‹ ์ž…', 10000000, null);
insert into employee_info values ('์ด๋Œ€๋ฆฌ', 'BBBB', 19891121, 'Female', 20100302, 'AAA', '๋Œ€๋ฆฌ', 100000000, 50000000);
insert into employee_info values ('๋ฐ•๋ถ€์žฅ', 'AAAA', 19701030, 'Male', 20090210, 'BBB', '์‹ ์ž…', 500000000, 50000000);
insert into employee_info values ('์‹ ์ธํ„ด', 'XXXX', 20001011, 'Female', 20231010, 'CCC', '์ธํ„ด', 5000000, null);
insert into employee_info values ('์ •์‚ฌ์›', 'NNNN', 19960501, 'Male', 20230210, 'CCC', '์‹ ์ž…', 10000000, null);
select * from employee_info;

 

4. ์กฐ์ธํ•ด์„œ ๊ธ‰์—ฌ ํ…Œ์ด๋ธ” ๋ฐ์ดํ„ฐ ๋ณด๊ธฐ

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

select emp_name, team_info.team_name, emp_level, emp_pay, emp_bonus
	from employee_info
    join team_info
		on team_info.team_code = employee_info.team_code;

 
์ „์ฒด์ฝ”๋“œ

create database employee_db;
use employee_db;

-- team_info table
create table team_info (
	team_code char(5) not null primary key,
    team_name char(20) not null
);


insert into team_info values ('AAA', '์ด๋ฌดํŒ€');
insert into team_info values ('BBB', '์ธ์‚ฌ๊ณผ');
insert into team_info values ('CCC', '๊ธฐํšํŒ€');

-- employee_info table
create table employee_info (
	emp_name char(10) not null,
    emp_code char(20) not null primary key,
    emp_birth date,
    emp_gender char(6),
    emp_start date not null,
    team_code char(5) not null,
    emp_level char(10) not null,
    emp_pay bigint not null,
    emp_bonus bigint,    
    foreign key (team_code) references team_info(team_code)
);

insert into employee_info values ('๊น€์‚ฌ์›', 'MMMM', 20000309, 'Female', 20230910, 'AAA', '์‹ ์ž…', 10000000, null);
insert into employee_info values ('์ด๋Œ€๋ฆฌ', 'BBBB', 19891121, 'Female', 20100302, 'AAA', '๋Œ€๋ฆฌ', 100000000, 50000000);
insert into employee_info values ('๋ฐ•๋ถ€์žฅ', 'AAAA', 19701030, 'Male', 20090210, 'BBB', '์‹ ์ž…', 500000000, 50000000);
insert into employee_info values ('์‹ ์ธํ„ด', 'XXXX', 20001011, 'Female', 20231010, 'CCC', '์ธํ„ด', 5000000, null);
insert into employee_info values ('์ •์‚ฌ์›', 'NNNN', 19960501, 'Male', 20230210, 'CCC', '์‹ ์ž…', 10000000, null);
select * from employee_info;


-- ๊ธ‰์—ฌํ…Œ์ด๋ธ” ์กฐ์ธํ•ด์„œ ๋ณด๊ธฐ
select emp_name, team_info.team_name, emp_level, emp_pay, emp_bonus
	from employee_info
    join team_info
		on team_info.team_code = employee_info.team_code;
728x90

"); wcs_do();