mysql - 向 MariaDB/MySQL 表添加外键

标签 mysql sql mariadb

我有 3 张 table :

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`),

它提出:

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 channels(id), add constraint events_event_type_foreign foreig' at line 5

我做错了什么? 谢谢

最佳答案

你缺少括号:

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`)

关于mysql - 向 MariaDB/MySQL 表添加外键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50038402/

相关文章:

mysql NOW() 定义时间

MySQL基于ID更新慢(15万条记录)

sql - 使用 CTE 显示数据中的传递关系

sql - 如何在 Rails 中使用带有 order 子句的 postgres AVG() 函数

mysql - MySQL UPDATE 的 WHERE 子句中的条件无效 - 更新了所有内容 - 而不是没有

mysql - glibc 和非 glibc 之间的 mariadb tarball 有何不同?

mysql - 外键关系 - 唯一元组

mysql - 如何修复mysql中的 "FUNCTION doesnt exist"错误?

mariadb - Sequelize 将 String 类型的列值返回为 Buffer 数组(MariaDB)

php - 检查插入或更新表