mysql数据库--表中数据的基本操作(4)

约束:
1.主键(primary key) : 它的整个列中元素唯一且非空。
auto_increment 自动增加 每次加1,只能用于创建表时。报错、删除数据也会自增。 需要 truncate table XXX,才会从1开始。

例如:create table student1(id int primary key auto_increment,name varchar(10));
       insert into student1(name) values('aaa'),('bbbb'),('cccc');

2.外键(foreign key):会校验外键在主表中是否存在
3.非空: not null
4.默认值 : default 默认值
5.检查约束 mysql enum()
6. 唯一(unique):唯一但是可以为空 。


1.插入数据

1.1 为所有列插入数据

1.1.1 在insert语句里指定所有字段名
语法格式:

   insert into 表名(字段名1,字段名2,...)  values(值1,值2,.....);

1.1.2 在insert语句里不指定字段名
语法格式:

   insert into 表名 values(值1,值2,....);

这种方式值必须和表中的字段对应,并且每个字段都要赋值,否则错误。

1.2 为指定列插入数据

语法格式:

 insert into 表名(字段名1,字段名2,...)  values(值1,值2,.....);

其他不插入数据的列使用默认值即可。

1.3 批量插入数据

1.3.1 为所有列批量插入数据

语法格式:

insert into 表名(字段名1,......) values(值1,...),(值1,....),......;

1.3.2 为指定列批量插入数据
语法格式:

insert into 表名(字段名1,…) values(值1,…),(值1,…),…;

2.查看数据

语法格式:

                  select * from 表名;

3.修改数据

3.1 更新全部数据

语法格式:

      update 表名 set 字段名=‘新值’;

3.2 更新部分数据

     update 表名 set 字段名=‘新值’ [where 条件];
     update 表名 set 字段名1=‘新值’,字段名2=‘新值’ [where 条件];

4.删除数据

删除表中所有数据,但保留表结构:

      delete from 表名;

删除表中指定数据:

     delete from 表名 where 条件;

5.单表查询数据

5.1普通查询

查询表中所有数据:

     select * from 表名;

指定字段查询数据:

     select 字段名1,字段名2,... from 表名;

带条件查询数据:

     select 字段名 form 表名 where 条件;

5.2 带between的查询(范围查询)

     select 字段名 form 表名 where 条件1 and 条件2;
     select 字段名 form 表名 where 字段名 between 条件1 and 条件2;

5.3模糊查询(like)

_表示任意一个字符,%任意多个字符

5.4查询并排序:

order by (升序)

     select * from 表名 order by 字段名;    +desc降序

order by 放在 where条件后,limit放在order后

5.5 limit : 限制

     select * from 表名 limit n ;   表示取前n条数据
     select * from 表名 limit offset(偏移量n) ,m;表示跳过前n条数据,再取m条数据

5.6 distinct:去重

     select distinct 字段名 from 表名 ;

5.7聚合函数:

max:

     select max(字段名) from 表名;

min:

     select min(字段名) from 表名;

sum:

     select sum(字段名) from 表名;

average:

     select avg(字段名) from 表名;

count:

     select count(字段名) from 表名 where 条件;
      select count(*) from 表名 where 条件;

5.8 分组查询 :gruop by

select 字段1,... from 表名 where 条件 group by 字段 [having 字段][order by 字段][limit n]

5.9 子查询

两次查询,将第一次查询的结果作为第二次查询的参数传入。

6.查询练习

创建员工表employee包含如下信息:

列名 员工编号eno 员工姓名ename 员工年龄eage 员工性别esex 员工值为ejob 员工入职时间ehiredate 员工奖金 ebonus 员工底薪ebsalary 部门编号deptno
要求 主键 不能为空 男女中选择
 create table employee(eno int primary key,ename varchar(5) not null,eage int,esex enum('男','女'),ejob varchar(6),ehiredate date,ebonus int,ebsalary int,deptno int)

2.增加如下员工信息

员工编号eno 员工姓名ename 员工年龄eage 员工性别esex 员工值为ejob 员工入职时间ehiredate 员工奖金 ebonus 员工底薪ebsalary 部门编号deptno
1 李鹏飞 32 经理 2016-11-12 5000 8000 10
2 王鹏飞 27 销售员 2018-10-20 2000 1000 10
3 肖美 24 前台 2019-03-21 0 3500 20
4 王乐乐 30 经理 2017-03-02 0 9000 20
5 张丽丽 28 行政人员 2019-11-11 0 5000 20
6 徐华 33 销售员 2019-11-17 500 4000 10
7 赵辉 40 经理 2016-11-17 0 50000 30
8 王伟 35 开发工程师 2018-11-28 0 30000 30
9 钱慧慧 28 开发工程师 2019-04-17 0 25000 30
10 孙雯彩 29 开发工程师 2017-09-15 0 20000 30
 insert into employee values(1,'李鹏飞',32,'男','经理',20161112,5000,8000,10);
 insert into employee values(2,'王鹏飞',27
  • 1
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值