数据库mysql基础2

数据库基础2

引擎

mysql 从 5.5.5 开始 默认引擎 是 innodb

以前是 MyISAM

查看 引擎

show engines \G # mysql 以 分号为结尾 \G分行显示 最佳阅读体验

myisam、innodb 区别

  1. myisam不支持事务 表锁 支持全文索引 不支持外键 读取效率高 如果你这张表读的多那么选择 myisam 引擎 更好
  2. myisam只缓存索引文件 数据文件的缓存 由 操作系统来完成
  3. innodb 支持事务、行锁 、支持外键

指定表引擎

创建表的时候 mysql> create table test6(id int(11))engine=myisam;
alter table 表名 engine=innodb;

增删改查

插入记录

  1. insert into 表名 values(值1,值2,值3,…值n);

    INSERT into users VALUE('13999999999',8,'ruirui','yangyang','123456',19); #value只能插入一行
    
    mysql> insert into users values('13888888888',1,'lijiarui','binggege','123abc',18);
    Query OK, 1 row affected (0.01 sec)  #插入一条记录  values 支持插入多行记录
    
    mysql> insert into users values('13888888888',2,'lijiarui','binggege','123abc',18),('13888888888',5,'lijiarui','binggege','123abc',18),('13888888888',3,'lijiarui','binggege','123abc',18),('13888888888',4,'lijiarui','binggege','123abc',18);  #插入多条
    Query OK, 4 rows affected (0.01 sec)
    Records: 4  Duplicates: 0  Warnings: 0
    
  2. insert into 表名(字段1,字段2,…,字段n) values(值1,值2,值3,…值n);

    mysql> insert into users(id,user_name,password) value(13,'haha','heiheihei');
    mysql> insert into users(id,user_name,password) values(9,'张三','123abc'),(10,'lisi','6666777'),(11,'wangwu','123123'),(12,'zhao
    si','9999120');
    
  3. 修改表字段 为主键自增 不用每次插入数据再插入主键了

    mysql> alter table test1 modify id int(11) not null primary key auto_increment;
    

查询

select

诸如 python中的print

mysql> select '66666' as haha;  # as 这里是起个别名

mysql> select '66666';
+-------+
| 66666 |
+-------+
| 66666 |
+-------+
1 row in set (0.00 sec)
mysql> select '66666' as haha;
+-------+
| haha  |
+-------+
| 66666 |
+-------+
1 row in set (0.00 sec)

查询语句 语法

select * from 表名

select 字段1,字段2,…字段n from 表名;

select 字段1[as 别名],字段2,…字段n from 表名;

mysql> select * from users;  
mysql> select id,user_name,age from users;
mysql> select id,user_name as 姓名,age as 年龄 from users;

单个字段不重复 distinct

mysql> select distinct user_name as 用户名 from users;
+----------+
| 用户名   |
+----------+
| binggege |
| yangyang |
| 张三     |
| lisi     |
| wangwu   |
| zhaosi   |
| haha     |
+----------+
7 rows in set (0.00 sec)

where 条件

符号 说明
> 大于
< 小于
= 等于
>= 大于等于
<= 小于等于
!= 不等于
and 并且
or 或者
is
is not 不是
select * from users where id<=5;
select * from users where id<=8 and age>18; #同时满足 and 左右的条件才出来结果  
select * from users where id<=8 or age>18; # 满足其中一个就返回
mysql> select * from users where jiaruilee is not null; #判断 jiaruilee  不为空的 
mysql> select * from users where jiaruilee is null; # jiaruilee 为空的 
like
mysql> select * from stars where username like '范冰冰';  #精确查询  跟下面精确查询结果一样
mysql> select * from stars where username='范冰冰';

mysql> select * from stars where username like '%冰冰';  #匹配冰冰为结尾的
mysql> select * from stars where username like '冰冰%';  #匹配冰冰为开头的
mysql> select * from stars where username like '%冰冰%'; #匹配包含冰冰的 

排序 order by

符号 说明
asc 升序
desc 降序

默认时 asc

mysql> select * from users where id<9 order by age;

mysql> select * from users where id<9 order by age asc;  #上面两个效果一样

mysql> select * from users where id<9 order by age desc;#降序排列  

多字段排序

mysql> select * from users where id<9 order by age desc,tel asc;

如果第一个字段已经排序好 那么第二个字段就不生效了

如果值一样 则值相同的字段按照第二个字段进行排序

mysql> select * from users where id<9 order by age desc,tel asc;
+-------------+----+-----------+-----------+----------+------+
| tel         | id | jiaruilee | user_name | password | age  |
+-------------+----+-----------+-----------+----------+------+
| 13999999994 |  6 | ruirui    | yangyang  | 123456   |   19 |
| 13999999996 |  8 | ruirui    | yangyang  | 123456   |   19 |
| 13888888881 |  3 | lijiarui  | binggege  | 123abc   |   18 |
| 13888888882 |  4 | lijiarui  | binggege  | 123abc   |   18 |
| 13888888883 |  5 | lijiarui  | binggege  | 123abc   |   18 |
| 13888888888 |  1 | lijiarui  | binggege  | 123abc   |   18 |
| 13888888889 |  2 | lijiarui  | binggege  | 123abc   |   17 |
| 13999999995 |  7 | ruirui    | yangyang  | 123456   |   16 |
+-------------+----+-----------+-----------+----------+------+
8 rows in set (0.00 sec)
age
上面  age  里边有 两个  19  四个18  针对 两个19 age结果一样  会对 tel 进行排序   
针对四个18 age 结果一样  那么会对tel进行排序 

limit 限制结果集

mysql> select * from users where age>=18 limit 5; #只显示符合条件的前五条结果 
+-------------+----+-----------+-----------+----------+------+
| tel         | id | jiaruilee | user_name | password | age  |
+-------------+----+-----------+-----------+----------+------+
| 13888888888 |  1 | lijiarui  | binggege  | 123abc   |   18 |
| 13888888881 |  3 | lijiarui  | binggege  | 123abc   |   18 |
| 13888888882 |  4 | lijiarui  | binggege  | 123abc   |   18 |
| 13888888883 |  5 | lijiarui  | binggege  | 123abc   |   18 |
| 13999999994 |  6 | ruirui    | yangyang  | 123456   |   19 |
+-------------+----+-----------+-----------+----------+------+


mysql> select * from users where age>=18 order by tel desc limit 5;  #排序并且限制结果集
+-------------+----+-----------+-----------+----------+------+
| tel         | id | jiaruilee | user_name | password | age  |
+-------------+----+-----------+-----------+----------+------+
| 13999999996 |  8 | ruirui    | yangyang  | 123456   |   19 |
| 13999999994 |  6 | ruirui    | yangyang  | 123456   |   19 |
| 13888888888 |  1 | lijiarui  | binggege  | 123abc   |   18 |
| 13888888883 |  5 | lijiarui  | binggege  | 123abc   |   18 |
| 13888888882 |  4 | lijiarui  | binggege  | 123abc   |   18 |
+-------------+----+-----------+-----------+----------+------+

区间结果集选择

mysql> select * from users where age>16 and age<18;
+-------------+----+-----------+-----------+----------+------+
| tel         | id | jiaruilee | user_name | password | age  |
+-------------+----+-----------+-----------+----------+------+
| 13888888889 |  2 | lijiarui  | binggege  | 123abc   |   17 |
+-------------+----+-----------+-----------+----------+------+
1 row in set (0.00 sec)

mysql> select * from users where age between 16 and 18; #包含 16 也包含 18 
+-------------+----+-----------+-----------+-----------+------+
| tel         | id | jiaruilee | user_name | password  | age  |
+-------------+----+-----------+-----------+-----------+------+
| 13888888888 |  1 | lijiarui  | binggege  | 123abc    |   18 |
| 13888888889 |  2 | lijiarui  | binggege  | 123abc    |   17 |
| 13888888881 |  3 | lijiarui  | binggege  | 123abc    |   18 |
| 13888888882 |  4 | lijiarui  | binggege  | 123abc    |   18 |
| 13888888883 |  5 | lijiarui  | binggege  | 123abc    |   18 |
| 13999999995 |  7 | ruirui    | yangyang  | 123456    |   16 |
| 13912345678 | 13 | NULL      | haha      | heiheihei |   16 |
+-------------+----+-----------+-----------+-----------+------+
7 rows in set (0.01 sec)

limit 区间结果集 选择 分页原理

select * from 表名 limit 偏移量,数量

用户想看第几页 用户决定

每页显示多少条 我们决定

第1页 偏移量 0 每页2条

2 2 2

3 4 2

4 6 2

5 8 2

6 10 2

7 12 2

(第几页-1)*每页显示的条数 = 偏移量

select * from 表名 limit 0,2; #第一页数据

统计函数 、聚合函数

  • 用户总数
  • 最大值
  • 平均值
  • 最小值
方法 说明
count 统计总数
sum 求和
avg 平均数
max 最大值
min 最小值
mysql> select count(*) as '总数' from users;
mysql> select count(id) as '总数' from users;
mysql> select sum(age) as '总和' from users;
mysql> select avg(age) as '平均值' from users;
mysql> select max(age) as '最大年龄' from users;
mysql> select min(age) as '最小年龄' from users;

分组 group by

mysql> select * from stars group by province; # 对省份进行分组 一样的结果只显示一个  
mysql> select id,username,province,count(province) from stars group by province; #统计数量之后 分组显示   


分组后 结果再过滤 having

mysql> select id,username,province,count(province) as res from stars group by province having res>1;
+----+----------+----------+-----+
| id | username | province | res |
+----+----------+----------+-----+
|  1 | 范冰冰   | 山东     |   2 |
|  7 | 刘亦菲   | 湖北     |   3 |
|  2 | 李冰冰   | 湖南     |   2 |
+----+----------+----------+-----+
3 rows in set (0.01 sec)

整体使用 SQL

select [字段1 [as 别名],[函数(字段2)]......字段n]
from  表名
[where  where 条件]
[group by 字段]
[having 指定结果]
[order by 字段 [desc]]
[limit]

多表联合查询

  • 内连接
  • 左连接
  • 右连接
  • 子查询
内连接

1.语法一 : select 表1.字段1 [as 别名],表n.字段 from 表1 [别名],表n where 条件;

mysql> select user.username as 用户名,order_goods.name as 商品名称 ,order_goods.buytime as 购买时间  from user,order_goods where
 user.uid=order_goods.uid;
 
 #给表起了别名  起了就得用  
 mysql> select u.username as 用户名,o.name as 商品名称 ,o.buytime as 购买时间  from user u,order_goods o where u.uid=o.uid;

2.语法2 select 表1.字段1 [as 别名],表n.字段 from 表1 inner join 表2 on 条件;

mysql> select u.username,o.name,o.buytime from user u inner join order_goods o on u.uid=o.uid;
+----------+------------+-----------+
| username | name       | buytime   |
+----------+------------+-----------+
| 闵锐     | 苹果三件套 |    123123 |
| 石阳     | 特斯拉     |  12312345 |
| 小牧     | 螺狮粉     | 123123123 |
| 昊昊     | 宠物狗     | 123456123 |
+----------+------------+-----------+
4 rows in set (0.00 sec)
左连接

select 表1.字段1 [as 别名],表n.字段 from 表1 left join 表2 on 条件; 以left join 左边的表为准

mysql> select u.username,o.name,o.buytime from user u left join order_goods o on u.uid=o.uid;
+----------+------------+-----------+
| username | name       | buytime   |
+----------+------------+-----------+
| 闵锐     | 苹果三件套 |    123123 |
| 石阳     | 特斯拉     |  12312345 |
| 小牧     | 螺狮粉     | 123123123 |
| 昊昊     | 宠物狗     | 123456123 |
| 旺恒     | NULL       |      NULL |
| 灿灿     | NULL       |      NULL |
+----------+------------+-----------+
6 rows in set (0.01 sec)



如果有 NULL 就显示默认值

(case when 表.字段 is null then 默认值 else 表.字段 end ) as 别名
mysql> select u.username,(case when o.name is null then '没时间买' else o.name end) as 商品名称,(case when o.buytime is null the
n '大忙人' else o.buytime end) as 购买时间 from user u left join order_goods o on u.uid=o.uid;
+----------+------------+-----------+
| username | 商品名称   | 购买时间  |
+----------+------------+-----------+
| 闵锐     | 苹果三件套 | 123123    |
| 石阳     | 特斯拉     | 12312345  |
| 小牧     | 螺狮粉     | 123123123 |
| 昊昊     | 宠物狗     | 123456123 |
| 旺恒     | 没时间买   | 大忙人    |
| 灿灿     | 没时间买   | 大忙人    |
+----------+------------+-----------+
6 rows in set (0.00 sec)

ifnull(表.字段,'默认值'),

mysql> select u.username,ifnull(o.name,'666') as 商品名称,ifnull(o.buytime,'888') as 购买时间 from user u left join order_goods
o on u.uid=o.uid;
+----------+------------+-----------+
| username | 商品名称   | 购买时间  |
+----------+------------+-----------+
| 闵锐     | 苹果三件套 | 123123    |
| 石阳     | 特斯拉     | 12312345  |
| 小牧     | 螺狮粉     | 123123123 |
| 昊昊     | 宠物狗     | 123456123 |
| 旺恒     | 666        | 888       |
| 灿灿     | 666        | 888       |
+----------+------------+-----------+
6 rows in set (0.01 sec)
右连接

select 表1.字段1 [as 别名],表n.字段 from 表1 right join 表2 on 条件; 以right join 右边的表为准

mysql> select u.username,o.name,o.buytime from user u right join order_goods o on u.uid=o.uid;
+----------+------------+-----------+
| username | name       | buytime   |
+----------+------------+-----------+
| 闵锐     | 苹果三件套 |    123123 |
| 石阳     | 特斯拉     |  12312345 |
| 小牧     | 螺狮粉     | 123123123 |
| 昊昊     | 宠物狗     | 123456123 |
+----------+------------+-----------+
4 rows in set (0.00 sec)
子查询 in not in = != exists not exists any all
mysql> select * from user where uid in (1,3,4,5);
+-----+----------+-------------+----------+
| uid | username | tel         | password |
+-----+----------+-------------+----------+
|   1 | 闵锐     | 13888888888 | abc123   |
|   3 | 石阳     | 13666666666 | 666666   |
|   4 | 小牧     | 13555555555 | 7777777  |
|   5 | 昊昊     | 13999999999 | 999999   |
+-----+----------+-------------+----------+
4 rows in set (0.01 sec)

mysql> select * from user where uid in (select uid from order_goods);
+-----+----------+-------------+----------+
| uid | username | tel         | password |
+-----+----------+-------------+----------+
|   1 | 闵锐     | 13888888888 | abc123   |
|   3 | 石阳     | 13666666666 | 666666   |
|   4 | 小牧     | 13555555555 | 7777777  |
|   5 | 昊昊     | 13999999999 | 999999   |
+-----+----------+-------------+----------+
4 rows in set (0.01 sec)

mysql> select * from user where uid not in (select uid from order_goods);
+-----+----------+-------------+----------+
| uid | username | tel         | password |
+-----+----------+-------------+----------+
|   2 | 旺恒     | 13777777777 | 123456   |
|   6 | 灿灿     | 13111111111 | 111111   |
+-----+----------+-------------+----------+
2 rows in set (0.01 sec)



>any 大于子查询种某个结果的值
>all  大于子查询结果中的所有值 
mysql> select * from stars where age>all(select age from stars where username in('范冰冰','李冰冰'));
+----+----------+-------------+----------+------+------+
| id | username | balance     | province | age  | sex  |
+----+----------+-------------+----------+------+------+
|  4 | 郭德纲   | 88888888.00 | 天津     |   50 |    0 |
+----+----------+-------------+----------+------+------+
1 row in set (0.01 sec)
# 查询比范冰冰 和 李冰冰  年龄大的人的信息 

mysql> select * from stars where age select * from stars where age
union union all

union 会把 union all的结果去重显示 (只有一个字段 会去重 )

使用union union all 两个表的字段数量 及类型 要一致

mysql> select uid as 序号,username as 姓名 from user union all select id,user_name from users;
+------+----------+
| 序号 | 姓名     |
+------+----------+
|    1 | 闵锐     |
|    2 | 旺恒     |
|    3 | 石阳     |
|    4 | 小牧     |
|    5 | 昊昊     |
|    6 | 灿灿     |
|    1 | binggege |
|    2 | binggege |
|    3 | binggege |
|    4 | binggege |
|    5 | binggege |
|    6 | yangyang |
|    7 | yangyang |
|    8 | yangyang |
|    9 | 张三     |
|   10 | lisi     |
|   11 | wangwu   |
|   12 | zhaosi   |
|   13 | haha     |
+------+----------+
19 rows in set (0.00 sec)

mysql> select uid as 序号,username as 姓名 from user union select id,user_name from users;
+------+----------+
| 序号 | 姓名     |
+------+----------+
|    1 | 闵锐     |
|    2 | 旺恒     |
|    3 | 石阳     |
|    4 | 小牧     |
|    5 | 昊昊     |
|    6 | 灿灿     |
|    1 | binggege |
|    2 | binggege |
|    3 | binggege |
|    4 | binggege |
|    5 | binggege |
|    6 | yangyang |
|    7 | yangyang |
|    8 | yangyang |
|    9 | 张三     |
|   10 | lisi     |
|   11 | wangwu   |
|   12 | zhaosi   |
|   13 | haha     |
|   14 | 闵锐     |
+------+----------+
20 rows in set (0.00 sec)

mysql> select uid,username from user union all select id,user_name from users;
+-----+----------+
| uid | username |
+-----+----------+
|   1 | 闵锐     |
|   2 | 旺恒     |
|   3 | 石阳     |
|   4 | 小牧     |
|   5 | 昊昊     |
|   6 | 灿灿     |
|   1 | binggege |
|   2 | binggege |
|   3 | binggege |
|   4 | binggege |
|   5 | binggege |
|   6 | yangyang |
|   7 | yangyang |
|   8 | yangyang |
|   9 | 张三     |
|  10 | lisi     |
|  11 | wangwu   |
|  12 | zhaosi   |
|  13 | haha     |
|  14 | 闵锐     |
+-----+----------+
20 rows in set (0.00 sec)

mysql> select uid from user union all select id  from users;
+-----+
| uid |
+-----+
|   6 |
|   4 |
|   3 |
|   2 |
|   1 |
|   5 |
|   1 |
|   2 |
|   3 |
|   4 |
|   5 |
|   6 |
|   7 |
|   8 |
|   9 |
|  10 |
|  11 |
|  12 |
|  13 |
|  14 |
+-----+
20 rows in set (0.00 sec)

mysql> select uid from user union select id  from users;
+-----+
| uid |
+-----+
|   6 |
|   4 |
|   3 |
|   2 |
|   1 |
|   5 |
|   7 |
|   8 |
|   9 |
|  10 |
|  11 |
|  12 |
|  13 |
|  14 |
+-----+
14 rows in set (0.00 sec)

mysql> select * from stars where age

此笔记转载至讲师笔记

你可能感兴趣的:(使用方法,mysql)