Wishlist 0 ¥0.00

mysql设置外键约束开启-关闭

在MySQL中删除一张表或一条数据的时候,出现

[Err] 1451 -Cannot delete or update a parent row: a foreign key constraint fails (...)
这是因为MySQL中设置了foreign key关联,造成无法更新或删除数据。可以通过设置FOREIGN_KEY_CHECKS变量来避免这种情况。

 

我们可以使用

    SET FOREIGN_KEY_CHECKS=0;
来禁用外键约束.
 
之后再用
    SET FOREIGN_KEY_CHECKS=1;
来启动外键约束.
 
查看当前FOREIGN_KEY_CHECKS的值可用如下命令

    SELECT  @@FOREIGN_KEY_CHECKS;

mysql导入sql文件过大或连接超时的解决办法

前段时间出差在现场开发的时候,导入数据库老是出问题。最后发现了一个神奇sql语句交给实施,只要导入出错就把如下语句执行就可以了。至今屡试不爽。

  1. set global max_allowed_packet=100 000 000;   
  2.   
  3. set global net_buffer_length=100000;   
  4.   
  5. SET GLOBAL  interactive_timeout=28800 000;  
  6.   
  7. SET GLOBAL  wait_timeout=28800000  
以上语句的解释:

–max_allowed_packet=XXX 客户端/服务器之间通信的缓存区的最大大小;

–net_buffer_length=XXX TCP/IP和套接字通信缓冲区大小,创建长度达net_buffer_length的行

interactive_timeout = 10; 对后续起的交互链接有效;

wait_timeout 对当前交互链接有效;

=============================================

以上语句主要解决:连接超时,导入的sql文件过大。

使用‘##__’替换表前缀,也很方便

在很多时候,我需要使用sql语句,这样就会使用到类似__TABLENAME__样的表名称,但打字起来并不顺手,我希望简便一些,借鉴了他人的处理方式,使用##__代替表前缀,清晰可辨,书写简易。

例如:

$sql = "SELECT * FROM ##__tablename WHERE cat_id=100";

解析后的$sql为:

"SELECT * FROM shop_tablename WHERE cat_id=100";


要做到这样的效果,需要变化两个地方:



1.在ThinkPHP/Library/Think/Model.class.php的 1417 行附近,

把:
$sql = strtr($sql,array('__TABLE__'=>$this->getTableName(),'__PREFIX__'=>$this->tablePrefix));

修改为:
//$sql = strtr($sql,array('__TABLE__'=>$this->getTableName(),'__PREFIX__'=>$this->tablePrefix));
$sql = strtr($sql,array('__TABLE__'=>$this->getTableName(),'__PREFIX__'=>$this->tablePrefix, C('SQL_REPLACE')=>$this->tablePrefix)); //添加##__替换表前缀




2.在数据库配置中,加上一句:

'SQL_REPLACE' => '##__', //替换表前缀



3.删除Runtime缓存后,就可以使用了。

mysqldump: Got error: 1146: Table doesnt exist when using LOCK TABLES

[Email Question]

Hi, after a restore of a system failure now mysqldump give me an error of this type:

mysqldump: Got error: 1146: Table ‘wordpress.wp_bpspro_db_backup’ doesn’t exist when using LOCK TABLES

-- MySQL dump 10.13 Distrib 5.5.37, for debian-linux-gnu (x86_64)
--
-- Host: localhost Database: wordpress
-- ------------------------------------------------------
-- Server version 5.5.37-0ubuntu0.12.04.1-log

/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8 */;
/*!40103 SET @OLD_TIME_ZONE=@@TIME_ZONE */;
/*!40103 SET TIME_ZONE='+00:00' */;
/*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */;
/*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */;
/*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */;
/*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */;

In my database this table does not exist, and the system doesn’t allow to create a table with this name.
Can you give me some idea?
Thanks


Problem was resolved by doing these steps.

My case was exactly what is describe in this post:  http://www.linux.org/threads/resolving-mysql-error-1146-table-doesnt-exist-when-doing-backup.2480/

mysqlcheck -u mysql_username -p database_name -> it’s give me that the table does not exists

show tables; -> it’s give me that the table is in list
i dropped the table with command “drop table table_name;” and then “show tables,” does not list it anymore.

From that moment mysqldump works fine.

Another method instead of a command line method to do the same thing would be to use phpMyAdmin and run an SQL Query.

Click the SQL tab in phpMyAdmin
Type: show tables
Click the Go button to run the SQL Query
If you see this error message below…
database_name.table_name
Error: Table ‘database_name.table_name’ doesn’t exist
status: Operation failed
Note:  database_name and table_name are just an example database name and an example table name.  You will see your actual database name and table name.
…click the phpMyAdmin SQL tab again.
Type (enter your actual table_name):  drop table table_name
Click the Go button to run the SQL Query and drop/delete the corrupt database table.

 

About Us

Since 1996, our company has been focusing on domain name registration, web hosting, server hosting, website construction, e-commerce and other Internet services, and constantly practicing the concept of "providing enterprise-level solutions and providing personalized service support". As a Dell Authorized Solution Provider, we also provide hardware product solutions associated with the company's services.
 

Contact Us

Address: No. 2, Jingwu Road, Zhengzhou City, Henan Province

Phone: 0086-371-63520088 

QQ:76257322

Website: 800188.com

E-mail: This email address is being protected from spambots. You need JavaScript enabled to view it.