(1)
外键的使用:
外键的作用,主要有两个:
一个是让数据库自己通过外键来保证数据的完整性和一致性.
如:
有表A,表B中一字段是表A某一字段的外键.当删除表A记录时,表B相应记录会作相应处理(删除/或设置外键列为NULL).但若删除表B记录,表A的记录,则不会受影响!
一个就是能够增加ER图的可读性
有些人认为外键的建立会给开发时操作数据库带来很大的麻烦.因为数据库有时候会由于没有通过外键的检测而使得开发人员删除,插入操作失败.他们觉得这样很麻烦
其实这正式外键在强制你保证数据的完整性和一致性.这是好事儿.
例如:
有一个基础数据表,用来记录商品的所有信息。其他表都保存商品ID。查询时需要连表来查询商品的名称。单据1的商品表中有商品ID字段,单据2的商品表中也有商品ID字段。如果不使用外键的话,当单据1,2都使用了商品ID=3的商品时,如果删除商品表中ID=3的对应记录后,再查看单据1,2的时候就会查不到商品的名称。
当表很少的时候,有人认为可以在程序实现的时候来通过写脚本来保证数据的完整性和一致性。也就是在删除商品的操作的时候去检测单据1,2中是否已经使用了商品 ......
使用临时表的好处:
使用临时表存放中间结果,加速查询,或存放临时结果.
(1)
创建临时表很容易,给正常的CREATE TABLE语句加上TEMPORARY关键字:
CREATE TEMPORARY TABLE tmp_table (
name VARCHAR(10) NOT NULL,
value INTEGER NOT NULL
)
(2)
临时表将在你连接MySQL期间存在。当你断开时,MySQL将自动删除表并释放所用的空间。当然你可以在仍然连接的时
候删除表并释放空间。
DROP TABLE tmp_table -- 不用加temporary
(2.2)
临时表的常用方法:
create temporary table temp_user select * from user;
(3)
如果在你创建名为tmp_table临时表时名为tmp_table的表在数据库中已经存在,临时表将有必要屏蔽(隐藏)非临时
表tmp_table。
(4)
使用临时表加速查询
把表的一个子集进行排序并创建临时表,有时能加速查询。它有助于避免多重排序操作,而且在其他方面还能简
化优化器的工作。例如:
SELECT cust.name,rcvbles.balance,……other columns
from cust,rcvbles
WHERE cust.customer_id = rcvlbes.customer_id
AND rcvblls.balance>0
AND cust.postcode>“980 ......
(1)
存储过程是数据库服务器端的一段程序.
mysql的存储过程,只有版本是mysql5.0或以上的才有此特性.
(2)
什么时候需要用存储过程
存储过程通常是一些经常要执行的任务,这些任务往往是针对大量的记录而进行的。在服务器上执行存储过程,可以
改善应用程序的性能。这是因为:
2.1.
服务器往往具有强大的计算能力和速度。
2.2
避免把大量的数据下载到客户端,减少网络上的传输量。
2.3
存储过程只在创造时进行编译,以后每次执行存储过程都不需再重新编译,而一般SQL语句每执行一次就编译一次,所
以使用存储过程可提高数据库执行速度。
2.4
存储过程可以重复使用,可减少数据库开发人员的工作量
2.5
安全性高,可设定只有某此用户才具有对指定存储过程的使用权
(3)
3.1
使用存储过程之前,须换一种分隔符.因为在存储过程中,存在多个SQL语句,所有须用另一个分隔符来表示结束符号.
分隔符: --->只对当前连接有效.下次连接时,仍以";"为分隔符.
DELIMITER //
用回原来的";",可以用: delimiter ;
3.2
第二部分是过程名,合法标识符的问题:
存储过程名对大小写不敏感,因此‘P1’和‘p1&rs ......
导出
select field1,field2,field3 from tablename into outfile '/home/output1.csv' fields terminated by ','optionally enclosed by ''lines terminated by '\n';
导入
load data local infile '/home/output1.csv' into table tablename fields terminated by ','lines terminated by '\n'(field1,field2,field3);
注意:
1.写文件的目标目录需要是mysql拥有写权限的目录。 ......
先前在测试mysql connect c++接口的时候运行其官方提供的例子
21.5.5.6. MySQL Connector/C++ Complete Example 2
The following code shows a complete example of how to use MySQL Connector/C++:
/* Copyright 2008 Sun Microsystems, Inc.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; version 2 of the License.
There are special exceptions to the terms and conditions of the GPL
as it is applied to this software. View the full text of the
exception in file EXCEPTIONS-CONNECTOR-C++ in the directory of this
software distribution.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to ......
先前在测试mysql connect c++接口的时候运行其官方提供的例子
21.5.5.6. MySQL Connector/C++ Complete Example 2
The following code shows a complete example of how to use MySQL Connector/C++:
/* Copyright 2008 Sun Microsystems, Inc.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; version 2 of the License.
There are special exceptions to the terms and conditions of the GPL
as it is applied to this software. View the full text of the
exception in file EXCEPTIONS-CONNECTOR-C++ in the directory of this
software distribution.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to ......
MySQL手册中看到的,记录下。
MySQL可以为多个列创建索引。一个索引可以包括15个列。对于某些列类型,可以索引列的前缀(参见7.4.3节,“列索引”)。
多列索引可以视为包含通过连接索引列的值而创建的值的排序的数组。
MySQL按这样的方式使用多列索引:当你在WHERE子句中为索引的第1个列指定已知的数量时,查询很快,即使你没有指定其它列的值。
假定表具有下面的结构:
CREATE TABLE test (
id INT NOT NULL,
last_name CHAR(30) NOT NULL,
first_name CHAR(30) NOT NULL,
PRIMARY KEY (id),
INDEX name (last_name,first_name)
);
name索引是一个对last_name和first_name的索引。索引可以用于为last_name,或者为last_name和first_name在已知范围内指定值的查询。因此,name索引用于下面的查询:
SELECT * from test WHERE last_name='Widenius';
SELECT * from test
WHERE last_name='Widenius' AND first_name='Mic ......