入门Mysql数据库

目录

0.数据类型

1)文本

2)数组

3)日期

1.操作数据库

1)创建一个叫test的数据库

2)删除一个叫test的数据库

3)查看所有的数据库

4)选中库,对库中表做操作前需要先选中库

5)  查看库中所有表-先选中

6)查看创建库的详细信息

2.表操作

1)创建表

2)查看创建表的详细信息

3)删除表

4)查看表的所有字段

5)修改字段类型

6)添加新的字段

7)指定位置添加字段

8)删除表字段

9)修改指定的字段为新字段

3.数据操作

1)  增加数据insert

2)删除数据delect: (不加条件删除为全删,即跑路删)

3)  更新数据(update),也要加上where(不然值全变了,即跑路改)

4)distinct将查询到的重复值过滤,只保留一条

5)查数据select

5.1)查询表中的所有数据

5.2)查询指定字段的所有数据

5.3)条件查询

5.4)模糊查询

5.5)  给多个字段来排序order by

5.6)  查询数量限制limit  (limit 数量)

5.7)  查询数量限时limit加偏移量(limit 偏移量,数量)

5.8)数据库统计/聚合函数(avg count max min sum)

5.9)  group by分组查询:

4.补漏

1)null的判断

 2)insert into和select联合使用:

3)内连查 inner join

4)  左外连接查询left outer join

5)  右外连接查询right outer join

6)全外连接

5.DCL 数据控制语言


0.数据类型

1)文本

https://img-blog.csdnimg.cn/20181127105914129.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzM3MzIxMDk4,size_16,color_FFFFFF,t_70

2)数组

https://img-blog.csdnimg.cn/20181127105941297.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzM3MzIxMDk4,size_16,color_FFFFFF,t_70

3)日期

https://img-blog.csdnimg.cn/20181127105954294.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzM3MzIxMDk4,size_16,color_FFFFFF,t_70

1.操作数据库

1)创建一个叫test的数据库

create database test

2)删除一个叫test的数据库

drop database test;

3)查看所有的数据库

show databases; 

4)选中库,对库中表做操作前需要先选中库

use 数据库名;

5)  查看库中所有表-先选中

use 数据库名;
show tables; 

6)查看创建库的详细信息

show create databases 库名;

2.表操作

1)创建表

create table 表名 (字段1 类型, 字段2 类型);

demo:
create table text(
name text,
age int
)

2)查看创建表的详细信息

show create table 表名; 

3)删除表

drop table 表名; 

4)查看表的所有字段

desc 表名;

5)修改字段类型

alter table 表名 modify 字段 字段类型;

https://img-blog.csdnimg.cn/2018112710365746.png

6)添加新的字段

alter table 表名 add 字段 字段类型

https://img-blog.csdnimg.cn/20181127103836464.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzM3MzIxMDk4,size_16,color_FFFFFF,t_70

7)指定位置添加字段

alter table 表名 add 字段 字段类型 after 字段;

https://img-blog.csdnimg.cn/20181127104008531.png

8)删除表字段

alter table 表名 drop 字段名;

https://img-blog.csdnimg.cn/20181127104115136.png

9)修改指定的字段为新字段

alter table 表名 change 原字段名字  新的字段名字 字段类型

https://img-blog.csdnimg.cn/20181127104333403.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzM3MzIxMDk4,size_16,color_FFFFFF,t_70

3.数据操作

1)  增加数据insert

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

https://img-blog.csdnimg.cn/20181127104747622.png

insert into 表名(字段1,字段2...) values(值1,值2,....);

https://img-blog.csdnimg.cn/20181127104825510.png

insert into 表名(字段1,字段2...) values(值1,值2,....),(值1,值2,....),(值1,值2,....);

https://img-blog.csdnimg.cn/20181127104838878.png

2)删除数据delect: (不加条件删除为全删,即跑路删)

delete from 表名 where 条件

https://img-blog.csdnimg.cn/20181127105111336.png

and相当于&&,or相当于||

3)  更新数据(update),也要加上where(不然值全变了,即跑路改)

update 表名 set字段1 = 值1, 字段2 = 值2 where 条件

https://img-blog.csdnimg.cn/20181127105413830.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzM3MzIxMDk4,size_16,color_FFFFFF,t_70

4)distinct将查询到的重复值过滤,只保留一条

select distinct 字段 from 表名。

https://img-blog.csdnimg.cn/20181127105703851.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzM3MzIxMDk4,size_16,color_FFFFFF,t_70

5)查数据select

5.1)查询表中的所有数据

select * from 表名

5.2)查询指定字段的所有数据

select 字段 from 表名 

5.3)条件查询

select 字段 from 表名 where 条件

条件的关系:>,<,>=,<=,=,!=

逻辑:or, and  (and相当于&&,or相当于||)

区间:id between 4 and 6 ;闭区间,包含边界

demo:

https://img-blog.csdnimg.cn/20181127111559291.png

5.4)模糊查询

select *from 表名 where 字段 like '%值%';

https://img-blog.csdnimg.cn/20181127111941239.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzM3MzIxMDk4,size_16,color_FFFFFF,t_70

5.5)  给多个字段来排序order by

select 字段列表/* from 表名 where 条件 order by 字段名1 asc/desc, 字段名2 asc/desc,.......

 1.asc 升序

 2.desc 降序

https://img-blog.csdnimg.cn/20181127114745277.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzM3MzIxMDk4,size_16,color_FFFFFF,t_70

5.6)  查询数量限制limit  (limit 数量)

限制3条数据:

https://img-blog.csdnimg.cn/20181127115250690.png

5.7)  查询数量限时limit加偏移量(limit 偏移量,数量)

限制3条数据同时,偏移值1条数据:会pass一条数据:

https://img-blog.csdnimg.cn/20181127115329847.png

分页接口的偏移值计算:

 limit (n-1)*每页数据量,每页数据量    ----->n为页数,从0页开始,还是从1页自己定

5.8)数据库统计/聚合函数(avg count max min sum)

SQL语句都是针对此表做操作:

https://img-blog.csdnimg.cn/20181127120011157.png

https://img-blog.csdnimg.cn/20181127120915282.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzM3MzIxMDk4,size_16,color_FFFFFF,t_70

查询名字有王的数据总数:

https://img-blog.csdnimg.cn/20181127121004696.png

use stockmgt;
Select max(MONEY) from t_user;//查此字段数据的最大值
Select min(MONEY) from t_user;//查此字段数据的最小值
Select sum(MONEY) from t_user;//查此字段数据的总和

5.9)  group by分组查询:

group by必须得配合聚合函数来用,分组之后你可以计数(COUNT),求和(SUM),求平均数(AVG)等。 

https://img-blog.csdnimg.cn/2018112714031360.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzM3MzIxMDk4,size_16,color_FFFFFF,t_70

再举个例子-密码相同的人的钱数总和:

https://img-blog.csdnimg.cn/20181127141224793.png

再看看密码相同的人的数量和,以及密码:

https://img-blog.csdnimg.cn/20181127141727598.png

4.补漏

1)null的判断

查表中USER_NAME字段为非空的数据:

Select *from t_user where USER_NAME is not null;

为空就是:

Select *from t_user where USER_NAME is null;

 2)insert into和select联合使用:

demo1:

https://img-blog.csdnimg.cn/20181127145222263.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzM3MzIxMDk4,size_16,color_FFFFFF,t_70

或者:insert into 表名1 select 字段1,字段2 from 表名2;

3)内连查 inner join

https://img-blog.csdnimg.cn/20181127174040242.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzM3MzIxMDk4,size_16,color_FFFFFF,t_70

https://img-blog.csdnimg.cn/20181127174329873.png

特点:只查询在连接的表中能够有对应(满足连接条件:u.user_id=o.buyer_id)的记录

4)  左外连接查询left outer join

以左边的表的数据为基准,去匹配右边的表的数据,如果匹配到就显示,匹配不到就显示为null

https://img-blog.csdnimg.cn/20181127180314994.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzM3MzIxMDk4,size_16,color_FFFFFF,t_70

5)  右外连接查询right outer join

以右边的表的数据为基准,去匹配左边的表的数据

https://img-blog.csdnimg.cn/20181127180616429.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzM3MzIxMDk4,size_16,color_FFFFFF,t_70

6)全外连接

mysql中没有full outer join关键字(oracle中,直接就使用full outer join关键字连接两表)

mysql中用union关键字连接左外连接和右外连接:

https://img-blog.csdnimg.cn/20181127181329370.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzM3MzIxMDk4,size_16,color_FFFFFF,t_70

5.DCL 数据控制语言

转:https://blog.csdn.net/h294590501/article/details/80358590

创建用户:create user'xiaoming'@'localhost' identified by '666666';

授权用户:grant all on test.*to'xiaoming'@'localhost';

刷新权限:flush privileges;

取消授权:revoke all on test.* from 'xiaoming'@'localhost';

删除用户: drop user'xiaoming'@'localhost';

猜你喜欢

转载自blog.csdn.net/qq_37321098/article/details/84561565