Добавление внешнего ключа в таблицу MariaDB / MySQL - PullRequest
0 голосов
/ 26 апреля 2018

У меня есть 3 таблицы:

CREATE TABLE `channels` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `status` enum('active','inactive') NOT NULL DEFAULT 'active’,
 `description` text DEFAULT NULL,
  `created_at` timestamp NULL DEFAULT NULL,
  `updated_at` timestamp NULL DEFAULT NULL,
  `deleted_at` timestamp NULL DEFAULT NULL,
  PRIMARY KEY (`id`),
) ENGINE=InnoDB AUTO_INCREMENT=485 DEFAULT CHARSET=utf8;

CREATE TABLE `categories` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `name` varchar(255) NOT NULL,
  `content` text DEFAULT NULL,
  `description` text DEFAULT NULL,
  `status` enum('active','inactive') NOT NULL DEFAULT 'active',
  `created_at` timestamp NOT NULL DEFAULT current_timestamp(),
  `updated_at` timestamp NOT NULL DEFAULT current_timestamp() ON UPDATE current_timestamp(),
  `deleted_at` timestamp NULL DEFAULT NULL,
  PRIMARY KEY (`id`),
) ENGINE=InnoDB AUTO_INCREMENT=128 DEFAULT CHARSET=utf8;

CREATE TABLE `events` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `name` varchar(255) NOT NULL,
  `content` text DEFAULT NULL,
  `timezone` varchar(255) DEFAULT NULL,
  `recurring` tinyint(1) NOT NULL DEFAULT 0,
  `all_day` tinyint(1) NOT NULL DEFAULT 0,
  `starts_at` timestamp NULL DEFAULT NULL,
  `ends_at` timestamp NULL DEFAULT NULL,
  `started_at` timestamp NULL DEFAULT NULL,
  `completed_at` timestamp NULL DEFAULT NULL,
  `status` enum('active','inactive','in_progress','complete') DEFAULT 'active',
  `created_at` timestamp NULL DEFAULT NULL,
  `updated_at` timestamp NULL DEFAULT NULL,
  `deleted_at` timestamp NULL DEFAULT NULL,
  PRIMARY KEY (`id`),
) ENGINE=InnoDB AUTO_INCREMENT=82 DEFAULT CHARSET=utf8;

Я хочу изменить таблицу событий, чтобы добавить больше полей и добавить внешние ключи для channel, event_type и client_event_type. Я пробовал это:

alter table `events` 
  add `channel` int unsigned not null, 
  add `event_type` int unsigned not null,
  add `client_event_type` int unsigned not null, 
  add constraint `events_channel_foreign` foreign key `channel` references `channels`(`id`),
  add constraint `events_event_type_foreign` foreign key `event_type` references `categories`(`id`),
  add constraint `events_clientEventType_foreign` foreign key `client_event_type` references `categories`(`id`),

Это приходит с:

У вас ошибка в синтаксисе SQL; проверьте руководство, которое соответствует вашей версии сервера MariaDB для правильного использования синтаксиса рядом с 'ссылками channels (id), добавьте ограничение events_event_type_foreign foreig 'в строке 5

Что я делаю не так? Спасибо

1 Ответ

0 голосов
/ 26 апреля 2018

Вам не хватает паратезов:

alter table `events` 
  add `channel` int unsigned not null, 
  add `event_type` int unsigned not null,
  add `client_event_type` int unsigned not null, 
  add constraint `events_channel_foreign` foreign key (`channel`) references `channels`(`id`),
  add constraint `events_event_type_foreign` foreign key (`event_type`) references `categories`(`id`),
  add constraint `events_clientEventType_foreign` foreign key (`client_event_type`) references `categories`(`id`)
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...