首先介绍了数据库中相关的一些知识点。从第五小节开始进入mysql的详细学习。

1. 什么是DB

DataBase:数据库,实际上就是一个文件集合,本质就是一个文件系统,数据按照特定的格式存储到文中,
		 使用sql语言对数据进行增删改查操作。

2. 什么是DBMS

DataBaseManagementSystem:数据库管理系统,管理数据库文件的软件。
指一种操作和管理数据库的大型软件,用于建立,使用和维护数据库,对数据进行统一的管理和控制,
用户通过DBMS访问数据库中的数据。

常见的DBMS : mysql oracle db2 sqlserver sqlite ...
1. mysql: 是oracle公司的,MySQL 08年被sun收购 09 sun被oracle收购 ,开源,市场占有最高
2. oracle: oracle公司,性能最强大的数据库,而且收费最贵,通常不缺钱的技术公司会选用,市场排名第二
3. DB2: IBM公司,闭源收费,通常一些银行项目会使用
4. sqlserver: 微软公司,排名第三,闭源收费,提供整套解决方案(web服务器,操作系统,数据库服务器等)
5. sqlite:轻量级数据库,应用在嵌入式或移动设备中,大小只有几十k,功能和性能较大型数据库要少很多。

3. 数据库的分类

1. 关系型数据库:经过数学理论验证,可以将现实生活中存在的各种关系,保存到数据库中,这种数据库
	称为关系型数据库,在此数据库中,以表的形式保存数据之间的关系。
2. 非关系型数据库: 主要为了解决特定的应用场景,如:缓存,高并发访问等,存储数据的方式有多种,
	redis是常见的非关系型数据库,redis是以键值对的形式保存数据。

4. 什么是sql

Stuctured Query Language: 结构化查询语言,使用sql语言和数据库服务器进行交互,通过sql告
						  诉数据库服务器对数据进行什么操作。
4.1 sql规范:
1. 以;(分号)结尾
2. 关键字之间有空格,通常只有一个,但多个也可以
3. 可以存在换行
4. 数据库名称和表名称区分大小写

5. 连接数据库

  • 打开终端或命令行 在终端中输入以下命令:
mysql -h localhost -P 3306 -u root -p
或者
mysql -uroot -p
回车之后,输入自己设定的密码。
  • 退出指令:
exit

6. 数据库相关的SQL语句

  • 每一个工程对应一个数据库,存储数据需要先创建一个数据库,然后在数据库中创建表
1. 查看所有数据库
		show databases;
2. 创建数据库
	-格式:create database 数据库名称;
		create database db1;
	-指定字符集: create database 数据库名称 character set gbk;
		create database db2 character set gbk;
3. 查看指定数据库详情
	-格式:show create database 数据库名称;
		show create database db1;
4. 删除数据库
	-格式:drop database 数据库名称;
		drop database db2;
5. 使用数据库
	-格式: use 数据库名称;
		use db1;

7. 和表相关的sql语句

  • 表是关系型数据库存储数据的单位,数据库中存储数据需要先创建表,往表中存储数据。
  • 执行表相关的sql时一定要先选择数据库
  • 创建表sql语句的执行过程: 在终端中写完sql语句后敲回车终端会把sql通过网络传输到DBMS(mysql),DBMS对sql语句进行解析,然后对数据库中的数据进行操作。
1. 创建表
	-格式: create table 表名(字段1名 字段1类型, 字段2名 字段2类型,.....);
	create table stu(id int,name varchar(10),age int, chinese int, math int,english int);	
2. 查询所有表
	show tables;
3. 查看单个表
	show create table t1;
	desc t1;
4. 删除表
	drop table t1;
5. 修改表名称
	rename table t1 to t2;
6. 修改表引擎和字符集
	alter table t1 engine=myisam/innodb charset=gbk/utf8
7. 添加字段
	alter table t1 add age int;
8. 删除字段
	alter table t1 drop age;
9. 修改字段名和类型
	alter table t1 change age myAge int;
10. 修改类型和位置
	alter table t1 modify age int first/after xxx;

8. 与数据相关

1. 插入数据
	insert into t1 (a,b,c) values(1,2,3),(1,2,3);
2. 查询数据
	select * from t1;
	select name,age from t1;
3. 修改数据
	update t1 set age=20 where id=1;
4. 删除数据
	delete from t1 where id=1;

牛刀小试1

1. 创建英雄表hero  字段(id name type sal)
	create table hero(id int,name varchar(10),type varchar(10),sal int);
2. 查看所有表
	show tables;
3. 查看指定表详情 和 表的字段信息
	-格式:show create table 表名;
	-格式:desc 表名;
4. 创建表指定引擎和字符集
	create table t1(id int,name varchar(10)) engine=myisam charset=gbk;
5. 删除表
	drop table t1;

牛刀小试2

1. 创建员工表(emp) ,字段有员工编号(empno),员工姓名(ename),员工工资(sal)
	create table emp(empno int, ename varchar(10),sal int);
2. 创建数据库newdb 在newdb中 创建 商品表(item) 字段有商品标题(title),价格(price),库存(num)
	create database newdb;
	use newdb;
	create table item(title varchar(10),price int,num int);
3. 查看员工表详情和字段信息
	use db1;
	show create table emp;
	desc emp;
4. 删除商品表
	use newdb;
	drop table item;

表的引擎

  1. innodb:支持数据库的高级操作,包括:事务 外键等
  2. myisam:仅支持数据的增删改查操作
drop engines;

表的修改

use db1;
create table person(id int,name varchar(10));
1. 修改表的名称
	-格式: rename table 原名 to 新名;
	rename table person to t_person;
2. 修改表的引擎和字符集
	-格式: alter table 表名 engine=myisam charset=gbk;
	alter table t_person engine=myisam charset=gbk;
	show create table t_person;
3. 添加表的字段
	-最后面格式: alter table 表名 add 字段名 字段类型;
		alter table t_person add age int;
		desc t_person;
	-最前面格式: alter table 表名 add 字段名 字段类型 first;
		alter table t_person add chinese int first;
	-某个字段的后面格式: alter table 表名 add 字段名 字段类型 after 字段名;	
		alter table t_person add math int after id;
4. 删除字段
	-格式:alter table 表名 drop 字段名;
		alter table t_person drop chinese;
5. 修改字段名称和类型
	-格式:alter table 表名 change 原字段名 新字段名 字段类型
	alter table t_person change age myage int;
6. 修改字段的类型和位置
	-格式:alter table 表名 modify 字段名 字段类型 first/after xxx;
	alter table t_person modify myage int after id;

牛刀小试3

1. 创建t_hero表 字段 id name 引擎myisam
	create table t_hero(id int,name varchar(10)) engine=myisam;
2. 修改表名为t_h
	rename table t_hero to t_h;
3. 修改表的引擎为innodb
	alter table t_h engine=innodb;
4. 添加age字段在id的后面
	alter table t_h add age int after id;
5. 删除id字段
	alter table t_h drop id;

数据相关的SQL

create table t_stu(id int,name varchar(10),age int);
1. 插入数据
	-全表插入:要求插入的数据的数量和顺序要和表字段的数量顺序一致,格式:insert into 表名 values(值1,值2,值3....);
		insert into t_stu values(1,'zhangsan',23);
	-指定字段插入格式:insert into 表名 (字段1,字段2...) values(值1,值2...);
		insert into t_stu (id,name) values(2,'lisi');
	-批量插入:
		insert into t_stu values(3,'悟空',23),(4,'八戒',20),(5,'沙僧',18);
		insert into t_stu (id,name) values(6,'刘备'),(7,'关羽'),(8,'貂蝉');
2. 查询数据
	select * from t_stu;
	select name,age from t_stu;

3. 删除数据
	delete from t_stu where name='八戒';
	delete from t_stu where age is null;
4. 修改数据
	update t_stu set name='张三' where id=1;
	update t_stu set name='卷帘大将',age=200 where id=5;

windows电脑出现命令行中无法插入中文数据的解决方案

在命令行中先登录mysql 然后执行 set names gbk; 通知mysql数据库服务器,客户端(命令行)的编码格式为gbk

总结

数据库相关
create database db1 character set utf8;
show databases;
show create database db1;
drop database db1;
use db1;
表相关
create table t1 (id int,name varchar(10));
show tables;
show create table t1;
desc t1;
drop table t1;
rename table t1 to t2;
alter table t1 engine=myisam charset=gbk;
alter table t1 add age int first/after xxx;
alter table t1 drop age;
alter table t1 change age myage int;
alter table t1 modify myage int first/after xxx;
数据相关
insert into t1 (id,name,age) values(a,b,c),(a2,b2,c2);
select id,name from t1;
update t1 set a=1,b=2 where id=1;
delete from t1 where id=1;
ange age myage int;
alter table t1 modify myage int first/after xxx;