2020-10-30

数据库(Datebase ,DB)

数据的定义:描述事物的符号记录

数据的含义称为数据的语义:数据与语义是不可分的

什么是数据库:长期存储在计算机内 有组织的 可共享的大量的数据集合

数据库的基本特征:
数据按一定的数据模型组织,描述和储存
可为各种用户共享 赘余度较小 易扩展
数据独立性较高

数据库管理系统(DateBase Management System DBMS)用途:
科学地组织和存储数据 高效获取和维护数据

1064 语法错误
1054 占位符错误

select vesion(); 查看数据库当前版本
DDL(操作数据库,表)
DQL(查询表中数据)
DML(增删改 表中 数据)
操作数据库
Create
create database 库名;
create database if not exists Sin;//先判断Sin是否存在 如果不存在创建Sin
create database Sin character set utf8;//设置创建的库字符格式为 utf-8

Retrieve
show create database Sin;//查看Sin的字符集格式
show databases; 查看当前所有数据库

Updata
alter database Sin character set gbk;

Delete
alter database Sin;
alter database if exists Sin;

操作表
Create
create table Sin(
id int,
name varchar(3),
age int
);

Rertrieve
show tables; //查询数据库中的所有表的名称
desc sin;//查询表结构

Updata
alter table stu rename to stu2;
alter table stu character set utf8;
alter table stu add id int;
alter table stu change id no int;
alter table stu modify id double;
alter table stu drop no;

Delete
drop table stu;
drop table if exists stu;

增删改表中数据
Create
insert into stu(id,name,age) value(1,’Sin’ ,18); //出数字类型外其余都使用引号 引起来 如无属性名 则需把每个属性都赋值

Delete
delete from stu [where];
truncate table stu;//删除表后 ,然后创建一张新的无数据的表

Updata
updata stu set id=1,name=“Sin”,age=18[where id = 1];//

查询表
select
id
from
stu3
where
name=‘Cos’;

select
字段列表
from
列表名称
where
条件列表
group by
分组字段
having
分组之后的条件
order by
排序
limit
分页限定

select name,address
from stu;

select distinct address //distinct 去重
from stu;
select * from stu where age in (22,18,26);//stu 中年龄 22,18,26 的数据
select * from stu where age between 20 and 30;//stu中年龄在 20-30 岁之间

_ 单个字符 %任意多个字符
select * from stu where name like ‘马%’;

查询语句

排序查询 select * from emp order by salary desc, id asc;//将emp表中的数据按照 salary 降序,id 升序
聚合查询 select count(salary) from emp; max min sun avg
分组查询 select sex,count(id),avg(math) from emp where math>40 group by sex having count(id)>2;
分页查询 select from limit 0,3;
开始的索引 = (当前页面-1)
每页显示的内容

约束
主键约束 : primary key drop primary key auto increment
非空约束 : not key not null alter table stu modify id int not null;
唯一约束 : unique drop index alter table stu modify index id ;
外键约束 : foreign key constraint 外键名称 foreign key(外键名称) references 主表名称(主列表名称)
on update cascade on delete cascade

多表查询:
内链接查询
select emp.name,depa.name from emp,depa where emp.dep_id = depa.id;
隐式内连接:
显式内连接: select 字段列表 from 表名1 inner join 表名2 on 条件
从那些表中查询数据 条件是什么
外连接查询
左外连接:select 字段列表 from 表1 left join 表2 on 条件;
左表的所有数据以及交集部分
子查询
查询中 嵌套 查询
emp.’salary’ = (select max(salary) from emp);
单行单列
普通工资小于平均工资的人
select * from emp where salary<(select avg(salary) from emp));
多行单列
查询 财务部 和 市场部 所有员工信息
select * from emp where dept_id in(select id from emp where name=‘财务部’ or name=‘市场部’));
多行多列
查询员工入职日期是 2011-11-11日之后的员工信息和部门信息
select * from dept t1,(select * from emp where emp.join_date > ‘2011-11-11’ ) t2 where t1.id=t2.dept_id;
1.带in关键字的子查询(一个查询语句的条件可能落在另一个select语句的查询结果中)
select * from t_book where bookType in(select id from t_bookType);
select * from t_book where bookType not in(select id from t_bookType);

2.带比较运算符的子查询(子查询可以使用比较运算符)
select * from t_book where price>=(select price from t_priceLevel where priceLevel=1);

3.带exists关键字的子查询(加入子查询查询到记录,则进行外层查询,否则,不执行外层查询)
select * from t_book where exists(select * from t_booktype);
select * from t_book where not exists(select * from t_booktype);

4.带any关键字的子查询(any关键字表示满足其中任一条件)
select * from t_book where price>= any(select price from t_priceLevel);

5.带all关键字的子查询(all关键字表示满足所有条件)
select * from t_book where price>= all(select price from t_priceLevel);

事务
如果一个包含多个步骤的业务操作,被事务操作,那么这些操作要么同时成功 要么同时失败
开启事务 start transaction
回滚 rollback
提交 commit
四大特征
原子性 持久性 隔离性 一致性

事务的隔离级别
概念:多个事务之间隔离,相互独立。
存在问题:
脏读:一个事务,读取到另一个事务没有提交的数据
不可重复读(虚读):在同一个事务中,两次读取到的数据不一样
幻读:一个事务操作数据表中所有记录,另一个事务添加了一条数据,则第一个事务查询不到自己的修改
隔离级别
read uncommitted :读未提交
read committed : 读已提交
repeatable read: 可重复读
serializable : 串行化

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值