MYSQL学习笔记

win10下

初学配合sqlyog可视化工具使用,方便理解。在界面中操作时,也可以查看到对应的脚本代码

MYSQL安装教程,建议配置环境变量

sqlyog网上下载一个**版即可。

0.基本命令

进入mysql后,每段代码以;结尾才会执行,直接按回车不执行。mysql代码不区分大小写,大写为了视觉强调(或者可以考虑命令用大写,变量用小写,自己习惯即可)

mysql -u root -p 通过cmd进入mysql

set password for [email protected] = password('123');   更改密码(@后为用户名,本地服务器一般就是localhost,引号内为密码)

source c:\mysql\populate.sql 执行一个已经编写好的脚本文件。(这里导入了SQL必知必会教程内的内容,后面以此为例)

show databases; 返回可用的数据库

use aaa;选择一个数据库,之后的操作便在该数据库下进行

show tables; 返回数据库内的列表

MYSQL学习笔记

show columns from customers; 展示其中一个表的结构

简写方式:describe customers;

MYSQL学习笔记

1、检索命令

select prod_name from products; 展示pruducts表中,prod_name中的元素,展示多列时用逗号隔开

select prod_name,others from products;

select * from products;

select distinct vend_id from products; 只返回不同值

select prod_name from products limit 5;只展示5个

select prod_name from products limit 3,4; 展示从行3开始的4个

select prod_name from products limit 4 offset 3;

 

MYSQL学习笔记

2、排序

select prod_name from products order by prod_name;默认以字母顺序(ASCII码?)排序

MYSQL学习笔记

select prod_id , prod_price,prod_name from products order by prod_price,prod_name;

选择多组数据以多个指标进行排序(price相同情况下,再根据name排序)

MYSQL学习笔记

 

3、数据过滤

select prod_name,prod_price from products where prod_price=2.50;

where表示指定的过滤条件,放在from之后,order之前

大小判断符号与一般编程语言相通,表示两者之间用between

select prod_name,prod_price from products where prod_price<=10;

select vend_id,prod_name from products where vend_id != 1003;

select prod_name,prod_price from products where prod_price between 5 and 10;

select prod_name from products where prod_price is null;

Null表示空值,含有空格、0等均不是空值

 

可以通过and or in not语句组合where条件,其中and优先级高于or

select prod_name,prod_price from products where (vend_id=1002 or vend_id=1003)and prod_price >=10;

MYSQL学习笔记

为了消除阅读歧义,通常即使顺序符合优先级,也要添加括号

 select prod_name,prod_price from products where vend_id in(1002,1003) order by prod_name;

in的使用比Or更加灵活,执行速率也更快

 select prod_name,prod_price from products where vend_id not in(1002,1003) order by prod_name;

----------------------------------------------------------------------------------------------------------

通配符:用来匹配值的特殊字符

like关键词用于引出通配符比较

%用于指代任意个字符

_用于指代1个字符

select prod_id,prod_name from products where prod_name like 'jet%';

表示查找以jet开头的任意词

MYSQL学习笔记

select prod_id,prod_name from products where prod_name like '%anvi%';

表示任意位置包含anvi

%可以指代0~任意个字符,注意表示后缀时,尾部若有空格会干扰查找

 

select prod_id,prod_name from products where prod_name like'_ ton anvil';

MYSQL学习笔记

通配符搜索效率较低

 

----------------------------------------------------------------------------------------------------------

正则表达式:用一组“规则字符串”来表达对字符串的逻辑过滤

select prod_name from products where prod_name regexp '.000' order by prod_name;

.在正则表达式中表示一个字符,与通配符_类似

或的正则表达式

select prod_name from products where prod_name regexp '1000|2000' order by prod_name;

select prod_name from products where prod_name regexp '[123] ton' order by prod_name;

MYSQL学习笔记

[123]等同于[1|2|3],其他的正则表达式有[^123]表示否定,[1-3]表示范围

 

| . ^这些字符被用作正则表达式,若查找的内容刚好需要包含这些字符,需要用\\引导

select vend_name from vendors where vend_name regexp '\\.' order by vend_name;

MYSQL学习笔记

匹配指定字符集:

MYSQL学习笔记

指定匹配数目:

MYSQL学习笔记

定位符:

MYSQL学习笔记