数据库简单语言 使用

来源:互联网 发布:专业生物英语软件 编辑:程序博客网 时间:2024/06/18 12:55

SQL语言的分类

DQL 数据库操作语言

select

DML数据库操作语言

insert、update、delete

DDL对象操作语言(数据定义语言)

create、alter、drop

DCL权限控制语言

grant、revoke

TCL事物控制语言

save \ point \ rollback \ set transac \ tion \ commit

数据操作语言针对表中数据、数据定义语言针对数据库对象

关系:整个二维表

关系名:表格名称

元组:行数据(记录)

属性:列数据(字段)

属性名:列名称(字段名)

主键:唯一确定元组的属性组(关键字)(唯一性)

域:属性的取值范围

首先查询出来的表只是伪表只是简单的读


AS别名 起小名

--  查询表的全部信息select * from emp;--员工表select * from dept;--部门表select * from salgrade;--工资表select * from bonus; --奖金表-- 查询  员工表中的 编号 姓名 工资 信息select empno,ename,sal from emp;-- 可以在查询的的时候进行  算数运算select empno,ename,sal*12 from emp;--  查询 名字 编号 工作 工资 select ename , empno,job,sal from emp;-- as  创建 别名select empno as 员工编号,ename as 员工姓名 , sal as 工资 from emp;-- as  可以省略select empno 员工编号 ,ename 员工姓名 ,sal 工资 from emp;--  因为 默认会大写 加上双引号后 会强转小写  单引号不能随便用select empno "eo" from emp;select empno eo from emp;select empno 员工编号 from emp;-- 如果含有特殊字符 可以双引号引起来  空格之类的select empno " 员工编号" from emp;
||两直竖线  表示连接符

--  连接符   用来连接 两个 属性  编号加月薪  字符串在数据库中 加单引号select ename ||'的月薪是' || sal from emp;-- 空格多加无所谓 不能少加select ename ||'月薪是'||sal from emp;
去除重复行 distinct
-- 去除重复行  因为工资有重复 所以会去掉select distinct sal from emp;--  没有一行完全一样 不会去掉   这样就是判断sal和ename两个了select distinct sal 工资,ename 员工姓名 from emp;-- 查询所有 并去除重复行select distinct * from emp;
order by 排序
-- 按照 工资降序排列select * from emp order by sal desc;-- 按照工资排序(升序) 有重复的会按照 编号降序排select * from emp order by sal ,empno desc;-- 按小名排select sal 员工工资,ename 员工姓名 from emp order by 员工工资 desc;

where 条件查询

-- 查询 名字是scott的员工信息  只能等select * from emp where ename = 'SCOTT';-- 查询 工资是 1250的员工信息select * from emp where sal = 1250;--查询 入职日期为 1981年9月28日的员工信息  select * from emp where hiredate='28-9月-81'; select * from emp where hiredate='28-9月-1981';select * from emp where hiredate=to_date('1981-9-28','yyyy-mm-dd');-- 查询 入职日期大于 1980年1月1日的员工信息select * from emp where hiredate >'28-9月-81';-- 查询入职日期小于 1980年1月1日的select * from emp where hiredate <'1-1月-80';--查询工资大于 1250元的select * from emp where sal >1250;-- 查询工资小于1250 的select * from emp where sal <1250;-- 查询工资不等于 1250 的select * from emp where sal !=1250;--查询工资 大于1250 或者小于1250的相当于不等于1250select * from emp where sal <1250 or sal >1250;-- 查询工资 不等于1250的 <>  相当与 不等于select * from emp where sal<>1250;

between A and B  在A和B之间 包含A 和B

-- between A and B  在A和B之间 包含A 和Bselect * from emp where sal between 800 and 1000;select * from emp where sal between 1000 and 1250;--效果相同select * from emp where sal >=800 and sal <=1000;
IN 集合

-- 查询工资在 in集合中 800,900,1000,1250四个中有没有select * from emp where sal in (800,900,1000,1250);--  效果一样select * from emp where sal=800 or sal=900 or sal=1000 or sal=1250;

is null 和is not null

-- 查询奖金为空的  注意只能 is 不能等于  因为null是字符表示  代表房子不存在select * from emp where comm is null;--代表空房子select * from emp where comm='';--查询奖金非空的select * from emp where comm is not null;select * from emp where comm != ''

like模糊查询

-- like 模糊查询  注意大小写select * from emp where ename like 'S%';--  查询 名字两个字并且 s开头的select * from emp where ename like 'S_';-- 查询最后一个字是S的select * from emp where ename like '%S';-- 查询含有S的名字的员工信息select * from emp where ename like '%S%';-- 查询 S开头的员工信息 并且 必须两位以上的select * from emp where ename like 'S_%';select * from emp where ename like '%_S';--  escape 表明就是一个斜杠  名字中带斜杠的 select * from emp where ename like '%\%' escape '\';-- 查询 工资大于900并且 名字是S开头的select * from emp where sal > 900 and ename like 'S%';-- 查询 工资大于900 并且 名字中含有S的select * from emp where sal >900 and ename like '%S%';-- 查询工资大于900 或者  名字中带有 S的select * from emp where sal >900 or ename like '%S%';-- 查询名字中不带有 S的select * from emp where ename not like '%S%';
字符的方法和格式

--名字首字母转大写select initcap(ename) from emp;-- 查询名字 全转小写select lower(ename) from emp;-- 查询名字全转大写select upper(ename) from emp;-- 在实验表上 从左边去除 113  dual是个不存在的表select ltrim('113admin','113') from dual;-- 首字母大写select initcap('abc') from dual;-- 将字符串全转大写select upper('abf') from dual; -- 从右边开始 去除adminselect rtrim ('123admin','admin') from dual;-- 去除左右的空格select ltrim(rtrim('  123admin  ')) from dual;-- 翻译 一个一个字符的替换 select translate ('jack','abcd','1234') from dual;--  单独字符的翻译 将 aj替换为12  select translate ('jack','aj','12') from dual;-- 替换 字符串 这个单个a就代表一个字符串 替换为blselect replace ('jack','a','bl') from dual;-- 替换 将aa替换为blselect replace ('jaack','aa','bl') from dual;-- 查找i字符第一次出现的位置 在第几位select instr ('adminin','i') from dual;-- 查找 mi首次出现的位置第几select instr ('adminin','mi') from dual;-- 查找 i出现的位置 从第5位开始算select instr ('adminin','i',5) from dual;-- 截取 从第三位开始截取5个 包含第三位select substr ('abcdefghijklmn',3,5) from dual;-- 拼接 将abc和1234拼接 (数字加不加引号无所谓)select concat('abcd',1234) from dual;-- 拼接 将abcd和1234 拼接到一块儿select concat ('abcd','1234') from dual;

数值函数

--数值函数-- 求绝对值select abs(-25) from dual;-- 求2的3次幂select power(2,3) from dual;-- 向上取值 天花板select ceil (12.3) from dual;-- 向下取值 地板砖select floor (12.6) from dual;-- 保留 小数后多少位select trunc (12.156,2) from dual;-- 只截断 小数点后面的 select trunc (121365,2) from dual;-- 保留两位第三位 四舍五入(小数有用)select round (12.156,2) from dual;-- 保留1位第二位四舍五入(小数有用)select round (125.23,1) from dual;-- 求平均值select sqrt(4) from dual;-- 8的3分之1幂 相当于 8的开立方select power(8,1/3) from dual;-- 取余数select mod(10,3) from dual;select mod(17,2) from dual;-- 取符号 看是整数还是负数   整数1负数-1select sign(-25) from dual;select sign(25) from dual;

日期函数

--日期函数--  至今时间到95年12月28号一共过了多少个月select months_between(sysdate,'28-12月-95') from dual;-- 将字符串转变为时间格式select months_between(sysdate,to_date('1995-12-28','yyyy-mm-dd')) from dual;-- 当前月份加1 只有月分加1 的方法select add_months(sysdate,1) from dual;--当前月份加 -1select add_months(sysdate ,-1) from dual;-- 给出当前日期计算离它最近的星期日是多少日期select next_day(sysdate,'星期日') from dual;-- 计算当前时间的月的最后一天select last_day(sysdate) from dual;-- 计算 当前时间月的最后一天是多少select last_day('2-2月-95') from dual;-- 超过6月去下一年的第一天select round (sysdate,'YEAR') from dual;-- 超过一个月的一半去下一个月的1号select round(sysdate,'MONTH') from dual;-- 超过星期的一半到下一星期的开始  美国是周日为开始 就是星期一select round(sysdate,'day') from dual;-- 本年的第一天 括号里面不分区大小写select trunc (sysdate,'year') from dual;-- 本月的开始 第一天select trunc(sysdate,'month') from dual;-- 本周的开始  (星期日)select trunc(sysdate,'day') from dual;

转换函数

--转换函数/*字符转日期 字符转数字日期转字符  数字转字符*/--将日期转为字符串的形式select to_char(sysdate,'yyyy-mm-dd hh24:mi:ss') from dual;select to_char(sysdate,'yyyy*mm*dd hh24:mi:ss') from dual;--其中的汉字需要用双引号引起来select to_date('2017年8月16日 20点40分55秒','yyyy"年"mm"月"dd"日" hh24"点"mi"分"ss"秒"') from dual;-- 字符转数字  格式必须对照select to_number('$12,125,547.01','$999,999,999.99') from dual;-- 将数字转换为 本地的钱数表示形式select to_char(1258476.25,'L999,999,999.99') from dual;--字符转变为 本地钱数表示形式select to_number('¥12,025,089.01','L999,999,999.99') from dual;-- 日期比较一般比较方式select * from emp where emp.hiredate>'1-1月-80';--第二种的表达方式select * from emp where emp.hiredate > to_date('80-1-1','yyyy-mm-dd');-- 第三种 转换日期为字符串进行比较select * from emp where to_char(emp.hiredate,'d-mon-yy') >'1980-1-1';


空转数字方式

--空转数字方式--查看emp表的comm(奖金) 有空值nullselect comm from emp;-- 将其中的null空值转为数字0select nvl(comm,0) from emp;select * and nvl(comm,0)from emp;-- 转变空值2 方法 不是空就是本身  是空就是0select nvl2(comm,comm,0) from emp;-- 转变方法2 可以转为 字符表示  不是空值就是有 空值就是没有select nvl2(comm,'有','没有') from emp;


多行函数

--多行函数-- 查询emp(工资表中)的工资的总和select sum(sal) from emp;-- 查询emp表中的 所有行多少  会去掉重复值 所以要查询所有的select count(*) from emp;-- 查询 emp表中 sal工资的最大值select max(sal) from emp;-- 查询emp表中 sal的最小值select min(sal) from emp;-- 查询emp表中 sal工资的平均值select avg(sal) from emp;-- 统计有佣金的总共人数  会去掉空值 没有的就会不统计select count(comm) from emp;-- 多行函数又叫聚合函数 可以写在同一行中  但是 多行函数不可以和单行函数一块儿使用select max(sal),min(sal),count(*),sum(sal),avg(sal) from emp;-- 求出工资的总和 再查询出 统计人数 利用算数运算符 得出平均值select sum(sal)/count(*) from emp;-- 统计非空的 和 统计非空的但是不重复的  select count(sal) ,count(distinct sal) from emp;

练习 

--查询部门编号为10 的员工信息select * from emp where deptno =10;--查询年薪大于3万的人员的姓名与部门编号 select * from emp where sal*12 >30000;--查询佣金为null的人员姓名与工资select ename,sal from emp where comm is null;--查询工资大于1500 且 and 含有佣金的人员姓名select ename from emp where sal>1500 and comm is not null;--查询工资大于1500 或 or含有佣金的人员姓名select ename from emp where sal >1500 or comm is not null;--查询姓名里面含有 S 员工信息 工资、名称select emp.*,emp.sal,emp.ename from emp where ename like '%S%';--求姓名以J 开头第二个字符O的员工姓名的与工资select ename,sal from emp where ename like 'JO%';--求包含%的雇员姓名 select ename from emp where ename like '%_%%' escape '%' and comm is not null;