更多 SQL 信息:可以查看w3c SQL 标准文档
DQL:
首先来搭建测试环境:
mysql -uroot -proot
show databases;
drop database test;
create database test CHARACTER SET 'utf8';
use test;
drop table if exists empl;
create table empl(
id int primary key auto_increment,
name varchar(50),
department_id int,
hire DATE,
phone CHAR(20),
job VARCHAR(50),
comm decimal(8,2),
sal float(8,2),
CONSTRAINT fk_empl_dept FOREIGN KEY (department_id) REFERENCES department (id)
)ENGINE=InnoDB DEFAULT CHARSET=utf8;
create table department(
id int primary key auto_increment,
dname VARCHAR(50)
)default charset=utf8;
alter table empl rename to 新名字; // 重命名table
alter table empl modify job varchar(40); //修改字段类型
alter table empl add(note varchar(200)) //添加属性
alter table empl drop note //删除属性
删除主键
首先删除自增长:
ALTER TABLE empl MODIFY COLUMN id INTEGER(11) NOT NULL;
然后才能删除主键
ALTER TABLE empl DROP INDEX PRIMARY;
添加主键
ALTER TABLE `user` MODIFY COLUMN `id` INT AUTO_INCREMENT PRIMARY KEY;
添加外键:
ALTER TABLE `empl` ADD CONSTRAINT `fk_empl_dept` FOREIGN KEY
(`department_id`) REFERENCES `department` (`id`);
插入数据
insert into department values (10,'蜀国'),(20,'魏国'),(30,'吴国'),(40,'汉朝');
insert into empl VALUES(0,'刘备','10','2000-12-22',123456789,'主公',8000.00);
insert into empl VALUES(0,'诸葛亮','10','2002-7-12',123499689,'军师',20000.00);
insert into empl VALUES(0,'张飞','10','2001-10-2',123422229,'将军',16000.00);
insert into empl VALUES(0,'关于','10','2002-7-12',123499989,'将军',8700.00);
insert into empl VALUES(0,'赵云','10','2003-4-18',123433389,'将军',8400.00);
insert into empl VALUES(0,'马超','10','2003-4-18',123433389,'将军',8400.00);
insert into empl VALUES(0,'黄忠','10','2003-4-18',123433389,'将军',8400.00);
insert into empl VALUES(0,'姜维','10','2003-4-18',123433389,'将军',8400.00);
insert into empl VALUES(0,'曹操','20','2003-2-10',123455789,'主公',1000.00);
insert into empl VALUES(0,'郭嘉','20','2001-8-17',123458889,'军师',6000.00);
insert into empl VALUES(0,'司马懿','20','2003-2-10',123455789,'军师',10000.00);
insert into empl VALUES(0,'荀彧','20','2003-2-10',123455789,'军师',10000.00);
insert into empl VALUES(0,'夏侯渊','20','2002-4-11',123453789,'将军',8000.00);
insert into empl VALUES(0,'夏侯敦','20','2003-2-10',123455789,'将军',19000.00);
insert into empl VALUES(0,'张辽','20','2003-2-10',123455789,'将军',10000.00);
insert into empl VALUES(0,'典韦 ','20','2002-4-11',123453789,'将军',8000.00);
insert into empl VALUES(0,'许褚','20','2003-2-10',123455789,'将军',19000.00);
insert into empl VALUES(0,'孙权','30','2003-2-10',123455789,'主公',1000.00);
insert into empl VALUES(0,'周瑜','30','2001-8-17',123458889,'军师',6000.00);
insert into empl VALUES(0,'鲁肃','30','2003-2-10',123455789,'军师',19000.00);
insert into empl VALUES(0,'陆逊','30','2002-4-11',123453789,'军师',8000.00);
insert into empl VALUES(0,'黄盖','30','2002-4-11',123453789,'将军',8000.00);
insert into empl VALUES(0,'吕蒙','30','2003-2-10',123455789,'将军',19000.00);
insert into empl VALUES(0,'太史慈','30','2003-2-10',123455789,'将军',10000.00);
insert into empl VALUES(0,'甘宁','30','2001-8-17',123458889,'将军',6000.00);
添加列:
添加奖级列
alter table empl add(comm decimal(9,2));
插入奖级随机值
update empl set sal=round(rand()*10000,-3), comm=round(rand()*10000,-3),
phone=(28000000+round(RAND()*100000,0))
where 1=1;
注解:round 四舍五 -3 代表 小数点后 -3 位。
rand 0~1 随机数
添加 管理列并赋值:()
ALTER TABLE `empl` ADD(mgr int);
用子查询 更新语句。
update empl e,(select * from empl where job="主公") d set e.mgr=d.id
where e.department_id=d.department_id and e.job!="主公";
删除语句:
delete from empl where id=8;
修改:
update empl set job='总助',sal=20001 where name='郭嘉';
函数:
去重:distinct
例如:select distinct job from empl;
数据拼接: concat
案例:select concat('我叫',name,',我的工作是',job) as '描述' from empl;
替换:如果空就用指定值代替:
select *,IFNULL(manager,'总经理') from empl;
模糊查询: ‘_’ 代替一个字符 ‘%’匹配 0~N 个字符
select * from empl where name like '夏__';
select * from empl where name like '%侯%';
排序:Order by 升序列 asc 降序 desc (默认是 asc升序)
select * from empl order by sal desc;
select * from empl where manager is not null order by sal desc,hire desc,job desc;
总数:count(*) 如果是空就不 加入累计 例如 manager 里有两个null
select concat('华夏公司一共',count(*) ,'人,不是经理的有:'
,count(manager) ) '简介' from empl;
平均值 avg() 最大 max() 最小 min() 求和 sum(sal);
无法计算的以默认用 0 代替 例如 汉字
select sum(sal) '总和',avg(sal) '平均',max(sal) '最高',min(sal) '最低' from empl;
分组查询
group by 分组字段
计算每个部门员工个数。
select job,count(*),max(sal) from empl group by job;
计算每个部门工资大于12000的员工个数。
select job,count(*),max(sal) from empl where sal>12000 group by job;
having
以前面查询的结果做条件来判断。
找出 各个部门员工工资大于12000的 并且在2人以上的部门。
select job,count(*),max(sal) from empl where sal>12000 group by job having count(*)>=2;
内链接:
inner
外链接:
左外链接:left outer join 以左边的表为主,右边为空,用null填充
select d.dname, e.name from
department d LEFT OUTER JOIN empl e
on e.department_id=d.id
右外链接:right outer join 以右边的表为主,左边为空,用null填充
select d.dname, e.name from
department d right OUTER JOIN empl e
on e.department_id=d.id
全外链接 中间用 union 链接。
(作用,如果左右两边,谁多用谁)
select d.dname, e.name from
department d LEFT OUTER JOIN empl e
on e.department_id=d.id
union
select d.dname, e.name from
department d right OUTER JOIN empl e
on e.department_id=d.id
子查询:
子查询可以出现的位置如下:
select(子查询 非标准) from(子查询) where(子查询)
例如:
from(子查询):
select e.* from (select max(sal) sal from empl) es,empl e where e.sal = es.sal;
where(子查询):
select * from empl where sal=(select max(sal) from empl);
分页:
limit 开始位置 , 取多少条;
select * from empl limit 0,5;
select * from empl limit 5,5;
select * from empl limit 10,5;
函数:
表插入表:select * into
创建索引 CREATE INDEX
条件检查 check