2017-09-18 16 views
0

長すぎた私は、MySQLデータベースを使用していると私はデータベース作成のsctiptを実行しようとしていますが、私はラインが関係しているかの表示せずに、次のエラーを取得:MySQLエラー:指定されたキーが

SQL Error (1071): Specified key was too long; max key length is 3072 bytes 

これは、私のスクリプト:

/*==============================================================*/ 
/* Table: MDEV_ADDRESS           */ 
/*==============================================================*/ 
create table MDEV_ADDRESS 
(
    id_address   bigint not null auto_increment, 
    street_1    varchar(2500), 
    street_2    varchar(2500), 
    zip_code    int, 
    city     varchar(255), 
    region    varchar(255), 
    country    varchar(255), 
    is_delivery_address bool, 
    primary key (id_address) 
); 

/*==============================================================*/ 
/* Table: MDEV_BANK_CARD          */ 
/*==============================================================*/ 
create table MDEV_BANK_CARD 
(
    id_bank_card   bigint not null auto_increment, 
    id_user    bigint not null, 
    card_type   varchar(150) not null, 
    card_number   int not null, 
    expiration_date  timestamp not null, 
    security_code  int not null, 
    holder_name   varchar(255), 
    primary key (id_bank_card) 
); 

/*==============================================================*/ 
/* Table: MDEV_CATEGORY           */ 
/*==============================================================*/ 
create table MDEV_CATEGORY 
(
    id_category   bigint not null auto_increment, 
    id_parent_category bigint, 
    name     varchar(1500) not null, 
    description   varchar(2500), 
    primary key (id_category) 
); 

/*==============================================================*/ 
/* Table: MDEV_CUSTOMER_HAS_ADDRESSES       */ 
/*==============================================================*/ 
create table MDEV_CUSTOMER_HAS_ADDRESSES 
(
    id_user    bigint not null, 
    id_address   bigint not null, 
    primary key (id_user, id_address) 
); 

/*==============================================================*/ 
/* Table: MDEV_LOGIN_ATTEMPT         */ 
/*==============================================================*/ 
create table MDEV_LOGIN_ATTEMPT 
(
    id_login_attempt  bigint not null auto_increment, 
    id_user    bigint not null, 
    attempt_date   timestamp not null, 
    ip_address   varchar(150) not null, 
    has_logged_in  bool not null, 
    log_out_date   timestamp, 
    os     varchar(255), 
    browser    varchar(255), 
    primary key (id_login_attempt) 
); 

/*==============================================================*/ 
/* Table: MDEV_NOTIFICATION          */ 
/*==============================================================*/ 
create table MDEV_NOTIFICATION 
(
    id_notification  bigint not null auto_increment, 
    id_user    bigint not null, 
    creation_date  timestamp not null, 
    message    varchar(2500) not null, 
    link_url    varchar(2500), 
    is_viewed   bool not null, 
    primary key (id_notification) 
); 

/*==============================================================*/ 
/* Table: MDEV_ORDER           */ 
/*==============================================================*/ 
create table MDEV_ORDER 
(
    id_order    bigint not null auto_increment, 
    id_user    bigint not null, 
    id_status   bigint not null, 
    reference_number  varchar(255) not null unique, 
    order_date   timestamp not null, 
    payment_date   timestamp, 
    payment_method  varchar(255), 
    primary key (id_order) 
); 

/*==============================================================*/ 
/* Table: MDEV_ORDER_LINE          */ 
/*==============================================================*/ 
create table MDEV_ORDER_LINE 
(
    id_order_line  bigint not null auto_increment, 
    id_product   bigint not null, 
    id_order    bigint not null, 
    quantity    int not null, 
    primary key (id_order_line) 
); 

/*==============================================================*/ 
/* Table: MDEV_PRODUCT           */ 
/*==============================================================*/ 
create table MDEV_PRODUCT 
(
    id_product   bigint not null auto_increment, 
    id_category   bigint, 
    reference_number  varchar(255) not null unique, 
    name     varchar(1500), 
    description   varchar(2500), 
    image_path   varchar(2500), 
    price    float, 
    primary key (id_product) 
); 

/*==============================================================*/ 
/* Table: MDEV_PRODUCT_HAS_TAGS         */ 
/*==============================================================*/ 
create table MDEV_PRODUCT_HAS_TAGS 
(
    id_product   bigint not null, 
    id_tag    bigint not null, 
    primary key (id_product, id_tag) 
); 

/*==============================================================*/ 
/* Table: MDEV_ROLE            */ 
/*==============================================================*/ 
create table MDEV_ROLE 
(
    id_role    bigint not null auto_increment, 
    name     varchar(1500) not null unique, 
    description   varchar(2500), 
    primary key (id_role) 
); 

/*==============================================================*/ 
/* Table: MDEV_SITE_PREFERENCES         */ 
/*==============================================================*/ 
create table MDEV_SITE_PREFERENCES 
(
    id_site_preferences bigint not null auto_increment, 
    name     varchar(1500) unique, 
    root_images_folder_path varchar(2500), 
    image_max_size  int, 
    image_max_size_unit varchar(2), 
    image_min_size  int, 
    image_min_size_unit varchar(2), 
    root_videos_folder_path varchar(2500), 
    video_max_size  int, 
    video_max_size_unit varchar(2), 
    video_min_size  int, 
    video_min_size_unit varchar(2), 
    primary key (id_site_preferences) 
); 

/*==============================================================*/ 
/* Table: MDEV_STATUS           */ 
/*==============================================================*/ 
create table MDEV_STATUS 
(
    id_status   bigint not null auto_increment, 
    name     varchar(1500) not null unique, 
    primary key (id_status) 
); 

/*==============================================================*/ 
/* Table: MDEV_TAG            */ 
/*==============================================================*/ 
create table MDEV_TAG 
(
    id_tag    bigint not null auto_increment, 
    name     varchar(1500) not null unique, 
    primary key (id_tag) 
); 

/*==============================================================*/ 
/* Table: MDEV_USER            */ 
/*==============================================================*/ 
create table MDEV_USER 
(
    id_user    bigint not null auto_increment, 
    id_status   bigint not null, 
    discriminator  varchar(50) not null, 
    u_email    varchar(255) not null unique, 
    u_password   varchar(255) not null, 
    u_username   varchar(255) unique, 
    u_first_name   varchar(255) not null, 
    u_last_name   varchar(255) not null, 
    u_birth_date   timestamp, 
    u_gender    varchar(150), 
    u_registration_date timestamp not null, 
    u_confirmation_date timestamp, 
    u_expiration_date timestamp, 
    u_locking_date  timestamp, 
    u_disabling_date  timestamp, 
    u_credentials_expiration_date timestamp, 
    u_deletion_date  timestamp, 
    u_locking_reason  varchar(5000), 
    u_disabling_reason varchar(5000), 
    u_deletion_reason varchar(5000), 
    u_used_language  varchar(150), 
    u_security_question varchar(255), 
    u_security_answer varchar(255), 
    u_code    varchar(255) not null unique, 
    c_phone    int, 
    e_job    varchar(255), 
    e_cnss_number  varchar(255), 
    primary key (id_user) 
); 

/*==============================================================*/ 
/* Table: MDEV_USER_HAS_ROLES         */ 
/*==============================================================*/ 
create table MDEV_USER_HAS_ROLES 
(
    id_role    bigint not null, 
    id_user    bigint not null, 
    primary key (id_role, id_user) 
); 

alter table MDEV_BANK_CARD add constraint FK_MDEV_CUSTOMER_HAS_BANK_CARDS   foreign key (id_user) 
    references MDEV_USER (id_user) on delete cascade on update cascade; 

alter table MDEV_CATEGORY add constraint  FK_MDEV_CATEGORY_HAS_SUB_CATEGORIES foreign key (id_parent_category) 
    references MDEV_CATEGORY (id_category) on delete cascade on update cascade; 

alter table MDEV_CUSTOMER_HAS_ADDRESSES add constraint FK_MDEV_CUSTOMER_HAS_ADDRESSES foreign key (id_user) 
    references MDEV_USER (id_user) on delete cascade on update cascade; 

alter table MDEV_CUSTOMER_HAS_ADDRESSES add constraint FK_MDEV_CUSTOMER_HAS_ADDRESSES2 foreign key (id_address) 
    references MDEV_ADDRESS (id_address) on delete cascade on update cascade; 

alter table MDEV_LOGIN_ATTEMPT add constraint FK_MDEV_USER_HAS_LOGIN_ATTEMPTS foreign key (id_user) 
    references MDEV_USER (id_user) on delete cascade on update cascade; 

alter table MDEV_NOTIFICATION add constraint FK_MDEV_CUSTOMER_HAS_NOTIFICATIONS foreign key (id_user) 
    references MDEV_USER (id_user) on delete cascade on update cascade; 

alter table MDEV_ORDER add constraint FK_MDEV_CUSTOMER_HAS_ORDERS foreign key (id_user) 
    references MDEV_USER (id_user) on delete cascade on update cascade; 

alter table MDEV_ORDER add constraint FK_MDEV_ORDER_HAS_STATUS foreign key (id_status) 
    references MDEV_STATUS (id_status) on delete cascade on update cascade; 

alter table MDEV_ORDER_LINE add constraint FK_MDEV_ORDER_HAS_ORDER_LINES foreign key (id_order) 
    references MDEV_ORDER (id_order) on delete cascade on update cascade; 

alter table MDEV_ORDER_LINE add constraint FK_MDEV_ORDER_LINE_HAS_PRODUCT foreign key (id_product) 
    references MDEV_PRODUCT (id_product) on delete cascade on update cascade; 

alter table MDEV_PRODUCT add constraint FK_MDEV_CATEGORY_HAS_PRODUCTS foreign key (id_category) 
    references MDEV_CATEGORY (id_category) on delete cascade on update cascade; 

alter table MDEV_PRODUCT_HAS_TAGS add constraint FK_MDEV_PRODUCT_HAS_TAGS foreign key (id_product) 
    references MDEV_PRODUCT (id_product) on delete cascade on update cascade; 

alter table MDEV_PRODUCT_HAS_TAGS add constraint FK_MDEV_PRODUCT_HAS_TAGS2 foreign key (id_tag) 
    references MDEV_TAG (id_tag) on delete cascade on update cascade; 

alter table MDEV_USER add constraint FK_MDEV_USER_HAS_STATUS foreign key (id_status) 
    references MDEV_STATUS (id_status) on delete cascade on update cascade; 

alter table MDEV_USER_HAS_ROLES add constraint FK_MDEV_USER_HAS_ROLES foreign key (id_role) 
    references MDEV_ROLE (id_role) on delete cascade on update cascade; 

alter table MDEV_USER_HAS_ROLES add constraint FK_MDEV_USER_HAS_ROLES2 foreign key (id_user) 
    references MDEV_USER (id_user) on delete cascade on update cascade; 

私はHeidiSQLを使用して、このスクリプトを実行していますし、それが表の作成に達したとき、明らか実行でエラーが発生します:

/*==============================================================*/ 
/* Table: MDEV_ROLE            */ 
/*==============================================================*/ 
create table MDEV_ROLE 
(
    id_role    bigint not null auto_increment, 
    name     varchar(1500) not null unique, 
    description   varchar(2500), 
    primary key (id_role) 
); 

私が知っている限り、私が作成している唯一のインデックスは主キーで、長さはわずか8バイトです。

誰かが私をここで助けることができればそれは素晴らしいでしょうか? ありがとうございます。

+0

テキストが大きなVARCHARがより優れている - https://stackoverflow.com/a/2023513/2316112 –

+0

ただし、最大65,535文字まで保存できるので、テキストはvarcharより大きい... – karim

+0

はい!それは異なった働きをします。あなたの懸念がある場合、ユーザが記述> 2500を入力する可能性がある場合、DBに送る前に、バックエンド(およびフロントエンド)でそれを検証しておくべきです。バリデーションはDB(唯一)で行うべきではありません! –

答えて

1

あなたはMySQLのバージョンは言及していませんでした。私はあなたがInnoDBとMySQL 5.7を使用していると仮定しています。

あなたはこのスニペットを見つけることができます。

The index key prefix length limit is 767 bytes for InnoDB tables that use the REDUNDANT or COMPACT row format. For example, you might hit this limit with a column prefix index of more than 255 characters on a TEXT or VARCHAR column, assuming a utf8mb3 character set and the maximum of 3 bytes for each character.

あなたは、前述の制約の対象であるインデックスを作成します固有の名前を持っているので、それが起こります。ここ

より:https://dev.mysql.com/doc/refman/5.7/en/innodb-restrictions.html

1

name varchar(1500) not null unique,

あなたは長すぎるvarchar型の一意のキーを持っています。

マルチバイト文字を使用しているため、ここではより多くのバイトが必要です。

+0

ありがとうございます。 – karim

+0

@karimもしあなたが好きなら、私よりも1分早いのでEugeniuの答えを受け入れてください – DanFromGermany

+0

それは大丈夫です、私は彼の答えを受け入れるつもりです – karim

1

767または766に、あなたのテーブルの作成に使用している一意のキーの長さを短くし、それが動作するだけでなく

関連する問題