简体   繁体   English

MySQL InnoDB AUTO_INCREMENT二级列

[英]MySQL InnoDB AUTO_INCREMENT secondary column

I am making a system where users can upload any file they want, and not use it to execute any kind of code. 我正在建立一个系统,用户可以上传他们想要的任何文件,而不是用它来执行任何类型的代码。 As a part of that, I rename every file, and store its original name in a MySQL table. 作为其中的一部分,我重命名每个文件,并将其原始名称存储在MySQL表中。 This table contains the id of the user who uploaded it, and a unique id of the upload. 此表包含上载它的用户的ID以及上载的唯一ID。 Currently I am doing it like this: 目前我这样做:

CREATE TABLE `uploads` (
    `user_id` INT(11) NOT NULL,
    `upload_id` INT(11) NOT NULL AUTO_INCREMENT,
    `original_name` VARCHAR(30) NOT NULL,
    `mime_type` VARCHAR(30) NOT NULL,
    `name` VARCHAR(50) NOT NULL,
    PRIMARY KEY (`user_id`, `upload_id`)
) ENGINE=MyISAM;

This means I will always have a unique combination of user_id and upload_id, and every users first upload has an id of 1. However I want to use a foreign key for the user_Id, so if I delete a user, its uploads would also be deleted. 这意味着我将始终拥有user_id和upload_id的唯一组合,并且每个用户首​​次上传的ID都为1.但是我想为user_Id使用外键,因此如果删除用户,其上传也将被删除。 This means I have to do it in InnoDB. 这意味着我必须在InnoDB中完成。 How would i go about that, since the above setup only works in MyISAM. 我将如何解决这个问题,因为上述设置仅适用于MyISAM。

My users table (wich i would get user_id from) looks like this: 我的用户表(我会得到user_id)看起来像这样:

    CREATE TABLE `".DATABASE."`.`users` (
    `user_id` INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
    `username` VARCHAR(30) NOT NULL,
    `email` VARCHAR(50) NOT NULL,
    `password` CHAR(128) NOT NULL,
    `salt` CHAR(128) NOT NULL 
) ENGINE = InnoDB;

What i want is for the uploads table to look like this: 我想要的是上传表格如下所示:

user_id  | upload_id
1        | 1
1        | 2
2        | 1
2        | 2
2        | 3
1        | 3

If that makes sense 如果这是有道理的

If I understood correctly: 如果我理解正确:

Replace the primary key to a unique index with the two fields. 将主键替换为具有两个字段的唯一索引。 Make the upload_id the primary key and user_id the foreign key then. 然后将upload_id作为主键,将user_id作为外键。

Unfortunately for this problem, the auto increment column will not start over at 1 for each user. 不幸的是,对于这个问题,每个用户的自动增量列不会从1开始。 It will just increment for each added row, regardless of any particular column value. 无论任何特定的列值如何,它都会为每个添加的行增加。

Edit: displaying my ignorance there, MyISAM tables apparently will start over at 1 for each user when a multi-column index this way. 编辑:在那里显示我的无知,当一个多列索引这样时,MyISAM表显然将从每个用户的1开始。 https://dev.mysql.com/doc/refman/5.0/en/example-auto-increment.html https://dev.mysql.com/doc/refman/5.0/en/example-auto-increment.html

Fortunately, it is probably not necessary for upload_id to start at 1 for each user. 幸运的是,对于每个用户, upload_id可能不需要从1开始。 Using auto increment on that column means you will always have a unique combination of user_id and upload_id even without using both columns as the primary key, or even creating a unique index, because every record will have a different upload_id . 在该列上使用自动增量意味着即使不使用两列作为主键,甚至创建唯一索引,也始终具有user_idupload_id的唯一组合,因为每条记录都将具有不同的upload_id You should still be able to implement the cascade delete with this setup. 您仍然可以使用此设置实现级联删除。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM