您的位置:首页 > 数据库 > Oracle

oracle的基操作

2016-02-26 10:16 513 查看
createtablespace ta
datafile'c:\yh.dbf'size2M
autoextendon
createuser yuanhao
identifiedby yuanhao
defaulttablespace ta
temporarytablespace temp
grantdbato yuanhao
createtable student(
sid number(5)
primarykey,
--学生编号
sname varchar2(20)
notnull,
--学生姓名
age number(3),--学生年龄
sex char(2),
--学生性别
birthday date,
--出生年月日
demo clob--简历

)
createtable student2(
sid number(5)
primarykey,
--学生编号
sname varchar2(20)
notnull,
--学生姓名
age number(3),--学生年龄
sex char(2),
--学生性别
birthday date,
--出生年月日
demo clob--简历

)
/*创建序列*/
createsequence seq_stu_num
incrementby1startwith1nomaxvalueminvalue1;
/*删除序列*/
dropsequence seq_stu_num ;
/*使用序列*/
select seq_stu_num.nextval
from dual;--dual是伪表,取下一个值
select seq_stu_num.currval
from dual;--dual是伪表,取当前值

insertinto student(sid,sname)
values(seq_stu_num.nextval,'aa');
commit;

/*插入*/
--把字符串转日期
insertinto student(sid,sname,birthday)
values(seq_stu_num.nextval,'aa',to_date('1993-07-02','yyyy-mm-dd'));
--日期转字符串
select sid,sname,to_char(birthday,'yyyy-mm-dd')from student;
--批量插入
insertinto student2(sid,sname,birthday)
select sid,sname,birthday
from student;

/*根据结果集创建表*/
createtable student3
asselect *
from student;
/*修改*/
update student
set sname='yuanhao',birthday=to_date('1993-07-02','yyyy-mm-dd')
where sid =
4;
/*删除*/
deletefrom student
where sid=5;
--另一种删除,不可回滚,删除速度快
truncatetable student3

/*查询,默认升序*/
select *
from student
orderby sid
desc;--id降序
select *
from student2
orderby sname,sid
asc;--先sname升序,再sid升序
select *
from student3;
/*列名指定*/
select sid
学号,sname 姓名 from student
/*常量*/
select sid
学号,sname 姓名,'aaaa'学校
from student--aaa是常量
/*链接*/
select sid ||'-'||sname
姓名,'aaaa'学校
from student--aaa是常量
/*模糊查找*/
select *
from student
where sname like'%a%';--前后可多个字符
select *
from student
where sname like'_a__';--前后指定个字符

select *
from student
where sname in('aa','bb');--查询姓名aa,bb的学生
select *
from student
where sname='aa'or sname='bb';--查询姓名aa,bb的学生
/*查询出生日期为空*/
select *
from student
where birthday isnull
select *
from student
where birthday isnotnull
select *
from student
where birthday =''--无用
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
----------------------------------重点------------------------------------------
/*查出前三行的数据*/
select t.*,rownumfrom student t
select t.*,rownumfrom student t
whererownum<4
select *
from student t
whererownum<4--在select有出现*后面有','号要继续查询其他属性的时候就每个查询属性要加上t.和表后面加上t
/*查询年龄前三的*/
select t.*,rownumfrom student t
orderby age
desc;--rownum是在排序之前给定序号的,所以降序后rounum会乱掉
--所以正确
select t.*,rownumfrom(select
* from student
orderby age
desc) t
/*查询2-4行的信息,因为rownum不能表示大于的形式,所以必须嵌套查询*/
select *
from(select t.* ,rownum r
from student t)
where r>1and r<5
/*根据年龄大小查询2-4行*/
select *
from (
select t.*,rownumas r
from (
select *
from t_user
orderby age
desc) t whererownum<5
) where r>=2
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: