1.SQL简介
SQL(Structured Query Language):结构化查询语言
Oracle才用C/S模式,客户端发起请求,通过网络传递给服务器,服务器对请求进行响应,
通过网络将结果返回给客户端,最终在客户端呈现结果。
2.SQL组成
(1).DDL(Data Definition Language):create alter drop,主要针对数据库对象的操作
(2).DML(Data Manipulation Language):insert delete update select,主要针对表中记录(数据)的操作
*insert
--创建一张emp01表,复制emp表的结构
create table emp01 as select * from emp where 1=2;
--给全部字段插入值
insert into emp01 values(1001,'shelly','MANAGER',7839,date'1989-12-08',3000,null,10);
--date声明某个字符串是一个日期:date'1989-12-08'
--给其中几个字段插入值,没有显示的字段默认null
insert into emp01(empno,ename,sal,job,deptno) values(1002,'ellen',4000,'salesman',20);
--往emp01表中插入一条ellen的记录,其内容跟emp01表一致,empno在原来的基础上加1000
insert into emp01(empno,ename,job,sal,deptno)
select empno+1000,ename,job,sal,deptno from emp01 where ename='ellen';
insert into emp01(empno,ename,job,sal,deptno)
select 3001,'LILY','ANALYST',3000,30 from dual;
--等价于
insert into emp01(empno,ename,job,sal,deptno) values
(3001,'LILY','ANALYST',3000,30);
--一条insert语句插入多条记录
insert into emp01(emp,ename,sal,job)
select 9999,'LILEI',3500,'MANAGER' from dual union
select 9998,'Lucy',4500,'MANAGER' from dual union
select 9997,'hanmeimei',5500,'MANAGER' from dual
--union:Oracle的集合运算并运算,求并集,使用的时候会去重
--union all:求并集运算,不去重
select 9999,'LILEI',3500,'MANAGER' from dual union
select 9999,'LILEI',3500,'MANAGER' from dual union
select 9999,'LILEI',3500,'MANAGER' from dual
select 9999,'LILEI',3500,'MANAGER' from dual union all
select 9999,'LILEI',3500,'MANAGER' from dual union all
select 9999,'LILEI',3500,'MANAGER' from dual
*delete
--删除表中所有记录
delete from emp01;
--删除表emp01中部门编号为10的员工信息
delete from emp01 where deptno=10;
*update
--更新emp01表中员工工资,如果工资小于1500,那么给员工涨1000
update emp01 set sal=sal+1000 where sal<1500;
*select
--查询表中所有记录
select * from emp01;
select empno,ename,job,mgr,hiredate,sal,comm,deptno from emp01;
(3).DCL(Data Control Language):grant revoke,控制存取权限
(4).TCL(Transcation Control Language):commit rollback savepoint,事务控制语言主要是针对DML操作的
--创建保存点
savepoint mark1
update emp01 set sal=sal+500 where comm is null;
rollback to mark1;--回滚到mark1保存点
commit;--提交
Oracle的运算符
*算数运算符:+ - * /
update emp01 set sal=sal+500 where comm is null;
*连接运算符:||
select ‘Dear ’||ename as 姓名 from emp01;
--查询每个员工的工资
select ename||'的工资是:'||sal from emp01;
*比较运算符:> >= < <= <>(!=) = is null between...and... in like
--查询工资大于3000的员工的信息
select * from emp01 where sal>3000;
--查询工资小于2000的员工的信息
select * from emp01 where sal<2000;
--查询部门编号不为20的员工的信息
select * from emp01 where deptno<>20;
select * from emp01 where deptno!=20;
--is null用来做空值的比较
--查询奖金为空的员工的信息
select * from emp01 where comm is null;
--查询奖金不为空的员工的信息
select * from emp01 where comm is null;
--between...and...
--查询工资在2000到5000之间的员工的信息
select * from emp where sal between 2000 and 5000;
--等价于
select * from emp where sal>=2000 and sal<=5000;
--in:后面加的是一个集合
--查询部门10或部门20中的员工信息
select * from emp where deptno in(10,20);
select * from emp where deptno=10 or deptno=20;
--like
--通配符
--_:表示匹配任意一个字符
--%:表示匹配0个或多个字符
--[]:表示匹配[]中任意一个字符
--查询姓名中带有LLEN的员工信息
select * from emp where ename like '%ELLEN%';
--查询姓名以A开头,以N结束的员工的信息
select * from emp where ename like 'A%N';
--查询员工姓名长度为5的员工的信息
select * from emp where ename like '_____';
*逻辑运算符
--查询表emp中部门10中的MANAGER或者部门20中的CLERK,显示其详细信息
select * from emp where deptno=10 and job='MANAGER'or deptno=20 and job='CLERK';
--查询部门10或者部门20中的MANAGER
select * from emp where (deptno=10 or deptno=20) and job='MANAGER';
select * from emp where deptno in(10,20) and job='MANAGER';
--运算符优先级:
--算数运算符>连接运算符>比较运算符>not>and>or
--date声明某个字符串是一个日期:date'1989-12-08'
insert into emp01 values(1001,'shelly','MANAGER',7839,date'1989-12-08',3000,NULL,10);
--使用默认日期格式
insert into emp01 values(1001,'shelly','MANAGER',7839,'08-12月-1989',3000,NULL,10);
--使用to_date将字符串转换为日期
insert into emp01 values
(1001,'shelly','MANAGER',7839,to_date('1989-12-08','yyyy-mm-dd'),3000,NULL,10);
--复制emp表的结构
create table emp01 as select * from emp where 1=2;
--复制emp表
create table emp02 as select * from emp;
--创建表emp03,复制emp表中的ename,sal,job这几列的内容
create table emp03(ename,sal,job) as select ename,sal,job from emp;
--创建表emp04,复制emp表中部门10中员工的信息
create table emp04 as select * from emp where deptnno=10;