删除表:drop table tableName
创建表:create table student(
xh number(4),
xm varchar2(20),
sex char(2),
birthday date,
sal number(7,2)
);
添加表中的字段:
alter table tableName add(
classId number(2)
);
查看表结构:desc tableName;
修改字段的长度:
alter table tableName modify
xm varchar2(30)
);
删除表中的字段:
alter table tableName drop column 字段名
修改表的名字:
rename tableName to newName;
向表中添加数据:
insert into tableName values(1,'','','01-3月(月字必须要)-2001','','');
日期默认格式:(DD-MON-YY)
修改日期默认格式:
alter seesion set nls_date_format = 'yyyy-mm-dd';
查询表中数据为空的字段名称:
select * from tableName where 字段名 is null;
删除表中所有数据:
delete from tableName;
恢复所删除表中的所有数据:
在删除之前要savepoint(保存点)才能rollback to ;
删除表中的所有记录,表结构还在,无法找回删除的记录
数度快:
truncate table tableName;
如何取消重复行:
select distinct * form
复制和本身一样的表结构数据到表中去:
insert into tableName(name,id) select * form tablseNAme
打开显示时间的开关:
set timing on;
如何处理null值:
nvl(comm,0)comm代表原有值,为空的话赋值0;
如何连接字符串(||):
like操作:
%:表示任意0到多个字符 _:表示任意单个字符
找出姓名首字母为S的人:
select name form tableName where name like 'S%';
找出姓名第三字母为S的人:
select name form tableName where name like '__S%';
select name form tableName where (a>500 or b<700) name like 'S%';
in操作:
找出编号为11,22,33的人:
select * from tableName where id in(11,22,33);
order by:
desc(降序)
asc(升序)
两个排序规则写法:
select * from tableName order by a asc,b desc;
列的别名,表的别名不能加as:
select (a+b)*12 as c from tableName order by c desc;
最大最小值
select max(a),min(b) from tableName;
子查询:select name,a from tableName where a=(select max(a) from tableName);
group by:用于对查询的结果进行分组统计
select avg(a),max(a),c from tableName group by c;
select avg(a),max(a),c,d from tableName group by c,d;
having:用于限制分组显示的结果
select avg(a),max(a),c from tableName group by c having avg(a)>1000;
顺序:group by>having>order by
多表查询(笛卡尔集:多表查询的条件是至少不能少于表的个数减一)
select a1.name,a2.name from tableNme as a1,tableName2 a2 where a1.id=a2.id;
between and语句
自连接:指在同一张表的连接查询
子查询:是指嵌入在其他SQL语句中的select语句,也叫嵌套查询
分页:
1.rownum:select a1.*,rownum rn(//行的id号) from (select * from tableName) a1;
2.显示rownum(oracle分配)
3.显示前十行数据:
select a1.*,rownum rn(//行的id号) from (select * from tableName) a1 where rownum<=10;
4.显示6-10行的数据:
select * from (select a1.*,rownum rn(//行的id号) from (select * from tableName) a1 where rownum<=10) where rn>=6;
几个查询的变化:
a.指定查询列,只要修改最里层的字段即可:
select * from (select a1.*,rownum rn(//行的id号) from (select name from tableName) a1 where rownum<=10) where rn>=6;
a.排序查询列,只要修改最里层的字段即可:
select * from (select a1.*,rownum rn(//行的id号) from (select name from tableName order by name) a1 where rownum<=10) where rn>=6;
快捷建表并把数据赋值到新表:
create table newTable (id2,name2) as select id1,name1 from oldTable;
合并查询:union(去重复行) union all(全部显示)intersect(取交集) minus(取差集)
select name,id from tableName where id>10 union/union all/intersect select name,id from tableName where name='jone';
事务:用于保证数据的一致性。
dml:数据库操作语言(增加、删除、修改)
事务几个重要操作:
1.设置保存点savepoint a;
2.取消全部事务rollback;
3.取消部分事务rollback to a.
事务和锁
只读事务:指的是只允许执行查询的操作,不允许执行其他的DML操作,保证取得特定时间点的信息。
set transaction read only;
字符函数:
lower(char):将字符串转化为小写格式;select lower(name) from tableName;
upper(char):将字符串转化为大写格式;select upper(name) from tableName;
lenth(char):返回字符串的长度;select * from tableName where lenth(name)=5;
substr(char,m,n):取字符串的子串;m:是从第几个开始;n:取几个;
select substr(name,1,3) from tableName;截取姓名的前三个字符;
replace(char1,search_string,replac_string):替代:select replace(name,'A','替代A') from tableName;
整合:
1.完成首字母大写:
select upper(substr(name,1,1)) from tableName;
2.完成后面字母小写
select lower(substr(name,2,lenth(name)-1)) from tableName;
3.合并前两个
select upper(substr(name,1,1)) || lower(substr(name,2,lenth(name)-1)) from tableName
数学函数:
1.round(n,m):四舍五入,n代表字段名称,m代表小数点后几位
2.trunc(n,m):用于截取数字
3.mod(m,n):取模
4.floor(n):返回小于或者是等于n的最大整数(向下取整)
5.ceil(n):返回大于或者等于n的最小整数(向上取整)
6.abs(n):绝对值;
7.exp(n):e的n次幂;
8:log(m,n)返回绝对值;
9:power(m,n)返回m的n次幂;
10:asin(n)反正玄值; acos(n)反余弦值;atan(n)反正切值;
dual:亚元表(虚拟的表),没表可用的时候用做测试
日期函数:
1.sysdate:该函数返回系统时间;
selete sysdate from dual;
2.add_month(d,n):加月份;
查询入职超过8个月的员工:selete * from tableName where sysdate>add_month('字段名','8');
查询入职天数:select sysdate-'字段名' "入职天数" from tableName;
3.last_day(d):返回指定日期所在月份的最后一天
当月倒数第三天入职人:select day,name from tableName where last_day(day)-2=day;
转换函数:
1.to_char:转换字符串
日期显示时/分/秒:select name,to_char(day,'yyyy-mm-dd hh24:mi:ss') from tableName;
货币转换:(人民币)select name,to_char(sale,'L99,999.99') from tableName;L:location:本地,自动判断
2.to_date函数。
系统函数:
1.terminal:当前会话客户所对应的终端的标识符;
2.lanuage:语言;
3.db_name:当前数据库名称;;
4.host:返回数据库所在的主机名称;
5.nls_date_formal:当前会话客户所对应的日期格式;
6.sesion_user:当前会话客户所对应的数据库用户名;
7.current_schema:当前会话客户对应的默认方案名;
查询主要信息:
select sys_context('userenv(固定格式)','db_name/language') from dual;
创建表:create table student(
xh number(4),
xm varchar2(20),
sex char(2),
birthday date,
sal number(7,2)
);
添加表中的字段:
alter table tableName add(
classId number(2)
);
查看表结构:desc tableName;
修改字段的长度:
alter table tableName modify
xm varchar2(30)
);
删除表中的字段:
alter table tableName drop column 字段名
修改表的名字:
rename tableName to newName;
向表中添加数据:
insert into tableName values(1,'','','01-3月(月字必须要)-2001','','');
日期默认格式:(DD-MON-YY)
修改日期默认格式:
alter seesion set nls_date_format = 'yyyy-mm-dd';
查询表中数据为空的字段名称:
select * from tableName where 字段名 is null;
删除表中所有数据:
delete from tableName;
恢复所删除表中的所有数据:
在删除之前要savepoint(保存点)才能rollback to ;
删除表中的所有记录,表结构还在,无法找回删除的记录
数度快:
truncate table tableName;
如何取消重复行:
select distinct * form
复制和本身一样的表结构数据到表中去:
insert into tableName(name,id) select * form tablseNAme
打开显示时间的开关:
set timing on;
如何处理null值:
nvl(comm,0)comm代表原有值,为空的话赋值0;
如何连接字符串(||):
like操作:
%:表示任意0到多个字符 _:表示任意单个字符
找出姓名首字母为S的人:
select name form tableName where name like 'S%';
找出姓名第三字母为S的人:
select name form tableName where name like '__S%';
select name form tableName where (a>500 or b<700) name like 'S%';
in操作:
找出编号为11,22,33的人:
select * from tableName where id in(11,22,33);
order by:
desc(降序)
asc(升序)
两个排序规则写法:
select * from tableName order by a asc,b desc;
列的别名,表的别名不能加as:
select (a+b)*12 as c from tableName order by c desc;
最大最小值
select max(a),min(b) from tableName;
子查询:select name,a from tableName where a=(select max(a) from tableName);
group by:用于对查询的结果进行分组统计
select avg(a),max(a),c from tableName group by c;
select avg(a),max(a),c,d from tableName group by c,d;
having:用于限制分组显示的结果
select avg(a),max(a),c from tableName group by c having avg(a)>1000;
顺序:group by>having>order by
多表查询(笛卡尔集:多表查询的条件是至少不能少于表的个数减一)
select a1.name,a2.name from tableNme as a1,tableName2 a2 where a1.id=a2.id;
between and语句
自连接:指在同一张表的连接查询
子查询:是指嵌入在其他SQL语句中的select语句,也叫嵌套查询
分页:
1.rownum:select a1.*,rownum rn(//行的id号) from (select * from tableName) a1;
2.显示rownum(oracle分配)
3.显示前十行数据:
select a1.*,rownum rn(//行的id号) from (select * from tableName) a1 where rownum<=10;
4.显示6-10行的数据:
select * from (select a1.*,rownum rn(//行的id号) from (select * from tableName) a1 where rownum<=10) where rn>=6;
几个查询的变化:
a.指定查询列,只要修改最里层的字段即可:
select * from (select a1.*,rownum rn(//行的id号) from (select name from tableName) a1 where rownum<=10) where rn>=6;
a.排序查询列,只要修改最里层的字段即可:
select * from (select a1.*,rownum rn(//行的id号) from (select name from tableName order by name) a1 where rownum<=10) where rn>=6;
快捷建表并把数据赋值到新表:
create table newTable (id2,name2) as select id1,name1 from oldTable;
合并查询:union(去重复行) union all(全部显示)intersect(取交集) minus(取差集)
select name,id from tableName where id>10 union/union all/intersect select name,id from tableName where name='jone';
事务:用于保证数据的一致性。
dml:数据库操作语言(增加、删除、修改)
事务几个重要操作:
1.设置保存点savepoint a;
2.取消全部事务rollback;
3.取消部分事务rollback to a.
事务和锁
只读事务:指的是只允许执行查询的操作,不允许执行其他的DML操作,保证取得特定时间点的信息。
set transaction read only;
字符函数:
lower(char):将字符串转化为小写格式;select lower(name) from tableName;
upper(char):将字符串转化为大写格式;select upper(name) from tableName;
lenth(char):返回字符串的长度;select * from tableName where lenth(name)=5;
substr(char,m,n):取字符串的子串;m:是从第几个开始;n:取几个;
select substr(name,1,3) from tableName;截取姓名的前三个字符;
replace(char1,search_string,replac_string):替代:select replace(name,'A','替代A') from tableName;
整合:
1.完成首字母大写:
select upper(substr(name,1,1)) from tableName;
2.完成后面字母小写
select lower(substr(name,2,lenth(name)-1)) from tableName;
3.合并前两个
select upper(substr(name,1,1)) || lower(substr(name,2,lenth(name)-1)) from tableName
数学函数:
1.round(n,m):四舍五入,n代表字段名称,m代表小数点后几位
2.trunc(n,m):用于截取数字
3.mod(m,n):取模
4.floor(n):返回小于或者是等于n的最大整数(向下取整)
5.ceil(n):返回大于或者等于n的最小整数(向上取整)
6.abs(n):绝对值;
7.exp(n):e的n次幂;
8:log(m,n)返回绝对值;
9:power(m,n)返回m的n次幂;
10:asin(n)反正玄值; acos(n)反余弦值;atan(n)反正切值;
dual:亚元表(虚拟的表),没表可用的时候用做测试
日期函数:
1.sysdate:该函数返回系统时间;
selete sysdate from dual;
2.add_month(d,n):加月份;
查询入职超过8个月的员工:selete * from tableName where sysdate>add_month('字段名','8');
查询入职天数:select sysdate-'字段名' "入职天数" from tableName;
3.last_day(d):返回指定日期所在月份的最后一天
当月倒数第三天入职人:select day,name from tableName where last_day(day)-2=day;
转换函数:
1.to_char:转换字符串
日期显示时/分/秒:select name,to_char(day,'yyyy-mm-dd hh24:mi:ss') from tableName;
货币转换:(人民币)select name,to_char(sale,'L99,999.99') from tableName;L:location:本地,自动判断
2.to_date函数。
系统函数:
1.terminal:当前会话客户所对应的终端的标识符;
2.lanuage:语言;
3.db_name:当前数据库名称;;
4.host:返回数据库所在的主机名称;
5.nls_date_formal:当前会话客户所对应的日期格式;
6.sesion_user:当前会话客户所对应的数据库用户名;
7.current_schema:当前会话客户对应的默认方案名;
查询主要信息:
select sys_context('userenv(固定格式)','db_name/language') from dual;