《深刻浅出mysql》读书笔记

一、mysql安装

二、SQL基础

DDL、DML、DCL
(1)数据库mysql

mysql -uroot -p;
create database test1;
show databases;
use test1;
drop database test1;

(2)表sql

//建立表
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;

三、数据类型

主要分为:数值类型、字符串类型、日期类型和时间类型code

相关文章
相关标签/搜索