mysql(5)初级使用

来源:互联网 发布:win10 不允许下载软件 编辑:程序博客网 时间:2024/05/22 00:08

首先,进入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,mingroup by:要分类聚合的字段with rollup:是否对分类聚合后的结果进行再汇总having:表示对分类后的结果再进行条件的过滤havingwhere 的区别在于,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.记录联合

    将两个表的数据按照一定的查询条件查询出来后,将结果合并到一起显示出来,这时,需要用到unionunion all

    select * from t1 union/union all select * from t2...union/union allselect * from tn;

    union和union all 的主要区别是:union all 是把结果集直接合并在一起,而union是将union all后的结果进行一次distinct,去除重复后的记录。

    //将emp和dept表中的部门编号的集合显示出来select * from emp;select * from dept;select deptno from empunion allselect deptno from dept;select deptno from empuinon 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 testselect * 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;显示的表也不是实际的表,而全部是视图

    0 0
    原创粉丝点击