数据库 SQL语句小结(更新中)

################ Navicat,单条执行sql ################

Navicat,数据库管理工具, 在查询的页面有好多命令,若单条执行:

1:可选中要执行的一条sql,右键—>运行已选择

2:ctrl+Shift+R

################ 根据旧表创建新表 ################

#1 只有全表结构,没有数据
create table stu_new like stu
#2 创建部分属性 ,不带结构带数据
create table stu_new1 as select id,name from stu
#3 创建部分属性表,不带结构不带数据
create table stu_new2 as select id,name from stu where 1=0

################ 删除表 ################

#1 删除表,只删除数据,主键继续递增
delete from stu
#2 删除表,只删除数据,主键重新编排
truncate table stu
#3 删除表结构及数据
drop table stu

################ 索引 ################

#1 创建索引
CREATE INDEX 索引名 ON 表名(列名)
#2 查看索引
show index from 表名
#3 删除索引
DROP INDEX 索引名 ON 表名

     注意:针对表中某一列创建索引后,在搜索时可以提高速度,但对模糊查询 like '%列名' 不起作用,

而且该列为字符型,查询时须加上 ' ' 。      

没有索引 select * from stu where stu_no=12345    慢
创建stu_no索引 select * from stu where stu_no=12345   无效
创建stu_no索引 select * from stu where stu_no='12345'    有效
创建stu_no索引 select * from stu where stu_no like '%12345'   无效
创建stu_no索引 select * from stu where stu_no like '12345%'    有效

 

                    

             

                                   

                                   

                                  

 ############### 针对低版本mysql,造成BLOB类型无法转化为String类型 ################

对于较低版本的mysql,使用GROUP_CONCAT 进行拼接,数据将变成BLOB类型,用String类型接收会出现错误。

解决方案:使用CAST(expr AS type)进行强转。

   例子:    select  GROUP_CONCAT( DISTINCT id ,':', name SEPARATOR  ',')   t  from stu ;  

   转化为   select  cast (GROUP_CONCAT( DISTINCT id ,':', name SEPARATOR  ',')  as char)   t from stu ;

                

posted @ 2015-06-29 11:10  sky_烁  阅读(409)  评论(0编辑  收藏  举报