`

mysql常用命令

 
阅读更多

describe tablename;  显示表的列结构

show  status , 用于显示广泛的服务器状态信息

show create database  name, 显示创建数据库的sql语句

show create table name  , 显示创建表的sql语句

select distinct columnname  , 选择不同值的列表

limit n , m   n从0开始,选取m列

order by 默认升序,降序为DESC ,位于from语句之后,limit之前

where比较语句中,between a and b  指定位于a、b之间,

in (a,b) 条件中符合a或者b, 比or更直观,更高效

mysql支持使用not对in、between和exists子句取反, where not in、between、exits

空值检查  where column is null

组合where子句中,优先处理and,再处理or操作符,所以条件的组合最好用小括号明确标出

like语句中,%表示任何字符出现任意次数,_下划线只匹配单个字符而不是多个字符,通配符效率低,尽量防在条件的后面

where条件判断中可以加 正则表达式,regexp '表达式' ,

    [123]   匹配1或2或3

    [^123]  匹配除1,2,3之外的字符

    ^[123]  匹配123开头的字符,$表示结尾

    [1-9]  匹配1到9之间的任何值

    {n}重复n次

    \\为转义字符,如\\.   \\-

concat()拼接字符串函数

Date() 返回日期时间的日期部分,如where Date(date) between '2005-09-01' and '2005-09-30';

Day() 返回一个日期的天数部分

聚集函数用来汇总数据,如avg(),count(),max(),min(),sum()

分组数据,group by 子句指示mysql分组数据,然后对每个组而不是整个结果集进行聚集,条件筛选用having而不是where,where没有分组的概念,where过滤行,having过滤分组

select  子句及其顺序  select -> from -> where -> group by -> having -> order by -> limit

select 子查询  如select from where in (select ...) 效率不高

 

外键为某个表中的一列,它包含另一个表的主键值,定义了两个表之间的关系,维护了引用完整性

inner join 并不以谁为基础,它显示符合条件记录

left join 以左表为基础,把右表关联进来,左表全部显示,右表中没有符合条件时为NULL也要显示出来

right join  刚好相反

union  组合查询多条select子句,功能上类似where语句中 or 分隔查询条件

MyIsam引擎提供全文本搜索,如:where Match(text) Aggainst('rabbit'),将检索出含有rabbit的列

 

插入数据格式

insert into tablename(c1,c2....) values (a,b....)

可以用insert select  将select出的数据作为insert的values

update tablename  set   where

delete from tablename where

alter table tablename add  columnname char(20);  alter table tablename drop column columnname;

alter table tablename add constraint fk_identy foreign key (a,id) references tableb (b.id);

 

视图是虚拟的表,只包含使用时动态检索数据的查询,可以重用sql语句,简化复杂的sql操作,保护数据

视图主要用于数据检索,而不用于更新

1.隐藏复杂sql,这通常都会涉及联接

    create view name as select表联接语句

2. 用视图重新格式化检索出的数据

    create view name as select格式化取出字段

   

存储过程就是为以后使用而保存的一条或多条mysql语句的集合,可将其视为批处理文件。复用性和安全性

call  存储过程(@参数);create procedure 存储过程(输出参数) begin ...  end

mysql游标只能用于存储过程

mysqldump用于数据库备份

 

如果Table1和Table2都存在,可使用以下语句复制表

INSERT INTO SELECT语句

      语句形式为:Insert into Table2(field1,field2,...) select value1,value2,... from Table1

 

 

 

 

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics