数据库导入

问题描述 投票:0回答:1

我尝试将文件 (

database.sql
) 导入到我的数据库服务器,但返回时出现以下错误:

All parts of a PRIMARY KEY must be NOT NULL; if you need NULL in a key, use UNIQUE insteadapp/config/database.sq|CREATE TABLE IF NOT EXISTS
'Newsnewsletter _groups _subscribers* ( 'group_id* int(10) unsigned DEFAULT NULL, 'subscriber_id" int(10) unsigned DEFAULT NULL, PRIMARY KEY (group_id', 'subscriber_id'), UNIQUE KEY 'group_id' (group_id', subscriber_id*) ) ENGINE=InnoDB DEFAULT CHARSET=utf8

它引用的文件部分是这样的:

84 DROP TABLE IF EXISTS `newsletter_groups_subscribers`;
85 CREATE TABLE IF NOT EXISTS `newsletter_groups_subscribers` (
86  `group_id` int(10) unsigned DEFAULT NULL,
87  `subscriber_id` int(10) unsigned DEFAULT NULL,
88   PRIMARY KEY (`group_id`, `subscriber_id`),
89   UNIQUE KEY `group_id` (`group_id`,`subscriber_id`)
90 ) ENGINE=InnoDB DEFAULT CHARSET=utf8;

我尝试用“UNIQUE”替换“NULL”

DROP TABLE IF EXISTS `newsletter_groups_subscribers`;
CREATE TABLE IF NOT EXISTS `newsletter_groups_subscribers` (
  `group_id` int(10) unsigned UNIQUE,
  `subscriber_id` int(10) unsigned UNIQUE,
  PRIMARY KEY (`group_id`, `subscriber_id`),
  UNIQUE KEY `group_id` (`group_id`,`subscriber_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

然后我得到这个不同的错误:

Duplicate key name 'group_id'app/config/database.sqlCREATE TABLE IF NOT EXISTS `nwsnewsletter_groups_subscribers` ( `group_id` int(10) unsigned UNIQUE, `subscriber_id` int(10) unsigned UNIQUE, PRIMARY KEY (`group_id`, `subscriber_id`), UNIQUE KEY `group_id` (`group_id`,`subscriber_id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8

我做错了什么?

sql null unique
1个回答
0
投票
PRIMARY KEY (`group_id`, `subscriber_id`)

表示您要从

group_id
subscriber_id
列添加表的主键,但主键不能建立在可为空的列上, 所以你有两个选择:

  1. 删除

    PRIMARY KEY
    定义并仅保留
    UNIQUE KEY

  2. 删除该列的可为空性

    group_id
    int(10) 无符号非空,

    subscriber_id
    int(10) 无符号非空,

© www.soinside.com 2019 - 2024. All rights reserved.