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
'๐ฅ๏ธ > DBMS' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[SQL Server] ํ ์ด๋ธ ๋ง๋ค๊ธฐ (0) | 2023.04.07 |
---|---|
SQL ๋ฐ์ดํฐ ํ์ ์ ๋ฆฌ (0) | 2023.04.07 |
[MariaDB] Windows 10์ MariaDB ์ค์นํ๊ณ ์ธ๋ถ ์ ์ํ๊ธฐ(VMVirtualBox ์ฌ์ฉ) (0) | 2023.04.06 |
[SQL Server] Windows 10์ SQL Server 2022 ์ค์นํ๊ธฐ (VM VirtualBox์ฌ์ฉ) (0) | 2023.04.06 |
[MySQL] Windows 10์ MySQL ์๋ฒ ์ค์น ๋ฐ ์ ์ ์ค์ ํ๊ธฐ(VMVirtualBox ์ฌ์ฉ) (0) | 2023.04.06 |