目录
数据库分类:
数据库大体可以分为 关系型数据库 和 非关系型数据库
关系型数据库(RDBMS):简单来说,关系模型指的就是二维表格模型,而一个关系型数据库 就是由二维表及其之间的联系所组成的一个数据组织。
常用的关系型数据库如:
1. Oracle:甲骨文产品,适合大型项目,适用于做复杂的业务逻辑,如ERP、OA等企业信息系统。收费。
2. MySQL:属于甲骨文,不适合做复杂的业务。开源免费。
3. SQL Server:微软的产品,安装部署在windows server上,适用于中大型项目。收费。
4. MariaDB: 基于 MySQL 的一个开源数据库产品。
非关系型数据库: (了解)不规定基于SQL实现。现在更多是指NoSQL数据库,如:
1. 基于键值对(Key-Value):如 memcached、redis 2. 基于文档型:如 mongodb 3. 基于列族:如 hbase 4. 基于图型:如 neo4j
数据库的操作:
运行mysql客户端, 连接服务器:
mysql -uroot -p (-u指定用户名, -p需要输入密码)
(运行这句之后才是在数据库中打SQL语句,这步是linux的指令)
SQL语句注意事项:
sql语句不区分大小写、不区分单引号和双引号;
每一条sql语句默认都是以英文的分号 ' ; ' 作为结尾;
库和表以及字段的名称不能是纯数字;
sql语句中库名称、表名称,尽量避免使用关键字,比如create, desc....
库的操作:
创建库: create database db_94; create database if not exists db_94; (不存在则创建)
查看库: show databases; 查看mysql所管理的所有的库. select database(); 查看当前所操作的库
选择所操作的库: use db_94;
删除库: drop database 94_db;
表的操作:
数据类型:
整数类型: bit(n) n比特位(1~64,默认为1). tinyint 单字节. smallint 2字节. int 4字节. bigint 8字节
浮点类型: float(M,D) double(M,D) decimal(M,D) 双精度 numeric(M,D) M表示浮点数中所有数字的个数, D表示浮点数中小数的个数. 例如: 88.85 float(4,2)
字符串类型: char(L) varchar(L)可变长字符串 text mediumtext blob L表示字符个数
日期类型: timestamp datetime
创建表:
创建表名为stu_tb的表, 表中列名分别为num, name, brith, en 分别对应类型.
create table stu_tb(num int, name varchar(32), birth datetime, en decimal(4, 2));
查看表:
show tables; 查看当前库中所有的表. desc stu_tb; 查看指定表的结构.
修改表结构.
alter table stu_tb modify num int primary key; 修改num列为主键
alter table stu_tb modify num char(10); 修改num字段类型为char(10)
alter table stu_tb add age int; 增加age列类型为int
alter table stu_tb drop age; 删除age列
删除表:
drop table stu_tb;
增删改查:
新增 inert into:
新增插入一条或多条数据:
insert into stu_ tb values(1, '张三' , '2003-03-04 10:28:00', 77.75);
insert into stu_tb values(2, '李四' , '2002-03-0410:28:00' , 48.75) , (3, '王五', '2004-05-08 16:28:00' , 56.5);
insert into stu_tb (sn,name, ch, math, en) values(4,"赵六",78,99,32); 某些字段不主动给出时可以以空null或默认值插入
删除 delete:
删除数据库表中的一条或多条数据, 通过where关键字设置过滤条件
delete from stu_tb where num=2; 删除学号为2的整行数据. delete from stu_tb; 删库跑路
修改 updata:
修改数据库表中的一条或多条数据,通过where关键字设置过滤条件
update stu_tb set birth="2002-03-04 10:28:00" where name='张三';
update stu_tb set birth="2001-08-09 11:33:00"; 针对所有数据制定字段的修改
查询 select:
select * from stu_tb; 表中所有数据.
select num, name from stu_tb; 查询指定字段数据.
select name, ch, math, en, ch+math+en from stu_tb; 查询字段也可以是表达式
select name,ch, math, en, ch+math+en as total from stu_tb; 给字段取别名(语数英三个字段总分)
select * from stu_tb where name='李四; 条件查询, where关键字设置过滤条件
=========================================================================
select * from stu_tb order by ch dese; 排序查询, 默认以asc升序进行排序
select* from stu_tb order by ch desc, math desc; 以多字段进行排序, 第一个字段相同则以第二字段进行排序
=========================================================================
select distinct ch from stu_tb; 对某个字段进行去重查询,查询结果是该字段所有的不重复的值.并且只能查询要去重的字段,不能有其他字段,
select distinct ch, name from stu _tb; 以某个组合多字段进行去重, 两个字段都相同才表示重复
=========================================================================
select * from stu_ tb order by ch limit 2 offset 0; 分页查询, 从第0条开始获取2条数据(某字段的topk);
select * from stu_tb limit 3; 获取前三条数据,通常与排序搭配使用, n表示一页有多少条, s表示从多少偏移量开始查询.
=========================================================================
关系运算符:
<、>、<=、>=、 =、!=、<> 不等于、<=>等于.
select * from stu_tb where math>60;
字段值是否为NULL的判断不能用' = ' :
select * from stu_tb where birth<=>NULL; select * from stu_tb where birth is NULL;
(select * from stu_tb where birth is NULL;)
select * from stu_tb where ch between 78 and 80; 开区间(包含边界)的查询
select * from stu_tb where name in (' 张三' , '李四' ); 查询指定字段值为in中包含有的.
=========================================================================
逻辑运算符:
逻辑与: 双目运算符,传入两个条件,两个条件同为真则结果为真 AND &&
逻辑或: 双目运算符,传入两个条件,任意条件为真,则结果为真 OR ll
逻辑非: 单目运算符,传入一个条件,条件为真,则结果为假,反之亦然 NOT
select * from stu_tb where math> =60 and math<80;
select * from stu_tb where math<60 or math>90;
select * from stu_tb where not math<60;
=========================================================================
查询进阶:
聚合函数: mysql数据库中内置的一些数据统计函数
select count(*) from stu_tb where en<60; 统计英语不及格的人数
select sum(ch) from stu_tb; 求班上的语文成绩综合
select avg(ch) from stu_tb; 求班上的语文平均成绩
select max(ch) from stu_tb; 求班上最高的语文成绩
select min(ch) from stu_tb; 求班上最低的语文成绩
=========================================================================
分组查询: 以指定字段为依据,对表中数据进行分组统计。
示例: 雇员表(emp)中包含有: 员工工号(id), 员工姓名(name),员工岗位(role),员工薪资(salary).
查询1: 查看不同岗位, 每个岗位的最大薪资是多少, 最小薪资是多少, 平均薪资, 员工姓名.
select role, max(salary), min(salary), avg(salary) from emp group by role;
查询2: 我现在要找工作,找公司中平均薪资大于10000的岗位
select role, avg(salary) from emp group by role having avg(salary)>10000;
查询结果中只能有分组依据字段以及聚合函数. 且过滤条件不能使用where子句,而是用having.
=========================================================================
键值约束: 对表中指定字段的描述以及约束
主键: primary key, 约束是表中指定字段,值不能为NULL,且数据不能产生重复;
主键一张表中只能有一个,但是存在组合主键, 以多个字段当做整体作为主键。
create table p_table(id int primary key,name varchar(32), age int);
create table p_table(id int,name varchar(32), age int,primary key p_k(id, name));
唯一键: unique key, 约束表中的指定字段,值不能重复
create table uni_table(id int primary key, name varchar(32) unique key, age int unique,sex int, unique key u_k(sex));
NULL值并不触发唯一约束。
非空约束: NOT NULL, 约束表中指定字段值不能为NULL
create table nn_table(id int primary key,name varchar(32) not null );
当表中没有指定主键,但指定了非空且唯一字段,则这个约束会升级为主键约束。
外键约束: foreign kesy ... references....限制当前指定字段的数据,必须在另一张表中指定字段的数据中存在
create table class_table(id int primary key,info varchar(32));
create table stu_table(id int primary key, name varchar(32), class_id int, foreign key (class_id) references class_table(id));
使用外键可以让表中的数据更加严谨,比如:学生信息中的班级id, 班级表中必须存在这个班级.
默认值: default, 当没有主动插入某个字段数据的时候,则自动以默认值进行新增
create table def_table(id int primary key,name varchar(32), sex varchar(1) default '男');
自增属性: auto_increment, 只能针对整数的主键字段进行设置,当不主动插入该字段数据的时候默认从1开始自增, 新增的数据为新增前该字段值最大值+1, 中间若有值被删除不会填补, 即删除不影响其他值.
create table auto_table(id int primary key auto_increment, name varchar(32));