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;
'๐ฅ๏ธ > DBMS' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
SQL ์ ์ฝ์กฐ๊ฑด (PK, FK, Unique, Check, Default) (0) | 2023.04.10 |
---|---|
[MySQL] ํ ์ด๋ธ ๋ง๋ค๊ธฐ (0) | 2023.04.10 |
[SQL Server] ํ ์ด๋ธ ๋ง๋ค๊ธฐ (0) | 2023.04.07 |
SQL ๋ฐ์ดํฐ ํ์ ์ ๋ฆฌ (0) | 2023.04.07 |
DB์ค๊ณ) ์ง์ ํ ์ด๋ธ๊ณผ ๊ธ์ฌ ํ ์ด๋ธ ์ค๊ณํ๊ธฐ (0) | 2023.04.07 |