C:\Users\PC> mysql -u root -p
Enter password: *******
mysql> use zerobase
Database changed
mysql> create table person2 #person TABLE이 이미 있어서
-> (
-> pid int NOT NULL,
-> name varchar(16),
-> age int,
-> sex char,
-> primary key (pid)
-> );
Query OK, 0 rows affected (0.01 sec)
mysql> desc person2;
+-------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| pid | int | NO | PRI | NULL | |
| name | varchar(16) | YES | | NULL | |
| age | int | YES | | NULL | |
| sex | char(1) | YES | | NULL | |
+-------+-------------+------+-----+---------+-------+
4 rows in set (0.00 sec)
mysql> create table animal
-> ( name varchar(16) NOT NULL,
-> type varchar(16) NOT NULL,
-> age int,
-> primary key (name, type)
->
-> );
Query OK, 0 rows affected (0.01 sec)
mysql> desc animal;
+-------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| name | varchar(16) | NO | PRI | NULL | |
| type | varchar(16) | NO | PRI | NULL | |
| age | int | YES | | NULL | |
+-------+-------------+------+-----+---------+-------+
3 rows in set (0.00 sec)
mysql> desc person2;
+-------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| pid | int | NO | PRI | NULL | |
| name | varchar(16) | YES | | NULL | |
| age | int | YES | | NULL | |
| sex | char(1) | YES | | NULL | |
+-------+-------------+------+-----+---------+-------+
4 rows in set (0.00 sec)
mysql> alter table person2
-> drop primary key;
Query OK, 0 rows affected (0.03 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> desc person2;
+-------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| pid | int | NO | | NULL | |
| name | varchar(16) | YES | | NULL | |
| age | int | YES | | NULL | |
| sex | char(1) | YES | | NULL | |
+-------+-------------+------+-----+---------+-------+
4 rows in set (0.00 sec)
mysql> desc animal;
+-------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| name | varchar(16) | NO | PRI | NULL | |
| type | varchar(16) | NO | PRI | NULL | |
| age | int | YES | | NULL | |
+-------+-------------+------+-----+---------+-------+
3 rows in set (0.00 sec)
mysql> alter table animal
-> drop primary key;
Query OK, 0 rows affected (0.03 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> desc animal;
+-------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| name | varchar(16) | NO | | NULL | |
| type | varchar(16) | NO | | NULL | |
| age | int | YES | | NULL | |
+-------+-------------+------+-----+---------+-------+
3 rows in set (0.00 sec)
mysql> desc person2;
+-------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| pid | int | NO | | NULL | |
| name | varchar(16) | YES | | NULL | |
| age | int | YES | | NULL | |
| sex | char(1) | YES | | NULL | |
+-------+-------------+------+-----+---------+-------+
4 rows in set (0.00 sec)
mysql> alter table person2
-> add primary key (pid);
Query OK, 0 rows affected (0.02 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> desc person2;
+-------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| pid | int | NO | PRI | NULL | |
| name | varchar(16) | YES | | NULL | |
| age | int | YES | | NULL | |
| sex | char(1) | YES | | NULL | |
+-------+-------------+------+-----+---------+-------+
4 rows in set (0.00 sec)
mysql> desc animal;
+-------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| name | varchar(16) | NO | | NULL | |
| type | varchar(16) | NO | | NULL | |
| age | int | YES | | NULL | |
+-------+-------------+------+-----+---------+-------+
3 rows in set (0.00 sec)
mysql> alter table animal
-> add constraint PK_animal
-> primary key (name, type);
Query OK, 0 rows affected (0.02 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> desc animal;
+-------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| name | varchar(16) | NO | PRI | NULL | |
| type | varchar(16) | NO | PRI | NULL | |
| age | int | YES | | NULL | |
+-------+-------------+------+-----+---------+-------+
3 rows in set (0.00 sec)
mysql> create table orders
-> (
-> oid int not null,
-> order_no varchar(16),
-> pid int,
-> primary key (oid),
-> constraint FK_person2 foreign key (pid) references person2(pid)
-> );
Query OK, 0 rows affected (0.03 sec)
mysql> desc orders;
+----------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------+-------------+------+-----+---------+-------+
| oid | int | NO | PRI | NULL | |
| order_no | varchar(16) | YES | | NULL | |
| pid | int | YES | MUL | NULL | |
+----------+-------------+------+-----+---------+-------+
3 rows in set (0.00 sec)
create table orders
oid int not null,
pid int,
primary key (oid),
constraint FK_person2
foreign key (pid)
💡FOREIGN KEY 의 constraint 가 자동 생성된 경우 확인하는 방법
(constraint를 생략한 쿼리는, 알아서 자동 생성됨)
SHOW create TABLE tablename;
ALTER TABLE tablename
DROP FOREIGN KEY fk_constraint;
▼ 예제에 적용
mysql> ALTER TABLE orders
-> DROP FOREIGN KEY fk_person2;
Query OK, 0 rows affected (0.01 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> desc orders;
+----------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------+-------------+------+-----+---------+-------+
| oid | int | NO | PRI | NULL | |
| order_no | varchar(16) | YES | | NULL | |
| pid | int | YES | MUL | NULL | |
+----------+-------------+------+-----+---------+-------+
3 rows in set (0.00 sec)
mysql> show create table orders;
+--------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table | Create Table
|
+--------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| orders | CREATE TABLE `orders` (
`oid` int NOT NULL,
`order_no` varchar(16) DEFAULT NULL,
`pid` int DEFAULT NULL,
PRIMARY KEY (`oid`),
💡KEY `FK_person2` (`pid`)💡
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci |
+--------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)
ALTER TABLE tablename
ADD FOREIGN KEY (column) REFERENCES REF_tablename(REF_column);
▼ 예제에 적용
mysql> ALTER TABLE orders
-> ADD FOREIGN KEY (pid) REFERENCES person2(pid);
Query OK, 0 rows affected (0.04 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> desc orders;
+----------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------+-------------+------+-----+---------+-------+
| oid | int | NO | PRI | NULL | |
| order_no | varchar(16) | YES | | NULL | |
| pid | int | YES | MUL | NULL | |
+----------+-------------+------+-----+---------+-------+
3 rows in set (0.00 sec)
mysql> show create table orders;
+--------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table | Create Table
|
+--------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| orders | CREATE TABLE `orders` (
`oid` int NOT NULL,
`order_no` varchar(16) DEFAULT NULL,
`pid` int DEFAULT NULL,
PRIMARY KEY (`oid`),
KEY `pid` (`pid`),
# 자동생성됨
💡CONSTRAINT `orders_ibfk_1`💡 FOREIGN KEY (`pid`) REFERENCES `person2` (`pid`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci |
+--------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)
foreign key 를 이용해서 관계를 형성하고자 함
C:\Users\PC>cd Documents
C:\Users\PC\Documents>cd sql_ws
C:\Users\PC\Documents\sql_ws> mysql -h "database-1.cj22sogoe8oa.ap-southeast-2.rds.amazonaws.com" -P 3306 -u admin -p
Enter password: *********
mysql> use zerobase
Database changed
mysql> show tables;
+--------------------+
| Tables_in_zerobase |
+--------------------+
| cctv |
| celeb |
| crime_status |
| oil_price |
| person |
| police_station |
| refueling |
| snl_show |
| sql_file |
| test1 |
| test2 |
+--------------------+
11 rows in set (0.15 sec)
mysql> desc police_station;
+--------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+--------+--------------+------+-----+---------+-------+
| name | varchar(16) | YES | | NULL | |
| adress | varchar(128) | YES | | NULL | |
+--------+--------------+------+-----+---------+-------+
2 rows in set (0.16 sec)
mysql> select count(distinct name) from police_station;
+----------------------+
| count(distinct name) |
+----------------------+
| 31 |
+----------------------+
1 row in set (0.15 sec)
mysql> desc crime_status;
+----------------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------------+-------------+------+-----+---------+-------+
| year | year | YES | | NULL | |
| police_station | varchar(8) | YES | | NULL | |
| crime_stype | varchar(16) | YES | | NULL | |
| status_type | char(2) | YES | | NULL | |
| case_number | int | YES | | NULL | |
+----------------+-------------+------+-----+---------+-------+
5 rows in set (0.15 sec)
📌 데이터 수 확인
mysql> select count(distinct police_station) from crime_status;
+---------------------------------+
| count(distinct police_station) |
+---------------------------------+
| 31 |
+---------------------------------+
1 row in set (0.15 sec)
📌 상위 3개 데이터 확인 : limit 3
mysql> select distinct name from police_station limit 3;
+------------------+
| name |
+------------------+
| 서울중부경찰서 |
| 서울종로경찰서 |
| 서울남대문경찰서 |
+------------------+
3 rows in set (0.16 sec)
mysql> select distinct police_station from crime_status limit 3;
+----------------+
| police_station |
+----------------+
| 중부 |
| 종로 |
| 남대문 |
+----------------+
3 rows in set (0.15 sec)
나만 특이하게 이러는 걸까...아무도 질문하거나 팀원들 스터디 노트에 적혀 있지 않은데...
ERROR 1267 (HY000): Illegal mix of collations (utf8mb4_unicode_ci,IMPLICIT) and (utf8mb4_0900_ai_ci,IMPLICIT) for operation 'like'
mysql> ALTER TABLE police_station DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci
mysql> alter table police_station convert to character set utf8 collate utf8_general_ci;
Query OK, 31 rows affected, 2 warnings (0.23 sec)
Records: 31 Duplicates: 0 Warnings: 2
mysql> alter table crime_status convert to character set utf8 collate utf8_general_ci;
Query OK, 310 rows affected, 2 warnings (0.21 sec)
Records: 310 Duplicates: 0 Warnings: 2
mysql> select c.police_station, p.name
-> from crime_status as c, police_station as p
-> where p.name like concat('서울', c.police_station, '경찰서')
-> group by c.police_station, p.name;
+----------------+------------------+
| police_station | name |
+----------------+------------------+
| 중부 | 서울중부경찰서 |
| 종로 | 서울종로경찰서 |
| 남대문 | 서울남대문경찰서 |
| 서대문 | 서울서대문경찰서 |
| 혜화 | 서울혜화경찰서 |
| 용산 | 서울용산경찰서 |
| 성북 | 서울성북경찰서 |
| 동대문 | 서울동대문경찰서 |
| 마포 | 서울마포경찰서 |
| 영등포 | 서울영등포경찰서 |
| 성동 | 서울성동경찰서 |
| 동작 | 서울동작경찰서 |
| 광진 | 서울광진경찰서 |
| 서부 | 서울서부경찰서 |
| 강북 | 서울강북경찰서 |
| 금천 | 서울금천경찰서 |
| 중랑 | 서울중랑경찰서 |
| 강남 | 서울강남경찰서 |
| 관악 | 서울관악경찰서 |
| 강서 | 서울강서경찰서 |
| 강동 | 서울강동경찰서 |
| 종암 | 서울종암경찰서 |
| 구로 | 서울구로경찰서 |
| 서초 | 서울서초경찰서 |
| 양천 | 서울양천경찰서 |
| 송파 | 서울송파경찰서 |
| 노원 | 서울노원경찰서 |
| 방배 | 서울방배경찰서 |
| 은평 | 서울은평경찰서 |
| 도봉 | 서울도봉경찰서 |
| 수서 | 서울수서경찰서 |
+----------------+------------------+
31 rows in set (0.17 sec)
mysql> desc police_station;
+--------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+--------+--------------+------+-----+---------+-------+
| name | varchar(16) | YES | | NULL | |
| adress | varchar(128) | YES | | NULL | |
+--------+--------------+------+-----+---------+-------+
2 rows in set (0.15 sec)
mysql> ALTER TABLE police_station
-> ADD PRIMARY KEY (name);
Query OK, 31 rows affected (0.25 sec)
Records: 31 Duplicates: 0 Warnings: 0
mysql> desc police_station;
+--------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+--------+--------------+------+-----+---------+-------+
| name | varchar(16) | NO | PRI | NULL | |
| adress | varchar(128) | YES | | NULL | |
+--------+--------------+------+-----+---------+-------+
2 rows in set (0.15 sec)
mysql> desc police_station;
+--------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+--------+--------------+------+-----+---------+-------+
| name | varchar(16) | NO | PRI | NULL | |
| adress | varchar(128) | YES | | NULL | |
+--------+--------------+------+-----+---------+-------+
2 rows in set (0.15 sec)
mysql> ALTER TABLE crime_status ADD COLUMN REFERENCE varchar(16);
Query OK, 0 rows affected (0.17 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> desc crime_status;
+----------------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------------+-------------+------+-----+---------+-------+
| year | year | YES | | NULL | |
| police_station | varchar(8) | YES | | NULL | |
| crime_stype | varchar(16) | YES | | NULL | |
| status_type | char(2) | YES | | NULL | |
| case_number | int | YES | | NULL | |
| REFERENCE 💡 | varchar(16) | YES | | NULL | |
+----------------+-------------+------+-----+---------+-------+
6 rows in set (0.15 sec)
mysql> ALTER TABLE crime_status
📌어떤거? 📌어디에 저장되어 있는지
-> ADD FOREIGN KEY (REFERENCE) REFERENCES police_station(name);
Query OK, 310 rows affected (0.23 sec)
Records: 310 Duplicates: 0 Warnings: 0
mysql> UPDATE crime_status c, police_station p
📌 crime.reference를 police.name과 매칭 시켜줌
-> SET c.REFERENCE = p.name
-> WHERE p.name LIKE concat('서울', c.police_station,'경찰서');
Query OK, 310 rows affected (0.17 sec)
Rows matched: 310 Changed: 310 Warnings: 0
mysql> SELECT distinct police_station, REFERENCE FROM crime_status;
+----------------+------------------+
| police_station | REFERENCE |
+----------------+------------------+
| 중부 | 서울중부경찰서 |
| 종로 | 서울종로경찰서 |
| 남대문 | 서울남대문경찰서 |
| 서대문 | 서울서대문경찰서 |
| 혜화 | 서울혜화경찰서 |
| 용산 | 서울용산경찰서 |
| 성북 | 서울성북경찰서 |
| 동대문 | 서울동대문경찰서 |
| 마포 | 서울마포경찰서 |
| 영등포 | 서울영등포경찰서 |
| 성동 | 서울성동경찰서 |
| 동작 | 서울동작경찰서 |
| 광진 | 서울광진경찰서 |
| 서부 | 서울서부경찰서 |
| 강북 | 서울강북경찰서 |
| 금천 | 서울금천경찰서 |
| 중랑 | 서울중랑경찰서 |
| 강남 | 서울강남경찰서 |
| 관악 | 서울관악경찰서 |
| 강서 | 서울강서경찰서 |
| 강동 | 서울강동경찰서 |
| 종암 | 서울종암경찰서 |
| 구로 | 서울구로경찰서 |
| 서초 | 서울서초경찰서 |
| 양천 | 서울양천경찰서 |
| 송파 | 서울송파경찰서 |
| 노원 | 서울노원경찰서 |
| 방배 | 서울방배경찰서 |
| 은평 | 서울은평경찰서 |
| 도봉 | 서울도봉경찰서 |
| 수서 | 서울수서경찰서 |
+----------------+------------------+
31 rows in set (0.15 sec)
mysql> SELECT c.police_station, p.adress FROM crime_status c, police_station p
-> WHERE c.REFERENCE = p.name
-> GROUP BY c.police_station;
+----------------+-----------------------------------------------------------+
| police_station | adress |
+----------------+-----------------------------------------------------------+
| 강남 | 서울특별시 강남구 테헤란로 114길 11 |
| 강동 | 서울특별시 강동구 성내로 33 |
| 강북 | 서울특별시 강북구 오패산로 406 |
| 강서 | 서울특별시 양천구 화곡로 73 |
| 관악 | 서울특별시 관악구 관악로5길 33 |
| 광진 | 서울특별시 광진구 광나루로 447 광진소방서 임시청사 (능동) |
| 구로 | 서울특별시 구로구 가마산로 235 |
| 금천 | 서울특별시 관악구 남부순환로 1435 |
| 남대문 | 서울특별시 중구 한강대로 410 |
| 노원 | 서울특별시 노원구 노원로 283 |
| 도봉 | 서울특별시 도봉구 노해로 403 |
| 동대문 | 서울특별시 동대문구 약령시로 21길 29 |
| 동작 | 서울특별시 동작구 노량진로 148 |
| 마포 | 서울특별시 마포구 마포대로 183 |
| 방배 | 서울특별시 서초구 방배천로 54 |
| 서대문 | 서울특별시 서대문구 통일로 113 |
| 서부 | 서울특별시 은평구 은평로9길 15 |
| 서초 | 서울특별시 서초구 반포대로 179 |
| 성동 | 서울특별시 성동구 왕십리광장로 9 |
| 성북 | 서울특별시 성북구 보문로 170 |
| 송파 | 서울특별시 송파구 중대로 221 |
| 수서 | 서울특별시 강남구 개포로 617 |
| 양천 | 서울특별시 양천구 목동동로 99 |
| 영등포 | 서울특별시 영등포구 국회대로 608 |
| 용산 | 서울특별시 용산구 원효로89길 24 |
| 은평 | 서울특별시 은평구 연서로 365 |
| 종로 | 서울특별시 종로구 율곡로 46 |
| 종암 | 서울특별시 성북구 종암로 135 |
| 중랑 | 서울특별시 중랑구 중랑역로 137 |
| 중부 | 서울특별시 중구 수표로 27 |
| 혜화 | 서울특별시 종로구 창경궁로 112-16 |
+----------------+-----------------------------------------------------------+
31 rows in set (0.15 sec)
기본 설정
mysql> exit
Bye
C:\Users\PC\Documents\sql_ws> mysql -u root -p
Enter password: *******
mysql> use zerobase
Database changed
mysql>
mysql> create table study
-> (
-> study_id int not null,
-> study_date date,
-> study_time time,
-> patient_id int,
-> primary key (study_id),
-> constraint FK_study foreign key (patient_id) references person2 (pid)
-> );
Query OK, 0 rows affected (0.03 sec)
mysql> desc study;
+------------+------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+------------+------+------+-----+---------+-------+
| study_id | int | NO | PRI | NULL | |
| study_date | date | YES | | NULL | |
| study_time | time | YES | | NULL | |
| patient_id | int | YES | MUL | NULL | |
+------------+------+------+-----+---------+-------+
4 rows in set (0.00 sec)
mysql> show create table study;
+-------+----------------------------------------------------
| Table | Create Table
+-------+----------------------------------------------------
| study | CREATE TABLE `study` (
`study_id` int NOT NULL,
`study_date` date DEFAULT NULL,
`study_time` time DEFAULT NULL,
`patient_id` int DEFAULT NULL,
PRIMARY KEY (`study_id`),
KEY `FK_study` (`patient_id`),
CONSTRAINT `FK_study` FOREIGN KEY (`patient_id`) REFERENCES `person2` (`pid`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci |
+-------+----------------------------------------------------
1 row in set (0.00 sec)
mysql> alter table study
-> drop primary key;
Query OK, 0 rows affected (0.04 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> desc study;
+------------+------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+------------+------+------+-----+---------+-------+
| study_id | int | NO | | NULL | |
| study_date | date | YES | | NULL | |
| study_time | time | YES | | NULL | |
| patient_id | int | YES | MUL | NULL | |
+------------+------+------+-----+---------+-------+
4 rows in set (0.00 sec)
3.생성한 테이블의 FOREIGN KEY를 삭제하세요.
mysql> alter table study
-> drop foreign key FK_study;
Query OK, 0 rows affected (0.01 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> desc study;
+------------+------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+------------+------+------+-----+---------+-------+
| study_id | int | NO | | NULL | |
| study_date | date | YES | | NULL | |
| study_time | time | YES | | NULL | |
| patient_id | int | YES | MUL | NULL | |
+------------+------+------+-----+---------+-------+
4 rows in set (0.00 sec)
mysql> show create table study;
+-------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table | Create Table
+-------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| study | CREATE TABLE `study` (
`study_id` int NOT NULL,
`study_date` date DEFAULT NULL,
`study_time` time DEFAULT NULL,
`patient_id` int DEFAULT NULL,
KEY `FK_study` (`patient_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci |
+-------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)
4.study 테이블의 patient_id를 person 테이블의 pid와 연결된 FOREIGN KEY로 등록하세요.
mysql> alter table study
-> add foreign key (patient_id) references person2 (pid);
Query OK, 0 rows affected (0.04 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> show create table study;
+-------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table | Create Table
+-------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| study | CREATE TABLE `study` (
`study_id` int NOT NULL,
`study_date` date DEFAULT NULL,
`study_time` time DEFAULT NULL,
`patient_id` int DEFAULT NULL,
KEY `patient_id` (`patient_id`),
CONSTRAINT `study_ibfk_1` FOREIGN KEY (`patient_id`) REFERENCES `person2` (`pid`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci |
+-------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)
5.study 테이블의 study_id를 PRIMARY KEY로 등록하세요.
mysql> alter table study
-> add primary key (study_id);
Query OK, 0 rows affected (0.03 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> desc study;
+------------+------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+------------+------+------+-----+---------+-------+
| study_id | int | NO | PRI | NULL | |
| study_date | date | YES | | NULL | |
| study_time | time | YES | | NULL | |
| patient_id | int | YES | MUL | NULL | |
+------------+------+------+-----+---------+-------+
4 rows in set (0.00 sec)