首先,进入mysql:mysql -h 123.345.789(随便写的) -u wicleqian -p
回车,输入密码.
show databases; create database test;//建数据库 //删除数据库:drop database dbname;请慎重删除!!! use test //然后建表 create table emp(ename(varchar(10),hiredate,sal decimal(10,2),deptno int(2)); desc emp;//查看表的定义
show create table emp \G;//更详细的信息 //删除表:drop table emp;慎用!
//修改表的类型
alter table emp modify ename varchar(20);
//添加字段
alter table emp add column age int(3);
//新加的字段加在ename后面
alter table emp add column birth date after ename;
//把age放在最前面
alter table emp modify age int(3) first;
//字段改名
alter table emp change age age1 int(4);
//删除字段
alter table emp drop age;
//更改表名
alter table emp rename emp1;
DML语句
(insert update delete select)
//插入
insert into emp(ename,hiredate,sal,deptno) values('zzx1','2000-01-01','2000',1);
insert into emp values('lisa','2003-02-01','3000',2);
//含可空字段,非空但是含有默认值的字段,自增字段,可以不用在insert后的字段列表里出现,没写的字段自动设置为NULL,默认值,自增的下一个数字。
insert into emp(ename,dal) values('dony',1000);
insert into dept values(5,'dept5'),(6'dept6');
//更新
update emp set sal=4000 where ename='lisa';
select * from emp;
select * from dept;
//同时更新多个表中的数据
update emp a,dept b set a.sal=a.sal*b.deptno,b.deptname=a.ename where a.deptno=b.deptno;
//查询
select * from tablename [where condition];
//查询不重复的记录
select distinct deptno from emp;
//条件查询
select * from emp where deptno=1;
select * from emp where deptno=1 and sal<3000;
//排序和限制
select * from tablename [where condition] [order by field1[desc/asc],field2[desc/asc],...];
//如果不写关键字,默认是生序排列(asc),如果只排序一个字段,则这些字段相同的记录将会无序排列
select * from emp order by sal;
select * from emp order by deptno,sal desc;
//对于排序后的记录,只希望显示一部分:select ...[limit offset_start,row_count];默认起始偏移量为0,只需写想要显示的记录即可。
select * from emp order by sal limit 3;//前3条记录
limit经常和order by配合使用来进行记录的分页显示。
聚合操作
select [field1,field2,...,fieldn]fun_name
from tablename
[where where_condition]
[group by field1,field2,...fieldn
[with rollup]]
[having where_condition]
fun_name:聚合操作,如sum,count(*)记录数,max,min
group by:要分类聚合的字段
with rollup:是否对分类聚合后的结果进行再汇总
having:表示对分类后的结果再进行条件的过滤
having 和 where 的区别在于,having是对聚合后的结果进行条件的过滤,而where是在聚合前就对记录进行过滤,如果逻辑允许,尽可能先用where,因为结果集减小,将大大提高聚合效率,最后再根据逻辑看是否用having进行再过滤。
//统计公司的总人数
select count(1) from emp;
//在此基础上,统计各个部门的人数
select deptno,count(1) from emp group by deptno;
//既要统计各部门人数,又要统计总人数
select deptno,count(1) from emp group by deptno with rollup;
//统计公司所有员工的最高薪水和最低薪水
select sum(sal),max(sal),min(sal) from emp;
表连接
同时显示多张表时,使用表连接
- 内连接(最常用):仅选出两张表中互相匹配的记录
- 外连接:选出其他不匹配的记录
-
//查询所有雇员的名字(在emp中)和所在部门名称(dept) select * from emp; select * from dept; select ename,deptname from emp,dept where emp.deptno=dept.deptno;
外连接
- 左连接 :包含所有的左边表中的记录甚至是右边表中没有和它匹配的记录
- 右连接:包含所有的右边表中的记录甚至是左边表中没有和它匹配的记录
-
//查询emp中所有用户名和所在部门名称: select * from emp; select * from dept; select ename,deptname from emp left join dept on emp.deptno=dept.deptno;
列出了所有的用户名,即使wicle并不存在合法的部门名称。
左连接可以转化为右连接:select ename,deptname from dept right join emp on dept.deptno=emp.deptno;//一样的
(6)子查询
当进行查询的时候,需要的条件是另外一个select语句的结果,这是,就要用到子查询。子查询关键字包括:in ,not in,= ,!=,exists,not exists等。
//从emp表中查询出所有部门在dept表中的所有记录 select * from emp; select * from dept; select * from emp where deptno in (select deptno from dept);
//如果查询记录数唯一,还可以用=代替in: select from emp where deptno = (select deptno from dept limit 1); //某些情况下,子查询可以转化为表连接 select * from emp where deptno in(select deptno from dept); = select emp.* from emp,dept where emp.deptno=dept.deptno; //表连接在很多情况下用于优化子查询
7.记录联合
将两个表的数据按照一定的查询条件查询出来后,将结果合并到一起显示出来,这时,需要用到union和union all
select * from t1 union/union all select * from t2 ... union/union all select * from tn;
union和union all 的主要区别是:union all 是把结果集直接合并在一起,而union是将union all后的结果进行一次distinct,去除重复后的记录。
//将emp和dept表中的部门编号的集合显示出来 select * from emp; select * from dept; select deptno from emp union all select deptno from dept; select deptno from emp uinon select deptno from dept;
union all:
union:
DCL语句
DCL: DBA用来管理系统中的对象权限时使用。
//创建一个数据库用户z1,既有对test数据库中所有表的select/insert权限: grant select,insert on test.* to 'z1'@'localhost' identified by '123'; exit //用z1登陆: mysql -h loacalhost -u z1 -p123; use test select * from emp; //管理员收回z1的insert权限 revoke insert on test.* from 'z1'@'localhost';
帮助的使用
? contents//显示所有可供查询的分类 ? data types ? int ? show ? create table ...
查询元数组信息
- 删除数据库test下所有前缀为tmp的表
- 将数据可test下所有存储引擎为Myisam的表改为innoDB
-
mysql5.0之后提供了一个新的数据库information_schema,用来记录Mysql中的元数据信息。
元数据:数据的数据,比如表名,列名,列类型,索引名等。
information_schema是一个虚拟数据库,物理上并不存在相关的目录和文件,show tables;显示的表也不是实际的表,而全部是视图