I'm trying to figure out exactly how to create table structures in databases correctly, and after doing my research I'm left with a couple of questions that I can't find an answer to:
- In MySQL, there is such a statement as INDEX, which I understand from the description to be synonymous to the word KEY.
INDEX (column_1)
INDEX is designed precisely to simplify field searching. For some reason it says that when we create tables via dbDelta we should use KEY and not INDEX. Why? INDEX is just not supported?
CREATE TABLE {$wpdb->prefix}dbdelta_test (
id bigint(20) NOT NULL AUTO_INCREMENT,
column_1 varchar(255) NOT NULL,
PRIMARY KEY (id),
KEY column_1 (column_1)
)
In WP we have a users table where the primary key is ID. For example, we need to create another table to store some additional data for these users (e.g. user_data).
Can we set ID from users table as primary key for user_data table? In theory, the uniqueness condition is met, we just won't set auto_increment for this field.
Something like this:
CREATE TABLE {$wpdb->prefix}user_data (
id INT unsigned NOT NULL,
some_field_1 VARCHAR(1000) NOT NULL default '',
some_field_2 BIGINT unsigned NOT NULL default 0,
PRIMARY KEY (id),
FOREIGN KEY (id)
REFERENCES {$wpdb->prefix}users (id)
ON UPDATE RESTRICT ON DELETE RESTRICT
I understand that it sounds like a perversion, but I'm really wondering if we can avoid creating an additional field with id for the second table, which would meaningfully copy the id from the first table completely.
Thank you!