itsource

MariaDB - 두 개의 외부 키로 테이블을 만들 수 없습니다.

mycopycode 2022. 11. 24. 20:47
반응형

MariaDB - 두 개의 외부 키로 테이블을 만들 수 없습니다.

질문이 있습니다.

create table bans
(
    id int auto_increment primary key ,
    reason int not null,
    player int not null,
    server int not null,
    starts timestamp default current_timestamp not null,
    ends DATETIME not null,
    constraint bans__fk_player
        foreign key (player) references players ('id'),
    constraint bans__fk_server
        foreign key (server) references servers ('id')
);

그 결과:

[2019-01-02 18:35:29] [42000][1064] (conn=75) You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near ''id'),
[2019-01-02 18:35:29] [42000][1064] You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near ''id'),

나는 단지 다음 사이에 1:n의 관계를 만들고 싶다.

players.id->bans.player말만 해bans__fk_player

servers.id->bans.server말만 해bans__fk_server

1) 이것은 SO people에 의해 이미 코멘트되어 있습니다.외부 키 정의에서 식별자 주변의 따옴표를 삭제해야 합니다.또한 mysql/MariaDB의 견적 사용에 대한 일반적인 논의는 이 SO 게시물을 참조하십시오.

2) 제약조건을 올바르게 정의하지 않은 것도 문제로, 외부 키의 이름이 누락되어 있습니다.구문은 다음 mysql turorial에서 설명하듯이 다음과 같습니다.

CONSTRAINT constraint_name
FOREIGN KEY foreign_key_name (columns)
REFERENCES parent_table(columns)

따라서 코드는 다음과 같습니다.

...
constraint bans__player
    foreign key bans__fk_player (player) references players (id),
constraint bans__server
    foreign key bans__fk_server (server) references servers (id)
...

이 db fielled.

이것 역시 동작해, 보다 짧은 구문을 생성합니다(제약 조건을 명시적으로 지정할 필요는 없습니다).

...
foreign key bans__fk_player (player) references players (id),
foreign key bans__fk_server (server) references servers (id)
...

그래도 오류가 발생할 경우 참조된 테이블(서버 및 플레이어)의 정의를 확인해야 합니다.두 테이블 모두 id는 테이블의 프라이머리 키이거나 고유한 제약조건에 의해 제어되어야 합니다.물론 숫자여야 합니다.

열을 인용하려면idMariaDB(MySQL에서도)에서는 다음과 같이 "back 틱"을 사용해야 합니다.

create table bans
(
    id int auto_increment primary key ,
    reason int not null,
    player int not null,
    server int not null,
    starts timestamp default current_timestamp not null,
    ends DATETIME not null,
    constraint bans__fk_player
        foreign key (player) references players (`id`),
    constraint bans__fk_server
        foreign key (server) references servers (`id`)
);

언급URL : https://stackoverflow.com/questions/54011259/mariadb-cant-create-a-table-with-two-foreign-keys

반응형