关于java:数据库

2次阅读

共计 18739 个字符,预计需要花费 47 分钟才能阅读完成。

一、数据库进入退出:

1.mysql -uroot -p 进入数据库 + 回车

2.exit; 退出数据库

二、数据库相干:

1.show databases; 查询数据库

2.create database db1; 创立一个数据库

3.show create database 数据库名; 查看数据库详情

4.create database 数据库名 character set gbk/utf8; 创立数据库指定字符集

5.drop database 数据库名; 删除数据库 留神!

6.use db1; 应用数据库 惟一没有 database 的语句 m 三表单相干: 操作表之前必须要有一个应用的数据库 1.create table 表名 (字段 1 名字 字段 1 类型 字段 2 名字 字段 2 类型); 创立表 create table person(name varchar(10),age int); (内代表字节数) 不给字节默认为 noll

show

2.show tables; 查问所有表

3.show create table 表名; 查看表详情

4.1.innodb:(默认)反对数据库的高级操作,如事务,外键等。2.mysiam: 只反对数据库的根底增删改查操作 引擎

5.create table 表名(字段 1 名字 字段 1 类型 字段 2 名字 字段 2 类型)engine=myisam charset=gbk/utf-8; create table db1 (name varchar(10),age int)engine=myisam charset=gbk; 创立表指定引擎和字符集

6.desc 表单名; 查看表字段信息 desc db1;

7.drop table 表名; 删除表 drop table db1;

8.rename table 原名 to 新名 批改表名; rename table student to stu;

9.alter table 表名 engine=myisam charset=gbk/utf-8 批改引擎和字符集 alter table stu engine=myisam charset=gbk/utf-8

10.1. 最初面格局:alter table 表名 add 字段名 字段类型; 增加表字段 2. 最后面格局:alter table 表名 add 字段名 类型 first; 3. 某字段名的前面格局:alter table 表名 add 新字段名 类型 after 旧字段名;

11.alter table 表名 drop 字段名; 删除表字段

12.alter table 表名 change 原名 新名 新类型; 批改字段名和类型

13.alter table 表名 modify 字段名 新类型 first/after xxx 批改字段类型和地位

三、插入数据:

1. 全表插入格局:insert into 表名 value(值 1,值 2); insert into db1 value(‘Tom’,30);

2. 指定字段格局:insert into 表名(字段名 1, 字段 2)value(值 1, 值 2); insert into db1(name)value(‘jerry’);

3. 批量插入: insert into db1 value(‘aa’,10),(‘bb’,20),(‘cc’,30); insert into user(name)value(‘xxx’),(‘yyy’),(‘ccc’);

4. 插入中文数据:insert into db1 value(‘ 刘德华 ’,50); 如果报错执行以下命令: set names gbk;

5. 查问数据:select 字段信息 from 表名 where 条件; select name/age/所有 from db1 条件; select from db1 where age<30; 查问年龄小与 30 的

6. 批改数据:update 表名 set 字段名 = 值 where 条件; update db1 set age=18 where name=’ 刘德华 ’;

7. 删除数据:delete from 表名 where 条件; delete from db1 where name=’xxx’;

四、主键束缚:主键束缚:示意数据唯一性的字段为主键 束缚:创立表时给表字段增加限度条件 主键束缚:限度值惟一且非空 格局:create table t1 (id int primary key,name varchar(10)); insert into t1 values(1,’AAA’); insert into t1 values(1,’BBB’);// 不能反复 insert into t1 values(null,’CCC’);// 不能为 null

五主键束缚 + 自增:从历史最大值根底上 +1 自增数值只加不减 格局:create table t2(id int primary key auto_increment,name varchar(10)); insert into t2 values(null,’aaa’);//1 insert into t2 values(2,’bbb’);//2 insert into t2 values(10,’bbb’);//10 insert into t2 values(null,’ddd’);//11 delete from t2 where id>=10; insert into t2 values(null,’eee’);//12

六、正文 comment 格局:create table t3(id int primary key auto_increment comment ‘ 这是 id’,name varchar(10) comment’ 名字 ’);

七、` 的作用用于润饰表名和字段名 能够省略

八、冗余; 因为表设计的不够正当导致呈现的大量反复数据成为冗余。通过正当拆分表的模式解决问题。练习 1

创立表:create table emp(id int primary key auto_increment,name varchar(10),age int,gender varchar(5),salary int,dept_id int);

create table dept(id int primary key auto_increment,name varchar(10),parent_id int);

插入数据:

insert into dept values(null,’ 团体总部 ’,null),(null,’ 教学研发部 ’,1),(null,’java 教研部 ’,2),(null,’ 市场部 ’,1),(null,’ 市场 A 部 ’,4);

insert into emp values(null,’ 苍老师 ’,18,’ 男 ’,5000,3),(null ,’ 小明 ’,19,’ 女 ’,20000,5);

练习 2

创立表:

create table iten(id int primary key auto_increment,name varchar(10),price int,num int,category_id int9);

create table category(id int primary key auto_increment,name varchar(10),parent_id int);

插入数据:insert into category(name,parent_id) valuse(‘ 家电 ’,null),(‘ 电视机 ’,1),(‘ 办公用品 ’,null),(‘ 打印机 ’,3);

insert into item values(null,’ 小米电视 ’,1888,200,2),(null,’ 惠普打印机 ’,1500,100,4);

九、事务:

事务是数据库中执行同一业务多条 SQL 语句的工作单元,事务能够保障多条 SQL 语句全副执行胜利或全副执行失败; 和事务相干的 SQL:
1. 开启事务: begin;
2. 提交: commit; 将内存中的改变一次性提交到磁盘中。
3. 回滚: rollback; 将内存中的改变革除,回滚到正确的点。
验证转账:create table user(id int primary key auto_increment,name varchar(10),money int,status varchar(5));

insert into user values(null,’ 超人 ’,50,’ 解冻 ’),(null,’ 蝙蝠侠 ’,20000,’ 失常 ’);

转账 SQL:update user set money=money-2000 where id=2 and status=’ 失常 ’;

update user set money=money+2000 where id=1 and status=’ 失常 ’;

有事务爱护的状况下 回滚流程:
1. 开启事务:begin;
2. 蝙蝠侠 -2000:update user set money=money-2000 where id=2 and status=’ 失常 ’;
3. 此时以后终端查问数据曾经产生扭转(因为查问到的是内存中的数据), 开启另一个终端查问会发现数据并未扭转
4. 超人 +2000 : update user set money=money+2000 where id=1 and status=’ 失常 ’;
5. 当判断处有一条执行失败,则须要回滚: ——–rollback;
6. 有事务爱护下提交流程
insert into user values(null,’ 灭霸 ’,20,’ 失常 ’); begin;
update user set money=money-5000 where id=2 and status=’ 失常 ’;
update user set money=money+5000 where id=3 and status=’ 失常 ’; commit;

保留回滚点:
begin; update user set money=200 where id=3;
savepoint s1; update user set money=400 where id=3;
savepoint s2; update user set money=600 where id=3;
rollback to s2;

10、SQL 分类: DDL Date Definition Language 数据定义语言 包含:create,alter,drop,truncate 不反对事务

DML Data Manipulation Language 数据操作语言 包含:insert update delete select(DQL)反对事务

DQL Data Query Language 数据查询语言 只包含:select

TCL Transaction Control Language 事务管制语言 包含:begin commit rollback savepoint xxx rollback to xxx

DCL Data Control Language 数据管制语言 调配用户权限相干的 SQL

11、数据类型:

1. 整数:int(m)和 bigint(m),m 代表显示长度 有余 m 长度时补零,须要联合 zerofill 关键字应用。create table t_int(id int,age int(10) zerofill); insert into t_int values(1,12); select * from t_int;

2. 浮点: double(m,d) m 代表总长度 d 代表小数长度 超高精度浮点数 decimal(m,d)当波及超高精度运算时时用。25.234 M=5 D=3

3. 字符串 char(m)固定长度,最大长度 255 益处 执行率高 varchar(m)可变长度 随着长度扭转 益处 节俭资源 最大长度 65535 然而超过 255 倡议应用 text text(m)可变长度,最大长度 65535

4. 日期 date:只能保留年月日 time:只能保留时分秒 datetime:年月日时分秒 最大值:9999-12-31 默认值为 null timestamp:事件戳 年月日时分秒 最大值:2038-1-19 默认值为以后零碎工夫 练习 1:create table t_date(t1 date,t2 time,t3 datetime,t4 timestamp); insert into t_date values(‘2019-05-17′,null,null,null); insert into t_date values(null,’16:29:00′,’2019-05-17 16:30:00’,null);

12、导入 sql 文件

1.windows 零碎 把文件放到 D 盘目录, 而后执行:source / 文件门路; linuex 零碎 2. 把文件放在桌面,而后执行 : mysql> source /home/soft01/ 桌面 /item_backup.sql;

验证:show tabale;

is null 和 is not null

select ename,sal from emp where mgr is null;

select ename,sal comm from emp where comm is n null;

13. 别名 select ename from emp; select ename as ‘ 姓名 ’ from emp; select ename ‘ 姓名 ’ from emp; select ename 姓名 from emp;

14. 比拟运算符 > , < , >= , <= , = , != ,<>

select ename,job,sal from emp where sal<=1600

select ename,job,sal from emp where jod=’manager’

select ename,deptno from emp where deptno!=23 G

select * from t_item where price=23 G

select title,price from t_item where price!=8443 G

15. 去重 distinct 把关键字 distinct 写在须要去重的的字段后面

1. 查问员工表有哪些职位 job select distinct job from emp;

2. 查问员工表部门编号有哪些 select distinct deptno from emp;

16.and 和 or 当须要满足一个条件同时满足时应用 and 当多个条件满足一个就行时应用 or

celect from emp where deptno=10 and sal<2000; 查问 30 号部门或工资大于 3000 的员工信息 celect from emp where deptno=30 or sal>3000;

17. 含糊查问 like % 代表 0 或多个未知字符 _代表单个未知字符

select ename from emp wgere like ‘%a%’;

select ename,sal from emp wgere like ‘_l%’;

select tatle from t_item wgere like ‘% 记事本 %’;

select ename,sal,job from emp wgere job like ‘%an%’ and sal>1500;

select * from t_item where sell_point like ‘% 赠 % and title like ‘%dell%’;

select * from t_item where price<100 and title like ‘% 笔记本 %’ G;

select * from t_item where image is not null title like ‘% 得力 %’ G;

select ename,sal from emp where ename not like ‘%a%’;

between x and y

select sal,ename from emp where sal>=2000 and sal<=3000; 查问工资在 2000 到 3000 之间的员工和工资(要求蕴含 2000 和 3000)select sal,ename from emp where between 2000 and 3000;

select title,price from t_item where price between 50 and 100;

select ename,sal from emp where sal not between 1000 and 2000;

in select from emp where sal=800 sal=1300 sal=1500; 查问员工价格为 800 1300 1500 的员工信息 select from emp where in(800,1300,1500);

select title,price from t_item where price in(56,58,89);

select ename,sal from emp where sal not in(3000,5000,1500);

select * from t_item where category_id in(238,917) G;

select title,price from t_item where title like ‘% 得力 %’ price between 50 and 200;

select ename,sal,mgr from emp where mgr is not null and<2000;

select ename,sal,mgr from emp where comm>0 and mgr is not null;

select distinct job from emp where ename like ‘%a%’ and<3000;

排序 order by
字段名 order by 字段名 asc(升序)默认 /desc(降序)
select ename,sal,mgr from emp order by sal;
10 号部门依照工资升降序
select ename,sal from emp where deptno=10 order by sal;

select ename,sal,comm from emp where comm>0 order by comm desc;

select title,price from t_item where price<100 order by price;

多字段排序 , 隔开 在前面在写一个字段

select title,price,category_id from t_item where price<200 order by category_id desc,price;

分页查问 limit 跳过条数, 申请条数

select ename,sal from emp order by sal limit 4,2;

select sal from emp order by sal desc limit 0,3;

select title,price from t_item order by price limit 10,5;

select * from emp where deptno=30 order by sal desc limit 0,1;

数值计算 + – * / 7%2 = mod(7,2)

select enaem,sal,5*sal 年终奖 from emp;

select title,price,num,price*num 总价值 from t_item;

select ename,sal+5 加新 5 from emp;

日期相干
1.SQL 语言中的 HelloWorld select ‘HelloWorld’;
2. 获取以后工夫 select now();

create table t_date(name varchar(10),birthday datetime); insert into t_date values(‘ 刘德华 ’,now());

3. 获取以后年月日,以后时分秒 current 以后 select curdate(),curtime();

4. 从年月日时分秒中提取年月日 和 提取时分秒 select date(now()),time(now());

查问每个商品的创立工夫(年月日)和(时 分 秒)select created_time,date(created_time) from t_item;

select created_time,time(created_time) from t_item;

5. 从残缺的年月日时分秒中提取工夫重量

select extract(year from now()); 年 select extract(month from now()); 月 select extract(day from now()); 日 select extract(hour from now()); 时 select extract(minute from now()); 分 select extract(second from now()); 秒

查问员工入职年份 select ename,extract(year from hiredate) from emp; 查问有员工入职的年份 + 去重 select distinct ename,extract(year from hiredate) from emp;

6. 日期格式化 date_format(工夫, 格局) %Y 四位年 %y 俩位年 %m2 位月 %c1 位年 %d 日 %H 24 小时 %h 12 小时 %i 分钟 %s 秒 测试:select date_format(now(),’%Y 年 %c 月 %d 号 %H 点 %i 分 %s 秒 ’); 以后工夫

7. 把非标准工夫格局转成规范工夫 str_to_date(工夫, 格局) 测试:select str_to_date(“19.04.2019″,”%d.%m.%Y %H 点 %i 分 %s 秒 ”);

ifnull() 格局 ; age = ifnull(x,y) 如果为 null 为 y 不为 null 为 x 测试:批改奖金为 null 的值为 0, 不为 null 则不变 update emp set comm=ifnull(comm,0);

聚合函数 对查问多条数据进行统计
:平均值,最大值,最小值,求和,计数
1. 平均值 avg(字段名) 测试:查问有奖金员工的均匀奖金 select avg(comm) from emp where comm>0;
2. 最大值 max(字段名) 测试:查问 20 号部门最高工资 select max(sal)from emp where deptno=20;
3. 最小值 min(字段名) 测试:查问有领导的员工最低工资是多少 select min(sal) from emp where mgr is not null;
4. 求和 sum(字段名)测试:查讯 20 号部门的工资总和 select sun(sal) from emp where deptno=20; 100 块钱以内的商品总和 select sun(price) from t_item where price<100;
5. 统计数量 count(字段名) 测试:查问 30 号部门有多少人 select count(*) from emp where deptno=30;

字符串相干
1. 字符串拼接 concat(‘s1′,’s2′)=’s1s2′ 测试:查问,每个员工的姓名工资 工资以元为单位 select ename,concat(sal,’ 元 ’) from emp;
2. 获取字符串长度 char_length(字段名) 测试:查问每个员工的姓名和名字长度 select ename,char_length(ename) from emp;
3. 获取字符串呈现的地位 instr(str,substr) select instr(‘abcdefg’,’d’);
4. 转大写 upper() lower() select upper(‘nba’),lower(‘ABC’);
5. 截取字符串 从右边截取 select left(‘abcdefg’,2); 从左边截取 select right(‘abcdefg’,2); 自在截取 substring(str, 地位, 长度) select substring(‘abcdefg’,2,3);
6. 去空白 trim() 去俩边不去除两头 select trim(‘ a b ‘);
7. 反复 repeat() select repeat(‘ab’,2);
8. 替换 replace(‘abcdefg abc’,’b’,’m’); select replace(‘abcdefg abc’,’b’,’m’); 9. 反转 reverse() select reverse(‘abc’);

数学相干
1. 向下取整 floor(num) select floor(3.85);
2. 四舍五入 round(num) round(3.8);
3. 四舍五入 round(num,m)m 代表小数位 select round(23.879,2);
4. 非四舍五入 truncate(num,m)m 代表小数位 select truncate(23,879,2);
5. 随机数 rand() select floor(rand()*6;)

分组 group by
格局:group by 字段名 n select…from 表名 where …group by …order by…limit…;
1. 查问每个部门的平均工资 selcet deptno,avg(sal) from emp group by deptno;
2. 查问每个部门的最高工资 select job,max(sal) from emp group by job;
3. 查问每个部门人数 select deptno,count(_) from emp group by deptno;
4.__查问每个部门的平均工资要求员工工资高于 1000 select deptno,avg(sal) from emp where sal>1000 group by deptno;
5. 查问每个主管 mgr 的手下人数 select mgr,count(_) from emp where mgr is not null group by mgr;

多字段分组 group by 字段名, 字段名;
select deptno,job,avg(sal) from emp group by deptno,job ;

1.select deptno,count(_),sum(sal) from emp group by deptno order by count(_),sum(sal) desc;

select deptno,count(*) c,sum(sal) s from emp group by deptno order by c,s desc; 2.select deptno,avg(sal),min(sal),max(sal) from emp where sal between 1000 and 3000 group by deptno order by avg(sal);

3.select job,count(*) c,sum(sal),avg(sal) a from emp where mgr is not null group by job order by c desc,a;

书写程序 select…from 表名 where …group by … order by…having…limit…;

having where 写一般条件 having 前面写聚合条件 需配合 group by 应用
2.select category_id,avg(price) a from t_item group by category_id having a<100;

3.select category_id,avg(price) a from t_item where category_id in(238,917) group by category_id having a;

4.select deptno,count(*),avg(sal) a from emp group by deptno having a>2000 order by a desc;

5.select deptno,sum(sal),avg(sal) a from emp where sal between 1000 and 3000 group by deptno having a>=2000 order by a;

6.select extract(year from hiredate) year,count(*) from emp group by year; 查问平均工资最高的部门编号和平均工资 7.select deptno,avg(sal) a from emp group by deptno order by a desc limit 0,1;

子查问(嵌套查问)
1. 查问工资高于平均工资的员工信息 select * from emp where sal>(select avg(sal) from emp);

2. 查问员工表中工资最高的员工信息 select * from emp where sal=(select max(sal) from emp);

3 查问工资高于 20 号部门最高工资的员工信息 select * from emp where sal>(select max(sal) from emp where deptno>20);

4. 查问 jones 雷同工作的其余员工信息 select * from emp where job=(select job from emp where ename=’jones’) and ename!=’jones’;

  1. select * from emp where deptno=(select deptno from emp where sal=(select min(sal) from emp)) and sal!=(select min(sal) from emp);

6.select max(HIREdate) from emp where HIREdate=(select max(HIREdate)from emp);

  1. select * from dept where deptno=(select deptno from emp where ename=’king’);
  2. select deptno from emp; select * from dept where deptno in(select distinct deptno from emp);
  3. select avg(sal) a from emp group by deptno order by a desc limit 0,1;

子查问能够写的地位 1. 写在 where 或 having 前面 当做查问条件 2. 写在创立表的时候 create table emp_10 as (select from emp where deptno=10); 3. 写在 from 前面 当成一张虚构表 select ename from (select from emp where deptno=10)newtable;

关联查问 同时查问多张表的查问形式称为关联查问 关联查问必须写关联关系,如果不写会失去俩张表的乘积,这个乘积称为笛卡尔积,工作中千万不要呈现这种状况 这是一个谬误的查问后果集 1. 查问每一个员工的姓名和对应的部门名 select e.ename,d.dname from emp e,dept d where e.deptno=d.deptno;

等值连贯和内连贯 1. 等值连贯:select from A,B where A.x=B.x and A.age>20;2. 内连贯:select from A join B on A.x=B.x where A.age>20;

1. 查问每一个员工的姓名和对应的部门名(内连贯) select e.ename,d.dname from emp e join dept d on e.deptno=d.deptno;

练习 1 select from emp e join dept d on e.deptno=d.deptno where d.loc=’new york’;

练习 2 select d.loc,d.dname from emp e join dept d on e.deptno=d.deptno where e.ename=’james’;

外连贯:查问一张表的全副和另外一张表的交加 或者俩张表的交加 格局:select *from A left/rigt join B on A.x=B.x where A.age=20;

1.select d.dname,e.ename from emp e right join dept d on e.deptno=d.deptno;

总结:如果查问的数据是俩张表的交加数据应用内连贯 如果查问的数据是一张表的全副和另一张表的交加数据应用外连贯

表设计:关联关系 外键 用于建设关系的字段,称为外键

一对一
create table user(id int primary key auto_increment , username varchar(10),password varchar(10)); create table userinfo(user_id int ,nick varchar(10),age int); insert into user values(null,’libai’,’admin’),(null,’libai’,’123456′);insert into userinfo values(1,’ 诗仙 ’,’38’),(2,’ 大耳贼 ’,’50’); select u.username,ui.nick from user u join userinfo ui on u.id=ui.user_id; 一对多 create table t_dept(id int primary key auto_increment,name varchar(10)); create table t_emp(id int,name varchar(10),dept_id int); insert into t_dept values(‘null’,’ 神仙 ’),(‘null’,’ 妖怪 ’); insert into t_emp values(null,’ 吾空 ’,1),(null,’ 猪八戒 ’,1),(null,’ 白骨精 ’,2),(null,’ 蜘蛛精 ’,2); select e.name,d.name from t_dept d join t_emp e on d.id=e.dept_id;

多对多

表设计 权限治理
create table user(id int,name varchar(10)); create table role(id int,name varchar(10)); create table module(id int,name varchar(10)); create table u_r(uid int, rit int); create table r_m(rid int, mid int);

insert into user values(1,’ 苍老师 ’),(2,’ 传奇弟 ’); insert into role values(1,’ 男游客 ’),(2,’ 男管理员 ’),(3,’ 女会员 ’),(4,’ 女管理员 ’); insert into module values(1,’ 男浏览 ’),(2,’ 男发帖 ’),(3,’ 男删贴 ’),(4,’ 女浏览 ’),(5,’ 女发帖 ’),(6,’ 女删贴 ’);

insert into u_r values(1,2),(1,3),(2,4); insert into r_m values(1,1),(2,1),(2,2),(2,3),(3,4),(3,6),(4,4),(4,5),(4,6);

select * from user u join u_r ur on u.id=ur.uid join role r on r.id=ur.rit join r_m rm on r.id=rm.rid join module m on m.id=rm.mid;

  1. select u.name,r.name from user u join u_r ur on u.id=ur.uid join role r on r.id=ur.rit;
  2. select u.name,m.name from user u join u_r ur on u.id =ur.uid join r_m rm on ur.rit=rm.rid join module m on m.id=rm.mid;
  3. select u.name,m.name from user u join u_r ur on u.id =ur.uid join r_m rm on ur.rit=rm.rid join module m on m.id=rm.mid where u.name=’ 苍老师 ’;
  4. select u.name,m.name from user u join u_r ur on u.id =ur.uid join r_m rm on ur.rit=rm.rid join module m on m.id=rm.mid where m.name=’ 女浏览 ’;

视图
create view 视图名 as(子查问); 创立一个 10 号部门
create view v_emp_10 as(select * from emp where deptno=10); 创立一个没有工资得员工表视图
create view v_emp_nosal as(select empno,ename,deptno fromemp);

create table t_department(id int auto_increment comment ‘ 部门 id’,name varchar(30) unique not null comment ‘ 部门名称 ’, primary key(id) )default charset=utf8;

insert into t_department(name)values(‘ 软件研发部 ’),(‘ 人力资源部 ’),(‘ 财务部 ’),(‘ 销售部 ’),(‘ 后勤部 ’);

alter table t_user add column department_id int;

update t_user set department_id=1 where id in (21,28,32); update t_user set department_id=2 where id in (24,25,26); update t_user set department_id=3 where id in (22,27,30); update t_user set department_id=4 where id in (23,29,31);

视图

视图的分类

  1. 简略视图:创立视图的子查问中不蕴含去重,分组,关联查问,聚合函数的视图称为简略视图,能够对视图中的数据进行增删改查操作
  2. 简单视图:和简略视图相同,只能对视图中的数据进行查问操作

· 创立查问每个部门平均工资,最高工资,最低工资,工资总和,部门人数的视图 create view v_emp_info as (select deptno,avg(sal),max(sal),min(sal),sum(sal),count(*) from emp group by deptno); #### 对简略视图进行增删改操作

· 操作形式和 table 一样 insert into v_emp_10 (empno,ename,deptno)values(10010,’Tom’,10); // 插入胜利 insert into v_emp_10 (empno,ename,deptno)values(10011,’Jerry’,20);// 数据净化 - 数据净化:当往视图中增加一条视图中不可见原表中却可见的数据称为数据净化,能够通过创立视图时增加 with check option 解决。create view v_emp_20 as(select * from emp where deptno=20) with check option; insert into v_emp_20 (empno,ename,deptno)values(10012,’ 刘备 ’,10);// 数据净化失败 insert into v_emp_20 (empno,ename,deptno)values(10012,’ 刘备 ’,20);// 胜利!

· 批改和删除只能操作视图中存在的数据 update v_emp_10 set ename=’TTT’ where ename=’Tom’;// 胜利 update v_emp_10 set ename=’xxx’ where ename=’Jerry’; 失败 delete from v_emp_10 where empno=10010; 胜利 delete from v_emp_10 where empno=10011; 失败

· 创立或替换视图 create or replace view v_emp_10 as (select ename,sal from emp where deptno=10);

· 删除视图 drop view v_emp_10;

· 别名:创立视图时应用了别名,则对视图操作时只能应用别名 create view v_emp_30 as (select ename name from emp where deptno=30); select ename from v_emp_30; 报错不意识 ename 应该用 name ### 束缚

束缚:创立表时给字段增加的限度条件

  1. 主键束缚 primary key
    限度主键的值惟一且非空
  2. 非空束缚 not null
    限度字段的值不能为 null create table t1(id int,age int not null); insert into t1 values(1,20); // 胜利 insert into t1 values(2,null);// 失败
  3. 惟一束缚 unique (u ni que)
    限度字段的值不能反复 create table t2(id int,age int unique); insert into t2 values(1,20);// 胜利 insert into t2 values(2,20);// 失败
  4. 默认束缚 default
    给字段增加默认值 create table t3(id int,age int default 20); insert into t3 values(1,10); insert into t3 values(2,null); insert into t3 (id) values (3);// 默认值失效
  5. 外键束缚 foreign key (否恩)

    外键:用于建设关系的字段称为外键

    增加外键束缚的字段值能够为 null,能够反复,然而不能是关联表中不存在的数据,如果建设了关系被关联的数据不能先删除,被关联的表不能先删除

    测试:create table mydept(id int primary key auto_increment,name varchar(10));

create table myemp(id int primary key
auto_increment,name varchar(10),dept_id int,
constraint fk_dept foreign key(dept_id) references mydept(id));

格局介绍:constraint 束缚名 foreign key(外键字段名) references 被依赖的表名(被依赖的字段名) insert into mydept values(null,’ 神仙 ’),(null,’ 妖怪 ’);

insert into myemp values(null,’ 悟空 ’,1);// 胜利 insert into myemp values(null,’ 刘德华 ’,3);// 失败 delete from mydept where id=1; // 失败 不能先删除 delete from myemp where id=1; // 胜利 delete from mydept where id=1; // 胜利 drop table mydept; // 失败 被依赖的表不能先删除 drop table myemp; // 胜利 drop table mydept;// 胜利 ### 索引

windows: source d:/item_backup.sql;.

linux: source /home/soft01/ 桌面 /item_backup.sql;

索引:索引是数据库中进步查问效率的技术,相似于字典的目录

索引的作用:数据会零散的保留在每一个磁盘块中,如果不应用索引,查问数据时会挨个遍历每一个磁盘块查找数据,如果应用了索引,磁盘块会以树桩构造保留,查找数据时可能大大降低拜访的磁盘块的数量,从而进步查问效率

有索引就肯定好吗?不是,因为数据量小的话会升高查问效率,所以只针对大量数据拜访时才应用索引技术

索引是越多越好吗?不是,因为索引会占用磁盘空间,只针对查问时频繁应用的字段创立索引

创立索引的格局:create index 索引名 on 表名(字段名(? 长度));

  1. 创立索引前先查问一次 看消耗工夫 1.39 秒 select * from item2 where title=’100′;
  2. 创立索引 create index i_item_title on item2(title);
  3. 验证 消耗工夫 0.01 秒 select * from item2 where title=’100′;.

查看索引 如果给表增加主键束缚,会主动创立主键的索引

格局:show index from 表名; show index from item2;

删除索引:drop index 索引名 on 表名; drop index i_item_title on item2;

复合索引:通过多个字段创立索引 create index i_item_title on item2(title,price); show index from item2;// 创立了两个索引

总结:

  1. 索引是数据库中进步查问效率的技术相似于目录
  2. 因为索引会占用磁盘空间不是越多越好,只针对查问时频繁应用的字段创立索引。
  3. 数据量越大索引成果越显著,如果数据很少可能会升高查问效率。
事务

· 事务是数据库中执行同一业务多条 SQL 语句的工作单元,保障多条 SQL 全副执行胜利或全副执行失败

· 相干 SQL:开启事务 begin 提交事务 commit 回滚事务 rollback 保留回滚点 savepoint xxx 回滚到某个回滚点 rollback to xxx;

· 事务的四大个性,ACID 个性,ACID 个性是保障事务正确执行的四大基本要素

  1. Atomicity:原子性,最小不可拆分,保障全副胜利或全副失败
  2. Consistency:一致性,从一个统一状态到另一个统一状态
  3. Isolation:隔离性,多个事务之间互不影响
  4. Durability:持久性,事务提交后数据保留到磁盘中长久失效
group_concat()办法 组连贯
  1. 查问每个部门的员工姓名和工资, 要求每个部门的数据显示在一行 select deptno,group_concat(ename,’:’,sal) from emp group by deptno;
  2. 查问每种工作的员工姓名,要求员工姓名一行显示 select job,group_concat(ename) from emp group by job;

1. 建库 create database tedu_store;

2. 用库 use tedu_store;

3. 建表

create table t_user(
    uid int auto_increment comment '用户 id', 
    username varchar(50) unique not null comment '用户名', 
    password char(32) not null comment '明码', 
    salt char(36) comment '盐值',
    gender int comment '性别,0- 女,1- 男',
    phone varchar(20) comment '手机号码',
    email varchar(50) comment '电子邮箱',
    avatar varchar(100) comment '头像',
    is_delete int comment '是否删除,0- 未删除,1- 以删除',
    created_user varchar(50) comment '创建人',
    created_time datetime comment '创立工夫',
    modified_user varchar(50) comment '最初批改人', 
    modified_time datetime comment '最初批改工夫',
    primary key(uid) )
default charset=utf8mb3;

正文完
 0