[英]Maximum number of columns in a table
Problem1: What is the maximum no of columns we can have in a table 问题1:表中我们可以拥有的最大列数是多少
Problem2: What is the maximum no of columns we should have in a table 问题2:表中应该具有的最大列数是多少
Answer 1: Probably more than you have, but not more than you will grow to have. 答案1:可能比你拥有的多,但不会超过你的成长。
Answer 2: Fewer than you have. 答案2:比你少。
Asking these questions usually indicates that you haven't designed the table well. 提出这些问题通常表明您没有很好地设计表格。 You probably are practicing the Metadata Tribbles antipattern. 您可能正在练习元数据Tribbles反模式。 The columns tend to accumulate over time, creating an unbounded set of columns that store basically the same type of data. 这些列往往会随着时间的推移而累积,创建一组无限的列,这些列基本上存储相同类型的数据。 Eg subtotal1
, subtotal2
, subtotal3
, etc. 例如subtotal1
, subtotal2
, subtotal3
等。
Instead, I'm guessing you should create an additional dependent table, so your many columns become many rows. 相反,我猜你应该创建一个额外的依赖表,所以你的许多列会变成很多行。 This is part of designing a proper normalized database. 这是设计适当的规范化数据库的一部分。
CREATE TABLE Subtotals (
entity_id INT NOT NULL,
year_quarter SMALLINT NOT NULL, -- e.g. 20094
subtotal NUMERIC(9,2) NOT NULL,
PRIMARY KEY (entity_id, year_quarter),
FOREIGN KEY (entity_id) REFERENCES Entities (entity_id)
);
My former colleague also wrote a blog about this: 我的前同事也写过一篇关于此的博客:
Understanding the maximum number of columns in a MySQL table 了解MySQL表中的最大列数
The answer is not so straightforward as you might think. 答案并不像你想象的那么简单。
SQL 2000 : 1024 SQL 2000:1024
SQL 2005 : 1024 SQL 2005:1024
SQL 2008 : 1024 for a non-wide table, 30k for a wide table. SQL 2008:1024用于非宽表,30k用于宽表。
The wide tables are for when you have used the new sparse column feature in SQL 2008 which is designed for when you have a large number of columns that are normally empty. 宽表用于在SQL 2008中使用新的稀疏列功能时,该功能是为大量通常为空的列而设计的。
Just because these limits are available, does not mean you should be using them however, I would start with designing the tables based on the requirements and then check whether a vertical partitioning of 1 table into 2 smaller tables is required etc. 仅仅因为这些限制是可用的,并不意味着你应该使用它们,但是,我会从根据需求设计表开始,然后检查是否需要将1个表垂直分区为2个较小的表等。
1) http://msdn.microsoft.com/en-us/library/aa933149%28SQL.80%29.aspx 1) http://msdn.microsoft.com/en-us/library/aa933149%28SQL.80%29.aspx
1024 seems to be the limit. 1024似乎是极限。
2) Much less than 1024 :). 2)远低于1024 :)。 Seriously, it depends on how normalized you want your DB to be. 说真的,这取决于你想要数据库的规范化程度。 Generally, the fewer columns you have in a table the easier it will be for someone to understand (normally). 通常,表中的列越少,人们就越容易理解(通常)。 Like for a person table, you might want to store the person's address in another table (person_address, for example). 与人员表一样,您可能希望将人员的地址存储在另一个表中(例如,person_address)。 It's best to break your data up into entities that make sense for your business model, and go from there. 最好将数据分解为对您的业务模型有意义的实体,然后从那里开始。
2) There are plenty of guidelines out there. 2)那里有很多指导方针。 In particular regarding database normalization. 特别是关于数据库规范化。 The overarching principle is always to be able to adapt. 总体原则始终是能够适应的。 Similar to classes, tables with large number of columns are not very flexible. 与类类似,具有大量列的表不是非常灵活。 Some of the questions you should ask yourself: 你应该问自己的一些问题:
Database design is an art more than a science. 数据库设计不仅仅是一门科学。 While guidelines and technical limitations will get you in the right direction, there are no hard rules that will make your system work or fail 100%. 虽然指导方针和技术限制将使您朝着正确的方向前进,但没有硬性规则可以使您的系统100%正常工作或失败。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.