文章目录
mysql版本
select @@version;
登陆操作
mysql -u root -p -h localhost -P 3306
使用数据库
use 数据库名称;
显示操作
显示当前可用数据库
show databases;
显示当前数据库中的数据表
show tables;
显示数据表中的所有列
方法一
show columns from customers;
方法二
describe customers;
SELECT 操作
返回不同信息
限制查询
一个参数
select prod_name from products limit 3;
两个参数
select prod_name from products limit 3, 2;
注意
当一个参数时,是从0行开始的
当两个参数时,也是从0行开始的
SELECT子句
排序
单列排序
select prod_name from products order by prod_name;
多列排序
多列排序规则,先按第一列排序,然后在按第二列排序
例如先按照姓排序,然后在按照名排序
select prod_id, prod_price, prod_name from products order by prod_price, prod_name;
降序排序
默认是按照升序进行排序
降序排序需要使用DESC
DESC
只运用在写在它前面的字段,如果想作用在多列,则每一列都需要加上DESC
select prod_id, prod_price, prod_name from products order by prod_price DESC;
升序排序
升序排序的关键字是ASC
order by 与 limit位置
order by应该在from后面
limit 应该在order by后面
WHERE 子句
WHERE和ORDER BY位置
ORDER BY应该位于WHERE后面
操作符
操作符 | 说明 |
---|---|
= | 等于 |
<> | 不等于 |
!= | 不等于 |
< | 小于 |
> | 大于 |
<= | 小于等于 |
>= | 大于等于 |
BETWEEN | 在指定的两个值之间 |
is null | 值是否为null |
is null
select cust_id from customers where cust_email is null;
AND 与 OR
mysql顺序是先解决and 再 解决or
可以使用()
select prod_id, vend_id, prod_price from products where (vend_id = 1001 or vend_id = 1003) and prod_price > 10;
IN
指定条件范围
select prod_name, prod_price from products where vend_id in (1001, 1003) order by prod_name limit 1;
NOT
not可以与in、between、exists配合使用
通配符
%
like ‘x%h’
like ‘%h’
like ‘x%’
可以匹配0、1、多个字符
xixixi空格
用 %xi
会影响匹配结果
% 不能匹配 null
_
只能匹配一个字符,不多不少
函数
拼接
concat
select concat(vend_name, '(', vend_address, ')') from vendors;
该函数将接受的字段通过'字段一','字段二'
连接
去空格
去掉左边空格
ltrim
去掉右边空格
rtrim
去掉左右空格
trim
文本函数
函数 | 说明 |
---|---|
Left | 返回串左边的字符 |
Length | 返回字符串的长度 |
Locate | 找出串的一个子串 |
Lower | 将串转换为小写 |
Right | 返回串右边的字符 |
SubString | 返回字串的 字符 |
Upper | 将串转换为大写 |
日期和时间函数
日期函数
日期操作
select cust_id, order_num from orders where date(order_date) > '2005-09-30';
函数可以小写
可以使用逻辑表达式
数值处理函数
别名
别名可以用在select中,where中
给列名起别名
使用 AS 别名名称
给表名起别名
select p.prod_name, p.prod_price, pr.note_text from products AS p, productnotes AS pr where p.prod_id = pr.prod_id and p.prod_id = 'TNT2';
算数计算
操作符 | 说明 |
---|---|
+ | 加 |
- | 减 |
* | 乘 |
/ | 除 |
汇总
聚集函数
函数 | 说明 | 备注 |
---|---|---|
avg | 平均值 | 忽略值为null的行 |
count | 行数 | count(*) 统计包含null的值。count(column) 统计不包含null的值 |
max | 最大值 | |
min | 最小值 | |
sum | 和 |
分组
group by
select vend_id, count(*) from products group by vend_id with rollup;
group by 必须出现在where子句之后。order by 子句之前
分组配合排序
select order_num ,count(*) AS count from orderitems group by order_num ORDER BY count;
过滤
having
select vend_id, count(*) from products where prod_price >= 10 group by vend_id having count(*) >= 2;
having和where区别
where在数据分组前过滤
having在数据分组后过滤
select 顺序
顺序 |
---|
select |
from |
where |
group by |
having |
order by |
limit |
使用子表查询
利用子条件进行查询
查找订购物品为TNT2的所有客户的名字。
select cust_name. cust_contact from customers where cust_id in (select cust_id from orders where order_num in (select order_num from orderitems where prod_id ='TNT2' )); ERROR 1054 (42S22): Unknown column 'cust_name.cust_contact' in 'field list' mysql> select cust_name, cust_contact from customers where cust_id in (select cust_id from orders where order_num in (select order_num from orderitems where prod_id ='TNT2' ));
计算字段使用子查询
select cust_name, cust_state, (select count(*) from orders where orders.cust_id = customers.cust_id )AS orders from customers;
外键
外键为某个表中的一列,它包含另一个表中的主键值
联结
内部联结
又称等值联结
select vendors.vend_id, vend_name, prod_name , prod_price from vendors inner join products on vendors.vend_id = products.vend_id;
联结多个表
显示编号为20005的订单中的物品
select products.prod_name, products.prod_price, orderitems.quantity from products, orders, orderitems where orderitems.prod_id = products.prod_id and orders.order_num = orderitems.order_num and orders.order_num = '20005';
自联结
查询ID为DTNTR供应商的其它产品
select p2.prod_id, p2.prod_name from products AS p1, products AS p2 where p1.vend_id = p2.vend_id and p1.prod_id = 'DTNTR';
select p2.prod_id, p2.prod_name from products AS p1, products AS p2 where p1.vend_id = p2.vend_id and p2.prod_id = 'DTNTR';
解析
只是查一个表,所以from products AS p1, products AS p2 要定义成两个别名,要不然mysql也不知道prod_id和prod_name是哪个表
and结尾 p2.prod_id = ‘DTNTR’、p1.prod_id = 'DTNTR’显示的结果不同
左联结
select customers.cust_id, orders.order_num from customers LEFT OUTER join orders on customers.cust_id = orders.cust_id ;
右联结
RIGHT OUTER JOIN
使用聚集函数的联结
select customers.cust_name, customers.cust_id, count(*) AS order_count from customers inner join orders on customers.cust_id = orders.cust_id group by customers.cust_id ;
组合查询
UNION
select vend_id, prod_id, prod_price from products where prod_price <= 5 union select vend_id, prod_id, prod_price from products where vend_id in (1001, 1002);
将两个select语句进行查询
可以理解成where 条件1 or 条件2
自动去掉重复行
UNION ALL
即使有重复行也会显示出来
与select区别,select也会自动去除重复行
所以如果需求是保留重复行,那么只能用UNION ALL,这是他俩却别
使用order by
要作用在最后一个select语句中
select vend_id, prod_id, prod_price from products where prod_price <= 5 union select vend_id, prod_id, prod_price from products where vend_id in (1001, 1002) order by vend_id, prod_price;
注意
两个select查询,每个select查询的列应该相同,顺序可以不一致
全文搜索
支持引擎
ENGINE=MyISAM
InnoDB不支持全文搜索
创建支持全文本搜索的表
CREATE TABLE productnotes ( note_id int NOT NULL AUTO_INCREMENT, prod_id char(10) NOT NULL, note_date datetime NOT NULL, note_text text NULL , PRIMARY KEY(note_id), FULLTEXT(note_text) ) ENGINE=MyISAM;
使用FULLTEXT关键字
可以指定多个列
关键字
match(列名)
against(搜索表达式)
select note_text from productnotes where match(note_text) against('rabbit');
全文本索引维护了一个等级
在全文本搜索中,会根据查找的关键字,维护了一个等级,最先出现的,频率高的,等级也会高。
等级高的最先返回
布尔文本搜索
略
插入
不指定列名
INSERT INTO customers VALUES(null, 'Coyote Inc.', '200 Maple Lane', 'Detroit', 'MI', '44444', 'USA', 'Y Lee', 'ylee@coyote.com');
指定列名
INSERT INTO customers (cust_name, cust_address, cust_city, cust_state, cust_zip, cust_country, cust_contact, cust_email) VALUES('Coyote Inc.', '200 Maple Lane', 'Detroit', 'MI', '44444', 'USA', 'Y Lee', 'ylee@coyote.com');
插入时哪些列可以省略
该列允许为null
该列设置了默认值
更新
update customers set cust_contact = 'yaoyan', cust_email = '10632@qq.com' where cust_id = 10001;
删除
删除记录
delete from customers where cust_id = 10005;
删除表,在重新创建
truncate table
异常
Cannot delete or update a parent row: a foreign key constraint fails
SET foreign_key_checks = 0; // 先设置外键约束检查关闭
drop table mytable; // 删除数据,表或者视图
SET foreign_key_checks = 1; // 开启外键约束检查,以保持表结构完整性
创建表
创建语句
CREATE TABLE customers
(
cust_id int NOT NULL AUTO_INCREMENT,
cust_name char(50) NOT NULL ,
cust_address char(50) NULL ,
cust_city char(50) NULL ,
cust_state char(5) NULL ,
cust_zip char(10) NULL ,
cust_country char(50) NULL ,
cust_contact char(50) NULL ,
cust_email char(255) NULL ,
PRIMARY KEY (cust_id)
) ENGINE=InnoDB;
not null
‘’ 中间无值,在not null是允许的
null
默认值
quantity int NOT NULL default 1
引擎
InnoDB
用于事物
memory
用在内存
MyISAM
用于文本搜索
主键
主键不能是null
对于单列
主键必须唯一
对于多个列
这些列的组合值必须唯一
语法
PRIMARY KEY (列名1, 列名2)
AUTO_INCREMENT
每个表只允许一个AUTO_INCREMENT的列,并且是必须被索引
获取最后一个AUTO_INCREMENT的id值
select last_insert_id();
更新表
添加一列
alert table vendors add vend_phone char(20);
删除一列
alert table vendors drop column vend_phone;
设置外键
ALTER TABLE orders ADD CONSTRAINT fk_orders_customers FOREIGN KEY (cust_id) REFERENCES customers (cust_id);
删除表
drop table tablename;
重命名表
rename table 久的表名 to 新的表名
视图
创建视图
create view productcustomers as select products.prod_name, products.prod_price, orderitems.quantity from products, orders, orderitems where orderitems.prod_id = products.prod_id and orders.order_num = orderitems.order_num;
使用视图
查看创建视图语句
show create view productcustomers;
删除视图
drop view productcustomers;
视图更新
视图用于检索,不是用于更新
事务
回滚
rollback
回退start transaction
之后的所有语句
显示提交
start transaction;
delete from orderitems where order_num = '20006';
commit;
设置保留点
savepoint delete1;
回滚保留点
rollback to delete1
管理用户
创建一个用户账号
create user ben identified by 'yaoyan';
刚创建的用户看不到任何表
删除一个用户
drop user ben;
查看用户当前具有的权限
show grants for ben;
ON . 代表没有任何权限
授予用户权限
grant select ON study_mysql.* to ben;
授权ben用户可以在study_mysql数据库里面的所有表使用select命令
撤销授权
revoke select on study_mysql.* from ben;
更改密码
alter user ben identified by '1234';