第1-9章 MySQL简介及SQL语句

1、了解SQL

  • 数据库是一个以某种有组织的方式存储的数据集合 ,保存有组织的数据的容器(通常是一个文 件或一组文件)。

  • 表是一种结构化的文件,可用来存储某种特定类型的数据

  • 模式(schema) 关于数据库和表的布局及特性的信息

  • 主键(primary key) 一一列(或一组列),其值能够唯一区分表中每个行 ,主键用来表示一个特定的行。没有主键,更新或删除表中特定行很困难

2、SQL简介

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

3&4、使用数据库

  • use crashcourse ; 选择数据库

  • SHOW DATABASES; 返回可用数据库的一个列表

  • show student_table; 返回当前选择的数据库内可用表的列表

  • select name from student_table; 显示student_table中的name列

  • select name,age from student_table; 检索多个列

  • select distinct age from student_table; 返回不同的值

  • select name from student_table limit 5; 返回的数据不多于5行

  • select name from student_table limit 5,5; 返回从第5行开始的5行

  • select student_table.name from student_table; 使用完全限定;显示student_table中的name列

5、检索排序语句

  • select name from student_table order by name; 显示student_table中的name列并按照name首字母排序

  • select name,age from student_table order by age,name; 先按照age排序然后按照name排序

  • select name,age from student_table order by age desc; 按照年龄降序排序

  • select * from student_table order by age desc limit 1; 找出最低年龄的学生

6&7、过滤数据

  • select * from student_table where age>20; 显示年龄大于20的学生

  • order by位于where之后

  • select * from student_table where age between 18 and 20;

  • select * from student_table where name is null; 显示name为null的记录

  • select * from student_table where id_class='a' and age<20; 显示班级为a并且年龄小于20的学生

  • select * from student_table where id_class='a' or age<20; 显示班级为a或者年龄小于20的学生

  • select * from student_table where id_class in ('a','b'); 显示班级为a或者b的班级

  • in的作用和or相同,但是in的执行效率更高,还可以包含其他的select语句

  • select * from student_table where id_class not in ('a','b'); 显示除a和b的班级

8、用通配符进行过滤

  • select * from student_table where name like '赵%'; %匹配任意个字符

  • select * from student_table where name like '赵_'; _匹配任1个字符

  • 通配符搜索的处理一般要比前面讨论的其他搜索所花时间更长

9、正则表达式

  • select * from student_table where name REGEXP '赵'; 会返回列中包含'赵'的记录

  • select * from student_table where age REGEXP '.0'; 返回包含0的记录

  • .是正则表达式语言中一个特殊的字符。它表示匹配任意一个字符 ;

  • LIKE匹配整个列。如果被匹配的文本在列值中出现, LIKE将不会找到它 。而REGEXP在列值内进行匹配,如果被匹配的文本在列值中出现, REGEXP将会找到它

  • select * from student_table where name REGEXP '赵|李'; 作用相当于or

  • 匹配几个字符之一

    • select * from student_table where age REGEXP '[12]0'; 返回10或者20的记录

    • select * from student_table where age REGEXP '^[12]0'; 返回除10和20以外的记录

  • 匹配范围

    • select * from student_table where age REGEXP '[1-3]0'; 返回10、20、30的记录

  • 匹配特殊字符

    • 为了匹配特殊字符,前边必须加\ \

    • select * from student_table where name REGEXP '\ \ .' ; 找出name包含.的记录

  • 匹配字符类

  • 匹配多个实例

    • select * from prod_table where name REGEXP '\ \ ([0-9]blocks?\ \ )' ; [0-9]匹配任意数字(这个例子中为1和5), sticks?匹配stick和sticks( s后的?使s可选,因为?匹配它前面的任何字符的0次或1次出现), \)匹配)

    • select * from prod_table where name REGEXP '[[:digit:]{4}]'; 匹配连在一起的任意4位数字。 等价于这种写法:select * from prod_table where name REGEXP '[0-9] [0-9] [0-9] [0-9]';

  • 定位符:为了匹配特定位置的文本

    • select * from prod_table where name REGEXP '^[0-9\ \ .]'; 以一个数(包括以小数点开始的数)开始的所有产品

    • ^有两种用法。在集合中(用[和]定义),用它来否定该集合,否则,用来指串的开始处

posted @ 2022-03-26 20:39  Z9Y1J5  阅读(42)  评论(0编辑  收藏  举报