表操作
1、修改表名
alter table 表名 rename 新表名;
mysql> alter table t1 rename t2;
2、添加字段
1. 添加新字段
alter table 表名 add 字段 类型 约束;
mysql> alter table t3 add id int not null;
mysql> alter table t3 add (id int not null,name varchar(20));
把字段添加到字段name后面
mysql> alter table t3 add id int after name;
把字段添加到第一个位置
mysql> alter table t3 add id int first;
3. 修改字段数据类型、约束
1.修改名称、数据类型、修饰符
#change修改字段名称,类型,约束,顺序
mysql> alter table t3 change id newid int after name
#修改了字段名称并更换了位置
2. 修改字段类型,约束,顺序 #modify不能修改字段名
alter table t3 modify 字段 数据类型 约束;
mysql> alter table t3 modify id int after name;
mysql> alter table t4 modify name varchar(30) charset utf8;
3. 删除字段
alter table 表名 drop 字段名;
mysql> after table t3 drop id;
4. 删除表
mysql> drop table t7,t6,t5,t4,t3;
4、插入数据
字符串必须引号引起来
记录与表头相对应,表头与字段用逗号隔开。
1.添加一条记录
insert into 表名(字段1,字段2,字段3,字段4) values(1,"tom","m",90);
mysql> insert into t3(id,name,sex,age) values(1,"tom","m",18);
注:添加的记录与表头要对应
2.添加多条记录
mysql> insert into t3(id,name,sex,age) values(2,"jack","m",19),(3,"xiaoli","f",20);
3.用set添加记录
mysql> insert into t3 set id=4,name="zhangsan",sex="m",age=21;
4.更新记录
update 表名 set 修改的字段 where 修改目标;
mysql> update t1 set name=tom where id=1;
5.删除记录
<1.删除单条记录
mysql> delete from t1 where id=1; #删除那个记录,等于就会删除那个整条记录
<2.删除所有记录
mysql> delete from t1;
5、增加约束和主键
1.增加约束(针对已有的主键增加 auto_increment)
mysql> alter table t1 modify id int not null auto_increment;
2.增加复合主键
mysql> alter table t1 add primary key(host_ip,port);
3.增加主键
mysql> alter table t1 add primary key(id);
4.增加主键和自动增长
mysql> alter table t1 modify id int primary key auto_increment;
5.删除主键
<1.删除自增约束
mysql> alter table t1 modify id int not null;
<2.删除主键
mysql> alter table t1 drop primary key;
6、表复制
表复制:key不会被复制: 主键、外键和索引
1.复制表结构+记录
create table 新表 select * from 旧表;
mysql> sreate table new_t1 select * from t1;
2.复制单个字段:
create table 新表 (select id,name from 旧表);
create table new_t1 (select id,name from t1);
表查询
单表查询
创建一个测试表:
mysql> CREATE TABLE t1(
id int primary key AUTO_INCREMENT not null,
name varchar(30) not null,
sex enum('male','female') default 'male' not null,
hire_date date not null,
post varchar(50) not null,
job_description varchar(100),
salary double(15,2) not null,
office int,
dep_id int
);
插入数据:
mysql> insert into t1(name,sex,hire_date,post,job_description,salary,office,dep_id) values
('jack','male','20180202','instructor','teach',5000,501,100),
('tom','male','20180203','instructor','teach',5500,501,100),
('robin','male','20180202','instructor','teach',8000,501,100),
('alice','female','20180202','instructor','teach',7200,501,100),
('awei','male','20180202','hr','hrcc',600,502,101),
('harry','male','20180202','hr',NULL,6000,502,101),
('emma','female','20180206','sale','salecc',20000,503,102),
('christine','female','20180205','sale','salecc',2200,503,102),
('zhuzhu','male','20180205','sale',NULL,2200,503,102),
('gougou','male','20180205','sale','',2200,503,102);
1、简单查询
select 字段1,字段2 from 表名 where 条件;
mysql> select * from t1; #查询所有记录,生产环境由于数据庞大,不建议使用
mysql> select id,name from t1; #多字段查询
1.条件查询 where
mysql> select name from t1 where id>5;
mysql> select id,name,salary from employee5 where salary>2000;
2.通过四则运算查询
SELECT name, salary, salary*14 FROM employee5;
SELECT name, salary, salary*14 AS Annual_salary FROM employee5;
SELECT name, salary, salary*14 Annual_salary FROM employee5;
3.设置别名 as
mysql> select name,salary,salary*14 as annual_salary from t1;
+-----------+----------+---------------+
| name | salary | annual_salary |
+-----------+----------+---------------+
| jack | 5000.00 | 70000.00 |
| tom | 5500.00 | 77000.00 |
| robin | 8000.00 | 112000.00 |
| alice | 7200.00 | 100800.00 |
| awei | 600.00 | 8400.00 |
| harry | 6000.00 | 84000.00 |
| emma | 20000.00 | 280000.00 |
| christine | 2200.00 | 30800.00 |
| zhuzhu | 2200.00 | 30800.00 |
| gougou | 2200.00 | 30800.00 |
+-----------+----------+---------------+
4.统计记录数量:count
mysql> select count(*) from t1;
统计字段得到数量:
mysql> select count(id) from t1;
5.避免重复distinct
mysql> select distinct post from employee5;
只能针对单一字段
6.concat()函数 连接
mysql> select concat(name, ' annual_salary: ' ,salary*14) as annual_salary from t1;
+-----------------------------------+
| annual_salary |
+-----------------------------------+
| jack annual_salary: 70000.00 |
| tom annual_salary: 77000.00 |
| robin annual_salary: 112000.00|
| alice annual_salary: 100800.00|
| awei annual_salary: 8400.00 |
| harry annual_salary: 84000.00 |
| emma annual_salary: 280000.00|
| christine annual_salary: 30800.00 |
| zhuzhu annual_salary: 30800.00 |
| gougou annual_salary: 30800.00 |
+-----------------------------------+
mysql> select id,concat(name, ' annual_salary: ' ,salary*14) as annual_salary from t1;
+----+-----------------------------------+
| id | annual_salary |
+----+-----------------------------------+
| 1 | jack annual_salary: 70000.00 |
| 2 | tom annual_salary: 77000.00 |
| 3 | robin annual_salary: 112000.00|
| 4 | alice annual_salary: 100800.00|
| 5 | awei annual_salary: 8400.00 |
| 6 | harry annual_salary: 84000.00 |
| 7 | emma annual_salary: 280000.00|
| 8 | christine annual_salary: 30800.00 |
| 9 | zhuzhu annual_salary: 30800.00 |
| 10 | gougou annual_salary: 30800.00 |
+----+-----------------------------------+
2、多条件查询
1. and ---与
select 字段1,字段2 from 表名 where 条件 and 条件;
mysql> select name from t1 where id>1 and salary>600;
2. or ---或
select 字段1,字段2 from 表名 where 条件 or 条件;
mysql> select name from t1 where id>1 and salary>600 or dep_id=100;
mysql> select name from t1 where id>4 or salary>10000;
3. between and ---什么和什么之间
mysql> select name from t1 where salary between 5000 and 10000;
4. not ---取反
mysql> select name from t1 where not id>5;
5. is null ---空的
mysql> select name,job_description from t1 where job_description is null;
mysql> select name,job_description from t1 where job_description is not null; #-取反 不是null
mysql> select name,job_description from t1 where job_description=''; #什么都没有==空
NULL说明:
1、等价于没有任何值、是未知数。
2、NULL与0、空字符串、空格都不同,NULL没有分配存储空间。
3、对空值做加、减、乘、除等运算操作,结果仍为空。
4、比较时使用关键字用“is null”和“is not null”。
5、排序时比其他数据都小(索引默认是降序排列,小→大),所以NULL值总是排在最前。
6. in ---集合查询
mysql> select name from t1 where salary in(4000,8000,6000,9000);
等于如下指令:
mysql> select name,salary from t1 where salary=4000 or salary=5000 or salary=6000 or salary=9000;
+-------+
| name |
+-------+
| robin |
| harry |
+-------+
mysql> select name from t1 where salary not in(4000,8000,6000,9000); #取反
+-----------+
| name |
+-----------+
| jack |
| tom |
| alice |
| awei |
| emma |
| christine |
| zhuzhu |
| gougou |
+-----------+
7. order by ---排序查询
mysql> select name,salary from t1 order by salary; #默认从小到大排序
+-----------+----------+
| name | salary |
+-----------+----------+
| awei | 600.00 |
| christine | 2200.00 |
| zhuzhu | 2200.00 |
| gougou | 2200.00 |
| jack | 5000.00 |
| tom | 5500.00 |
| harry | 6000.00 |
| alice | 7200.00 |
| robin | 8000.00 |
| emma | 20000.00 |
+-----------+----------+
mysql> select name,salary from t1 order by salary desc; #从大到小排序
8. limit ---限制
mysql> select name,salary from t1 order by salary desc limit 0,1;
从第0条记录开始,取一条记录(默认第一条记录索引号为0)
+------+----------+
| name | salary |
+------+----------+
| emma | 20000.00 |
+------+----------+
9. 函数
max() 最大值
mysql> select max(salary) from t1;
查询薪水最高的人的详细信息:
mysql> select name,sex,hire_date,post,salary,dep_id from t1
-> where salary = (select max(salary) from t1); #子查询
min()最小值
select min(salary) from t1;
avg()平均值
select avg(salary) from t1;
now() 现在的时间
select now();
sum() 计算和
select sum(salary) from t1 where post='sale';
3、分组查询
mysql> select dep_id from t1;
+--------+
| dep_id |
+--------+
| 100 |
| 100 |
| 100 |
| 100 |
| 101 |
| 101 |
| 102 |
| 102 |
| 102 |
| 102 |
+--------+
mysql> select dep_id from t1 group by dep_id;
+--------+
| dep_id |
+--------+
| 100 |
| 101 |
| 102 |
+--------+
mysql> select group_concat(name) from t1;
+-----------------------------------------------------------------+
| group_concat(name) |
+-----------------------------------------------------------------+
| jack,tom,robin,alice,tianyun,harry,emma,christine,zhuzhu,gougou |
+-----------------------------------------------------------------+
mysql> select group_concat(name) from t1 group by dep_id;
+------------------------------+
| group_concat(name) |
+------------------------------+
| jack,tom,robin,alice |
| tianyun,harry |
| emma,christine,zhuzhu,gougou |
+------------------------------+
mysql> select dep_id,group_concat(name) as name from t1 group by dep_id;
+--------+------------------------------+
| dep_id | name |
+--------+------------------------------+
| 100 | jack,tom,robin,alice |
| 101 | awei,harry |
| 102 | emma,christine,zhuzhu,gougou |
+--------+------------------------------+
#查询dep_id和name,group_concat将name进行分组连接并设置一个别名,再将他们以dep_id进行分组
配合函数一起使用:
mysql> select post,max(salary) as salary from t1 group by post;
+------------+----------+
| post | salary |
+------------+----------+
| hr | 6000.00 |
| instructor | 8000.00 |
| sale | 20000.00 |
+------------+----------+
4.使用正则表达式查询
select * from t1 where name regexp '^ali';
select * from t1 where name regexp 'wei$';
select * from t1 where name regexp 'm{2}';
对字符串匹配的方式
where name = 'tom';
where name like 'to%'; //锚定开头
where name regexp 'wei$'; //锚定结尾
5.多表查询
多表的连接查询:
1.交叉连接: 生成笛卡尔积,它不使用任何匹配条件
2.内连接: 只连接匹配的行
3.外连接之左连接: 会显示左边表内所有的值,不论在右边表内匹不匹配
外连接之右连接: 会显示右边表内所有的值,不论在左边表内匹不匹配
4.全外连接: 包含左、右两个表的全部行
建立两个测试表:
mysql> create table t2(
-> id int auto_increment primary key,
-> name varchar(30),
-> age int,
-> dep_id int
);
mysql> insert into t2(name,age,dep_id) values
-> ('tom',19,200),
-> ('jack',20,203),
-> ('alice',18,200),
-> ('robin',20,200);
mysql> create table t3(dep_id int,name varchar(30));
mysql> insert into t3 values(200 'hr'),(203 'it');
1、交叉连接
mysql> select t2.name,t3.name from t2,t3 where t2.name='tom';
+------+------+
| name | name |
+------+------+
| tom | hr |
| tom | it |
+------+------+
交叉连接就是将两表的数据进行交叉组合,显示出组合(1和1,2进行组合,结果可以是1,1 | 1,2)
mysql> select t2.name,t2.age,t2.dep_id,t3.name from t2,t3;
+-------+------+--------+------+
| name | age | dep_id | name |
+-------+------+--------+------+
| tom | 19 | 200 | hr |
| tom | 19 | 200 | it |
| jack | 20 | 203 | hr |
| jack | 20 | 203 | it |
| alice | 18 | 200 | hr |
| alice | 18 | 200 | it |
| robin | 20 | 200 | hr |
| robin | 20 | 200 | it |
+-------+------+--------+------+
2、内连接
连接条件:两表之间有一个共同的纽带,纽带名可以不一样
#只显示匹配到的行
mysql> select t2.id,t2.name,t2.age,t3.name from t2,t3 where t2.dep_id=t3.dep_id;
mysql> select id,t2.name,age,t3.name from t2,t3 where t2.dep_id=t3.dep_id;
#两表字段名不同,可以不指定表名
+----+-------+------+------+
| id | name | age | name |
+----+-------+------+------+
| 1 | tom | 19 | hr |
| 2 | jack | 20 | it |
| 3 | alice | 18 | hr |
| 4 | robin | 20 | hr |
+----+-------+------+------+
实战
现有三张表内容如下:
mysql> select * from student;
+--------+--------+
| stu_id | name |
+--------+--------+
| 1001 | 张三 |
| 1002 | 李四 |
| 1003 | 王五 |
+--------+--------+
mysql> select * from scores;
+--------+---------+--------+
| stu_id | cour_id | scores |
+--------+---------+--------+
| 1001 | 1 | 67 |
| 1001 | 2 | 84 |
| 1001 | 3 | 56 |
| 1002 | 1 | 88 |
| 1002 | 2 | 72 |
| 1002 | 3 | 70 |
| 1003 | 1 | 82 |
| 1003 | 2 | 68 |
| 1003 | 3 | 77 |
+--------+---------+--------+
mysql> select * from course;
+---------+-----------+
| cour_id | cour_name |
+---------+-----------+
| 1 | 语文 |
| 2 | 数学 |
| 3 | 英语 |
+---------+-----------+
1)查询不及格学生的信息,要求显示学号、姓名、科目、分数
mysql> select student.stu_id,name,cour_name,scores
> from student,scores,course
> where student.stu_id=scores.stu_id
> and course.cour_id=scores.cour_id
> and scores<60;
2)查询出总分第一名考的最好的课程分数,要求显示学生学号、姓名、最高分科目名称,分数和总分
mysql> select student.stu_id,name,cour_name,max(scores),
(select sum(scores) from scores group by stu_id order by
sum(scores) desc limit 1) as sum,
> course.cour_name,max(scores),name,scores.stu_id
> from course,student,scores
> where course.cour_id=scores.cour_id
> and student.stu_id=scores.stu_id
> group by name,scores.stu_id,course.cour_name
> order by sum(scores) desc limit 1;
+------+-----------+-------------+--------+--------+
| sum | cour_name | max(scores) | name | stu_id |
+------+-----------+-------------+--------+--------+
| 230 | 语文 | 88 | 李四 | 1002 |
+------+-----------+-------------+--------+--------+
3、外连接
外连接语法:
select 字段列表 from 表1 left|right join 表2 on 表1.字段 = 表2.字段;
左连接:
mysql> insert into t2(name,age,dep_id) values('awei',18,204);
mysql> insert into t3(name,dep_id) values('sale',206);
为了显示出效果,插入两条内连接无法匹配的数据
mysql> select id,t2.name,age,t3.name
-> from t2 left join t3 on t2.dep_id=t3.dep_id;
+----+-------+------+------+
| id | name | age | name |
+----+-------+------+------+
| 1 | tom | 19 | hr |
| 3 | alice | 18 | hr |
| 4 | robin | 20 | hr |
| 2 | jack | 20 | it |
| 5 | awei | 18 | NULL |
+----+-------+------+------+
显示出左边表所有数据,不管纽带和右表之间是否能够正确匹配,无法匹配显示为null
右连接:
mysql> select id,t2.name,age,t3.name
-> from t2 right join t3 on t2.dep_id=t3.dep_id;
+------+-------+------+------+
| id | name | age | name |
+------+-------+------+------+
| 1 | tom | 19 | hr |
| 2 | jack | 20 | it |
| 3 | alice | 18 | hr |
| 4 | robin | 20 | hr |
| NULL | NULL | NULL | sale |
+------+-------+------+------+
显示出右表中所有数据,不管是否能和左表正确匹配,无法匹配显示为null
4.全外连接
全部匹配一遍,t2表中的一条记录与t3表中全部记录匹配一次,将t2表中记录循环完结束;
mysql> select * from t2 full join t3;
+----+-------+------+--------+--------+------+
| id | name | age | dep_id | dep_id | name |
+----+-------+------+--------+--------+------+
| 1 | tom | 19 | 200 | 200 | hr |
| 1 | tom | 19 | 200 | 203 | it |
| 1 | tom | 19 | 200 | 206 | sale |
| 2 | jack | 20 | 203 | 200 | hr |
| 2 | jack | 20 | 203 | 203 | it |
| 2 | jack | 20 | 203 | 206 | sale |
| 3 | alice | 18 | 200 | 200 | hr |
| 3 | alice | 18 | 200 | 203 | it |
| 3 | alice | 18 | 200 | 206 | sale |
| 4 | robin | 20 | 200 | 200 | hr |
| 4 | robin | 20 | 200 | 203 | it |
| 4 | robin | 20 | 200 | 206 | sale |
| 5 | awei | 18 | 204 | 200 | hr |
| 5 | awei | 18 | 204 | 203 | it |
| 5 | awei | 18 | 204 | 206 | sale |
+----+-------+------+--------+--------+------+
复合条件连接查询
查询出年龄大于18,且连同所属部门,id,name,age 一同显示
mysql> select id,t2.name,age,t3.name as dept_name
-> from t2,t3 where t2.dep_id=t3.dep_id and age>18;
+----+-------+------+------+
| id | name | age | name |
+----+-------+------+------+
| 1 | tom | 19 | hr |
| 4 | robin | 20 | hr |
| 2 | jack | 20 | it |
+----+-------+------+------+
mysql> select id,t2.name,age,t3.name as dept_name
-> from t2,t3 where t2.dep_id=t3.dep_id order by age;
+----+-------+------+-----------+
| id | name | age | dept_name |
+----+-------+------+-----------+
| 3 | alice | 18 | hr |
| 1 | tom | 19 | hr |
| 4 | robin | 20 | hr |
| 2 | jack | 20 | it |
+----+-------+------+-----------+
子查询
mysql> select * from t2 where dep_id in (select dep_id from t3);
+----+-------+------+--------+
| id | name | age | dep_id |
+----+-------+------+--------+
| 1 | tom | 19 | 200 |
| 2 | jack | 20 | 203 |
| 3 | alice | 18 | 200 |
| 4 | robin | 20 | 200 |
+----+-------+------+--------+
mysql> select * from t2 where dep_id in (select dep_id from t3 where age>18);
+----+-------+------+--------+
| id | name | age | dep_id |
+----+-------+------+--------+
| 1 | tom | 19 | 200 |
| 4 | robin | 20 | 200 |
| 2 | jack | 20 | 203 |
+----+-------+------+--------+