数据库总结(二)
2018-05-24 本文已影响11人
风的低语
查看所有的数据库 show databases
创建数据库 create database [if not exists] studb
切换数据库 use studb
展示所有的表格 show tables
创建表 create table if not exists stuTable (id int,name varchar(20),gender varchar(2))ENGINE='innoDB' default charset='utf8'
插入 insert into stuTable (id,name,gender) values (1,'yangshaofeng','M');
查找 select * from stuTable
更新 update stuTable set name = 'ziji' where id = 2
删除记录 delete from stuTable where id = 1;
查看创建表的语句 show create table stuTable;
表重命名 rename table 旧名字 to 新名字
删除表 drop table newStuTable
删除数据库 drop database studb
alter 增加字段 删除一个字段 给字段扩充长度
修改字段或者表的编码 修改字段的名字
修改字段的类型和名字
alter table [表名字] change [旧字段] [新字段] [新字段的类型] [新字段的约束]
新增字段
alter table [表名字] add([字段] [类型] [约束],[字段] [类型] [约束])
删除字段
alter table [表名字] drop 字段
// 查询
select * from stuTable; // 查找所有
select * from stuTable where id = 4 // 根据子句查找
select * from stuTable where name = 'xiaohan' and phone = '120 // &&
select name,phone from stuTable where phone = '119' or phone = '120' // ||
// 模糊查找
select count(name) from stuTable where name like '%xiao%' // like % 模糊查找
// 排序
select score from scoreTable order by score // 升序
select * from scoreTable order by score desc // 降序
select score from scoreTable where score > 70 order by score limit 0,1
// limit begin,pageCount
// 函数
count() 统计个数
max() 最大值
min() 最小值
avg() 平均值
// 分组
group by
having 分组之后过滤
where 分组之前,会影响分组结果
// 去除重复
distinct
// 删除外键
alter table drop foreign key '外键名'
// 添加外键
alter table [表名字]
add constraint id_fk foreign key (id) references student (id)
alter table 表名
add constraint 约束名 约束类型具体的约束说明
// 关联查询
select * from stuTable st, stuScore ss where st.id = ss.stuid;
//笛卡尔积
a表和b表的每一条数据匹配
b表和a表的每一条数据匹配
// 内关联 (inner join)
// 复合两个表的,出结果
// 左关联 (left join)
// 左表把所有符合条件的列出来,右表中有就有,没有为null
// 右关联 (right join)
右表把所有符合条件的列出来,左表中有就有,没有为null
解决数据库冗余问题
三大范式
1.第一范式是最基本的范式。如果数据库表中的所有字段值都是不可分解的原子值,就说明该数据库表满足了第一范式。
2.第二范式需要确保数据库表中的每一列都和主键相关,而不能只与主键的某一部分相关(主要针对联合主键而言)。也就是说在一个数据库表中,一个表中只能保存一种数据,不可以把多种数据保存在同一张数据库表中 (订单)
3. 第三范式需要确保数据表中的每一列数据都和主键直接相关,而不能间接相关。
用户管理
// 创建用户
CREATE user 'zhangsan'@'localhost' [Identified by '密码']
// 删除用户
drop user [用户名]
// 给权限
GRANT SELECT ON myschool.view_student TO `student`@`localhost`;
// 修改密码
mysqladmin –u root –p password "新密码"
// 修改其他用户密码
SET PASSWORD FOR `teacher`@`localhost`= PASSWORD("8888");
// 删除权限
DROP USER `student`@`localhost`;
// 查看用户权限
show grants for zx_root
// 回收权限
revoke select on dmc_db.* from 用户(如果没有权限会报错)
id
1 2 4 8 3 10 2 3 4 20 3 4 30 7 9 2 8
// 二分查找
1 2 2 2 3 3 3 4 4 4 7 8 8 9 10 20 30
hash索引
b-tree 结构
innodb b+tree
索引会加快查找速度
更新,删除,添加索引表的也需要变化
索引建立在经常是用where的字段上
查找: 添加
9 1
索引
// 修改表结构
alter Table [表名] add index(english)
// 创建索引
CREATE INDEX indexName ON mytable(username(length));
// 直接指定索引
CREATE TABLE mytable(
ID INT NOT NULL,
username VARCHAR(16) NOT NULL,
INDEX [indexName] (username(length)) )
// 显示索引
SHOW INDEX FROM table_name;
// 删除索引
ALTER TABLE table_name DROP INDEX index_name
事务
SAVEPOINT identifier 一个事务可以有多个标记点
ROLLBACK TO identifier;把事务回滚到标记点;
SET AUTOCOMMIT = 0; // 默认值, 自动提交
SET AUTOCOMMIT = 1; // 手动提交
与其给事务定义,不如说一说事务的特性。众所周知,事务需要满足ACID四个特性。
1. A(atomicity) 原子性。一个事务的执行被视为一个不可分割的最小单元。事务里面的操作,要么全部成功执行,要么全部失败回滚,不可以只执行其中的一部分。
2. C(consistency) 一致性。一个事务的执行不应该破坏数据库的完整性约束。如果上述例子中第2个操作执行后系统崩溃,保证A和B的金钱总计是不会变的。
3. I(isolation) 隔离性。通常来说,事务之间的行为不应该互相影响。然而实际情况中,事务相互影响的程度受到隔离级别的影响。文章后面会详述。
4. D(durability) 持久性。事务提交之后,需要将提交的事务持久化到磁盘。即使系统崩溃,提交的数据也不应该丢失。
备份
1. 导出
SELECT * FROM [表名] INTO OUTFILE '/tmp/tutorials.txt'
导出mysql格式
./mysqldump -u root -p [数据库名] [表名] > /Users/yangshaofeng/Desktop/dump.txt
./mysqldump -u root -p [数据库名] > database_dump.txt
恢复数据库
source 文件
begin
updata money = 200 where name = 'da'
updata money = 800 where name = 'xbj'
commit
默认是自动提交
jdbc
c3p0
dbutils