一、数据库
1、持久化:把数据保存到掉电式存储设备中以供之后使用、(内存 ---> 数据库中(磁盘、xml数据文件))
2、定义:Database
存储数据的“仓库”,保存一系列有组织的数据。
3、DBMS : 数据库管理系统 : 用来管理数据库的大型软件、建立和使用、维护数据库。
4、MySQL 44.3%
SQL Server 30.8%
5、面向对象: Sybase 、DB2 、Oracle 、MySQL 、Access 、Visual Foxpro
符合SQL标准,性能差 : MS SQL Server 、 Imformix 、PostgreSQL
6、SQL : 结构化查询语言 NOSQL : 非结构化查询语言
7、MySQL : 是一种开放源代码的关系型数据库管理系统、快速检索。
8、MySQL 逻辑架构:
1、连接层:最上层是连接一些 客户端和链接服务、本地sock通信和大多数基于客户端、服务端工具实
现的类似TCP\IP的通信,只要完成类似于连接处理、授权认证、相关的安全方案。在该层引入线程
池概念。
2、服务层:完成部分的核心服务功能、SQL接口
3、引擎层: 存储引擎层、负责MySQL中数据的存储和提取、 服务器通过API 于 存储引擎进行通信、
4、存储层:数据存储层、将数据存储在运行设备的文件系统上、并完成于存储引擎的交互。
9、代码方法:
1、DDL 数据定义语句 、create 、drop、alter
2、DML 数据操作语句、insert 、delete 、update、select
3、DCL数据控制语句、grant、revoke
(1)库方法
1、打开mysql 命令:
cmd -- mysql -u root -P 3306 -p
Enter password: root;
2、打开、关闭 mysql 服务器
net start\stop mysql80 // start 打开 stop 关闭
3、创建数据库
create databases 库名; //创建一个库默认类型
create databases 库名 charset=utf8;
4、查询当前版本号
select version();
5、查询当前时间
select new();
6、注释
‘#’ 或者 /* *
7、查看所有数据库
show databases;
8、进入数据库
use 库名;
9、删除库
drop database 库名;
10、查询当前所在数据库
select database();
(2)表方法
1、创建表、 起别名 sl ??、 空格 ?? 起别名
create table 表名(字段名1 字段类型1,字段名2 字段类型2) // create table 表名(name char,age int)
2、查看当前数据库中所有表
show tables;
3、打开表
desc 表名;
4、表赋值
insert into 表名 values (“周瑞兴”,18); // 一起赋值
insert into 表名(name)values(“周瑞兴”); // 单个赋值
5、修改值
update 表名 set 字段名=修改的内容; // 修改值
update 表名 set 字段名=修改的内容 where name = "zrx"; // 修改name = zrx 的字段名的值
6、删除字段
delete from 表名 where 字段名=xxx; // 删除 字段名 = “??”
delete from 表名 where 字段名 > 1; // 删除字段名 > 1 的字段
7、清空数据
truncate 表名;// 清空偏移量
delete table 表名; // 删除 不清空偏移量
8、查看表
select * from 表名; // 查看表所有字段
select * from 表名 where name like "周%"; // 查看name字段 所有周开头的
select * from 表名 where age > 18 and name =="zrx"; //查看age > 18 同时 name = zrx 的 字段
select 字段1,字段2 from 表名; // 查看 字段1、字段2
9、去重查找
select distinct name from 表名;
10、排序查找
select * from 表名 order by age; //升序
select * from 表名 order by age desc; //降序
11、限制查找
select * from 表名 limit 3://只查看前3条
12、查询出现次数
select count(字段1) from 表名 where 字段=“z”; // 查看字段 = “z” 出现的次数
max()最大值、min()最小值、avg()平均值、sum()求和、count()求频数。
13、查看表的具体信息
show create table 表名;
14、增加表成员
alter table 表名 add 列名 字段类型(n); //默认最后一列
alter table 表名 add 列名 字段类型(n)after 列名; //某一列后面
alter table 表名 add 列名 字段类型(n)first; //第一列
17、修改表成员类型、约束条件
alter table 表名 modify 列名 字段类型 ; // 修改列名元素 默认最后一列、after 、first
15、重命名表
alter table 表名 rename 新表名;
rename table 表名 to 新表名;
16、删除列
alter table 表名 drop 列名;
17、修改列名
alter table 表名 change 列名 新列名 数据类型 ;
(3)约束
1、数据完整性: 数据的精确性和可靠性
(1)、实体完整性: 同一个表中、不能存在两条完全相同无法区分的记录
(2)、域完整性: 年龄范围 0 - 120 、性别范围 男\女
(3)、引用完整性:具体的部门 、再部门表中要能找到
(4)、用户自定义完整性:用户名唯一、密码不能为空
2、特点:
(1)、键约束:
1、主键约束:pk(primary key) 唯一键 uk(umiqe key) 非空(not null)
2、唯一约束 + 非空约束 : 不能有重复、不能出现空值;如果是多列、组合的值不能重复。
3、每个表只能有一个主键约束
4、mysql 主键名 总是 primary
5、创建主键、自动创建主键索引、删除时 自动删除
(2)、not null 约束;非空约束
(3)、check约束:检查约束
(4)、default约束:缺省约束
3、查看某个表的约束和索引
select * from information_schema.table_constraints where table_name = "表名";
4、创建主键
create table 表名(id int primary key auto_increment,password int not null);// 直接声明 不能复合
// 给id 定义主键 primary key、 auto_increment 自动向下补齐、设置password 不能为空
create table 表名(id int ,password int not null,primary key(id,password));// 复合主键
5、修改主键
alter table 表名 add primary key(列名); // 建表后添加主键
alter table 表名 modify 列名 字段类型 primary key; // 建表后修改主键
6、创建条件限制
create table 表名(password int default 1); // 默认1
default current_timestamp // 默认当前时间
default current_timestamp on update current_timestamp // 默认更新修改后的时间
7、添加 检查约束
create table 表名(age int check (age > 0)); // age 值必须 大于等于 0
create table 表名(age int check sex in( 男:女));
(4)、外键
1、一张表 关联另一个表格中的值; // 一对一
foreign key (本列表)references 健表(健表列表);
// 本列表name 关联 健表 列表
on delete cascade // 跟随删除
on update cascade // 跟随改变
2、单表查询
select distinct 字段1,字段2 from 表名
where 条件
// where username= ”zrx“; 只查询 name = zrx 的
group by 组名
//分组 select ?? from 表名 froup by 列名 ; 按列名分把??分组
//分组 select ? ? ,sum(??)from 表名 froup by 列名 ;每个列名的??值的和
//分组 select ?? from 表名 where id < 2 froup by 列名 ; id < 2的分组
//分组 select ?? from 表名 between 1 and 3 froup by 列名 ; 1 到 3 之间查询
having sum(salary) > 500 筛选
// 删选 salary 的和 大于 500 的值
order by 排序
// order by sum(salary)asc ;默认升序
// order by sum(salary ) desc; 降序
limit 2; 限制条数
// 之查看 2条
(4)算数运算符
1、 + - * / %
2、= >< >= <= !=
3、like ”周%“ like ”周_“ // 模糊查询 _ 占位一个字符