How do I add indices to MySQL tables?

后端 未结 7 1955
北荒
北荒 2020-11-22 13:54

I\'ve got a very large MySQL table with about 150,000 rows of data. Currently, when I try and run

SELECT * FROM table WHERE id = \'1\';

the

7条回答
  •  醉话见心
    2020-11-22 14:06

    A better option is to add the constraints directly during CREATE TABLE query (assuming you have the information about the tables)

    CREATE TABLE products(
        productId INT AUTO_INCREMENT PRIMARY KEY,
        productName varchar(100) not null,
        categoryId INT NOT NULL,
        CONSTRAINT fk_category
        FOREIGN KEY (categoryId) 
        REFERENCES categories(categoryId)
            ON UPDATE CASCADE
            ON DELETE CASCADE
    ) ENGINE=INNODB;
    

提交回复
热议问题