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. This table contains the id of the user who uploaded it, and a unique id of the upload. 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. This means I have to do it in InnoDB. How would i go about that, since the above setup only works in MyISAM.
My users table (wich i would get user_id from) looks like this:
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.
Unfortunately for this problem, the auto increment column will not start over at 1 for each user. 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. 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. 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
. You should still be able to implement the cascade delete with this setup.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.