이슈 발생

스크린샷 2022-08-18 오후 4.15.51.png

위의 코드로 primary key를 foreign key로 지정하려니 error가 떴다.

해결

alter table [테이블명] 
add foreign key( [컬럼명] ) 
references [참조한 테이블]( [참조할 컬럼명] ) [옵션];

<aside> 🐶

  1. CASCADE
  1. SET NULL
  1. NO ACTION
  1. SET DEFAULT
  1. RESTRICT

출처: [what is programming:티스토리]

https://prinha.tistory.com/entry/MySQL-PRIMARY-KEY-FOREIGN-KEY-설정하는-다양한-방법

create table orders (
	ordnum int primary key auto_increment,
	id varchar(20) not null,
	orddate timestamp not null,
    pnum int not null,
    ispay bool not null,
	pamount int not null
);

alter table orders
 add foreign key( id ) references member(id)
 on delete restrict on update cascade;
    
alter table orders
 add foreign key( pnum ) references product(pnum)
 on delete restrict on update cascade;

테이블을 우선 생성하고, 수정하면 되는 일이었다.

에러가 뜨지도 않고, order table과 product table 사이에 연동도 잘되더라.