JAVA学习第20天;字段约束;

JAVA学习第20天;字段约束;,第1张

JAVA学习第20天;字段约束; 字段约束

是通过不同的方式,给不同的字段添加不同的约束,实现不同的效果,目前主要学习l了唯一约束,非空约束,主键约束
1唯一约束:如果为一个列添加了唯一约束那么这个列的值就必须是唯一的(即不能重复),但可以为空。 unique
2.非空约束:非空约束:如果为一个列添加了非空约束,那么这个列的值就不能为空,但可以重复。 not null
3.主键约束:如果为一个列添加了主键约束,那么这个列的值就不能为空,不可以重复. primary key
主键自增策略 : 当主键为数值类型时,为了方便维护,可以设置添加自增策略auto_increment

基础函数

lower 数据转小写

select  'ABC',lower (ABC) form dept;      //因为dept表中没有 大写ABC  满足不了转小写的任务要求  直接 查找 'ABC',然后通过lower转换

upper 数据转大写

select   upper(dname) from dept    //将dept中的dname 表头 转化为大写

length 数据的长度

select length (dname) from dept 

substr 数据的截取

select dname , substr(1,3) from dept     //从1截取  截取3个

concat 数据的拼接

select danme ,concat(dname,'abc') from dept   //把dname 的数据都拼接 'abc'

replace 数据的替换

select  dname ,replace(dname,'a','ccc')from dept  // 把a数据替换成ccc

ifnull 数据替换null 如果是null 替换成想替换的数据

select  comm(null,0)comm from dept   // comm(null,0) 将数据中存在的null 转为0    后面的comm  因为是  "空格"行为,所以是改名字
round&ceil&floor

1round 四舍五入

 select   comm  ,round (comm)  from emp      //四舍五入取整
 select   comm  ,round (comm,1)  from emp   //四舍五入并保留一位小数

ceil 向上取整

select comm,ceil(comm) from emp

floor 向下取整

select comm,floor(comm) from emp
now

now 是获取系统当前时间
select now()
year 是获取系统当前年
select year(now())

year&month&day
 hour()时 minute() 分  second() 秒
select  now(),hour(now()),minute(now()),second(now()) from emp;

year & month  &dat
select  now(),year(now()),month(now()),day(now()) from emp;

转义字符

作为sql 语句.内容中出现单撇就会乱套,进行转义即可

select 'ab'cd' -- 单引号是一个SQL语句的特殊字符
select 'ab'cd' --数据中有单引号时,用一个转义变成普通字符
条件查询

distinct 去重 去掉重复的记录行

select distinct  	xxx  from dept;           将dept表中的xxx标头中的重复记录行去掉

where 根据判断条件查询目标

  查询   *=所有     表名                    查询条件
select*from          dept   where     deptno=0
select ename      dept  where     loc='二区'   //查询 dept表中  住在二区的 员工名字

like模糊查询 通常使用& 模糊符: 通配0~n个字符

//比如   查询名字中带有a的员工信息
select*from  emp  dept   where ename  like'&a&'   
//比如查询 工作岗位中 董字开头的员工姓名
select ename from emp  where  job like '董&'  
//比如查询 工作岗位中 董字结尾的员工姓名
select ename from emp  where  job like '&董'  

null

//查询没有奖金的员工
 select*from emp where comn is  null
 //查询有奖金的员工
 select*from emp where comn  is not null
//#练习12:查询工资在5000~10000的员工信息
select*from emp where    sal>=5000 and sal<=10000  //更加灵活
select*from emp where sal between 5000 and 10000 //效果同上
//#查询2015年至2019年入职的员工信息
select*from emp where hiredate>='2015-1.1'and '2019.12.31'<=hiredate
select*from emp where  year(hiredate) between2015 and 2019

扩展

char 和varchar 的区别
char字符串的长度是固定的, char(n) n最大为255 ,
varchar字符串的长度不是固定的,varchar(n) n 最大是65535

char (10) 保存10个字符 , 保存abc 剩余的7个会用空格补齐
varchar(10)保存 10个字符,保存abc 不会发生这种情况
char 浪费资源,但是查询很快
varchar 节约资源,但是查询较慢

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/zaji/5684162.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-17
下一篇 2022-12-17

发表评论

登录后才能评论

评论列表(0条)

保存