使用命令:
CREATE TABLE IF NOT EXISTS `test`.`t1` (
`col` VARCHAR(16) NOT NULL
) ENGINE=MEMORY;
在 MySQL 查询浏览器中运行两次会导致:
表 't1' 已存在 错误 1050
我原以为创建表“IF NOT EXISTS”不会引发错误。我错过了什么还是这是一个错误?我正在运行5.1版。谢谢。
使用命令:
CREATE TABLE IF NOT EXISTS `test`.`t1` (
`col` VARCHAR(16) NOT NULL
) ENGINE=MEMORY;
在 MySQL 查询浏览器中运行两次会导致:
表 't1' 已存在 错误 1050
我原以为创建表“IF NOT EXISTS”不会引发错误。我错过了什么还是这是一个错误?我正在运行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
您可以使用以下查询在 MySql 中为特定数据库创建表。
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'
(在 OS X 10.6 上运行)
使用以下参数创建 mysql 连接。“'raise_on_warnings':错误”。它将忽略警告。例如
config = {'user': 'user','password': 'passwd','host': 'localhost','database': 'db', 'raise_on_warnings': False,}
cnx = mysql.connector.connect(**config)
我在debian上遇到了与@CraigWalker类似的问题:我的数据库处于DROP TABLE
失败状态,因为它找不到表,但CREATE TABLE
也失败了,因为MySQL认为表仍然存在。因此,尽管当我查看 phpmyadmin 时它不存在,但损坏的表仍然存在于某个地方。
我只是通过复制包含带有一些MyISAM
和一些InnoDB
表的数据库的整个文件夹来创建这个状态
cp -a /var/lib/mysql/sometable /var/lib/mysql/test
(不建议这样做!)
test
在 phpmyadmin的新数据库中不可见的所有 InnoDB 表。
sudo mysqladmin flush-tables
也没有帮助。
我的解决方案:我不得不删除新的测试数据库drop database test
并复制它mysqldump
:
mysqldump somedatabase -u username -p -r export.sql
mysql test -u username -p < export.sql
如果有人在 Phpmyadmin 导出后遇到此错误,请使用自定义选项并添加“删除表”语句清除此错误。
好吧,已经提供了很多答案,而且很多答案也很有意义。
有些人提到这只是警告,有些人提供了一种临时方法来禁用警告。当您的数据库中的事务数量很高时,所有这些都会起作用,但会增加风险。
我今天遇到了类似的情况,这是我提出的查询...
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
or Oracle
。