Second column of MySQL InnoDB AUTO_INCREMENT

I am creating a system in which users can download any file that they want, and not use it to execute any code. As part of this, I rename each file and save its original name in the MySQL table. This table contains the identifier of the user who downloaded it and a unique identifier for the download. I am currently 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 that I will always have a unique combination of user_id and upload_id, and each user has the first upload identifier 1. However, I want to use the foreign key for user_Id, so if I delete the user, his download will also have to be deleted. That means I have to do this in InnoDB. How would I do this since the above setting only works in MyISAM.

The table of my users (with which I should get user_id) is as follows:

    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;

I want 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

+4
source share
2 answers

If I understood correctly:

Replace the primary key with a unique index with two fields. Make upload_id the primary key and user_id the foreign key.

0
source

, 1 . , .

: , MyISAM, -, 1 , . https://dev.mysql.com/doc/refman/5.0/en/example-auto-increment.html

, upload_id 1 . , user_id upload_id , upload_id. .

0

Source: https://habr.com/ru/post/1599148/


All Articles