Computer Science/Database

W3Schools SQL | #47 SQL FOREIGN KEY CONSTRAINT 이용법

토마토. 2022. 10. 30. 00:02

SQL FOREIGN KEY Constraint

SQL FOREIGN KEY Constraint

Foreign key - 테이블 간의 관계를 생성한다.

child table - foreign key 있는 테이블
parent table - primary key 있는 테이블

SQL FOREIGN KEY on CREATE TABLE

CREATE TABLE Orders (
    OrderID int NOT NULL, 
    OrderNumber int NOT NULL, 
    PersonID int, 
    PRIMARY KEY (OrderID),
    FOREIGN KEY (PersonID) REFERENCES Persons(PersonID)
);

SQL FOREIGN KEY on ALTER TABLE

ALTER TABLE Orders
ADD FOREIGN KEY (PersonID) REFERENCES Persons(PersonID);

DROP a FOREIGN KEY Constraint

ALTER TABLE Orders
DROP FOREIGN KEY FK_PersonOrder;