---恢复内容开始---

数据库 :按一定的格式存放数据

数据库管理系统: 高效地获取和维护数据的功能

数据库分类;  1关系型: sqllite ; db2; oracle;access; sqlserver;MySQL

         2 非关系型 : mongodb ,redis ,memcache

 

操作文件夹:(库)

——增:create database db1 charset utf8;

——查:show databases;

    show database db1;(指定库)

——改:alter database db1 charset gbk;

——删:drop database db1;

 

 

操作文件夹:(表)

  先切换到文件夹下 :use db1

 

——增:create table DA(id int,name char);

 

——查:show create table 表名; (查看表的信息)

    desc 表名; (查看表的结构)

    show tables;(查看所有的表)

    select database();(查看当前所在的库)

 

——改:alter table da change name NAME char(4);(更改字段 和 类型)

    alter table da add id char;(添加id)

    alter table da modify sex char(8);(修改类型显示宽度)

 

——删:drop tables da;

    delect from 库名.da; (先找到再删,只能简单的删)

 

 

 

 

操作文件夹:(记录)

 

——增:insert into da values(1,'egon'),(2,'egon'),(4,'alex');

    insert into 库名.da(id,name) values(5,'alex');

 

——查:select * from da; (插入时查看的结果)

 

    select name,id from 库名.da;(查看库的内容)

    

——改:update da set name='bs' where id=3;(参照条件改)

    update 库名.da set name='AAA'; (全部改)

 

——删:truncate da;

 

 约束:

  primary key   字段为主键,可以唯一的标识 (加速查寻)

  foreign key  字段为表的外键 (建立表之间的关联)

  not null   字段不能为空

  unique key   字段的值是唯一的(加速查寻)

  auto_increment 整数类型,且为主键(自增字段的初始值)

   default    字段默认值

   unsigned  无符号

    zerofill   用0 填充

 

    constraint uni_  约束

   not null unique = primary key (主键)

一张表只能有一个主键 但却可以有多个not null unique

  primary key(字段1,字段2) 联合唯一

只能有一个自增字段 并且该字段必须为key

基于会话级别:

    set session auto_increment_increment=2;(修改会话级别的步长)

基于全局级别的:

  set global auto increment offset=2;(初始值设置)

  初始的值不能大于步长 否则就会被忽略

Foreign key 外键

varchar    更精确

on delete cascade 被关联表一块删除

on update cascade 被关联表一块更改

需要用到的:

Delete from emp where dep_id=200;

Delete form dep where id=200;

 

Update dep set id=2002 where id=202;

 

 

插入数据:

 

   create table 新表名(字段 数据类型[约束条间]。。。) select 字段。。。 from 旧表名

 

   create table 新表名(字段 数据类型[约束条件]。。。) select 字段 as 别名 from 旧表名 [where 条件];

 

    as 起一个别名,起别名时,默认有as 所以可以不用加as 就可以起别名。

 

 

修改数据:

   update 表名 set 字段=记录 where 条件;

 

 

 

杂碎的内容:

  concat:字符串的拼接,可以拼成任意的格式

  concat_ws:第一个参数指定的是分割符,后面加上自己要看的字段

  \G:将乱了的记录重新按行显示。

 

 

二 常用的select查看

 select 的基本格式:

  select distinot:去重 字段名。。。 from 表名

    where 约束条件        :取出来的记录默认为是一个组,在这里就可以使用聚合函数,约束条件就是默认某些字段的约束。

    group by 字段名          :后面加上某个字段,就会按照那个字段分组,分组过后可以直接查看分组的字段,要想查看其他的字段,必须要借助聚合函数,分组是为了一类一类的处理数据。而分组的字段是依据字段的约束条件不唯一。

    having 过滤语句         :只能跟在分组的后面,处理一些分组过后的约束条件。

    order by 字段 排序       :排序。后面加上需要按照排序的字段,后面可以同时加上多个排序条件,如果前面的排序有重复的,才会执行后面的,如果没有重复的,后面的就不会执行。他是在取出了相应字段的记录后才开始执行的。

    limit   限制条件              :规定查看的范围,在最后才开始运行。

 

 

 集合函数:min:最小值   max:最大值    avg:计算平均值    sum:计算总和     count:计算个数

  where 条件约束:后面可以加上比较符号(>  <  =  !=   <>  <=  >=)   逻辑运算符(and    or    not)    

          in:什么或什么或什么          between:在什么和什么之间    like:像什么 后面出入两个符号   % :任意多个字符  _ :表示任意一个字符  is:什么是什么

 

 

 group by 分组:后面加上一个字段名

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
mysql> select post,count(id),group_concat(namefrom employee group by post;
+----------------------------+-----------+-------------------------------------------------------+
| post                       | count(id) | group_concat(name)                                    |
+----------------------------+-----------+-------------------------------------------------------+
| operation                  |         5 | 张野,程咬金,程咬银,程咬铜,程咬铁                      |
| sale                       |         5 | 歪歪,丫丫,丁丁,星星,格格                              |
| teacher                    |         7 | alex,wupeiqi,yuanhao,liwenzhou,jingliyang,jinxin,成龙 |
| 老男孩驻沙河办事处外交大使 |         1 | egon                                                  |
+----------------------------+-----------+-------------------------------------------------------+
rows in set (0.00 sec)
 
mysql> select post,min(salary) from employee group by post;
+----------------------------+-------------+
| post                       | min(salary) |
+----------------------------+-------------+
| operation                  |    10000.13 |
| sale                       |     1000.37 |
| teacher                    |     2100.00 |
| 老男孩驻沙河办事处外交大使 |     7300.33 |
+----------------------------+-------------+
rows in set (0.00 sec)
 
mysql> select post,max(salary) from employee group by post;
+----------------------------+-------------+
| post                       | max(salary) |
+----------------------------+-------------+
| operation                  |    20000.00 |
| sale                       |     4000.33 |
| teacher                    |  1000000.31 |
| 老男孩驻沙河办事处外交大使 |     7300.33 |
+----------------------------+-------------+
rows in set (0.00 sec)
 
mysql> select post,sum(salary) from employee group by post;
+----------------------------+-------------+
| post                       | sum(salary) |
+----------------------------+-------------+
| operation                  |    84000.13 |
| sale                       |    13001.47 |
| teacher                    |  1062900.31 |
| 老男孩驻沙河办事处外交大使 |     7300.33 |
+----------------------------+-------------+
rows in set (0.00 sec)
 
mysql> select post,avg(salary) from employee group by post;
+----------------------------+---------------+
| post                       | avg(salary)   |
+----------------------------+---------------+
| operation                  |  16800.026000 |
| sale                       |   2600.294000 |
| teacher                    | 151842.901429 |
| 老男孩驻沙河办事处外交大使 |   7300.330000 |
+----------------------------+---------------+
rows in set (0.00 sec)

 

  having  过滤条件:后面加的内容和where一样的,不过需要借助于聚合函数过滤记录

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
mysql> select post from employee group by post having count(id)>5;
+---------+
| post    |
+---------+
| teacher |
+---------+
1 row in set (0.00 sec)
 
mysql> select post from employee group by post having count(id)>5 or avg(salary) >10000;
+-----------+
| post      |
+-----------+
| operation |
| teacher   |
+-----------+
rows in set (0.00 sec)

 

  order by :后面跟上:asc:升序,从小到大排序    ;     desc:降序,从大到小排序。order by:默认是从小到达排序的。

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
mysql> select from employee order by age asc,id desc;
+----+------------+--------+-----+------------+----------------------------+--------------+------------+--------+-----------+
| id | name       | sex    | age | hire_date  | post                       | post_comment | salary     | office | depart_id |
+----+------------+--------+-----+------------+----------------------------+--------------+------------+--------+-----------+
| 18 | 程咬铁     | female |  18 | 2014-05-12 | operation                  | NULL         |   17000.00 |    403 |         3 |
| 17 | 程咬铜     | male   |  18 | 2015-04-11 | operation                  | NULL         |   18000.00 |    403 |         3 |
| 16 | 程咬银     | female |  18 | 2013-03-11 | operation                  | NULL         |   19000.00 |    403 |         3 |
| 15 | 程咬金     | male   |  18 | 1997-03-12 | operation                  | NULL         |   20000.00 |    403 |         3 |
| 12 | 星星       | female |  18 | 2016-05-13 | sale                       | NULL         |    3000.29 |    402 |         2 |
| 11 | 丁丁       | female |  18 | 2011-03-12 | sale                       | NULL         |    1000.37 |    402 |         2 |
|  7 | jinxin     | male   |  18 | 1900-03-01 | teacher                    | NULL         |   30000.00 |    401 |         1 |
|  6 | jingliyang | female |  18 | 2011-02-11 | teacher                    | NULL         |    9000.00 |    401 |         1 |
|  1 | egon       | male   |  18 | 2017-03-01 | 老男孩驻沙河办事处外交大使 | NULL         |    7300.33 |    401 |         1 |
| 14 | 张野       | male   |  28 | 2016-03-11 | operation                  | NULL         |   10000.13 |    403 |         3 |
| 13 | 格格       | female |  28 | 2017-01-27 | sale                       | NULL         |    4000.33 |    402 |         2 |
|  5 | liwenzhou  | male   |  28 | 2012-11-01 | teacher                    | NULL         |    2100.00 |    401 |         1 |
| 10 | 丫丫       | female |  38 | 2010-11-01 | sale                       | NULL         |    2000.35 |    402 |         2 |
|  9 | 歪歪       | female |  48 | 2015-03-11 | sale                       | NULL         |    3000.13 |    402 |         2 |
|  8 | 成龙       | male   |  48 | 2010-11-11 | teacher                    | NULL         |   10000.00 |    401 |         1 |
|  4 | yuanhao    | male   |  73 | 2014-07-01 | teacher                    | NULL         |    3500.00 |    401 |         1 |
|  2 | alex       | male   |  78 | 2015-03-02 | teacher                    | NULL         | 1000000.31 |    401 |         1 |
|  3 | wupeiqi    | male   |  81 | 2013-03-05 | teacher                    | NULL         |    8300.00 |    401 |         1 |
+----+------------+--------+-----+------------+----------------------------+--------------+------------+--------+-----------+
18 rows in set (0.00 sec)

 

  limit 限制条件:后面输入整数类型,只传一个值是查看记录的条数,默认从第1条记录开始查看,如果出入两个数,第一个数是初始值,就是从那一条的下一条开始查看,第二个数就是查看记录的条数。

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
mysql> select from employee where id limit 6,10;
+----+--------+--------+-----+------------+-----------+--------------+----------+--------+-----------+
| id | name   | sex    | age | hire_date  | post      | post_comment | salary   | office | depart_id |
+----+--------+--------+-----+------------+-----------+--------------+----------+--------+-----------+
|  7 | jinxin | male   |  18 | 1900-03-01 | teacher   | NULL         | 30000.00 |    401 |         1 |
|  8 | 成龙   | male   |  48 | 2010-11-11 | teacher   | NULL         | 10000.00 |    401 |         1 |
|  9 | 歪歪   | female |  48 | 2015-03-11 | sale      | NULL         |  3000.13 |    402 |         2 |
| 10 | 丫丫   | female |  38 | 2010-11-01 | sale      | NULL         |  2000.35 |    402 |         2 |
| 11 | 丁丁   | female |  18 | 2011-03-12 | sale      | NULL         |  1000.37 |    402 |         2 |
| 12 | 星星   | female |  18 | 2016-05-13 | sale      | NULL         |  3000.29 |    402 |         2 |
| 13 | 格格   | female |  28 | 2017-01-27 | sale      | NULL         |  4000.33 |    402 |         2 |
| 14 | 张野   | male   |  28 | 2016-03-11 | operation | NULL         | 10000.13 |    403 |         3 |
| 15 | 程咬金 | male   |  18 | 1997-03-12 | operation | NULL         | 20000.00 |    403 |         3 |
| 16 | 程咬银 | female |  18 | 2013-03-11 | operation | NULL         | 19000.00 |    403 |         3 |
+----+--------+--------+-----+------------+-----------+--------------+----------+--------+-----------+
10 rows in set (0.00 sec)

 

他们的执行顺序是:from——》where—》group by—》having—》distinct—》order—》limit。如果上一个没有顺序还是不会改变

 

  distinct:去掉重复

 

 

 

only_full_group_by:使用方法  set @@lobal sql_mode='only_full_group_by'   修改全局的一条信息

 

 where后面还可以跟上regexp:正则表达式条件约束,regexp后面跟的内容和正则表达式里面的内容差不多

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
mysql> select from employee where name regexp 'a.';
+----+------------+--------+-----+------------+---------+--------------+------------+--------+-----------+
| id | name       | sex    | age | hire_date  | post    | post_comment | salary     | office | depart_id |
+----+------------+--------+-----+------------+---------+--------------+------------+--------+-----------+
|  2 | alex       | male   |  78 | 2015-03-02 | teacher | NULL         | 1000000.31 |    401 |         1 |
|  4 | yuanhao    | male   |  73 | 2014-07-01 | teacher | NULL         |    3500.00 |    401 |         1 |
|  6 | jingliyang | female |  18 | 2011-02-11 | teacher | NULL         |    9000.00 |    401 |         1 |
+----+------------+--------+-----+------------+---------+--------------+------------+--------+-----------+
rows in set (0.05 sec)
 
 
mysql> select from employee where name regexp '^a.';
+----+------+------+-----+------------+---------+--------------+------------+--------+-----------+
| id | name | sex  | age | hire_date  | post    | post_comment | salary     | office | depart_id |
+----+------+------+-----+------------+---------+--------------+------------+--------+-----------+
|  2 | alex | male |  78 | 2015-03-02 | teacher | NULL         | 1000000.31 |    401 |         1 |
+----+------+------+-----+------------+---------+--------------+------------+--------+-----------+
1 row in set (0.00 sec)
 
mysql> select from employee where name regexp '^j.';
+----+------------+--------+-----+------------+---------+--------------+----------+--------+-----------+
| id | name       | sex    | age | hire_date  | post    | post_comment | salary   | office | depart_id |
+----+------------+--------+-----+------------+---------+--------------+----------+--------+-----------+
|  6 | jingliyang | female |  18 | 2011-02-11 | teacher | NULL         |  9000.00 |    401 |         1 |
|  7 | jinxin     | male   |  18 | 1900-03-01 | teacher | NULL         | 30000.00 |    401 |         1 |
+----+------------+--------+-----+------------+---------+--------------+----------+--------+-----------+
rows in set (0.00 sec)
 
mysql> select from employee where name regexp '^j.*n$';
+----+--------+------+-----+------------+---------+--------------+----------+--------+-----------+
| id | name   | sex  | age | hire_date  | post    | post_comment | salary   | office | depart_id |
+----+--------+------+-----+------------+---------+--------------+----------+--------+-----------+
|  7 | jinxin | male |  18 | 1900-03-01 | teacher | NULL         | 30000.00 |    401 |         1 |
+----+--------+------+-----+------------+---------+--------------+----------+--------+-----------+
1 row in set (0.00 sec)
 
 mysql> select from employee where name REGEXP '^jin.*[n|g]$';
+----+------------+--------+-----+------------+---------+--------------+----------+--------+-----------+
| id | name       | sex    | age | hire_date  | post    | post_comment | salary   | office | depart_id |
+----+------------+--------+-----+------------+---------+--------------+----------+--------+-----------+
|  6 | jingliyang | female |  18 | 2011-02-11 | teacher | NULL         |  9000.00 |    401 |         1 |
|  7 | jinxin     | male   |  18 | 1900-03-01 | teacher | NULL         | 30000.00 |    401 |         1 |
+----+------------+--------+-----+------------+---------+--------------+----------+--------+-----------+
rows in set (0.00 sec)

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

---恢复内容结束---

posted on 2017-10-29 07:14  远去  阅读(283)  评论(0编辑  收藏  举报