昨晚小组讨论学习get到两个新知识,一个是静姐提出的“正交测试法”,一个是月萍提到的视图,因为对这两个概念不太熟悉,于是今天查了相关资料进行了总结,也趁此通过实例总结了下mysql的基础知识,希望小组能够多多讨论,我相信每个人在讨论中都能有所收获,再由点及面多多思考,能够学到更多的知识。
登录mysql数据库mysql -uroot -p'111111'
(1)创建数据库worker
mysql> create database worker;
Query OK, 1 row affected (0.00 sec)
(2)切换数据库worker
mysql>use worker;
Databasechanged
(3)创建员工表worker_info1(字段:编号,姓名,性别,电话)
创建员工表worker_info2(字段:编号,邮箱,工资,入职日期,职能)
mysql> create table worker_info1(ID int(10),workername varchar(20),sex varchar(10),tel int(20));
mysql> create table worker_info2(ID int(10),email varchar(30),salary int(20),employdatesdate,department varchar(30));
(4)查看表结构
mysql> desc worker_info1;
+------------+-------------+------+-----+---------+-------+
| Field| Type| Null | Key |Default | Extra |
+------------+-------------+------+-----+---------+-------+
| ID| int(10)| YES||NULL||
| workername | varchar(20) | YES||NULL||
| sex| varchar(10) | YES|| NULL||
| tel| int(20)| YES||NULL||
+------------+-------------+------+-----+---------+-------+
(5)在worker_info1表中新增qq字段,要求qq字段在电话前面
在worker_info2表中修改入职日期为最后一个字段
将worker_info2表中的email字段改为mailbox
(alter删除、增加、修改表字段:alter table表名drop字段名;alter table表名add字段名字段类型;alter table表名字段名字段类型;alter
table表名旧字段名新字段名字段类型;修改表名:alter table旧表名rename to新表名)
mysql> alter table worker_info1 add qq int(10) after sex;
mysql> alter table worker_info2 modify employdates date after department;
mysql> alter table worker_info2 change email mailbox varchar(30);
(6)向每个表中插入5条记录
mysql> insert into worker_info1 values(1,'xiaoming','man',456568584,13745782587);
mysql>insert into worker_info1 values(2,'xiaohong','woman',4346568584,13755782587);
mysql> insert into worker_info1 values(3,'xiaoguang','man',246568584,1535782587);
mysql> insert into worker_info1 values(4,'xiaogang','man',244368584,1530982587);
mysql> insert into worker_info1 values(5,'xiaoli','woman',904368584,15900982587);
mysql> insert into worker_info2 values(1,'xiaomm@163.com',12121,'yanfa','20110304');
mysql> insert into worker_info2 values(2,'xiaohong@163.com',14121,'ceshi','20130304');
mysql> insert into worker_info2 values(3,'xiaoguang@163.com',10121,'yunwei','20150904');
mysql> insert into worker_info2 values(4,'xiaogang@163.com',17121,'UI','20140604');
mysql> insert into worker_info2 values(5,'xiaoli@163.com',9121,'chanpin','20130604');
(7)修改worker_info2表中ID为1的mailbox和salary字段(修改或更新数据表中的数据用update,语法:update表名set字段名=values,字段名2=values2 where)
mysql> update worker_info2 set mailbox='xiaoming@163.com' ,salary=10110 where ID=1;
(8)查询员工的名字和入职日期的记录
mysql> select workername,employdates from worker_info1,worker_info2 where worker_info1.ID=worker_info2.ID;
(9)查询入职时间为2014年以前的员工姓名(where条件用and or连接)
mysql> select workername from worker_info1,worker_info2 where worker_info1.ID=worker_info2.ID and
year(employdates)<2014;
(10)查询工资最低和最高的员工姓名(order by排序,默认为升序,加关键字desc为降序排列)
mysql> select workername from worker_info1,worker_info2 where worker_info1.ID=worker_info2.ID order by salary limit 1;
mysql> select workername from worker_info1,worker_info2 where worker_info1.ID=worker_info2.ID order by salary desc limit 1;
(11)查询平均工资
mysql> select avg(salary) from worker_info2;
(12)统计男员工和女员工的数量
mysql> select count(*) from worker_info1 where sex='man';
mysql> select count(*) from worker_info1 where sex='man';
(13)内连接左连接右链接
内连接:(inner join)读取两张表重合的部分;
左连接:(left join)读取左边表的全部数据即使右边表不存在对应数据;
右链接:(right join)读取右边表的全部数据即使左边表不存在对应数据;
首先分别在worker_info1和work_info2中添加一条数据如下
mysql> insert into worker_info1 values(6,'xiaoqiang','man',34545354,2345321344);
mysql> insert into worker_info2 values(7,'xiaoliang@163.com',22323,'xiangmujingli','20120908');
内连接(可直接使用join)
mysql> select a.ID,a.workername,b.salaryfrom worker_info1 ajoin worker_info2 bon a.ID=b.ID;
+------+------------+--------+
| ID| workername | salary |
+------+------------+--------+
|1 | xiaoming|10110 |
|2 | xiaohong|14121 |
|3 | xiaoguang|10121 |
|4 | xiaogang|17121 |
|5 | xiaoli|9121 |
+------+------------+--------+
5 rows in set (0.00 sec)
可看出是两个表的重合部分
左连接(left join)
mysql> select a.ID,a.workername,b.salary from worker_info1 a left join worker_info2 b on a.ID=b.ID;
+------+------------+--------+
| ID| workername | salary |
+------+------------+--------+
|1 | xiaoming|10110 |
|2 | xiaohong|14121 |
|3 | xiaoguang|10121 |
|4 | xiaogang|17121 |
|5 | xiaoli|9121 |
|6 | xiaoqiang|NULL |
+------+------------+--------+
6 rows in set (0.00 sec)
以上可以看出结果包含左边表的全部数据
右连接(right join)
mysql> select a.ID,a.workername,b.salary from worker_info1 a right join worker_info2 b on a.ID=b.ID;
+------+------------+--------+
| ID| workername | salary |
+------+------------+--------+
|1 | xiaoming|10110 |
|2 | xiaohong|14121 |
|3 | xiaoguang|10121 |
|4 | xiaogang|17121 |
|5 | xiaoli|9121 |
| NULL | NULL|22323 |
+------+------------+--------+
6 rows in set (0.00 sec)
以上可以看出查询结果包含右边表的全部数据
注意对比三种连接的结果数据,体会三种连接的不同
(14)分组(group by)根据一个或多个字段对结果集进行分组
首先修改下表worker_info2的数据
mysql> update worker_info2 set department='yanfa' where ID=4;
mysql> update worker_info2 set department='ceshi' where ID=7;
mysql> update worker_info2 set department='ceshi' where ID=3;
接下来我们用group by语句将数据表按职能进行分组,并统计每种职能有多少条记录
mysql> select department ,count(*) from worker_info2 group by department;
+------------+----------+
| department | count(*) |
+------------+----------+
| ceshi|3 |
| chanpin|1 |
| yanfa|2 |
+------------+----------+
3 rows in set (0.00 sec)
(15)删除表中数据(delete from表where)
mysql> delete from worker_info2 where ID=7;
(16)视图:视图是一个虚拟表,其内容由查询来定义,使用视图的好处有:
(a)视图能简化用户操作
(b)视图使用户能以多种角度看待同一数据
(c)视图对重构数据库提供了一定程度的逻辑独立性
(d)视图能够对机密数据提供安全保护
(e)适当的利用视图可以更清晰的表达查询
例:单表创建视图
mysql> create view view2 as select ID,workername,tel from worker_info1;
mysql> select * from view2;
多表创建视图
mysql> create view view1 as select a.ID,a.workername,b.salary from worker_info1 a,worker_info2 b where a.ID=b.ID;
mysql> select * from view1;