Mysql笔记

206 阅读6分钟

1. 什么是MySQL

数据的所有存储、检索、管理和处理实际上是由数据库软件--DBMS(数据库管理系统)完成的。MySQL是一种DBMS,即它是一种数据库软件。

1.1 客户机-服务器软件

DBMS可分两类:

  • 基于文件系统
  • 基于客户机-服务器 Mysql、Oracle和SQL server等数据库是基于客户机-服务器 用户通过请求数据(查询性别为男的用户列表)—> 客户机软件通过网络提交该请求给服务器软件。服务器软件处理这个请求,根据需要过滤、丢弃和排序数据,结果返回到客户机软件。 客户机和服务器不管是否安装在两台或者一台计算机上,客户机软件都要与服务器软件进行通信。

1.2 连接数据库

在dos窗口下使用命令行进入数据库

mysql -u root(用户名) -p密码

2. select语句

2.1 select查询

select 表中单个列名 from 表名;检索单列数据

select 表中多个列名(列名用逗号隔开) from 表名;检索多列数据

2.2 distinct关键字

当检索的单列中有很多重复值时,使用distinct关键字可以检索唯一值

select distinct 列名 from 表名;

不能部分使用distinct关键字 如:select distinct 列名,列名 from 表名

当distinct关键字指示多列时,除非两个列的数据都不相同,否则将检索所有行(不会检索部分单列的重复项)

2.3 限制结果 limit子句

select 表中列名 from 表名
limit 5; (默认从第一行(0)开始索引后面五行)
select 表中列名 from 表名
limit 55; (起始行,索引行数)
另一种代替语法:
limit 4 offset 3 (从第三行开始,索引后面四行)

2.4 排序数据 order by子句

select id,name from 表名
order by id;
order by id desc;

关键字desc 降序排序 asc(ascending)升序排序

只应用到直接位于其前面的列名

select id,name from 表名
order by id desc,name;(只按id降序排序)

可通过order by和limit可找出一列中最高和最低的值

2.5 过滤数据 where子句

select id,name from 表名
where name='1000';
where id between A and B  AB之间范围筛选
where id is null; 空值索引

2.6 组合where子句

where name='1000' and/(or) id is null;

多条where子句过滤数据

and优先级高于or 当需要保持次序时可以用()

where A or B and C;  将会先执行BandC
where (A or B) and C; 这样能保证AB的附加条件上与C同时筛选

2.7 in not关键字

in操作符可替代上述();

where id in (1000,1001);
in用法等同于
where id=1000 or id=1001;
否定之后的条件
where id not in (1000,1001);

2.8 通配符

like操作符是谓词 指示后面跟的搜索模式利用通配符匹配而不是直接相等匹配

2.8.1 百分号(%)通配符

where id like 'ss%'; 索引ss开头的所有字符
where id like '%ss%'; 包含ss的所有字符
where id like 's%e'; s开头e结尾的所有字符

2.8.2 下划线(_)通配符

%通配符的区别在于一个 _通配符只能识别单个字符

where id like '_ss' 索引?ss的所有字符

3. 正则表达式

如果你需要查找名字中间有数字的所有文件,一个文本块中找到所有重复的单词,这些都可以使用一个正则表达式

REGEXP(regexp)后面跟的是正则表达式

where name regexp '1000'; 索引包含1000字符的内容
where name regexp '.000' 任意如1000,2000,3000的字符
where name regexp binary 'sss';  用binary关键字区分大小写

3.1 or匹配

where name regexp '1000|2000'; 索引1000和2000的信息
where name regexp '[123] Ton' 索引1|2|3 Ton的信息 也可以用[1-3]=[123]
where name regexp '\\.' 当需要匹配特殊字符时,需要用\\.查找.
where name regexp '^[0-9\\.]' 索引由1-9和.开头的信息
^表示文本开头的  $表示文本结尾的

3.2 字符拼接

mysql使用Concat()函数来实现拼接

select Concat(name,'(',country,')')
from 表名 索引name (country)

3.3 执行算术计算

select  id, 
        quantity,
        price,
        quantity*price AS expanded_price
from 表名

4. 汇总数据

4.1 聚集函数(aggregate function)

AVG() 平均值
count() 返回某列的行数
max() 最大值
min() 最小值
sum() 总和
select avg(price) AS avg_price
from 表名
distinct关键字排除相同的值
select avg(distinct avg_price) AS avg_price
from 表名

GROUP BY 分组

select id,count(*) as num_prods
from 表名
group by id;

having类似于where关键词,where是对分组前进行筛选,而having是对分组后筛选

select id,count(*) as num_prods
from 表名
group by id;
having count(*) >= 2;

5. 子查询

子查询嵌套用()包含,又内向外处理

select cust_name,cust_contact
from 表名
where cust_id = IN (select cust_id,count(*) as num_prods
from 表名)

6. 多表联结

外键:为某个表中的一列,它包含另一个表的主键值,定义了两个表之间的关系

等值联结:

select vend_name,prod_name,prod_price
from vendors,products
where vendors.vend_id = products.vend_id
order by vend_name,prod_name;

内部联结:

select vend_name,prod_name,prod_price
from vendors inner join products
on vendors.vend_id = products.vend_id

首选inner join语法

使用表别名:

select vend_name,prod_name,prod_price
from vendors as v,products as p
where v.vend_id = p.vend_id
order by vend_name,prod_name;

7. 数据插入

INSERT:

insert into customers(cust_name,
    cust_address,
    cust_city,
    cust_state)
values('Pep E. Lapew',
    '100 Main Street',
    'Los Angeles',
    'CA');  上面指定了顺序 ,下面values语句可以不按顺序插入也是正确的 

insert select语句:将查询语句的结果插入表中

insert into customers(cust_name,
    cust_address,
    cust_city,
    cust_state)
select cust_name,
    cust_address,
    cust_city,
    cust_state
from custnew;

8. 更新数据

UPDATE 插入数据

update 表名
set cust_email = '1249280492@qq.com'
where cust_id = 10005;

DELETE 删除数据

delete from 表名
where cust_id = 10006

9. 创建和操纵表

create table 表名
(cust_id    int  not null AUTO_INCREMENT,
 cust_name  char(50) not null,
 cust_address char(50) null,
 PRIMARY KEY(cust_id)
)enging=InnoDB;

10. 视图

查询某个特定产品的用户。任何需要这个数据的人都必须理解相关表的结构,并且知道如果创建查询和对表进行联结。

通过视图(view)可以联结多个表,隐藏复杂的sql

create view productcustomers(视图名) AS
select cust_name,cust_contact,prod_id
from customers as c,orders as o,orderitems as ot
where c.cust_id = o.cust_id and ot.order_num = o.order_num;
select cust_name,cust_contact
from productcustomers
where prod_id= 'xxx';

因为视图不包含数据,每次使用视图时,都必须处理查询执行时所需的任一个检索、如果用多个联结和过滤创建了复杂的视图或者嵌套视图时,可能会发现性能明显下降。在部署使用了大量视图的应用前,应进行测试

11.触发器

11.1 什么是触发器

  • 每当增加一个顾客到某个数据库表时,都检查其电话号码格式是否正确
  • 每当订购一个产品时,都从库存数量中减去订购的数量

触发器: 即在某个表发生更改时使某事件自动处理

11.2 创建触发器

  • 唯一的触发器名
  • 触发器关联的表
  • 触发器应该响应的活动(delete、insert或update)
  • 触发器何时执行
create trigger newproduct(触发器名) after insert on product(表名)
foreach row select 'xxx';