-- 表的数据操作(增insert 删delete 改update 查select)--========================================
! uu 是表名
-- 标准添加(指定所有表字段,给定所有字段值进行添加)insertinto uu(id,name,age) values(1,'zhangsan',20);-- 标准添加(不同之处是字段顺序)insertinto uu(age,id,name) values(22,2,'lisi');-- 指定部分字段添加值insertinto uu(id,name) values(3,'wangwu');-- 不指定字段信息添加值,注意字段值得顺序和个数要和表结构一致insertinto uu values(4,'zhaoliu',25);-- 批量添加值insertinto uu values(5,'u01',21),(6,'u02',22),(7,'u03',23);-- 查看所有字段所有值select * from uu;-- 修改uu表的id值为3的信息,将age改为18update uu set age=18where id=3;-- 修改id为6的信息,将age改为28,name改为uu04update uu set age=28,name='uu04'where id=6;-- 删除id值为100的信息deletefrom uu where id=100;-- 删除id值大于50的所有信息deletefrom uu where id>50;
修改表结构
格式: alter table 表名 action(更改选项);
更改选项:
1. 添加字段:alter table 表名 add 字段名信息
-- 在user表的最后追加一个num字段 设置为int not null
mysql> alter table user add num int not null;
-- 在user表的email字段后添加一个age字段,设置int not null default 20;
mysql> alter table user add age int not null default 20 after email;
-- 在user表的最前面添加一个aa字段设置为int类型
mysql> alter table user add aa int first;
2. 删除字段:alter table 表名 drop 被删除的字段名
例如:-- 删除user表的aa字段
mysql> alter table user drop aa;
3. 修改字段:alter table 表名 change[modify] 被修改后的字段信息 (修改记得必须要写属性值)
其中:change可以修改字段名, modify 不修改
-- 修改user表中age字段信息(类型),(使用modify关键字的目的不修改字段名)
mysql> alter table user modify age tinyint unsigned not null default 20;
-- 修改user表的num字段改为mm字段并添加了默认值(使用change可以改字段名)
mysql> alter table user change num mm int not null default 10;
4. 添加和删除索引
-- 为user表中的name字段添加唯一性索引,索引名为uni_name;
mysql> alter table user add unique uni_name(name);
-- 为user表中的email字段添加普通索引,索引名为index_eamil
mysql> alter table user add index index_email(email);
-- 将user表中index_email的索引删除
mysql> alter table user drop index index_email;
5. 更改表名称:
ALTER TABLE 旧表名 RENAME AS 新表名
6. 更改AUTO_INCREMENT初始值:
ALTER TABLE 表名称 AUTO_INCREMENT=17. 更改表类型:
ALTER TABLE 表名称 ENGINE="InnoDB"
MySQL数据库中的表类型一般常用两种:MyISAM和InnoDB
区别:MyISAM类型的数据文件有三个frm(结构)、MYD(数据)、MYI(索引)
MyISAM类型中的表数据增 删 改速度快,不支持事务,没有InnoDB安全。
InnoDB类型的数据文件只有一个 .frm
InnoDB类型的表数据增 删 改速度没有MyISAM的快,但支持事务,相对安全。
数据DQL 操作:数据查询
格式:
select [字段列表]|* from 表名
[where 搜索条件]
[groupby 分组字段 [having 子条件]]
[orderby 排序 asc|desc]
[limit 分页参数]
1. where条件查询
1. 查询班级为lamp138期的学生信息
mysql> select * from stu where classid='lamp138';
2. 查询lamp138期的男生信息(sex为m)
mysql> select * from stu where classid='lamp138'and sex='m';
3. 查询id号值在10以上的学生信息
mysql> select * from stu where id>10;
4. 查询年龄在20至25岁的学生信息
mysql> select * from stu where age>=20and age<=25;
mysql> select * from stu where age between 20and25;
5. 查询年龄不在20至25岁的学生信息
mysql> select * from stu where age not between 20and25;
mysql> select * from stu where age<20or age>25;
6. 查询id值为1,8,4,10,14的学生信息
select * from stu where id in(1,8,4,10,14);
mysql> select * from stu where id=1or id=8or id=4or id=10or id=14;
7. 查询lamp138和lamp94期的女生信息
mysql> select * from stu where classid in('lamp138','lamp94') and sex='w';
mysql> select * from stu where (classid='lamp138'or classid='lamp94') and sex='w
查询
where条件查询
--========================================================--1. 查询id为3的学生信息
mysql> select * from stu whereid=3;
--2. 查询性别sex为m的所有信息
mysql> select * from stu where sex='m';
--3. 查询年龄大于25岁的所有学生信息
mysql> select * from stu where age>25;
--4. 查询学生年龄是22~27岁的所有信息
mysql> select * from stu where age>=22and age<=27;
mysql> select * from stu where age between22and27;
--5. 查询学生年龄在20~25岁之外的所有信息
mysql> select * from stu where age<20or age>25;
mysql> select * from stu where age notbetween20and25;
--6. 查询性别sex为w,并且年龄大于22岁的所有信息
mysql> select * from stu where sex='w' and age>22;
--7. 查询classid不为null的所有信息
mysql> select * from stu where classid isnot null;
--8. 查询id值为1,3,5,9,12的学员信息
mysql> select * from stu whereidin(1,3,5,9,12);
--9. 查询name字段值是以zh开头的所有信息
mysql> select * from stu wherename like "zh%";
mysql> select * from stu wherename regexp "^zh"; --正则写法--10.查询姓名name中含有ang子串的所有信息
mysql> select * from stu wherename like "%ang%";
mysql> select * from stu wherename regexp "ang";
-11. 查询姓名是任意四位字符构成的信息。
mysql> select * from stu wherename like "____";
mysql> select * from stu wherename regexp "^[a-z0-9]{4}$";
统计函数
MySQL的统计函数(聚合函数):
max() min() count() sum() avg()
- 分组 group by 字段名[,字段名]
统计班级classid值为2的男女生各多少人?
select sex,count(*) from stu where classid=2groupby sex;
- 排序:order by 字段名[,字段名] desc|asc
-- asc 默认升序 desc 降序
获取每个班级的平均年龄,并按平均年龄降序,
select classid,avg(age) from stu groupby classid orderbyavg(age) desc;select classid,avg(age) anum from stu groupby classid orderby anum desc;-- limit 关键字 查询部分数据 -- 例如: .... limit m; 查询数据只显示前m条 -- 例如: .... limit m,n; 排除前m条,然后再查询出前n条
统计每个班级的人数,按人数从大到小排序,取前3条。
select classid,count(*) num from stu groupby classid orderby num desc limit 3;
分页公式:.... (页号-1)*页大小, 页大小;
多表查询
表之间的关系有:1对1 1对多 多对多
1. 嵌套查询, 一个查询的结果是另外sql的条件
2. where关联查询
3. join连接查询(左联,右联,内联)
-- 查询年龄最大的所有学生信息
mysql> select * from stu where age=(select max(age) from stu);
+----+---------+------+-----+---------+| id | name | age | sex | classid |
+----+---------+------+-----+---------+
| 9 | xiaoli | 29 | w | 2 |
| 14 | zhangle | 29 | m | 5 |
+----+---------+------+-----+---------+-- 查询python02期的所有学生信息
mysql> select * from stu where classid=(select id from classes where name='python02');
mysql> select * from stu where classid in(select id from classes where name='python02');
where关联查询
已知:员工personnel表和部门department表,其中员工表中的did字段为部门表id主键关联。
查询所有员工信息,并显示所属部门名称
要求:显示字段:员工id 部门 姓名
mysql> select p.id,d.name,p.namefrom personnel p,department d where p.did = d.id;
+----+-----------+-----------+
| id | name | name |
+----+-----------+-----------+
| 2 | 人事部 | 李玉刚 |
| 10 | 人事部 | 阿杜 |
| 4 | 市场部 | 刘欢 |
连接join查询
左联:left join
右联:right join
内联:inner join
查询新闻信息,并补齐新闻类别信息
mysql> select n.id,n.title,t.namefrom news n,ntype t where n.ntid=t.id;
mysql> select n.id,n.title,t.namefrom news n inner join ntype t on n.ntid=t.id;
同上,但采用的是左联查询left join
mysql> select n.id,n.title,t.namefrom news n left join ntype t on n.ntid=t.id;
统计每个新闻类别下的新闻数量,采用where关联统计
mysql> select t.name,count(n.id) from ntype t,news n where t.id=n.ntid group by t.id;
统计每个新闻类别下的新闻数量,采用左联统计
mysql> select t.name,count(n.id) from ntype t left join news n on t.id=n.ntid group by t.id;
1. MySQL的表复制
复制表结构
mysql> create table 目标表名 like 原表名;
复制表数据
mysql> insert into 目标表名 select * from 原表名;
create table stu2 like stu;
复制stu 名为stu2 ; 只复制结构不复制内容
insert into stu2 select * from stu limit 5;
赋值stu数据前五条添加到 stu2;
2. 数据表的索引
创建索引
CREATEINDEX index_name ON table_name (column_list)
CREATE UNIQUE INDEX index_name ON table_name (column_list)
删除索引
DROP INDEX index_name ON talbe_name
3. mysql视图
创建视图:
mysql> create view v_t1 asselect * from t1 where id>4and id<11;
Query OK, 0 rows affected (0.00 sec)
view视图的帮助信息:
mysql> ? view
ALTER VIEW
CREATE VIEW
DROP VIEW
查看视图:
mysql> show tables;
删除视图v_t1:
mysql> drop view v_t1;
8、有关慢查询操作:
开户和设置慢查询时间:
vi /etc/my.cnf
log_slow_queries=slow.log
long_query_time=5
查看设置后是否生效
mysql> show variables like"%quer%";
慢查询次数:
mysql> show global status like"%quer%";