1)数据库的类型有哪些?至少各举例三个
关系型数据库:
informix IBM+HP
db2 IBM
oracle 甲骨文 中大型企业
mysql 甲骨文 互联网公司
sqlserver 微软 传统企业
非关系型数据库:
mongodb 大数据,爬虫
redis 缓存,验证,消息队列
cassandra facebook

2)数据库,表,数据的关系?
数据库就是存放数据的仓库,分为:数据库软件、数据库数据。常说的数据库是指数据可数据
数据库由表组成,表由数据组成。
数据——>表——>数据库

3)安装数据库的方法有哪些?
1.win:下载msi安装文件或者使用集成工具
2.Linux:rpm安装,二进制文件安装,yum自动安装

4)启动服务的命令?
linux:service mysql start
win:对应图形界面操作

5)mysql登录的命令
mysql是一款c/s架构软件
mysql mysql的客户端服务
mysqld mysql的服务端服务
mysql的管理员:root
登陆命令
mysql 登陆本地数据库
mysql -u root -p 管理员登陆本地数据库
mysql -h 127.0.0.1 -u root -p 管理员登陆IP为127.0.0.1的数据库

6)操作数据库的命令:展示所有数据库?查看数据库的名字?切换数据库
查看所有的数据库:show databases;
查看当前数据库名字:select database();
切换数据库:use + 要切换到的数据库名;

7)表的查看操作:查看当前表有哪些?查看表中的所有数据?
show tables;
select * from table_name; (table_name是表的名)

8)表的创建和删改:创建、查看表名,查看表格的某一列
创建表:
create table 表名(
列名(字段、表头) 数据类型,
列名 数据类型,

);
例子: 创建表goods,商品名称name 字符类型,最多10个字符,
商品价格 price 数字类型,出厂日期 ddate 日期类型
create table goods(
name char(10),
price int,
ddate date
);
查看表结构:desc 表名; 例如:desc goods;
删除表结构: drop table 表名; 例如:drop table goods;

9)删除、修改、增加列
增加(alter…add…):goods表中增加一列,商品id(gid),整型
alter table goods add gid int;
修改(modify):修改goods表中商品id的数据类型,修改为字符类型,最多8个字符
alter table goods modify gid char(10);
删除(drop column): 删除goods表中gid列
alter table goods drop column gid;

补充:向列里增加数据(insert into …values();)
数据的新增
字符类型数据需要加单引号
日期类型数据需要加单引号
数字类型数据直接使用
注意列的顺序和值的顺序对应
1.向所有列新增数据
语法:insert into 表 values (值1,值2…);
向goods表中新增数据,名字是o8ma,价格250,出产日期 2008-01-01
insert into goods values(‘o8ma’,250,‘2008-01-01’);
2.向指定列新增数据
语法:insert into 表(列1,列2) values(值1,值2);
向goods表中新增数据,名字ladeng,售价2500
insert into goods(price,name) values(2500,‘ladeng’);
注意:如果指定列新增数据,没有新增数据的列的值为空
3.新增多条数据
insert into goods values(‘axiba’,200,‘2000-01-01’),(‘wadaxiwa’,500,‘1999-01-01’),(‘haleshao’,1000,‘2001-01-01’);

10)5种约束
主键约束:列中的数据唯一,不重复,每张表中只能由一个主键,非空 (primary key)
唯一约束:列中的数据唯一,不重复,作为主键约束的补充,可以为空,不可以被外键引用 (unique)
非空约束:列中的数据不能为空 (not null)
检查约束:列中的数据是否满足条件 (check)
外键约束:列中的数据依赖于另外一张表中的列的数据 (references)
外键约束需要注意:
1.被参考表的被参考列一定是存在的
2.被参考列一定是主键约束
3.参考列和被参考列的数据类型必须一致
例子:create table 表 (
列 数据类型 primary key,
列 数据类型 unique,
列 数据类型 not null,
列 数据类型 check(条件),
列 数据类型 references 被参考表(被参考列)
);

11)SQL的分类:
SQL:结构化查询语言
DDL:数据库定义语言 create alter drop truncate (对表、列操作时用到)
DML:数据库操作语言 insert delete update (对数据进行操作时用到)
DCL:数据库控制语言 grant revoke (对数据库赋权时用到,revoke为取消授权)
DQL:数据库查询语言 select (查询数据时用到)

12)事务以及结束事务的方式
事务:一件事有开端就一定要有结束,DML语句会触发事务的开始。
结束事务的方式:
commit; 提交数据到数据库中,除非认为修改,否则不会改动
rollback; 数据库状态恢复到最后一次提交的状态

13)数据库的管理小工具有哪些?
mysql workbench;
sqlyog(小海豚);
navicat;
dbeaver。

补充:授权。语法:grant 权限 to 用户@IP identified by 密码;
例如:grant all privileges on . to root@’%’ identified by ‘test’;

grant:						授权命令
all privileges:				赋予所有权限
on :						在哪个数据库的哪个表
*.*:                        前面的*代表数据库,所有数据库,后面的*代表所有的表
root: 						用户名
%:                          允许访问的IP地址,%代表所有IP都可以
identified by 'test'        设置远程登陆的用户的登陆密码是test1.可以为不存在的数据库授权。

补充:
1.可以为不存在的数据库授权。
2.如果grant命令标识的用户不存在,它将被创建。
3.如果创建一个用户而没有包括identified by子句,则不需要密码就能登录。
4.如果现有用户被授予新权限,并且在grant命令中使用了identified by子句,则该用户的旧密码将被新密码代替。
5.表级grant只支持如下权限类型:alter、create、createview、delete、drop、grant、index、insert、references、select、show view、update.
6.列级grant只支持以下权限类型:insert、select、update.
7.在grant命令中引用数据库名和主机名时,支持_和%通配符。因为_字符在Mysql数据库名中也是合法字符,所以如果在grant中用到,需要用反斜线进行转义。
8.如果希望创建和删除用户,而且运行的是Mysql 5.0.2或更高版本,可以考虑使用create user和Drop user命令。
9.不能引用*.*来删除某用户对所有数据库的权限。相反,必须用单独的revoke命令逐个显式地引用每一个权限。

14)数据的增删改
一、数据的增加(insert):
字符类型数据需要加单引号
日期类型数据需要加单引号
数字类型数据直接使用
注意列的顺序和值的顺序对应
1.向所有列新增数据
语法:insert into 表 values (值1,值2…);
向goods表中新增数据,名字是o8ma,价格250,出产日期 2008-01-01
insert into goods values(‘o8ma’,250,‘2008-01-01’);
2.向指定列新增数据
语法:insert into 表(列1,列2) values(值1,值2);
向goods表中新增数据,名字ladeng,售价2500
insert into goods(price,name) values(2500,‘ladeng’);
注意:如果指定列新增数据,没有新增数据的列的值为空
3.新增多条数据
insert into goods values(‘axiba’,200,‘2000-01-01’),(‘wadaxiwa’,500,‘1999-01-01’),(‘haleshao’,1000,‘2001-01-01’);

二、数据的删除(delete)
delete 可以删除指定数据,DML,删除数据可以找回
truncate 只能删除全表数据,DDL,删除数据无法找回

a.delete语法
delete from 表 where 条件;
注意:如果增加where条件,删除的是指定数据
	删除goods表中名字是o8ma的所有数据
	delete from goods where name='o8ma';
	删除goods表中所有数据
	delete from goods;
b.truncate语法(截断)
truncate table 表;
	删除goods表中所有数据
	truncate table goods;

三、数据更改(update)
数据的修改
字符类型数据需要加单引号
日期类型数据需要加单引号
数字类型数据直接使用

语法:update 表 set 列=值 where 条件;
注意:如果不增加where条件,默认修改的是整个列的数据,如果增加where条件,修改的是指定行的列
	修改goods表中名字是o8ma的修改价格为100
	update goods set price=100 where name='o8ma';
	修改goods表中所有价格为0
	update goods set price=0;

15)数据查询的四种方式:
1.简单查询:
可以查询所有数据
查询ecs_goods表中所有的数据
select * from ecs_goods;
可以查询指定行的数据
查询ecs_goods表中商品名称是KD876的所有信息
select * from ecs_goods where goods_name=‘KD876’;
可以查询指定行的列的数据
查询ecs_goods表中商品名称是KD876的本店售价
select shop_price from ecs_goods where goods_name=‘KD876’;
可以查询指定列的数据
查询ecs_goods表中所有的商品名称
select goods_name from ecs_goods;
语法:
select 列 from 表 where 条件;
select后面,from前面是和列相关的,where条件和行相关
*代表所有列
where后面加的条件类型:
(a)比较:
> 大于
< 小于
= 等于
>= 大于等于
<= 小于等于
!= 不等于
<> 不等于
(b)逻辑、关系:
and 同时满足
or 满足一个条件即可
not 不满足条件
©区间:between…and…(区间是包括了端点,小在前,大在后)
例如:查询ecs_goods表中本店售价范围2300和4000之间的所有商品信息
select * from ecs_goods where shop_price>=2300 and shop_price<=4000;
select * from ecs_goods where shop_price between 2300 and 4000;
(d)like 模糊查询关键字
- 一个字符
% 任意个字符
注意:模糊查询主要是和字符类型一起使用
例如:1.查询ecs_goods表中商品名称以诺基亚开头的所有信息
select * from ecs_goods where goods_name like ‘诺基亚%’;
2查询ecs_goods表中商品名称以6结尾的所有信息
select * from ecs_goods where goods_name like ‘%6’;
3.查询ecs_goods表中商品名称是6个字符的所有商品信息
select * from ecs_goods where goods_name like ‘______’;
(e)in(在…里面,或的关系)
例如:查询ecs_goods表中商品名称是KD876的或者诺基亚的所有信息
select * from ecs_goods where goods_name in (‘KD876’,‘诺基亚’);
(f)空值查询。空是没有任何数据类型,没有任何值,不能用于比较和运算;
查询时候使用is null或者is not null。
例如:1.查询ecs_goods表中suppliers_id为空的所有数据
select * from ecs_goods where suppliers_id is null;
2.查询ecs_goods表中suppliers_id非空的所有数据
SELECT * FROM ecs_goods WHERE suppliers_id IS NOT NULL;
(f)常用函数
upper() 大写
lower() 小写
round() 小数点后保留几位
avg() 平均值
SELECT goods_name,LOWER(goods_name) FROM ecs_goods;
SELECT AVG(shop_price),ROUND(AVG(shop_price),2) FROM ecs_goods;

2.多表查询:
	a.笛卡尔积
			两张表不做任何关联产生的数据,造成无效数据增多,数据量成倍增加,可以通过内联查询、左联查询、右联查询来避免产生笛卡尔积
			select * from ecs_goods;--33
			select * from ecs_category;--14
			select * from ecs_goods,ecs_category;--462
	b.语法:
		(1)select 列 from 表1 inner join 表2 on 表1.列=表2.列;
		(2)select 列 from 表1,表2 where 表1.列=表2.列;
		(3)关联列要求:a.名字相同;b.作用相同;c.主外键关系
	(c)实例
		(1)and的运用以及步骤	
			查询商品名称是KD876的分类名称
				1.分析列
					商品名称
					分类名称
				2.分析表
					商品名称   ecs_goods
					分类名称   ecs_category
				3.关联
					select * from ecs_goods,ecs_category
					where ecs_goods.cat_id=ecs_category.cat_id;
				4.过滤
					select cat_name from ecs_goods,ecs_category
					where ecs_goods.cat_id=ecs_category.cat_id
					and goods_name='KD876';
		(2)like,%的应用
			查询商品名称以诺基亚开头的商品的分类名称()
				1.分析列
					商品名称
					分类名称
				2.分析表
					ecs_goods
					ecs_category
				3.关联
					select * from ecs_goods e1,ecs_category e2
					where e1.cat_id=e2.cat_id;
				4.过滤
					select e2.cat_name from cs_goods e1,ecs_category e2
					where e1.cat_id=e2.cat_id
					and e1.goods_name like '诺基亚%';
		(3)查询分类名称以手机结尾的商品名称
				select g.goods_name  from ecs_category c,ecs_goods g
				where c.cat_id=g.cat_id
				and c.cat_name like '%手机';
		(4)between...and...的应用
			询商品本店售价范围2000和3000之间的商品的分类名称
			select  c.cat_name from ecs_goods g,ecs_category c
			where g.cat_id=c.cat_id
			and g.shop_price between 2000 and 3000;
		(5)去重函数distinct(),就是把这些相同条件的意外的信息打印出来(查询)
			SELECT  DISTINCT(c.cat_name) FROM ecs_goods g,ecs_category c
			WHERE g.cat_id=c.cat_id
			AND g.shop_price BETWEEN 2000 AND 3000;

3.子查询:一条SQL语句的执行结果依赖于另外一条SQL语句的执行结果
	(a)例子:查询商品名称是KD876的分类名称
			分析列
				商品名称
				分类名称
			分析表
				ecs_goods
				ecs_category
			分析关联列
				cat_id
			1.查询商品表中满足条件的cat_id
				select cat_id from ecs_goods where goods_name='KD876';--4

			2.查询分类表中cat_id为上面查询出来结果的分类名称
				select cat_name from ecs_category where cat_id=(select cat_id from ecs_goods where goods_name='KD876');
				
	(b)注意:子查询可以应用在数据的新增,修改和删除中
		--2.jpg
			3.insert into achievement values((select id from student where name='Robert'),80);
			4.update achievement set mark=87 where id=(select id from student where name='Rose');
			5.
			delete from achievement where id=(select id from student where name='Betty');
			delete from student where name='Betty';
	(c)注意:子查询分为单行子查询、多行子查询
		单行子查询,查询子句返回的结果是1行,可以使用=或者in
		多行子查询,查询子句返回的结果是多行,只能用in
			例子:查询以诺基亚开头的商品的分类名称有哪些?
				a.查询商品表中满足条件的cat_id
				select cat_id from ecs_goods where goods_name like '诺基亚%';

				b.查询分类表中cat_id为上面查询出来结果的分类名称
				select cat_name from ecs_category where cat_id in (select cat_id from ecs_goods where goods_name like '诺基亚%');
	
4:分组查询:按照某种条件进行分类
		(1).分组函数
			max()   最大
			min() 	最小
			avg()	平均
			sum()   求和
			count() 计数
			分组函数通常是和分组一起使用,也可以单独使用
				例子:
					(a)查询ecs_goods表中最高最低平均本店售价是多少
					select max(shop_price),min(shop_price),avg(shop_price) from ecs_goods;
					(b)查询本店售价高于平均本店售价的商品名称
					select avg(shop_price) from ecs_goods;
					select goods_name from ecs_goods where shop_price>(select avg(shop_price) from ecs_goods);
		(2)	分组查询
					a)语法:select 列 from 表 where 条件
							group by 分组条件
								having 分组后过滤条件;
					b)having和where区别:
						having 分组后过滤,后面可以直接跟分组函数
						where 分组前过滤,后面不能直接跟分组函数
		(3)排序和分页
			a)排序:按照某种顺序进行排列。
				语法:select 列 from 表 where 条件 group by 分组条件 having 分组后过滤条件 order by 排序条件;
				顺序:从小到大	
					order by 列 
					order by 列 asc
				逆序:从大到小
					order by 列 desc
			b)分页(limit)
				语法:select 列 from 表 where 条件 group by 分组条件 having 分组后过滤条件 order by 排序条件 limit 限制条件
				例子:
					查询ecs_goods表中前5行数据
					select * from acs_goods limit 5;
					查询ecs_goods表中6-15行数据
					select * from ecs_goods limit 5,10;  (n-1开始,共10行数据)

分组条件:每后面跟的就是分组条件,一定不能出现一对多的情况

16)导入sakila数据库和进行练习
导入sakila数据库
https://dev.mysql/doc/index-other.html
1.把下载后的sakila数据库压缩包传入linux中
2.解压sakila压缩包文件
tar -xzvf sakila-db.tar.gz
3.进入到已经解压到的目录中
cd sakila-db
4.登陆mysql数据库
mysql -u root -p
5.导入sakila-schema.sql
source sakila-schema.sql;
6.导入sakila-data.sql
source sakila-data.sql

17)delete和truncate的区别
delete删除数据后自增序列会继续生效,删除了可以找回数据。
truncate删除数据后自增序列会初始化,删除了无法找回数据。

18)分组查询
19)排序和分页
20)补充:入门存储过程看菜鸟教程
https://www.runoob/w3cnote/mysql-stored-procedure.html

更多推荐

数据库