数据库MYSQL(2)

###主键约束
主键:用于表示数据唯一性的字段称为主键
约束:就是对表字段的值添加限制条件
主键约束:保证主键的值唯一并且非空
格式: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
insert into t1 values(2,‘ddd’);
####主键约束+自增
格式:create table t2(id int primary key auto_increment,name varchar(10));
测试:insert into t2 values(null,‘aaa’); 1
insert into t2 (name) values(‘bbb’);2
insert into t2 values(10,‘ccc’);10
insert into t2 values(null,‘ddd’);11
delete from t2 where id>=10;
insert into t2 values(null,‘eee’);12
delete from t2;
insert into t2 values(null,‘fff’);13
自增数值只增加不减少,从历史最大值的基础上+1;
–truncate table t2; //删除表并创建新表
####注释comment
格式:create table t3(id int primary key auto_increment comment ‘这是个主键’,
name varchar(10) comment ‘这是名字’);
查看注释内容:show create table t3;
#####和单引号的区别:用于修饰表名和字段名
create table t4(id int,name varchar(10));
':用于修饰文本
####数据冗余
如果表设计不合理,可能会出现大量的重复数据,这种现象称为数据冗余,
通过拆分表的形式解决此问题
练习:创建表保存以下信息
保存集团总部下财务部里面的财务A部的张三工资8000 年龄18
保存集团总部下研发部的李四工资800 年龄75
需要保存的信息有:部门名 员工姓名 工资 年龄
考虑数据冗余问题所以创建两个表员工表和部门表
create table t_emp(id int primary key auto_increment,name varchar(10),sal int,age int,dept int);
create table t_dept(id int primary key auto_increment,name varchar(10),parent_id int);
//插入数据 先插入部门表数据然后再插入 员工表数据
insert into t_dept values(null,‘集团总部’,null),(null,‘财务部’,1),(null,‘财务A部’,2),(null,‘研发部’,1);
insert into t_emp values(null,‘张三’,8000,18,3),(null,‘李四’,800,75,4);
练习:分类category 价格price 库存num
1.保存家电分类下洗衣机分类下的海尔洗衣机价格2300 库存38;
2.保存办公用品下,笔分类下的晨光圆珠笔 价格5 库存100;

create table category(id int primary key auto_increment,name varchar(10),parent_id int);
create table item(id int primary key auto_increment,name varchar(10),price int,num int,category_id int);
insert into category values(null,‘家电’,null),(null,‘洗衣机’,1),(null,‘办公室’,null),(null,‘笔’,3);
insert into item values(null,‘海尔洗衣机’,2300,38,2),(null,‘晨光圆珠笔’,5,100,4);
####事务(transaction)是数据库中执行同一业务多条SQL语句的工作单元,事务
可以保证多条SQL语句全部执行成功或全部执行失败
和事务相关的SQL语句

1.创建表
create table user(id int primary key auto_increment,name varchar(10),money int,state varchar(5));
2.插入数据
insert into user values(null,‘李雷’,5000,‘正常’),(null,‘韩每每’,50,‘正常’),(null,‘Lucy’,10,‘冻结’);
3.李雷给Lucy转账SQL
update user set money=4000 where id=1 and state=‘正常’;
update user set money=1010 where id=3 and state=‘正常’;
select*from user;
以上代码无事务保护,则导致数据库内部的数据李雷丢了1000块钱;
开启事务
begin;
让李雷-1000
update user set money=3000 where id=1 and state=‘正常’;
让Lucy+1000
update user set money =1010 where id=3 and state=‘正常’;
因为一条成功一条失败,所以不能把内存中的改变提交到磁盘中,所以需要执行回滚指令,
执行之前可以打开另外一个窗口查看数据库中的数据是否发生改变(变的是内存种的数据,
数据库里面的数据并没有发生改变)
rollback;
转账成功执行流程
1.开启事务
begin;
2.让李雷-1000
update user set money=3000 where id=1 and state=‘正常’;
3.让韩梅梅+1000;
update user set money=1050 where id=2 and state=‘正常’;
4.由于转账业务的多条SQL全部执行成功,所以可以执行提交指令 把内存中的改动
提交到磁盘中
commit;
和事务相关的SQL语句;
1.begin:开启事务
2.rollback:回滚事务 把内存中的改动清除
3.commit:提交事务 把内存中的改动一次性的提交到磁盘中
第二种开启事务的方式
数据库中事务默认是自动提交的
1.查看数据库自动提交的状态
show variables like ‘%autocommit=0’;
2.修改自动提交的状态 0关闭 1开启;
set autocommit=0;
3.修改李雷钱为50000;
update user set money =50000 where id=1;
-savepoint :保存回滚点
set autocommit=1;
李雷开始是50000
begin;
update user set money =10000 where id=1;
savepoint s1;
update user set money =20000 where id=1;
savepoint s2;
update user set money =30000 where id=1;
rollback to s1;
总结事务相关指令
1begin开启事务
2.rollback 回滚事务
3.commit 提交事务
4.查看自动提交状态 show variables like ‘%autocommit%’
5.修改自动提交状态 set autocommit=0/1;
6.保存回滚点 savepoint s1;
7.回滚到回滚点 rollback to s1;
###SQL分类
####DDL Data Definition Language
数据定义语言
包括:create ,alter ,drop, truncate 不支持事务
不支持事务
###DML Data Manipulation Language
数据操作语言
包括:insert delete update select (DQL) 支持事务
支持事务:
###DQL Data Query Language
数据查询语言
只包括select
###TCL Transaction Control Language
事务控制语言
包括:begin,rollback commit,savepoint xxx,rollback to xxx
###DCL Data Control Language
数据控制语言
用于分配用户权限相关的SQL

####数据类型
1.整数类型:int(m) 对应java中的int,bigint(m)对应java中的long,m代表显示长度,需要结合
zerofill使用
create table t_int(id int,age int(8) zerofill);
insert into t_int values(1,28);
select* from t_int;
2.浮点数:double(m,d)m代表总长度,d代表小数长度 76.232(m=5 d=3)
decimal 超高精度小数,当涉及超高精度运算时使用。
3.字符串:char(m)固定长度 最大255 char(10)“abc“ 所占长度10,varchar(m)
可变长度 最大65535 varchar(10) “abc”所占长度3,
varchar最大65535 但是建议保存255以内的长度,超过255使用text
text 可变长度 最大65535;
4.日期
data :只能保存年月日
time:只能保存时分秒
datatime:保存年月日时分秒,最大值9999-12-31,默认值null
timestampe:保存年月日时分秒,最大值2038-1-19,默认值为当前时间
create table t_date(d1 date,d2 time,d3 datetime,d4 timestamp);
insert into t_date values(‘2018-11-15’,‘16:58:33’,null,null);
insert into t_date values(‘2018-11-15’,‘16:58:33’,‘2018-10-18 12:30:18’,null);

###导入*.sql数据到数据库
window系统
source d:/tables.sql;
linux 系统
source /home/soft01/桌面/tables.sql;
查询所有表 看是否有四张表
###is null 和is not null
查询没有上级领导的员工编号,姓名,工资
select empno,ename,sal from emp where mgr is null;
2.查询emp表中没有奖金comm的员工姓名,工资\奖金
select ename,sal,comm from emp where comm is null;
3.查询emp表中有奖金comm的员工姓名,工资\奖金
select * from emp where comm is not null and comm>0;
####别名
把查询到的员工姓名ename 改成 名字
select ename as ‘名字’ from emp;
select ename ‘名字’ from emp;
select ename 名字 from emp;
####去重
1.查询emp表中出现的所有职位job
select distinct job from emp;
####比较运算符 >,<,>=,<=,=,!=和<>
1.查询工资小于等于1600的员工姓名和工资
select ename,sal from emp where sal<=1600;
2.查询部门编号是20的所有员工的姓名、职位job和部门编号deptno
select ename,job,deptno from emp where deptno=20;
3.查询职位是manager的所有员工姓名和职位
select ename,job from emp where job=‘manager’;
4.查询部门不是10号部门的所有员工姓名和部门编号(两种写法)
select ename,deptno from emp where deptno!=10;
select ename,deptno from emp where deptno<>10;
5.查询t_item表中单价price 等于23的商品信息
select * from t_item where price=23;
6查询t_item表中单价不等于8443的商品标题title和单价
select title,price from t_item where price!=8443;
####and 和or
and 和 java中的&&效果一样
or和java中的||效果一样

1.查询不是10号部门并且工资小于3000的员工信息
select from emp where deptno !=10 and sal<3000;
2.查询部门编号为30号或者上级领导为7698的员工姓名,职位,上级领导和部门编号
select ename,job,mgr,deptno from emp where deptno =30 or mgr=7698;
1.查询emp表中工资为5000,1500,3000的员工信息
select * from emp where sal=5000 or sal=1500 or sal= 3000;
使用in
select * from emp where sal in (5000,1500,3000);
2.查询emp表中工资不等于5000,1500,3000的员工信息
select * from emp where sal not in (5000,1500,3000);
####between x and y 包括x 和y
1.查询emp表中工资在2000到3000之间的员工信息
select
from emp where sal>=2000 and sal<=3000;
between and
select * from emp where sal between 2000 and 3000;
2.查询emp表中工资在1000到3000之外的员工信息
select * from emp where sal not between 1000 and 3000;
####模糊查询 like
-_:代表单个未知字符
-%:带表0或多个未知字符
举例
1.查询以a开头 like ‘a%’
2 以m结尾 like ‘%m’
3.第二个字符是a like ‘a%’
4.第三个字符是x倒数第二个字符是y like ‘x%y
5.倒数第三个字符是x like '%x

####like 案例
1.查询 t_item 表中 标题中包含记事本的商品信息
select title from t_item where title like ‘%记事本%’;
2.查询单价低于100的记事本(title包含记事本)
select * from t_title where price<100 and title like ‘%记事本%’;
3.查询单价在50到200之间的得力商品(title包含得力)
select* from t_item where price between 50 and 200 and title like ‘%得力%’;
4.查询有图品的得力商品(有图片image字段不为null)
select * from t_item where image is not null and title like ‘%得力%’;
5.查询分类(category_id)为238,917的商品信息
selectfrom t_item where category_id in(238,917);
6.查询有赠品的商品信息(卖点sellpoint包含赠字)
select
from t_item where sell_point like ‘%赠%’;
7.查询标题中不包含得力的商品标题
select title from t_item where title not like ‘%得力%’;
8.查询价格在500到2000之外的商品信息
select * from t_item where price not between 50 and 200;
####排序
-格式 order by 字段名 desc降序/asc生序;
1.查询所有员工的姓名和工资,按照工资升序排序
select ename,sal from emp order by sal;
2.查询所有员工的姓名,工资,和部门编号,按照部门编号降序排序
select ename, sal,deptno from emp order by deptno desc;
多字段排序:order by 字段名1 asc/desc,字段名2 asc/desc;
1.查询所有员工的姓名,工资和部门编号,按照部门编号降序排序,如果部门
一致则按照工资生序排序
select ename,sal,deptno from emp order by deptno desc,sal;
####分页查询 limit
格式:limit 跳过的条数,请求的数量
举例:
1.请求第一页20条数据 limit 2,20
select name from emp;
2.请求第三页10条数据 limit 20,10
3.请求的5页 8条数据 limit 32,8
公式:limit((页数-1)*每页数量,每页数量)
练习:
1.查询所有商品按照单价升序排序,显示第二页每页7条数据
select * from t_item order by price limit 7,7;
2.查询员工表中所有员工的姓名和工资,按照工资降序排序,显示第3页每页三条数据
select ename,sal from emp order by sal desc limit 6,3;
3.查询所有员工中工资前三名的姓名和工资
select ename,sal from emp order by sal desc limit 0,3;
4.查询工资最高的员工姓名和工资
select ename,sal from emp order by sal desc limit 0,1;

猜你喜欢

转载自blog.csdn.net/qq_43165760/article/details/86138146
今日推荐