mysql基础 day03

1.列约束

 (1)唯一约束 —— unique

  声明了唯一约束的列不允许出现重复的值,允许插入null,甚至多个null;可能会影响数据的排序 

 (2)默认值约束 —— default

  可以使用default关键字来设置默认值,具体应用方式有两种

  insert  into  family  values(40, default); #通过default调用当前列的默认值

  insert  into  family(fid)  values(50);  #给指定的列提供了值,没有出现的列自动调用默认值

 (3)检查约束 —— check

  也称为自定义约束

  create  table  student(

    score  tinyint  check(score>=0  and  score<=100)

);

  mysql不支持检查约束,会影响数据的插入速度

  后期用JS来完成验证

 (4)外键约束

  声明了外键约束的列,插入的值必须得在另一个表的主键列中出现过才行;外键列要和对应的主键列类型要保持一致

  foreign key(外键列)  references  另一个表(主键列)

外键约束使用目的是为了让两个表之间产生关联

 

2.自增列

 auto_increment  自动增长,如果设置了自增列,在插入数据的时候,只需要赋值为null,就会获取当前的最大值然后加1插入

 注意事项:

   自增列只能添加在整数形式的主键列

   允许手动赋值 

3.简单查询

 (1)查询特定的列 

  select 列名,列名...  from  表名;

 (2)查询所有的列

  select * from  表名;

 (3)给列起别名

  select  列名 as  别名, 列名 as 别名  from 表名;

 (4)显示不同的记录

  select distinct  列名 from 表名;

 (5)查询时执行计算

  select 2+3+4+5; 

 (6)查询的结果排序

  Select * from 表名  order by 列名 asc;  #asc 升序的

  Select * from 表名 order by 列名 desc;    #desc降序

   describe 描述

   descendant  降序的

字符串排序,按照字符的Unicode码排列

不加排序规则,默认是按照升序排列

 (7)条件查询

  示例:查询出编号为5的员工

  select * from emp where eid=5;

>  <  >=  <=   =   !=(不等于)

  练习:查询出不在20号部门的员工有哪些

  select * from emp where deptId!=20;

  练习:查询出没有明确部门的员工有哪些

  select * from emp where deptId is null;

  练习:查询出有明确部门的员工有哪些

  select * from emp where deptId is not null;

  练习:查询出工资在5000~8000之间的员工有哪些

  select * from emp where salary>=5000 and salary<=8000;

  select * from emp where salary>=5000 && salary<=8000;

and(&&)   并且

or(||)   或者

  练习:查询出工资在5000以下或者8000以上的员工有哪些

  select * from emp where salary<5000 or salary>8000;

  select * from emp where salary<5000 || salary>8000;

  练习:查询出1993年出生的员工有哪些

  select * from emp where birthday>='1993-1-1' and birthday<='1993-12-31';

  练习:查询出20号部门或者30号部门的员工有哪些

  select * from emp where deptId=20 or deptId=30;

  select * from emp where deptId in(20,30);

  练习:查询出不在20号部门并且也不在30号部门的员工有哪些

  select * from emp where deptId!=20 and deptId!=30;

  select * from emp where deptId not in(20,30);

 (8)模糊条件查询

  select * from 表名 where 列名 like '%查询条件%';

  select * from 表名 where 列名 like '%a_';

 %  匹配任意个字符   >=0

  _   匹配任意一个字符  =1

  以上两个匹配符必须结合like关键字使用

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值