数据库 SQL语句小结(更新中)
2015-06-29 11:10
423 查看
################ 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 '%列名' 不起作用,
而且该列为字符型,查询时须加上 ' ' 。
############### 针对低版本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 ;
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,使用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 ;
相关文章推荐
- 如何利用Navicat 导出和导入sql文件
- xampp安装配置memcache
- Mysql 在windows下切换用户
- mac下mysql安装和创建用户权限
- Oracle中 Instr 这个函数
- oracle ORA-01000: maximum open cursors exceeded问题的解决方法
- NoSQL数据库探讨 -- 非关系型数据库
- 阿里云PHP Redis代码示例
- 【MySQL】常见的mysql 进程state
- Linux下php安装Redis扩展
- SQL语句创建计划
- MySQL数据库备份的几种方式
- linux 下安装redis以及php Redis扩展
- MySQL基本数据类型与适用范围
- ***超强、超详细Redis数据库入门教程
- MySQL 动态行转列
- 数据库设计工具推荐
- ORACLE的sign函数和DECODE函数
- 在Ubuntu系统中安装MariaDB数据库的教程
- Redis 存储图片 [base64/url/path]vs[object]