title: Adding a Foreign Key to MariaDB / MySQL table
body: <p>I have 3 tables:</p>
  
  <pre><code>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;
  </code></pre>
  
  <p>I want to alter the events table to add more fields and add foreign keys to channel, event_type and client_event_type. 
  I've tried this:</p>
  
  <pre><code>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`),
  </code></pre>
  
  <p>It comes up with :</p>
  
  <blockquote>
    <p>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 'references <code>channels</code>(<code>id</code>),   add constraint
    <code>events_event_type_foreign</code> foreig' at line 5</p>
  </blockquote>
  
  <p>What am I doing wrong?
  Thanks</p>