增删改查数据库

–插入数据

insert into emp values(123,’张si’,”,”,”);

insert into emp1(empno) values(234);

commit;

–删除数据

delete from emp where empno = 222;

commit;

–更新数据

update emp set empno=2323, ename=’zhangsan’;

update emp1 set sal=’111′ where sal=’1311′;

–查询数据

select * from emp1;–shift+home键,然后按f8执行。

select distinct ename, sal from emp1; –查询唯一(如果是两个值的话,是联合唯一。)

select empno 雇员编号, ename as 雇员名称 from emp; –为查询结果设置别名。

SELECT ‘编号是:’ || empno || ‘的雇员姓名是:’ || ename || ‘,基本工资是:’ || sal 雇员信息 FROM emp ; — 结果是这样拼接出来的。

select empno , ename, (sal+200)*12 as 年薪 from emp; –查询结果可设置函数。

select count(*) from emp;

select * from emp where empno in(‘112′,’211’);

select * from emp where empno =’23’ or empno =’222′;

select * from emp where empno like ‘%222%’;

select * from emp order by sal desc;

–left join .. on , right join ..on . 左右连接。

select t1.empno , t1.ename from emp t1 right join emp1 t2 on t1.empno=t2.empno;

–group by有一个原则,就是select后面的所有列中,没有使用聚合函数的列,必须出现在group by 后面。 先group by 分组,再聚合查询。  

–having 通常与group by 联合使用, 用来过滤由group by 语句返回的记录值。having的存在弥补了where关键字不能与聚合函数联合使用的不足。 

select name , sum(number) from test group by name;

select id, count(course) as numcourse , avg(score) as avgscore from student group by id having avg(score)>80;