mysql中如何判断 一个数据表是否已经存在

1.
 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;
 
------------------------------------------
2. 
 可以用IF EXISTS 选项 ;
 
drop table 表名 if exists 表名;
 
SHOW TABLES LIKE 'TB1' ;
 
SELECT table_name FROM information_schema.TABLES WHERE table_name ='yourname';
------------------------------------------
3.
DROP PROCEDURE IF EXISTS mydb.Clear_DB;
CREATE PROCEDURE mydb.`Clear_DB`(  
  )
BEGIN
  DECLARE done INT DEFAULT 0; #游标的标志位
  DECLARE a varchar(20);
  DECLARE b varchar(20);
  DECLARE cur1 CURSOR FOR SELECT table_name FROM information_schema.TABLES WHERE table_name like 'http_200909%' ;
  DECLARE CONTINUE HANDLER FOR SQLSTATE '02000' SET done = 1;

  OPEN cur1;
  REPEAT
    FETCH cur1 INTO a;
    IF NOT done THEN  
       set b=concat(b,'select *  from ',a,' where name=http://scholar.google.com.hk union all'); # 拼 命令   
    END IF;
     
  UNTIL done END REPEAT;
  CLOSE cur1;
  SET @E=mid(b,1,length(b)-9);   # 去掉最后一个UNION ALL
       PREPARE stmt1 FROM @E;  
          EXECUTE stmt1; # 执行命令  
          DEALLOCATE PREPARE stmt1; #释放对象
END;
--------------------------------------
5. ×××××××××××××××××××有用的×××××××××××××××××××××
select `TABLE_NAME` from `INFORMATION_SCHEMA`.`TABLES` where `TABLE_SCHEMA`='dbname' and `TABLE_NAME`='tbname' 

返回不为空就存在,为空就不存在


如果是创建数据库和创建表的时候判断,可以这样

create database if not exists `dbname`;

DROP TABLE IF EXISTS `tbname`;
CREATE TABLE `tbname`(...)
------------------------------------
`INFORMATION_SCHEMA`.从版本5以后开始有的。之前只能用show tables like 'mytable';

       
       
mysql>  select  table_name  from  `INFORMATION_SCHEMA`.`TABLES`  where  table_name = 't
5'  and  TABLE_SCHEMA= 'test' ;
+ ------------+
| table_name |
+ ------------+
| t5         |
+ ------------+
1 row  in  set  (0.06 sec)
mysql> select table_name from `INFORMATION_SCHEMA`.`TABLES` where table_name ='t5' and TABLE_SCHEMA='test'; +------------+ | table_name | +------------+ | t5         | +------------+ 1 row in set (0.06 sec)
===============================
6. ×××××××××××××××××好用的××××××××××××××××××××××××××

如何判断mysql数据库中是否存在某张表

方法1、DROP TABLE IF EXISTS tablename;

方法2、做一个sql查询,比如:select * from tablename/select count(*) from tablename,如果返回值为空,则表不存在。

方法3、查询:SHOW TABLES LIKE table1,判断返回值。如:
if(mysql_num_rows(mysql_query("SHOW TABLES LIKE '".$table."'")==1)
{
	echo "Table exists";
} 
else
{
	echo "Table does not exist"; 
}
方法4、CREATE TABLE IF NOT EXISTS `yourdb`.`yourtable` (...) 
------------------------------
7.×××××××××××××××××××××good××××××××××××××××××××××××××××
mysql判断数据库或表是否存在
(1) 判断数据库存在, 则删除:  
      drop database if exists db_name;
(2) 判断数据表存在, 则删除:
      drop table if exists table_name; 
      
      
注: db_name, table_name可用``(1键旁边那个键)号引起来, 也可不引起来.
(1) 如果单纯显示是否存在数据库, 则可用
     show databases like 'db_name';
(2) 单纯显示是否存在数据表, 则可用 
     show tables like 'table_name'; 
注: 这里db_name, table_name必须用''(单引号)引起来.
------------------------------------

判断表是否存在

SELECT table_name FROM information_schema.TABLES WHERE table_name ='yourname';

判断存储过程是否存在

select * from information_schema.ROUTINES a where a.SPECIFIC_NAME='sp_analy setimeout'

  • 2
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值