创建表的语法
create table 表名
(
列名 数据类型 ,
列名 数据类型
......
) ;
增 添加
insert into 表名(列名,列名...) values (值,值...) ;
删 删除delete
delete from 表名 ; where 条件 ;
改 修改
update 表名 set 列名 = 值,列名 = 值 where 条件;
查 查询
select 列名,列名...from 表名 ;
select s_id ,s_name ,s_age ,sex from s7 ;
--当你要查看这个表中的所有列时,可以使用 “*”来代替所有的列名 。select * from s7 ;
对表进行 增,删,改的时候,都需要 提交 commit ,提交之后才表示数据真正到数据库中
回滚 rollback ,没有提交的数据才能够回滚
char(n) 定长字符串,n最大 2000字节 当位数不足的时候会自动用空格来填充
varchar2(n) 变长字符串,n最大是4000字节
number(m,n) m表示总长度,n表示小数点后面的位数
number(5,2) 999.99
number(38)
number(5)
int 整数类型
date 日期类型,DD-MM-YY 01-11月-00
DDL: 数据定义语言 create ,drop ,alter
create table ,create user
drop table ,drop user
--修改表
--添加列语法
alter table 表名 add 列名 数据类型 ;
--删除列语法
alter table 表名 drop column 列名 ;
--给s7 表添加一列 tage int
alter table s7 add tage int ;
alter table s7 drop column tage ;
select tage , tid ,tname from s7 ;
DML:数据操作语言 insert , update ,delete
增 添加
insert into 表名(列名,列名...) values (值,值...) ;
删 删除delete
delete from 表名 ; where 条件 ;
改 修改
update 表名 set 列名 = 值,列名 = 值 where 条件;
DCL:数据控制语言
grant授权 , revoke
grant 权限名,权限名... to 用户 ;
revoke 权限名,权限名 ..... from 用户名 ;
DQL:数据查询语言
select 查询
数据管理命令
事务性控制命令
commit ,rollback 这两个都是事务性控制命令
SQL语言中,关键字,表名,列名 不区分大小写 ,where 后面条件的值需要区分大小写 。
and 并且,表示多个条件同时成立才能够查询到数据
or 或者,表示只要满足其中的一个条件就查询出来
between and 在什么,什么之间
in 在列表中的数据都查询出来
< any 小于最大的
> any 大于最小的
> all 大于最大的
< all 小于最小的
--% 任意长度的任意字符串
--小% 表示的是 以“小” 字开头的字符串都查询出来
-- _ 表示的是一个长度的任意字符串
使用distinct 关键,可以将查询结果集中的重复数据给过滤掉
order by 排序的关键字 ,要按照那一列来进行排序,就将那一列的列名跟在后面
其中升序的关键字是 asc 默认可以省略,降序是 desc
as 给列起别名,可以省略
lower 函数,将大写字符转换小写
select lower('ABCDE') FROM dual;
upper 将小写转大写
select upper('abcde') from dual;
concat 用来连接两个字符串
select concat('abc' , '1234') from dual;
--ltrim 去掉字符串左边的空格 left
select ltrim(' abcde') from dual;
select rtrim(' abcde ') from dual;
select trim(' abcde ') from dual;
abs取绝对值的函数
select 3.14 from dual ;
--向下取整
select floor(3.14) from dual ;
--向上取整
select ceil(3.14) from dual;
--查看数据库系统当前时间
select sysdate from dual;
--添加或减去月份
select add_months(sysdate ,-4) from dual;
--last_day求某个月份的最后一天
select last_day(add_months(sysdate,4)) from dual;
--to_char 将时间转换为指定格式的字符串
select to_char(sysdate,'yyyy-mm-dd dy hh:mi:ss') from dual;
--强字符串转换为时间
select to_date('2017-2-14' , 'yyyy-mm-dd') from dual;
select e_salary from emp
order by decode(e_salary,null,
(select avg(e_salary) from emp),e_salary) ;
select 'abc' || 123 || 4566 from dual;
select concat('abc',123) from dual;
select e_salary from emp ;
--max 求某一列中的最大值 min求最小值
select max(e_salary) from emp ;
select min(e_salary) from emp ;
--sum 求和函数
select sum(e_salary) from emp ;
132245
--avg 求平均值函数
select avg(e_salary) from emp ;
--count 统计有效数据的数量
select count(e_salary) from emp ;
--count(*) 统计这个表有多少行数据
select count(*) from emp ;
select * from emp order by deptno ;
--分组 使用 group by 关键字
--order by 排序语句,永远在sql语句的结尾
--分组统计每个部分下的人数,sql语句如下
select
emp.deptno ,count(*)
from emp group by deptno order by deptno ;
--只统计部门人数多于一人的部门 。
select
emp.deptno ,count(*)
from emp
group by deptno
having count(*) > 1
order by deptno ;
--having 关键字是对分组之后的数据进行二次过滤的。
--where 后面不能跟聚合函数列
--当查询中出现了group by的时候,select 显示的列,
--要么是group by 分组列,要么是聚合函数列
select tname,decode(tname,'小花','本科','中专') from s7
order by tname ;
--查询job,用decode函数来进行替换
select job,decode(job,'OPERATION','op','vp_ceo') from t1 ;
--接下来按照decode函数这一列来进行分组
select decode(job,'OPERATION','op','vp_ceo') ,count(*)
from t1 group by decode(job,'OPERATION','op','vp_ceo') ;
select job,count(*) from t1 group by job ;
--decode函数第二种用法 ,动态排序
select * from dept order by d_name desc ;
--要求 销售部拍第一位
select * from dept
order by decode(d_name,'产险系统开发部',1,'销售部',2,3)
--decode函数第三种用法
--行转列
select * from sales ;
select s_years ,
max(decode(s_months , '1季度',s_money)) as 一季度 ,
min(decode(s_months , '2季度',s_money)) as 二季度,
sum(decode(s_months , '3季度',s_money)) as 三季度,
avg(decode(s_months , '4季度',s_money)) as 四季度
from sales group by s_years ;
select deptno from emp order by deptno ;
select * from dept order by d_id ;
--多表中查询数据 ,from后面跟上多个表名
--如果需要从多个表中查询出来关联数据,需要多个表之间有相同意义的字段
--并且这个字段的数据类型也要相同
select emp.*,dept.d_name,dept.d_loc from emp ,dept
where deptno = d_id ;
--在emp表中存在的部门编号deptno ,同时也在dept表中存在d_id 的数据,才查询出来
-- 这种连接我们称之为 内连接
--左外连接和右外连接中,基表的内容会全部显示
--查询所有部门下的员工信息 ,那么我们是以 部门表为基表
--左外连接实现 dept表是基表 ,左外连接以左边的表为基表
select * from dept left join emp on dept.d_id = emp.deptno
order by dept.d_id ;
--还可以用 (+) 来实现左外,右外连接
select * from dept,emp where emp.deptno = dept.d_id (+) ;
--用左外连接实现的sql,也可以用右外连接来改变,查询结果是一样的
--右外连接以右边的表为基表
select dept.*,emp.* from emp right join dept on dept.d_id = emp.deptno ;
--左外连接 匹配的数据 是 15 条 不匹配的数据 3 条
--右外连接 匹配的数据 是 15 条 不匹配的数据 2 条
--全外连接
select * from dept full join emp on dept.d_id = emp.deptno ;