mysql语法(一)

SQL的命令类别:

    数据定义语言  DDL : Data Definition Language;  creat(创建) alter(修改) drop(删除)

    数据操纵语言  DML : Data Manipulation Language;  insert(插入) select(查询) update(修改) delete(删除)

    事务控制语言  TCL : Task Control Language;  commit(提交) rollback(回滚)

    数据控制语言  Data Control Language;  grant(授权) revoke(取消授权)

详细用法及代码实现:

    create table test01(id int,name varchar(255)) -- create table 表名

    alter table test01 ADD(sal int) -- alter table + add 添加列

    alter table test01 drop [column] name -- alter table + drop [column] 列名

    alter table stu add lil varchar(100) first; -- 在最前面(第一列)插入

    alter table stu add llll varchar(100) after id; -- 插入在指定列名之后

    alter table stu add(num int(10),str varchar(100)); -- 插入多列

    blob.png

    drop table test01 -- drop table 删除表

    alter table test1 modify column address varchar(50) after tel; -- 重新定义列并改变列顺序

    blob.png

    alter table test1 change stunum number varchar(100) after tel; -- 修改列名称和定义并改变顺序

    blob.png

    alter table test1 rename test01; -- alter + rename  一个数据表更名

    rename table 表名1 to 新表名1[,表名2 to 新表名2]... -- rename to , to ...  多个数据表更名

    insert into test01 values(1,9000,'Tom','US',138000) -- insert into 表名称 + values (值1, 值2,....) 向表格中添加新的行

    insert into test01 (id) value (1) -- insert into + value 指定要插入数据的列

    insert into test01 (id,name) values (3,'Jery') -- insert into table_name (列1, 列2,...) + values (值1, 值2,....) 指定要插入数据的列

    update test01 set id=4 where id=3 -- UPDATE 表名称 SET 列名称 = 新值 WHERE 列名称 = 某值 修改表中的数据

    update test01 set sal =11000 where sal is null -- 修改一个指定字段值

    update test01 set address='China',tel=186000 where id=6; -- 修改多个指定字段值(注意:同时修改多个列的值的时候,满足where条件的列必须在同一行才能够修改成功)

    delete from test01 where name is null -- detete from + where 删除(指定)行

    select 查询信息

    select * from emp -- 查询所有员工信息

    select * from dept; -- 查询所有部门信息

    select * from salgrade; -- 查询所有薪资等级信息

    select empno,ename,deptno from emp; -- 仅查询员工编号、员工姓名、部门编号信息

    select ename,sal*12 from emp; -- 查询员工姓名和他们的年薪

    select sal*12 as 年薪 from emp; -- 查询员工年薪,并起别名为年薪  也可忽略as,写成:select sal*12 年薪 from emp;

    select 2*4 from emp -- 简单的数学运算,但会出现冗余

    select 2*4 from dual -- 简单的数学运算,可消除冗余  dual是一个虚拟的表

    select distinct 2*4 from emp; -- 另一个可消除冗余的数学运算

    select sysdate() from dual; -- 获得当前日期时间  2016-08-15 14:53:13

    select now() from dual; -- 获得当前日期时间  2016-08-15 14:53:58

    比较操作符:between and;in;like;is null;=;>;<;>=;<=;<>;     逻辑操作符:and;or;not;

    select e.ename,e.sal from emp e where e.sal between 2000 and 3000; -- 查询工资在2000-3000内的员工姓名和工资

    select e.ename,e.sal from emp e where e.sal>=2000 and e.sal<=3000 or e.sal=800; -- 查询工资在2000-3000内的员工姓名和工资

    select e.ename,e.job,e.sal from emp e where e.sal in(800,1500,3000); -- 查询工资是800、1500或3000的员工姓名,职位和工资

    select e.ename,e.job,e.sal from emp e where e.sal not in(800,1500,3000); -- 查询工资是不是800、1500和3000的员工姓名,职位和工资

    select e.ename,e.sal from emp e where e.ename like 'A%'; -- 查询名字首字母为'A'的员工的姓名和...

    select e.ename,e.job from emp e where e.ename like '_A%' -- 查询名字第二个字母为'A'的员工的姓名和...

    select e.ename,e.job from emp e where e.ename like '%A%' -- 查询名字中包含字母'A'的员工的姓名和...

    百分号%:0-n   下划线_:1-1   like '%\%%' 默认转义字符\   like '%$%%' escape '$' 指定转义字符

    select e.ename,e.hiredate from emp e where e.comm is null; -- 查询没有奖金的员工的姓名和...

    select e.ename,e.hiredate from emp e where e.comm=0; -- 查询没有奖金的员工的姓名和...

    select ename from emp where ename>'FORD'; -- 查询排在“FORD”之后的员工信息

    select e.ename,e.hiredate from emp e where e.hiredate>'1981-03-22'; -- 查询在1981年3月22日之后入职的员工的姓名和入职时间

    select deptno from emp; -- 查询部门编号,出现冗余

    select distinct deptno from emp; -- 查询部门编号,消除冗余

    select distinct deptno,job from emp; -- 同时查询部门编号与岗位,消除冗余

    排序问题:  ORDER BY子句跟在SELECT 语句之后    ASC: 升序, 缺省值、DESC: 降序

    select * from emp order by hiredate; -- 按照时间排列(默认升序)

    select * from emp order by sal; -- 按照薪资排列(默认升序)

    select * from emp order by sal asc; -- 按照薪资升序排列;

    select * from emp order by sal desc; -- 按照薪资降序排列;

    select * from emp order by deptno asc,sal desc; -- 先按照部门升序排序,部门相同的员工按照工资降序排序

    select * from emp order by job asc,sal desc; -- 先按照职位升序排序,职位相同的员工按照工资降序排序

集合操作符

     select * from emp where(deptno=10 and job='manager')or(deptno=20 and job='clerk')-- 查找所有10部门的经理和 20部门的办事员

union 联合两个结果集(去除掉重复记录)

    select * from emp where(deptno=10 and job='manager') union select * from emp where(deptno=20 and job='clerk');

union all 联合两个结果集(不去除掉重复记录)

    select * from emp where(deptno=10 and job='manager') union all select * from emp where(deptno=20 and job='clerk');

聚合函数  select  分组字段,聚合函数... from … where …

    select sum(sal) from emp; -- 查找公司的工资总和

    select count(*) from emp; -- 查找公司的总人数

    select avg(sal) from emp; -- 查找公司的平均工资

    select max(sal) from emp; -- 查找公司的最高工资

    select min(sal) from emp; -- 查找公司的最低工资

    select deptno,sum(sal) from emp group by deptno; -- 查看每个部门的总工资

    select * from emp group by deptno,ename; -- 分别查看每个部门的员工的所有信息

    select deptno,max(sal) from emp group by deptno; -- 求各个部门的最高薪水('子查询'中有加强版

    select * from emp where ename not like '_A%' and sal>800 order by sal*12 desc; -- 查询姓名第二个字母不是”A”且薪水大于800元的员工信息,按年薪降序排列

    select deptno,avg(sal) from emp where sal>1200 group by deptno having avg(sal)>1500 order by avg(sal) desc; -- 将员工薪水大于1200且部门平均薪水大于1500的部门编号和平均薪水列出来,按部门平均薪水降序排列

Having和where的区别

     having用来筛选聚合之后的结果集

     where用来筛选表中的记录

    select deptno,avg(sal) from emp group by deptno having avg(sal)>2000; -- 查看平均工资超过2000的部门的编号和平均工资

    select sum(sal) from emp where sal>2000 group by deptno having sum(sal)>5000 order by sum(sal) desc;-- 查找每个部门工资高于2000的员工的总工资,要求只显示总工资高于5000的,按总工资降序排列

查询语法总结:

    select [distinct] {*| column [alias], ...} from table [where condition(s)] [group by column(s)][having condition(s)][order by {column, expr, alias} [asc|desc]]; (where-group-having-order)

    select sal from emp where sal>2000 limit 3; -- TOP子句用于规定要返回的记录的数目  限制数据长度为定值之内


下接:  mysql语法(二)


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

cpongo11

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值