day03

1.列约束

 (1)默认值约束 —— default

  在插入值的时候使用默认值

  可以使用default来设置默认值和应用默认值

  具体应用默认值有两种方式

  insert  into  laptop  values(4, ‘小米Air’, default, ....);

  insert  into  laptop(lid,title)  values(5,’小新700’);  

  给特定的列提供值,没有出现的列自动应用默认值

  练习:删除family表下fname的约束,然后给fname添加默认值约束,设置默认值为‘未知’,插入数据测试。

 (2)检查约束 —— check

  称作自定义约束,可以自己添加约束条件

  create  table  student(

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

);

  mysql不支持检查约束,会极大的影响数据的插入速度,后期使用js代替。

 (3)外键约束

  声明了外键约束的列,取值范围会在另一个表的主键列

  注意事项:外键列要和对应的另一个表主键列的列类型保持一致

  foreign  key(外键列)  references  另一个表(主键列)   注:主键要唯一性

 

2.自增列

 auto_increment:自动增长,在插入值的时候只需要赋值为null,就会自动获取最大值然后加1插入。

 注意事项:

   自增列必须添加在整数形式的主键列

   允许手动赋值

 练习:编写脚本文件tedu.sql,设置编码utf-8,先丢弃再创建数据库tedu,进入数据库创建保存部门数据的表dept,包含部门编号did(主键、自增列),部门名称dname(唯一约束),插入以下数据

  10  研发部   20  市场部    30  运营部    40  测试部

  创建保存员工数据的表emp,包含编号eid(主键、自增列),姓名ename(非空约束),性别sex(默认值约束),生日birthday,工资salary,所属部门编号deptId(外键约束);插入若干条数据

  

3.简单查询

 (1)查询特定的列

  示例:查询出所有员工的编号和姓名

  select  eid,ename  from emp;

  练习:查询出所有员工的姓名、生日、工资

  select  ename,birthday,salary  from emp;

 (2)查询所有的列

  select  eid,ename,sex,birthday,salary,deptId  from emp;

  select * from emp;

 (3)给列起别名

  示例:查询出所有员工的编号和姓名,使用汉字别名

  select  eid as 编号,ename as 姓名  from emp;

  练习:查询出所有员工的姓名、性别、生日、工资,使用一个字母作为别名

  select  ename a,sex b,birthday c,salary d  from emp;

as关键字可以省略,保留即可。

给列起别名的目的是为了简化列名称。

 (4)显示不同的记录

  示例:查询出都有哪些性别的员工

  select  distinct sex  from  emp;

  练习:查询出员工都分布在哪些部门

  select  distinct deptId  from emp;

 (5)查询时执行计算

  示例:计算 1+3+5+8*7.4+5*4.39

  select  1+3+5+8*7.4+5*4.39;

  练习:查询出所有员工的姓名及其年薪

  select  ename,salary*12  from emp;

  练习:假设每个员工的工资增长1000,年终奖20000,查询出所有员工的姓名及其年薪,使用汉字别名

  select  ename 姓名,(salary+1000)*12+20000 年薪 from emp;

 (6)查询结果排序

  示例:查询出所有的部门,结果按照部门编号升序排列

  select * from dept order by did asc;  #ascendant 升序的

  示例:查询出所有的部门,结果按照部门编号降序排列

  select * from dept order by did desc;  #descendant  降序

  练习:查询出所有的员工,结果按照工资降序排列

  select * from emp order by salary desc;

  练习:查询出所有的员工,结果按照年龄从大到小排列(生日从小到大)

  select * from emp order by birthday asc;

  练习:查询出所有的员工,结果按照姓名的升序排列

  select * from emp order by ename;

按照字符串排序是按照字符的编码排列

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

  示例:查询出所有的员工,结果按照工资的升序排列,要求男员工显示在前女员工显示在后

  select * from emp order by sex desc, salary;

  练习:查询出所有的员工,结果按照部门升序排列,如果部门相同按照年龄从小到大排列

  select * from emp  order by deptId,birthday desc;

 (7)条件查询

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

  select * from emp where eid=5;

  练习:查询出姓名为king的员工所有列

  select * from emp where ename='king';

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

  select * from emp where deptId=20;

  练习:查询出工资在6000以上的员工有哪些

  select * from emp where salary>6000;

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

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

  select * from emp where deptId!=20;

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

  select * from emp where deptId is null;

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

  select * from emp where deptId is not null;

  练习:查询出工资在7000以上的男员工有哪些

   select * from emp where salary>7000 and sex=1;

   select * from emp where salary>7000 && sex=1;

  练习:查询出工资在6000~9000直接的员工有哪些

   select * from emp where salary>=6000 && salary<=9000;

   select * from emp where salary between 6000 and 9000;

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

   select * from emp where salary<6000  or  salary>9000;

   select * from emp where salary<6000  ||  salary>9000;

   select * from emp where salary not between 6000 and 9000;

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

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

   select * from emp where birthday between '1993-1-1' and '1993-12-31';

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

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

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

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

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

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

and (&&)  并且,两个条件都满足

or (||)  或者,两个条件满足其一

between  and   在两者之间

not  between  and  不在两者之间

is  null   值为null

is  not  null   值不为null

in(  )   满足其中一个

not  in(  )    都不满足

 (8)模糊条件查询

  示例:查询出姓名中含有字母o的员工有哪些

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

  练习:查询出姓名中以o结尾的员工有哪些

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

  练习:查询出姓名中第二个字符是o员工有哪些

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

%  匹配任意个字符   >=0

_  任意一个字符      =1

 

软件下载地址

链接:https://pan.baidu.com/s/1xL02vqb_XnLZoZzyjTRNKQ

提取码:a94v

 

课后任务

 (1)复习今天内容,整理思维导图

 (2)删除代码,保留注释,重新编写SQL命令

   练习:查询出工资在10000以上的男员工的姓名,生日,工资;按照工资降序排列。

 (3)查看学子商城数据库设计

 (4)预习js第1天

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值