简体   繁体   English

如何在不索引列的情况下在innodb表中添加外键约束?

[英]How do I add foreign key constraints in my innodb table without indexing the columns?

i read some advice about table indexing, and i fount out that indexes should be introduced later when your sql queries are running slow.. 我阅读了一些有关表索引的建议,我发现当您的sql查询运行缓慢时,应该在以后引入索引。

my problem is I index almost all my columns in one table. 我的问题是我在一个表中几乎索引了所有列。 this table is tied to other table, for example the users table column country is tied to the country table 该表与其他表绑定,例如,用户表列国家/地区与国家/地区表绑定

user table          country table
id                  id
countryId           name

I added an index in the countryId column, so that I can link it to the country table's id because i can't create a foreign key constraints when I don't have an index on the column. 我在countryId列中添加了索引,因此可以将其链接到国家/地区表的ID,因为当列上没有索引时,无法创建外键约束。

Basically i have many columns that are link to other table just to add constraints to my column. 基本上,我有很多列链接到其他表,只是为了向我的列添加约束。

for example, the country table is not more than 100, so if someone just accidentally inserted a value of 101, mysql won't accept that data because the country table only have 1-100.. 例如,国家/地区表不超过100,因此,如果有人不小心插入了值101,则mysql将不会接受该数据,因为国家/地区表只有1-100。

So how can i avoid adding indexes and still have the function of table data constraints? 那么,如何避免添加索引却仍然具有表数据约束的功能?

  1. That advice is awful. 那建议真可怕。

  2. Index columns that you will be searching on or where you need particular constraints such as unique values. 索引要在其上搜索或需要特定约束(例如唯一值)的列。

The only reason NOT to add indexes is that they do take up space. 不添加索引的唯一原因是它们确实占用了空间。 But, unless you're working with so much data that you'll need a database administrator, you probably won't have to worry about it. 但是,除非您要处理大量数据,否则需要数据库管理员,否则您不必担心。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM