Oracle(一)

322 阅读3分钟

-- Oracle中的建表语句 -- Oracle中的主要数据类型 -- 1.字符型 -- varchar(10) 定长的字符型数据 -- char(2) 字符量较少时(定长) -- varchar2(20) 变长的字符型数据 -- 2.数值型 -- number(4) 不带小数点的数值 -- number(8,2) 数据的长度是8位,小数点后占俩未 -- 3.日期型 create table users( id number(4) primary KEY, name varchar(10), password varchar2(10), sex char(2), addr varchar2(20), birthday date, sal number(8,2) );

-- 删除表的语句 drop table users;

-- 查询表中的数据 select * from users;

-- 查询表的结构:只能在命令行模式下使用 desc users;

-- sql语句是不区分大小写的,包括登录是的账号和密码 selECT * FroM EmP;

-- select语句的使用 --从表中查询部分的字段 select empno,ename,sal,comm from emp;

-- 在查询过程中,对于数值型的数据可以执行 +,-,,/运算 -- 可以给查询字段取别名,又不同的方式 select empno 员工编号,ename 员工姓名,sal 月薪,sal12 年薪 from emp;

-- 在算数运算中出现null,则结果为null,在数值类型中null不等于0.0; ()可以改变运算顺序 并影响结果 select empno 员工编号,ename as "员工姓名",sal "月薪",sal12 年薪,comm "奖金",(sal12 + comm) 年收入 from emp;

-- 用||可以将俩列或者多列合并起来 select empno,ename,job,empno || ename,empno || ename || job as 员工信息 from emp;

-- 在连接表达式中出现字符数据,字符型的数据必须要使用 '' -- 在连接表达式中出现null,结果就是原来的字符型数据 -- 任何类型都支持空值null select empno || '的经理是' || MGR "从属关系" from emp;

-- 对于日期型的数据可以进行 +,- 运算 ,+ - 就是+ - 一个天数 select empno,ename, hiredate "雇佣日期" ,(hiredate + 90 ) "转正日期" from emp;

select empno,ename, hiredate "转正日期" ,(hiredate - 90 ) "雇佣日期" from emp;

-- 俩个日期型的数据相减,得到的是俩个日期直接相差的天数 -- sysdate取得当前的天数 select empno 员工编号,ename 员工姓名,(sysdate - hiredate)/365 "工作年限" from emp;

-- 关键字:DISTINCT(distinct)去除单列重复的数据 DEPTNO:部门(外键) select deptno from emp;

select distinct deptno from emp;

-- 去除多列重复的数据 select JOB 职位, DEPTNO 部门 from emp;

select distinct JOB,DEPTNO from emp;

-- 使用where语句来对数据进行过滤 select * from emp where deptno = 10;

-- 对于字符型的数据和日期型的数据必须要使用 '' ; 字符型的数据是严格区分大小写的 select * from emp where ename = 'SMITH';

-- 对于日期型的数据格式是严格的,是 DD-MM-YYYY(年-月-日) select * from emp where emp.hiredate = '17-12月-1980';

-- 可以改变当前会话的日期格式 alert session set nls_date_format = "YYYY-MM-DD HH:MI:SS"

-- 在查询语句中使用其他的比较运算符 select * from emp where sal >= 2000;

select * from emp where sal != 2000; -- 不等于

select * from emp where sal <> 2000; -- 不等于

-- 使用 between ... and... select * from emp where sal between 1000 and 3000;

-- 在Oracle中用 AND 来执行 &&(与) 的功能 select * from emp where sal >= 1000 and SAL <= 3000;

-- 使用IN select * from emp where JOB IN ('SALESMAN','MANAGER');

-- 在Oracle中用 OR 来执行 ||(或) 的功能 select * from emp where JOB = 'SALESMAN' OR Job = 'MANAGER';

-- 使用 NOT 运算符来表示 非 select * from emp where not sal >= 1000 and SAL <= 3000;

select * from emp where comm is not null;

-- 关键字 LIKE ,用于字符型数据的查询,可以执行模糊查询 -- % 表示 0 个或者多个字符 select * from emp where ENAME LIKE '%A%';

select * from emp where ENAME LIKE 'A%';

-- _ 表示一个字符 select * from emp where ENAME LIKE '_A%';

-- \ 表示转义符的意思 select * from emp where ENAME LIKE '%%%' escape '';

-- 对于空值要使用 IS NULL 进行比较 select * from emp where comm is null;

select * from emp where comm is not null;

-- 运算顺序是按照 先 NOT 再 AND 后 OR 的顺序 select * from users where name = 'admin' and password = '123456';

-- sql注入攻击 admin' OR 'X' = 'X select * from users where name = 'admin' OR 'X' = 'X' and password = '456789'; --相当于是:where name = 'admin' OR ('X' = 'X' and password = '456789');

-- 对查询的数据进行排序,使用 ORDER BY 子句 ;默认升序排列 加不加 ASC 默认都是升序排列 加 DESC 进行降序排列 select * from emp where sal > 2000 order by sal;

-- 显示的时候默认按照指定的字段升序排列 select * from emp order by sal;

-- 显示的时候用指定的字段升序排列
select * from emp order by sal ASC;

-- 显示的时候用指定的字段降序排列 关键字:DESC select * from emp order by sal desc;

-- 可以按照多个字段进行排列
select * from emp order by deptno,sal;

-- 可以使用字段的别名进行排序 select empno,ename,sal,(sal*12) "年薪" from emp order by "年薪";