1、mysql安装
2、SQL基础
DDL、DML、DCL
(1)数据库
mysql -uroot -p;
create database test1;
show databases;
use test1;
drop database test1;
(2)表
//创建表
create table emp(ename varchar(10), sal decimal(10,2))
//查看表定义
desc emp;
//删除表
drop table emp;
//修改表
alter table emp modify ename varchar(20);(修改字段类型)
alter table emp add column age int(3);(增加字段)
alter table emp drop column age;(删除字段)
alter table emp change age age1 int(4);(字段改名)
alter table emp add birth data after ename;(修改字段排列顺序)
alter table emp modify age int(3) first;(修改字段age,把它放在最前面)
alter table emp rename emp1;(更改表名)
(3)数据
//插入数据
insert into emp (ename, age) values ('lala', 18);
select * from emp;
//更新数据
update emp set sal=4000 where ename='lisa';
//删除数据
delete from emp where ename='dony';
//查询数据
select * from emp;
select distinct deptno from emp;(查询不重复的记录)
select * from emp where deptno=1 and sal<3000;
select * from emp order by sal limit 1,3;(排序和限制)
select deptno, count(1) from emp group by deptno;(聚合)
select deptno, count(1) from emp group by deptno having count(1)>1;
select sum(sal),max(sal),min(sal) from emp;
//表连接
//内连接仅选出两张表中互相匹配的记录,外连接会选出其他不匹配的记录
select ename, deptname from emp, dept where emp.deptno=dept.deptno
//外连接又分为左连接和右连接:
//左连接:包含所有的左边表中的记录甚至是右边表中没有和它匹配的记录
//右连接:包含所有的右边表中的记录甚至是左边表中没有和它匹配的记录
select ename, deptname from emp left join dept on emp.deptp=dept.deptno
//子查询:当进行查询时,需要的条件是另外一个select语句的结果。关键字主要包括: in, not in, =, !=, exists, not exists
select * from emp where deptno in(select deptno from dept)
//记录联合
使用union和union all关键字,使用union会把重复的数据去掉
select deptno from emp union select deptno from dept;
3、数据类型
主要分为:数值类型、字符串类型、日期类型和时间类型
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。