IN/NOT IN/EXISTS/NOT EXISTS
简单用法 见http://space.itpub.net/12928228/viewspace-439094,其中EXISTS/NOT EXISTS用于取交集和差集。
SELECT DISTINCT pub_name FROM publishers WHERE EXISTS
(SELECT * FROM titles WHERE pub_id = publishers.pub_id AND type ='business');
SELECT pub_name FROM publishers WHERE NOT EXISTS
(SELECT * FROM titles WHERE pub_id = publishers.pub_id AND type ='business');
Oracle 中通过sql trace来分析NOT IN/MINUS/NOT EXISTS的比较 http://space.itpub.net/24862808/viewspace-708947
连接join(LEFT JOIN/RIGHT JOIN/内连接/全连接/自连接/交叉连接)
基本知识参考http://www.cnblogs.com/eflylab/archive/2007/06/25/794278.html
Tip:用连接来代替子查询
tab1 LEFT JOIN tabl2 ON tab1.yy = tab2.zz WHERE
LETT JOIN以其左边的表为基准,RIGHT JOIN以其右边表为基准。
SQL查询的基本原理
第一、 单表查询:根据WHERE条件过滤表中的记录,形成中间表(这个中间表对用户是不可见的);然后根据SELECT的选择列选择相应的列进行返回最终结果。
第二、 两表连接查询:对两表求积(笛卡尔积)并用ON条件和连接连接类型进行过滤形成中间表;然后根据WHERE条件过滤中间表的记录,并根据SELECT指定的列返回查询结果。
第三、 多表连接查询:先对第一个和第二个表按照两表连接做查询,然后用查询结果和第三个表做连接查询,以此类推,直到所有的表都连接上为止,最终形成一个中间的结果表,然后根据WHERE条件过滤中间表的记录,并根据SELECT指定的列返回查询结果。 理解SQL查询的过程是进行SQL优化的理论依据。
子查询与连接查询的区别
索引
非聚集索引和聚集索引
默认是非聚集索引。
对于有大量重复值,且经常有范围查询,order by,group by发生的列,考虑使用聚集索引。
http://database.51cto.com/art/201007/210727.htm
Mysql实现分页
SELECT * from tab LIMIT offset rows;
-------------------------
ubuntu下安装mysql
mysql安装文件 mysql-5.5.25-ubuntu10.04-i686.deb
$ sudo dpkg -i mysql-5.5.25-ubuntu10.04-i686.deb
查找mysql安装目录(-mmin -10最近修改10分钟修改过)
$ sudo find . -path "*mysql*" -mmin -10
发现在/opt 目录下
$ sudo chown -R guxu . (修改文件所有者)
$ groupadd mysqlgroup (添加组mygroup)
groupadd: cannot lock /etc/group; try again later.
guxu@guxu-Lenovo-B460:/opt$ sudo groupadd mysqlgroup
$ useradd -g guxu mysqlgroup ( 将guxu用户添加到mygroup组中)
useradd: cannot lock /etc/passwd; try again later.
guxu@guxu-Lenovo-B460:/opt$ sudo useradd -g guxu mysqlgroup
$ scripts/mysql_install_db --user=guxu (初始化数据库,添加用户名为guxu的用户)
出错如下:
/opt/mysql/server-5.5/bin/mysqld: error while loading shared libraries: libaio.so.1:
cannot open shared object file: No such file or directory
原因是未安装libaio,执行安装
$ sudo apt-get install libaio-dev
$ find /lib -name "*libaio*"
/lib/libaio.so.1
/lib/libaio.so.1.0.1
再次执行下面命令,安装成功,并显示一些有用的信息:
Installing MySQL system tables...
OK
Filling help tables...
OK
To start mysqld at boot time you have to copy
support-files/mysql.server to the right place for your system
PLEASE REMEMBER TO SET A PASSWORD FOR THE MySQL root USER !
To do so, start the server, then issue the following commands:
/opt/mysql/server-5.5/bin/mysqladmin -u root password 'new-password'
/opt/mysql/server-5.5/bin/mysqladmin -u root -h guxu-Lenovo-B460 password 'new-password'
Alternatively you can run:
/opt/mysql/server-5.5/bin/mysql_secure_installation
which will also give you the option of removing the test
databases and anonymous user created by default. This is
strongly recommended for production servers.
See the manual for more instructions.
You can start the MySQL daemon with:
cd /opt/mysql/server-5.5 ; /opt/mysql/server-5.5/bin/mysqld_safe &
You can test the MySQL daemon with mysql-test-run.pl
cd /opt/mysql/server-5.5/mysql-test ; perl mysql-test-run.pl
$ cp support-files/my-medium.cnf /etc/mysql/my.cnf
$ cp support-files/mysql.server /etc/init.d/
$ sudo chown -R guxu init.d/mysql.server (修改所有者)
$ sudo mv init.d/mysql.server init.d/mysql (重命名,可以执行service mysql stop/start/restart)
未完,继续http://blog.163.com/zjc_8886/blog/static/2408175201107105632955/