Why most SQL databases allow defining the same index twice?

后端 未结 6 1456
没有蜡笔的小新
没有蜡笔的小新 2021-01-21 20:09

Why most SQL databases allow defining the same index (or constraint) twice?

For example in MySQL I can do:

CREATE TABLE testkey(id VARCHAR(10) NOT NULL,          


        
6条回答
  •  深忆病人
    2021-01-21 20:41

    All programming languages allow you to write redundancies:

    That's just an example, you could obviously have duplicate code, duplicate functions, or duplicate data structures that are much more wasteful.

    It's up to you to write good code, and this depends on the situation. Maybe there's a good reason in some rare case to write something that seems redundant. In that case, you'd be just as put out if the technology didn't allow you to do it.

    You might be interested in a tool called Maatkit, which is a collection of indispensable tools for MySQL users. One of its tools checks for duplicate keys:

    http://www.maatkit.org/doc/mk-duplicate-key-checker.html

    If you're a MySQL developer, novice or expert, you should download Maatkit right away and set aside a full day to read the docs, try out each tool in the set, and learn how to integrate them into your daily development tasks. You'll kick yourself for not doing it sooner.

    As for naming indexes, it allows you to do this:

    ALTER TABLE testkey DROP KEY `id`, DROP KEY `id_2`;
    

    If they weren't named, you'd have no way to drop individual indexes. You'd have to drop the whole table and recreate it without the indexes.

提交回复
热议问题