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:表示对分类后的结果再进行条件的过滤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 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...
查询元数组信息
元数据:数据的数据,比如表名,列名,列类型,索引名等。
information_schema是一个虚拟数据库,物理上并不存在相关的目录和文件,show tables;显示的表也不是实际的表,而全部是视图
0 0
- mysql(5)初级使用
- MYSQL的初级使用
- MySql初级使用指导
- MySQL序列的使用--初级
- MySQL Workbench的使用教程 (初级入门版)
- GSEA使用(初级)
- MySql 安装及初级使用详解
- mysql初级
- mysql 初级
- sed初级使用-------shell学习(7)
- awk初级使用-------shell学习(6)
- josephus Problem 初级(使用数组)
- SEO初级篇(关键字的使用)
- 服务器nginx开启并使用(初级)
- Android Studio安装使用(初级)
- 07.Mybatis初级使用-resultMap(一)
- 08.Mybatis初级使用-resultMap(二)
- 使用IDEA配置MAVEN(初级入门)
- 《php核心技术与最佳实践》笔记4
- 网上商城—管理员删除商品
- linux编译ffmpeg2.6.2
- android剪贴板
- HDU ACM 1591 Encoded Love-letter
- mysql(5)初级使用
- c++ copy construct
- Android自定义锁屏的实现
- Shell基础-Bash基本功能-别名与快捷键
- 机房重构系统的分类
- 互联网公司Android客户端面试 知识点
- 实践1
- P123 第38题 powers sum_of_powers !!!
- 内存分配