【云计算】MySQL增删查改及基本函数解析
小标 2018-12-24 来源 : 阅读 790 评论 0

摘要:本文主要向大家介绍了【云计算】MySQL增删查改及基本函数解析,通过具体的内容向大家展现,希望对大家学习云计算有所帮助。

本文主要向大家介绍了【云计算】MySQL增删查改及基本函数解析,通过具体的内容向大家展现,希望对大家学习云计算有所帮助。


  数据完整性 数据的准确性和可靠性。 完整性约束 实体完整性

实体:记录


实体完整性约束保证数据记录之间是准确的(能够唯一标识一个实体)。


主键约束: 唯一的,不能为空。 primary key




 
 
 


 

# 1.添加主键约束


 

create table student(


 

sid int primary key,


 

sname varchar(20)


 

);


 

# 2.添加主键约束


 

create table student(


 

sid int,


 

sname varchar(20),


 

primary key(sid)


 

);


 

# 3.表创建之后添加主键约束


 

create table student(


 

sid int,


 

sname varchar(20)


 

);


 

#主键约束


 

alter table student add CONSTRAINT PK_SID primary key(sid);


 字段选择
 

唯一的,不要选择业务字段。


 主键添加
 

一张表中只能有一个主键,但是可以有联合主键(多个字段整体作为主键)。


 

3.每张表必须设置主键


     


唯一约束: 不能重复 可以添加多个(unique)




 
 
 


 

alter table userinfo add CONSTRAINT UQ_CARD unique(card);


     


主键自增: 从1开始,每次自身加1(oracle不能使用,序列)




 
 
 


 

create table student(


 

sid int primary key auto_increment,


 

sname varchar(20)


 

);


     


删除约束:


alter table student drop primary key


域完整性

域: 字段


类型约束


非空约束: 不能为空 not null


默认值: default 值




 
 
 


 

#非空和默认值


 

create table student(


 

sid int primary key auto_increment,


 

sname varchar(20) not null,


 

gender bit(1) default 1


 

);


     


引用完整性

一张表中通用列的取值必须参考另外一张表主键字段。


外键约束:foreign key




 
 
 


 

alter table student add CONSTRAINT FK_CID foreign key(cid)


 

REFERENCES classroom(cid);


 主外键关联 外键关联字段名称可以不一样,但是类型必须一致。    


自定义完整性

check约束: 在mysql中不能使用




 
 
 


 

Alter table student add constriaint check(age between 1 and 150)


     


运算符


 
 
 


 

#算术运算符


 

select 1+1;


 

select 1-1;


 

select 1*2;


 

select 3/2; # 1.5


 

select 3 div 2; #整除


 

select 3/0; # null


 

# 比较运算符 = != >= <= true 1 false 0


 

select 1!=1;


 

# is null / is not null / between ...and .../ in / not in


 

# 逻辑运算符 and or !


 

select 1=1 and 1=2;


 

select 1=1 or 1=2;


 

select !1=1;


 

# 位运算符 & | ^


 

select 3 & 2; # 2


 

select 3 | 2; # 3


 

select 3 ^ 2; # 1


     


DML 添加数据(insert)


 
 
 


 

Insert into tname[(字段名称…)] values(值…);


 

#添加数据classroom


 

#给所有字段添加值,和表中字段顺序一致


 

insert into classroom values(3,'bd1809');


 

#给cname字段添加值,和指定字段的顺序必须一致


 

insert into classroom(des,cname) values('des','bd1810');


 

#添加三条记录(批量插入)


 

insert into classroom(cname,des) values('bd1811','des'),


 

('bd1812','des'),


 

('bd1901','des');


 

#将classroom的值整体复制到classroom1


 

insert into classroom1 select * from classroom;


     


删除数据 delete


 
 
 


 

delete from tname [where 条件]


 

truncate:清空 文件级别清空 自增重置


 

delete:删除 只修改数据 逐行删除 自增不会重置


     


修改数据 update


 
 
 


 

Update tname set 字段=新值,… [where 条件]


 

update classroom set des = 'des' where cid = 2;


     


查询数据 select


 
 
 


 

Select 字段|表达式 from 表|视图|结果集


 

Where 条件


 

Group by 分组


 

Having 分组之后进行检索


 

Order by 排序


 

Limit 限制结果


 

# 查询所有信息


 

select * from emp;


 

#查询所有员工的姓名和工资


 

select ename,sal from emp;


 

#查询工资>2000的员工信息


 

select * from emp where sal > 2000;


 

#查询工资在1000到2000之间的员工信息(范围查询)


 

select * from emp where sal >= 1000 and sal <=2000;


 

select * from emp where sal between 1000 and 2000;


 

#查询编号(empno)为7521,7369,7788的员工信息。(集合查询)


 

select * from emp where empno = 7521 or empno = 7369 or empno = 7788;


 

select * from emp where empno in (7521,7369,7788);


 

#别名(简化 做解释说明) [as] 别名


 

#字段 表达式 结果集 表 都可以起别名


 

select ename,sal*1.05 nsal from emp;


 

# 查询所有职位信息 去重


 

select distinct job from emp;


 

# 查询在10号部门工资最高的员工信息


 

#1.10号部门所有员工


 

select * from emp where deptno =10;


 

#2.排序,取第一个


 

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


 

# 排序 order by 排序字段 默认升序排序 asc | desc


 

#根据员工工资降序排序,工资一致的按照员工编号的降序排序。


 

select * from emp order by sal desc,empno desc;


 

# 限制结果查询 limit 下标,长度 (仅适用于mysql,分页查询)


 

select * from emp limit 0,5;


 

# 查询所有有奖金的员工信息


 

select * from emp where comm is not null;


 

# 模糊查询 like %:0到多个任意字符 _:1个字符


 

# 查询名字以s打头的员工信息


 

select * from emp where ename like 's%';


 

#包含


 

select * from emp where ename like '%s%';


 

# 第二个字符为L的所有员工信息


 

select * from emp where ename like '_l%';


     


函数 单行函数


 
 
 


 

#函数


 

#数学函数


 

select PI()* 2 *2; #pi


 

select CEIL(-12.3); #向上取整


 

select FLOOR(12.3); #向下取整


 

select ROUND(8.45,-1); #四舍五入


 

select MOD(5,2); #取模


 

select RAND(); #随机数 [0,1)


 

select POW(2,3); #幂运算


 

#随机从emp中获取两条记录。


 

select * from emp order by RAND() limit 2;


 

#字符函数


 

select LENGTH('this is a dog'); #获取长度


 

select length(ename) from emp;


 

select LOWER('THIS');


 

select UPPER('this');


 

select SUBSTR('this is zs',1,6); #下标从1开始


 

#select REPLACE(str,from_str,to_str);


 

select trim(' this is '); #去两端空格


 

select LPAD('aa',10,'*'); #左填充


 

select RPAD('aa',10,'*'); #右填充


 

#日期函数


 

select NOW(); #当前时间


 

select SYSDATE(); #获取系统时间


 

select CURRENT_DATE();


 

select CURDATE();


 

select CURRENT_TIME();


 

select CURTIME();


 

select YEAR('1998-09-09');


 

select YEAR(NOW());


 

select MONTH(date);


 

select DAY(date);


 

#获取当前月最后一天


 

select LAST_DAY('2018-02-02');


 

#日期计算


 

select DATE_ADD(NOW(),interval 2 MONTH);


     


聚合函数 分组函数


 
 
 


 

# 聚合函数


 

# min() max() avg() count() sum()


 

select max(sal) from emp;


 

select min(sal) from emp;


 

select avg(sal) from emp;


 

select count(*) from emp; #记录数


 

select count(1) from emp; #记录数


 

select count(comm) from emp; #字段非空总数


 

select sum(sal) from emp;


 

#分组 group by 分组条件 having:分组之后进行检索


 

select deptno,avg(sal) from emp group by deptno;


 

# 查询平均工资大于2000的部门的编号和平均工资。


 

# 1.where在group by之后


 

# 2.where中不能使用聚合函数


 

select deptno,avg(sal) from emp group by deptno having avg(sal) > 2000;


     


加密函数


 
 
 


 

#加密函数


 

select MD5('root');


 

select SHA('root');


 

select password('root');


     




          

本文由职坐标整理并发布,希望对同学们有所帮助。了解更多详情请关注职坐标大数据云计算大数据安全频道!

本文由 @小标 发布于职坐标。未经许可,禁止转载。
喜欢 | 0 不喜欢 | 0
看完这篇文章有何感觉?已经有0人表态,0%的人喜欢 快给朋友分享吧~
评论(0)
后参与评论

您输入的评论内容中包含违禁敏感词

我知道了

助您圆梦职场 匹配合适岗位
验证码手机号,获得海同独家IT培训资料
选择就业方向:
人工智能物联网
大数据开发/分析
人工智能Python
Java全栈开发
WEB前端+H5

请输入正确的手机号码

请输入正确的验证码

获取验证码

您今天的短信下发次数太多了,明天再试试吧!

提交

我们会在第一时间安排职业规划师联系您!

您也可以联系我们的职业规划师咨询:

小职老师的微信号:z_zhizuobiao
小职老师的微信号:z_zhizuobiao

版权所有 职坐标-一站式IT培训就业服务领导者 沪ICP备13042190号-4
上海海同信息科技有限公司 Copyright ©2015 www.zhizuobiao.com,All Rights Reserved.
 沪公网安备 31011502005948号    

©2015 www.zhizuobiao.com All Rights Reserved

208小时内训课程