自己学习记录用
show databases;
use mysql;
show tables;
show columns from customers;//查看customers表的表列
show status;
show create database; //查看创建的特定的数据库
show grants; //查看授权用户安全权限
show errors;或show warnings;//查看服务器错误或警告信息
selecl语句 用于检索数据
select a from aa;//从aa表检索名为a的列
select a,b,c from aa;//检索多个列,从aa表检索名为a,b,c的列
select * from aa;//检索aa表的所有列
select distinct a from aa;//使用DISTINCT关键字,可以只返回每行唯一的值,即若值相同的话,不会在检索结果出现第二次
limit字句
select a from aa limit 5;//LIMIT 5指示检索结果只返回5行的内容
select a from aa limit 5,5;//LIMIT 5, 5指示MySQL返回从行5开始的5行。第一个数为开始位置,第二个数为要检索的行数,即从第五行开始,检索出第六到第十行的内容
select aa.a from aa;//使用完全限定的表名
order by 字句 用于给检索出的结果排序
select a from aa order by a;//order by a为给列名为a的列排序
select a,b,c from aa order by b,c;//选中a,b,c列,给b,c列排序
select a,b form aa order by a desc,b;//默认情况为升序排序,使用desc关键字可以降序排序,desc只应用于位于其前的列,所以该语句只给a降序,b照常排序
select a from aa order by a desc limit 1;//将order by于limit结合,检索出值最大的一行
where字句 用于数据过滤
select a,b from aa where num=2;//检索出a,b列中num值为2的行
where字句的操作符:=、<>(不等于)、!=(不等于)、 <、 >、 <=、 >=、 between(指定的两个值之间)
select a,b from aa where num between 5 and 10;//检索出介于5与10之间的值
select a form aa where num is null;//检索列中存在null值的行
select a,b from aa where num=3 and age=10;//检索出同时满足num=3,age=10条件的行
select a,b from aa where num=3 or num=5;//检索出只要满足其中一个条件的行
select a,b from aa where (num=3 or num=5) and age =10;//and操作符在计算次序中优先级高,所以要加()让()的先条件过滤,不然它会先过滤and后的条件
select a,b from aa where num in (3,5);//该语句功能等同于使用or时的功能,检索出num=3和num=5的所有行
select a,b from aa where num not in (3,5);//检索出num不等于3或5的所有行
***
利用like操作符 配合通配符 %(匹配多个字符) 和 _ (匹配单个字符)来过滤条件
***
select name,age from aa where name like 'aa%';//匹配name列中以aa开头的所有行
select name,age from aa where name like '%abc%';//匹配name列中只要存在abc这个值的所有行
select name,age from aa where name like '_ aa';//匹配出如包含1 aa,2 aa,3 aa值的行
select name,age from aa where name like '% aa';//若是%的话,可能匹配出 .1 aa值的行
在where字句中使用regexp关键字 用正则表达式来过滤搜索
select num from aa where num regexp '10';//过滤出num列中包含10的行
select num from aa where num regexp '.10';//过滤出num列中包含".10"的行
select num from aa where num regexp '10|20';//正则表达式的or操作
select name from aa where name regexp '\\.';//过滤出以点结尾的行,\\.表示查找点
在正则中用\\来转义那些具有特殊意义的字符,比如点,|,?,\什么的
\\f(换页) \\n(换行) \\r(回车) \\t(制表) \\v(纵向制表)
****正则中的字符类的匹配*****
[:alnum:] 任意字母和数字(同[a-zA-Z0-9])
[:alpha:] 任意字符(同[a-zA-Z])
[:blank:] 空格和制表(同[\\t])
[:cntrl:] ASCII控制字符(ASCII 0到31和127)
[:digit:] 任意数字(同[0-9])
[:graph:] 与[:print:]相同,但不包括空格
[:lower:] 任意小写字母(同[a-z])
[:print:] 任意可打印字符
[:punct:] 既不在[:alnum:]又不在[:cntrl:]中的任意字符
[:space:] 包括空格在内的任意空白字符(同[\\f\\n\\r\\t\\v])
[:upper:] 任意大写字母(同[A-Z])
[:xdigit:] 任意十六进制数字(同[a-fA-F0-9])
******************************
***********正则中的重复元字符********
元 字 符 说 明
* 0个或多个匹配
+ 1个或多个匹配(等于{1,})
? 0个或1个匹配(等于{0,1})
{n} 指定数目的匹配
{n,} 不少于指定数目的匹配
{n,m} 匹配数目的范围(m不超过255)
*****************************************
例子:
select name from producets where name regexp 'stacks?';//匹配包含stack或stacks的行。stacks的s后面接?,表示s会出现0或1次
select name from producets where name regexp 'a{4}';//匹配a出现了四次的行
select name from producets where name regexp 'a{4,}';//匹配a出现了不少于四次的行
select name from producets where name regexp 'a{4,6}';//匹配a出现了四次到六次的行
*****正则中的定位符*****
元 字 符 说 明
^ 文本的开始
$ 文本的结尾
[[:<:]] 词的开始
[[:>:]] 词的结尾
*****************************
计算字段 <---> 将位于不同表列的同一行字段拼接在一起,形成一个新的列,并用as关键字给其创建别名
字符串拼接用Concat()函数
例如:
select Concat(vend_name,' (',vend_country,')') from vendors order by vend_name;
//Concat()拼接串,即把多个串连接起来形成一个较长的串。上面的SELECT语句连接以下4个元素: 存储在vend_name列中的名字;包含一个空格和一个左圆括号的串;存储在vend_country列中的国家;包含一个右圆括号的串。
select Concat(RTrim(vend_name),' (',vend_country,')') from vendors order by vend_name;
//使用RTrim()函数可以去掉值右边的所有空格
别名alias 用AS关键字
select Concat(vend_name,' (',vend_country,')') as vent_title from vendors;//给拼接的字符串指定了一个别名vent_title
***执行算术计算***
select num1,num2,num3,num2*num3 as num2*3 form num;//匹配num1,num2,num3,并将num2,num3相乘并设置别名
****************函数******************
##########常用的文本处理函数###########
函 数 说 明
Left() 返回串左边的字符
Length() 返回串的长度
Locate() 找出串的一个子串
Lower() 将串转换为小写
LTrim() 去掉串左边的空格
Right() 返回串右边的字符
RTrim() 去掉串右边的空格
Soundex() 返回串的SOUNDEX值,SOUNDEX考虑了类似的发音字符和音节,使得能对串进行发音比较而不是字母比较
SubString() 返回子串的字符
Upper() 将串转换为大写
#########################
Soundex()函数例子:
select cust_name,cust_contact from customers where Soundex(cust_contact)=Soundex('Y Lie');
//假设现在cust_contact列中存在一行的值为“Y Lee”,若不加Soundex()函数的话,因为匹配错误,
将不检索出该行;而若加上Soundex()函数,由于Lee与Lie发音相似,则将该行检索出来
#############常用日期和时间处理函数#############
函 数 说 明
AddDate() 增加一个日期(天、周等)
AddTime() 增加一个时间(时、分等)
CurDate() 返回当前日期
CurTime() 返回当前时间
Date() 返回日期时间的日期部分
DateDiff() 计算两个日期之差
Date_Add() 高度灵活的日期运算函数
Date_Format() 返回一个格式化的日期或时间串
Day() 返回一个日期的天数部分
DayOfWeek() 对于一个日期,返回对应的星期几
Hour() 返回一个时间的小时部分
Minute() 返回一个时间的分钟部分
Month() 返回一个日期的月份部分
Now() 返回当前日期和时间
Second() 返回一个时间的秒部分
Time() 返回一个日期时间的时间部分
Year() 返回一个日期的年份部分
##########################################
例子:下列的两个方法检索出2005年9月下的所有数据
select cust_id,order_num from orders where Data(order_data) between '2005-09-01' and '2005-09-30';
select cust_id,order_num from orders where Year(order_data)=2005 and Month(order_data)=9;
##############常用数值处理函数
函 数 说 明
Abs() 返回一个数的绝对值
Cos() 返回一个角度的余弦
Exp() 返回一个数的指数值
Mod() 返回除操作的余数
Pi() 返回圆周率
Rand() 返回一个随机数
Sin() 返回一个角度的正弦
Sqrt() 返回一个数的平方根
Tan() 返回一个角度的正切
##########################
汇总数据
################SQL聚集函数#########
函 数 说 明
AVG() 返回某列的平均值
COUNT() 返回某列的行数,两个用法:COUNT(*),返回包含null值的行总数数;COUNT(列名),返回不包含null值的行总数
MAX() 返回某列的最大值
MIN() 返回某列的最小值
SUM() 返回某列值之和
##############################
select AVG(distinct prod_price) as avg_price from products where vend_id =1003;//加入distinct参数,在计算平均值时则只考虑将不同的值进行平均计算
组合聚集函数:
select COUNT(*) as num_items,MIN(prod_price) as price_min,MAX(prod_price) as price_max,AVG(prod_price) as price_vag from products;
**************分组数据*************
group by字句
select vend_id,COUNT(*) as num_prods from products group by vend_id;//按vend_id分组,组中的每个vend_id都对应着自己的行数
利用having字句进行过滤分组
select cust_id,COUNT(*) as order from orders group by cust_id having COUNT(*)>=2;//给cust_id分组,并过滤出行数大于2的那些分组
select vend_id,COUNT(*) as num_prods from products where prod_price >=10 group by vend_id having COUNT(*) >=2;//先过滤出prod_price>=10的行,按vend_id分组后,再过滤出组中>=2的分组
利用group by分组后,再语句最后加上order by字句进行排序
*************SELECT子句及其顺序
子 句 说 明 是否必须使用
SELECT 要返回的列或表达式 是
FROM 从中检索数据的表 仅在从表选择数据时使用
WHERE 行级过滤 否
GROUP BY 分组说明 仅在按组计算聚集时使用
HAVING 组级过滤 否
ORDER BY 输出排序顺序 否
LIMIT 要检索的行数 否
******************************************************************
drop命令 例如: drop database aaa;drop tables bbb;利用drop命令可以删除创建的数据库或表
表操作命令:select、insert update delete ---->这四条命令用于表内容操作
insert into aa(aa,bb,cc) values('1aa','2bb','3cc');//往aa表插入数据,定义aa,bb,cc列名,在values里写入对应的值
update aa set name='aa' where age='10';//当age=10时,更改name为aa
delete from aa;//删除aa表所有行,当aa表还是存在的
delete from aa where age='10';//删除aa表中age=10的那一行
表创建
create table customers
(
id int not null auto_increment,
name char(50) not null ,
address char(50) null ,
city char(50) null ,
primary key (id)
)engine=innodb
//创建customers表,定义各个列名、数据类型,通过primary key指定表的主键,通过engine=innodb选择表的引擎类型
安全管理
创建用户账号
create user aaa identified by '000000'; //创建一个数据库用户aaa,设置它的密码为000000
rename user aaa to bbb;//将aaa用户名更改为bbb
drop user bbb; //将bbb这个账号删除(包括相关的权限)
show grant for aaa;//通过该条命令可以查看创建的用户有什么权限
grant select on aa.* to bbb;//授权给bbb用户具有aa数据库下所有表的select访问权限,即只读访问权限
revoke select on aa.* from bbb;//取消赋予bbb用户的select权限(该权限必须存在,不然会报错)
grant select,delete,update,create on *.* to examuser@localhost identified by '000000'
更改用户口令(密码)
set password for bbb = Password('111111'); //将用户bbb的密码更改为111111
set password = Password('123456'); //若不指定用户,则更改当前用户密码