日期:2014-05-16  浏览次数:20725 次

insert的时候,如何判断表是否存在
需求是这样的,表是按天生成的,例如table20110101,table20110102....
但是中间有可能某天的表不存在
这样,我按天循环insert的时候就会出错,请问怎么在insert的同时判断表是否存在,不存在就停止操作?

------解决方案--------------------
探讨
需求是这样的,表是按天生成的,例如table20110101,table20110102....
但是中间有可能某天的表不存在
这样,我按天循环insert的时候就会出错,请问怎么在insert的同时判断表是否存在,不存在就停止操作?

------解决方案--------------------
关联 系统表,实例sql如下。看看
 select * information_schema.TABLES 
where table_schema='test' and table_name='test';

------解决方案--------------------
SQL code

create procedure sp()
begin
   if exists(select 1 information_schema.TABLES where table_schema='test' and table_name='test') then
      insert into test values(1);
   end if;

end;

------解决方案--------------------
SQL code

create procedure sp()
begin
   if exists(select 1 from information_schema.TABLES where table_schema='test' and table_name='test') then
      insert into test values(1);
   end if;

end;

------解决方案--------------------
建议重新考虑你的数据库设计,这种分表设计都少见。

你应该使用一张表来存储所有的记录,而不是每天一张表。

如果有效率上的考虑,则可以使用分区表。
------解决方案--------------------
按天分表,确实比较少见。估计楼主的需求也比较特殊。
------解决方案--------------------
探讨
按天分表,确实比较少见。估计楼主的需求也比较特殊。

------解决方案--------------------
探讨
引用:
按天分表,确实比较少见。估计楼主的需求也比较特殊。


按天分表,是最笨的设计方案了。

------解决方案--------------------
mysql分区表
探讨
引用:
引用:
按天分表,确实比较少见。估计楼主的需求也比较特殊。


按天分表,是最笨的设计方案了。

如果每天数据量超大,那怎么分表,我现在做到一个项目也是按天分表,一天要采集几千万的数据,这种应该怎么分?

------解决方案--------------------
探讨

SQL code

create procedure sp()
begin
if exists(select 1 from information_schema.TABLES where table_schema='test' and table_name='test') then
insert into test values(1);
end if;

end;


……