一.什么是数据库

数据库(Database)是按照数据结构来组织、存储和管理数据的仓库。每个数据库都有一个或多个不同的 API
用于创建,访问,管理,搜索和复制所保存的数据。我们使用关系型数据库管理系(RDBMS)来存储和管理大数据量。关系型数据库是建立在关系模型基础上的数据库,借助于集合代数等数学概念和方法来处理数据库中的数据。

二.RDBMS

1.RDBMS即关系数据库管理系统的特点:
 		①数据以表格的形式出现 		
 		②每行为各种记录名称 		
 		③每列为记录名称所对应的数据域 		
 		④许多的行和列组成一张表单 	
 		⑤若干的表单组成database
	
2.RDBMS专业术语 		
	数据库: 数据库是一些关联表的集合。 		
	数据表: 表是数据的矩阵。在一个数据库中的表看起来像一个简单的电子表格。 		
	列: 一列(数据元素) 包含了相同类型的数据, 例如邮政编码的数据。 		
	行:一行(=元组,或记录)是一组相关的数据,例如一条用户订阅的数据。 		
	冗余:存储两倍数据,冗余降低了性能,但提高了数据的安全性。 		
	主键:主键是唯一的。一个数据表中只能包含一个主键。你可以使用主键来查询数据。 		
	外键:外键用于关联两个表。 		
	复合键:复合键(组合键)将多个列作为一个索引键,一般用于复合索引。 		
	索引:使用索引可快速访问数据库表中的特定信息。索引是对数据库表中一列或多列的值进行排序的一种结构。类似于书籍的目录。 		
	参照完整性: 参照的完整性要求关系中不允许引用不存在的实体。与实体完整性是关系模型必须满足的完整性约束条件,目的是保证数据的一致性。 		
	表头(header): 每一列的名称; 		
	列(col): 具有相同数据类型的数据的集合; 		
	行(row): 每一行用来描述某条记录的具体信息; 		
	值(value): 行的具体信息, 每个值必须与该列的数据类型相同; 		
	键(key): 键的值在当前列中具有唯一性。

三.mysql简介

MySQL是一个关系型数据库管理系统,由瑞典MySQL AB 公司开发,属于 Oracle 旗下产品。MySQL是最流行的关系型数据库管理系统之一,在 WEB 应用方面,MySQL是最好的 RDBMS (Relational Database Management System,关系数据库管理系统) 应用软件之一。
MySQL是一种关系型数据库管理系统,关系数据库将数据保存在不同的表中,而不是将所有数据放在一个大仓库内,这样就增加了速度并提高了灵活性。
MySQL所使用的 SQL 语言是用于访问数据库的最常用标准化语言。MySQL软件采用了双授权政策,分为社区版和商业版,由于其体积小、速度快、总体拥有成本低,尤其是开放源码这一特点,一般中小型网站的开发都选择
MySQL 作为网站数据库。

四.mysql的基本用法

1.创建表

create table 表名 (
列名 类型 [约束] ,
列名 类型 [约束] ,
列名 类型 [约束] ,

列名 类型 [约束]
)

eg:
	create table student(
		id  varchar(10) primary key ,
		name varchar(10) ,
		sex varchar(2),
		age int ,
	)
2.查询表的数据

select 字段列表 from 表名 [条件]

eg:
select * from student ;
3.插入数据

insert into 表名 [(字段名1,字段名2…)] values (字段值1,字段值2,…);

eg:
	insert into student values ('1001' ,'Amber','女',18);
	insert into student(id,name,sex,age) values ('1002' ,'yumiao','女',18);
4.修改数据

update 表名 set 字段名 = 新字段值 [条件]

eg:
update  student  set  age = 17  where name = 'yumiao';
5.删除数据

delete from 表名 [条件]

eg:
delete from student  where stu_name = 'Amber';
6.删除表

drop table 表名

drop table student ;
7.约束

auto_increment 自增长
primary key :主键约束(非空、唯一)
foreign key 外检约束 (本表字段) references 外表(外表字段)
not null 非空约束
unique 唯一约束
default 默认
enum 枚举(可以为null)

eg:
CREATE  TABLE student2(
	id  int    auto_increment primary key ,
	name varchar(10) not null ,
	sex  enum('男','女'),
	address varchar(100) unique ,
	age int ,
	foreign key (id) references student(id)
	date timestamp default current_timestamp 
);
8.别名(as 取别名 关键字可省略)
select id as stuId,name  as "stuName" from student2 ;
9.排序order by(升序asc 降序desc)
select  * from student2 order by  age asc ,id desc ;
10.模糊查询 like (% : 0-N个字符, _一个字符)
select * from student2 where name like '__l%';
11.去重distanct(重复的查询结果只显示一次)
select distinct sex from student2 ;
12.聚合函数(max最大值 min最小值 avg平均值 sum求和 count)
select  max(age)   from student2 ;	//student2表中年龄最大的
select  min(age)   from student2 ;	//student2表中年龄最小的
select  avg(age)   from student2 ;	//计算student2表的平均年龄
select  sum(age)  from student2 ;	//计算student2表的总年龄
select count(*) from student2 ;  //计算表中总数据条数
13.分组:group by(只能select分组的字段跟聚合函数。分组后面如果要用条件过滤,需要使用having,而不能使用where)
//计算student2表中年龄大于18的最大年龄、最小年龄、平均年龄、总年龄,并按性别进行分组
select max(age),min(age),avg(age),sum(age) from student2 group by sex having avg(age)>18;
14.内连接/左连接/右连接
select * from stutest ,student2 where stutest.id = ;--内连接
select * from stutest   inner join student2  on stutest.id = ; --内连接
select * from stutest   left join student2  on stutest.id = ; --左连接
select * from stutest   right join student2  on stutest.id = ; --右连接
15.分页

参数可以为一个或者两个,参数必须是整数常量。
select 列名 from 表名 limit 显示前几条数据
select 列名 from 表名 limit 从第几条数据开始(从0开始计算),查询的总条数

eg:
select * from student2 limit 5	//查询前5条记录
select  *  from student2 limit 4 ,2 ; //从下标为4的记录开始查询,总共查询2条数据
16.增加列

alter table 表名 add (列名 列类型 [约束] , 列名 列类型 [约束] , … );
alter table 表名 add column 列名 列类型 [约束];
alter table 表名 add column 列名 列类型 [约束] [FIRST]/[AFTER 列名];

eg:
// 增加一列到首列/到某列的后面
alter table student add (weight int);
17.修改列

alter table表名 change 旧列名 新列名 类型

eg:
  alter table student change  stu_weight  stu_weight1  DECIMAL
18.修改表名

alter table 表名 renameto 新表名

eg:
alter table student rename to student1
19.删除列

alter table 表名 drop column 列名

eg:
alter table student1 drop column weight
20.删除表

drop table 表名

eg:
drop table student