《MYSQL必知必会》

1、 同一个数据库中不允许出现同名表;不同的数据库中可以出现同名表
2、 每一行记录都用有一个key(一列或一组列作为key)
3、 作为key的列不允许值为空(NULL)
4、 多个列作为key时,多个列的组合必须唯一,但其中单个列的值可以不唯一
5、 好习惯:作为key的列的取值最好不要变
6、 SQL是操作数据库的结构化查询语言;MYSQL是一种数据库管理系统,即是一种数据库软件
7、 MYSQL、 Oralcle、SQL Server等数据库都是基于客户机-服务器的数据库
8、 查看所有数据库:show databases;
切换数据库:use 数据库名;
切换到某个数据库后,看数据库中的所有表:show tables;
查看表中所有列:show columns from 表名;或者:describe 表名
显示允许的show语句:help show
9、 SQL语言不区分大小写 如select和SELECT 意思是一样的
10、除非确实需要表中的每个列,否则别用*,检视不需要的列会降低性能
11、去掉重复的行:select distinct vend_id from products;
Distinct 放在列的前面;distinct应用于所有列,不仅是前置它的列
12、只要查询结果中的前几行:select name from produdct limit 5 order by time;
只要查询结果中的第4~9行:Select name form product limit 5,5 order by time;
Limite + 开始行+行数
13、一般order by后面接的是查询的列,其实用非检索的列也是可以的!!!
14、默认按升序排序;明确指定按升序排:asc
15、按降序排:select price,name for product order by price desc,name;
Distinct是对所有列有效,desc只对其前面的那一列有效
Distinct加在列名前面,desc加在列名后面
若多个列想按降序排,则多个列都要在后面加上desc
16、Select name,price from prodct where name = ‘fuses’;
用来筛选的列是字符串类型,所以要加单引号
17、Select price from product where price between 5 and 10;
18、若列中不包含值,则称其包含空值NULL
空值检查:select price from product where price is null;
19、NULL与不匹配:在通过过滤选择不具有特定值的行时,你可能希望返回具有NULL值的行,但是,不行
20、where中的字句可以用and 或or 方式连接
select * from product where price>10 and id = 1003;
select * from product where id =1003 or id =1006;
21、where子句中即有and也有or时,应该用圆括号明确的进行分组
where (id =1002 or id=1003) and price >=10;
22、in和or的功能相当,但in更快,且in可以包含其他select语句
where id in (1002,1003)
23、not 常与 in、between、exists 一起用
where id not in (1003,1004)
24、% 表示任何字符出现任意次数(当然报错0次)
-下划线只匹配单个字符
Select name form product where name like ‘jet%’;
Where name like ‘jet-‘;
25、若数据库中的anvil后面有空格,用’%anvil’是匹配不上的
26、尽量不要使用通配符,因为有了通配符,搜索速度会很慢;
把通配符置于搜索模式的开始处,搜索速度最慢
27、第9章正则表达式没怎么看
28、将字段进行拼接时,多数数据库管理系统使用+或||来实现拼接,但MYSQL则使用concat函数来拼接
Select concat(name,country) as title from vendors;
用AS定义列名,亦可用AS修改当前的列名(重命名)
29、去掉查询结果中字段的空格:RTrim、LTrim、Trim
30、Select quantity ,price,quantity*price as expand_price from order;
MYSQL支持 + - * /
31、对日期操作时,建议使用yyyy-mm-dd这种格式,消除了多义性
32、数据库中存的是2005-09-01 11:30:05,用date()获取日志部分
Where date(order_date)=’2001-09-01’
可以通time()获取时间部分
33、检索2005年9月下的订单:where year(order_date)=2005 and month(order_date)=9
34、SQL常用聚集函数:AVG、COUNT、MAX、MIN、SUM
35、COUNT(*)不忽略值为NULL的列;COUNT(列名)忽略值为NULL的列
36、统计数量和:select sum(quantity) as test from order
统计总价:select sum(price*quantiry) as totalprice from order
37、计算各个不同价格的平均值:select sum(distinct price) form order
38、分组:group by ; 过滤分组having
按id进行分组,计算每组的行数:Select id,count(*) from order group by id
39、Having类似where,以为差别是where过滤的是行,having过滤的是分组
Where在分组前进行过滤;having在分组后进行过滤
列出具有2个以上,价格为10以上的产品供应商:
Select id,cout(*) from order where price>10 group by id having count(*)>2;
40、Select id, sum(quantiry*price) as total from order group by id order by total
41、Select 子句顺序:
Select ->from ->where ->group by ->having ->order by->limit
42、外键:外键为某表中的一列,它包含另一个表的主键值,定义了两个表之间的关系
43、联结:从多个表中查数据:select vend_name,product_name price from vend, product where vend.vend_id=product.vend_id;
所有联结都应该有where语句,否则返回的是笛卡尔积
44、联结是耗时的,联结的表越多,性能下降的越厉害!
45、外部联结:联结包含了那些在相关表中没有关联行的行
如查询每个客户下单数量,包括尚未下过单的,一个是客户表一个是订单表,有的客户在订单表中无相应记录
Select customers.id ,orders.num from customers left outer join orders on orders.id=customers.id
46、使用外部联结outer join,必需包含关键字right或left
47、union组合查询等价于多一个where条件,这两种技术在不同的查询中性能也不通;但where代替不了union all
48、Union中的每个查询必需包含相同的列、表达式或聚集函数
49、Union主要用于如下两种场景:
1) 从不同的表返回类似数据
2) 对同张表执行多个查询
Select id from order where id in(1002,1005) union select id from order where price >100;
50、Union从查询结果中自动去掉了重复的行,若不想去掉重复行,可以用union all
51、第18章,全文搜索 需要再看
52、全文搜索类似like,但比like要快,因为它建立了索引
53、当数据库被多个客户访问时,若select是最重要的,则可以在insert和into之间天爱关键词low_priority,指示MYSQL降低insert语句的优先级,update、delete也是
54、Insert into 表名(列名)values(列值),(列值)
Update tbl_order set name =’11’ where id =0;
Delete from tbl_order where id = 0;
55、从一个表中读数据,插入到另一个表中:Insert select
Insert into order (name,age) select id,old from customers;
56、更新多行时,若某一行更新失败,则整改更新失败,错误之前更新的行也被恢复成原来的值。
即使某条更新错误,也要继续更新,可以使用ignore关键字,如:
Update ignore tbl_name ……..
57、删除表中所有行,可以用truncate table;这个速度更快。他是删掉原表,再建一个新表
58、创建表:create table 表名
(列名 列的属性 是否可以为空 默认值 ,
Id int not null default 1 ,
…….,
Primary key (id))
59、仅在不存在时创建:creat table if not exists 表名

原文地址:https://www.cnblogs.com/testzcy/p/6821058.html