MYSQL基础学习

来源:互联网 发布:java字符型转换成整型 编辑:程序博客网 时间:2024/05/12 02:40

offer 103  && offer 102

第一章:MySQL和SQL入门

1.3.3 MySQL的体系结构

MySQL与mysql的区别, MySQL指的是一个完整的MySQL RDBMS, 而mysql是一个特定的客户程序的名称。

在MySQL中,你真正可以信赖的原则是:从服务器返回的数据行的先后顺序没有任何保证,除非你事先设定。

如果希望查询结果按希望的先后顺序,必须给查询命令增加一条ORDER BY 子句。

1.4.4 执行SQL语句

select now();

select now()\G ; 竖排显示结果

1.4.7 insert添加新的数据行

insert into student values('Avery','F',null),('Nathan','M',null);   添加了两行

insert into member (last_name, frist_name) values ('stein','waldo');  插入一行

insert into member (last_name, frist_name) values ('stein','waldo'), ('hello','world');  插入两行

1.4.9 检索

专用的MySQL比较操作符<=>能完成null值与null值之间的比较。其余的= 或者 != 都只能返回null,但是 null <=> null 能返回 1 ,<=>是等于的意思。

加个 @能设置SQL变量 “@变量名 := 值” , 

having 子句, having子句与where子句的相似之处是它们都是用来设定查询条件的,输出的行必须符合这些查询条件。having子句与where子句的不同之处是count()之类的汇总函数的计算结果允许在having子句里面出现。

带有having子句的查询命令特别适合用来查找在某个数据列里重复出现的值。

with rollup 聚合子句。

第二章:使用SQL管理数据




select 语句的基本语法如下:

select select_list

from table_list

where row_constraint

group by grouping_columns

order by grouping_columns

having group_constraint

limit count;

 

inner join  两边均不完全保留

left join 左边的table完全保留


把子查询改写为联结查询:

联结查询要比子查询的执行效率高。


1. 改写用来选取匹配值的子查询:

select * from table1

where column1 in (select column2a from talbe2 where column2b = value);

改为

select table1.* from table1 inner join table2

on table1.column1 = table2.column2a where table2.column2b = vaule;


2. 改写用来选取非匹配值的子查询:

select * from table1

where column1 not in (select column2 from table2);

改为

select table1.*

from table1 left join table2 on table1.column1 = table2.column2

where table2.column2 is null;



事务:Transaction , rollback, 


MyISAM : 数据表级锁定机制

InnoDB:数据行级锁定机制


fulltext 全文检索。



MySQL还可以使用正则表达式进行匹配,操作符是REGEXP而不是LIKE,下面是一些比较常用的正则表达式模式字符。

'abc' REGEXP ‘a.c'       1

'e' REGEXP '[aeiou]'     1

'f' REGEXP '[aeiou]'       0

'abc' REGEXP '[a-z]'       1

'abc' REGEXP '[^a-z]'     0

'abcdef'   REGEXP  ’a.*f'   1

'abc'   REGEXP  '[0-9]*abc'   1

'abc'    REGEXP   '[0-9][0-9]*'   0

如果不想包括 0 , 就用“+”代替‘ * ’

这里你可能感到迷惑, 模式匹配 ’X*' 将匹配连续出现的任意个数的‘X' 字符:

而下面, ’d*‘ 出现0个或者更多的d都可以,所以可以匹配。

'abc'  REGEXP  'cd*'    1

'abc'   REGEXP   'cd+'    0

'abcd' REGEXP   'cd+'    1

 

’^pattern' 和 'pattern$' 形式的模板将分别匹配以 pattern 开头或者结尾的字符串,而 ‘^pattern$’ 形成的模板将匹配整个字符串是且仅是pattern的情况。


3. 表达式中的null值,

1 + null   null

1 |  null   null


1  and  null   null

1  or   null     1

0 and null    0

0   or null   null


如果要你管null值用作比较操作符或模式匹配操作哦符的操作数,那么,除专门用来处理null值的 “<=>” “is null” “ is not null ”

其他的操作结果都是null;

1  = null   null

null = null  null

1  <=>  null   0

null like '%'      null

null regexp   '.*'    null

null  <=>   null      1

1 is null    0

null is null   1


'abc'  ||  'def'    0




未完待续。



0 0
原创粉丝点击