0

I have a very large database I would like to split up into tables. I would like to make it so when I run a distinct, it will make a table for every distinct name. The name of the table will be the data in one of the fields.

EX:

A    ---------   Data 1
A    ---------   Data 2
B    ---------   Data 3
B    ---------   Data 4 

would result in 2 tables, 1 named A and another named B. Then the entire row of data would be copied into that field.

select distinct [name] from [maintable]
-make table for each name
-select [name] from [maintable]
-copy into table name
-drop row from [maintable]

Any help would be great!

4

2 回答 2

1

我会建议你不要这样做。

一种解决方案是创建索引,以便您可以快速访问数据。但是,如果您只有几个名称,这可能不是特别有效,因为索引值将选择几乎所有记录。

另一种解决方案是所谓的分区。确切的机制因数据库而异,但基本思想是相同的。表的不同部分(如name您的情况所定义)将存储在不同的位置。当查询仅查找特定名称的值时,只会读取该数据。

通常,拥有多个具有完全相同数据列的表是不好的设计。以下是一些原因:

  • 添加列、更改类型或添加索引必须执行多次而不是一次。
  • 对跨表的列强制执行主键约束非常困难——您会丢失主键。
  • 涉及多个名称的查询变得更加复杂。
  • 插入和更新更复杂,因为您必须首先确定正确的表。这通常会导致在其他基本操作中过度使用动态 SQL。

尽管可能有一些简化(考虑到安全性),但大多数数据库都有其他机制优于将数据拆分到单独的表中。

于 2013-05-09T15:19:54.383 回答
0

你想要的是

CREATE TABLE new_table
  AS (SELECT .... //the data that you want in this table);
于 2013-05-09T14:47:36.270 回答