使用以下命令:
CREATE TABLE IF NOT EXISTS `test`.`t1` (
`col` VARCHAR(16) NOT NULL
) ENGINE=MEMORY;
在MySQL Query Browser中运行此命令两次会导致:
表 't1' 已经存在 错误代码1050
我本以为使用"IF NOT EXISTS"语句创建表时不会出现错误。我是否遗漏了某些内容或这是一个bug?我正在运行版本5.1。谢谢。
使用以下命令:
CREATE TABLE IF NOT EXISTS `test`.`t1` (
`col` VARCHAR(16) NOT NULL
) ENGINE=MEMORY;
在MySQL Query Browser中运行此命令两次会导致:
表 't1' 已经存在 错误代码1050
我本以为使用"IF NOT EXISTS"语句创建表时不会出现错误。我是否遗漏了某些内容或这是一个bug?我正在运行版本5.1。谢谢。
在我的5.0.27版本中完全正常
我只是收到一个警告,而不是错误提示,表已经存在;
如前所述,这只是一个警告而不是错误,但(如果像我一样)您希望事情无警告运行,可以禁用该警告,然后在完成后重新启用它。
SET sql_notes = 0; -- Temporarily disable the "Table already exists" warning
CREATE TABLE IF NOT EXISTS ...
SET sql_notes = 1; -- And then re-enable the warning again
create database if not exists `test`;
USE `test`;
SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0;
/*Table structure for table `test` */
CREATE TABLE IF NOT EXISTS `tblsample` (
`id` int(11) NOT NULL auto_increment,
`recid` int(11) NOT NULL default '0',
`cvfilename` varchar(250) NOT NULL default '',
`cvpagenumber` int(11) NULL,
`cilineno` int(11) NULL,
`batchname` varchar(100) NOT NULL default '',
`type` varchar(20) NOT NULL default '',
`data` varchar(100) NOT NULL default '',
PRIMARY KEY (`id`)
);
create database if not exists `test`;
USE `test`;
SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0;
/*Table structure for table `test` */
***CREATE TABLE IF NOT EXISTS `tblsample` (
`id` int(11) NOT NULL auto_increment,
`recid` int(11) NOT NULL default '0',
`cvfilename` varchar(250) NOT NULL default '',
`cvpagenumber` int(11) NULL,
`cilineno` int(11) NULL,
`batchname` varchar(100) NOT NULL default '',
`type` varchar(20) NOT NULL default '',
`data` varchar(100) NOT NULL default '',
PRIMARY KEY (`id`)
);***
DROP TABLE
操作失败,但是CREATE TABLE
同样失败,因为MySQL认为该表已经存在。(这种状态可能会干扰您的IF NOT EXISTS子句)。sudo mysqladmin flush-tables
对于我来说,在没有sudo
的情况下,我会遇到以下错误:
mysqladmin: refresh failed; error: 'Access denied; you need the RELOAD privilege for this operation'
config = {'user': 'user','password': 'passwd','host': 'localhost','database': 'db', 'raise_on_warnings': False,}
cnx = mysql.connector.connect(**config)
raise_on_warnings
设置为"true",所以即使我使用了IF NOT EXISTS
,也会引发异常。 - Pikamander2我在Debian上遇到了与@CraigWalker类似的问题:我的数据库处于这样的状态,即DROP TABLE
失败,因为找不到表,但CREATE TABLE
也失败,因为MySQL认为该表仍然存在。因此,尽管我在phpmyadmin中查看时它并不存在,但这个损坏的表仍然存在于某个地方。
我通过复制包含一些MyISAM
和一些InnoDB
表的数据库的整个文件夹来创造了这种状态。
cp -a /var/lib/mysql/sometable /var/lib/mysql/test
(这不是推荐的做法!)
在 phpmyadmin 中,所有 InnoDB 表格都无法在新建立的数据库 test
中看到。
sudo mysqladmin flush-tables
也没有帮助。
我的解决方案:我必须使用 mysqldump
删除新的 test 数据库并将其复制。
mysqldump somedatabase -u username -p -r export.sql
mysql test -u username -p < export.sql
已经提供了很多答案,而且很多都是有道理的。
有些人提到这只是警告,有些人提供了一种暂时禁用警告的方法。所有这些方法都可以解决问题,但是当您的数据库中的交易数量很高时,会增加风险。
我今天遇到了类似的情况,这里是我想出的查询语句...
declare
begin
execute immediate '
create table "TBL" ("ID" number not null)';
exception when others then
if SQLCODE = -955 then null; else raise; end if;
end;
/
这很简单,如果在运行查询时出现异常,它将被抑制。您可以将其用于 SQL
或 Oracle
。