常用Oracle数据表操作命令

创建表,包括外键和主键
create table score(
sco_id number constraint pk_score primary key,
stu_id number ,
constraint fk_student foreign key(stu_id) references student(stu_id),
cou_id number ,
constraint fk_course foreign key(cou_id) references course(cou_id)

   )

select * from test1;–查询全部内容
select name from test1;–查询所有的name
select name,salary from test1 where id=5;–查询id=5的信息
select * from test1 where salary is null;

insert into test1 values(12,’zhang’,3000);–插入一条消息
insert into test1(id,name) values(11,’wang’);–插入id和name

update test1 set name=’改变’,salary=2000 where id=12; –修改id=12的name和salary

delete test1 where id=11;–删除id=11的信息
delete test1 where id=12 and name=’改变’;–删除id=12,name=‘改变’的信息

–排序
select * from test1 order by salary;–默认升序ASC
select * from test1 order by salary desc;–降序,会将空的排在最前面
select * from test1 order by salary desc nulls last;–将空的排在最后,first为最前

–常用的where字句中的关键字and\or\in\not in\between and\like
select id,name from test1 where id=3 and name=’李四’;–查询id为3且名字为‘李四’的信息
select * from test1 where id=3 or id=4;–查询id为3或4的信息
select * from test1 where id in(1,2,3,4);–查询id为1,2,3,4,的信息
select * from test1 where id not in(1,2,3,4);–查询id不为1,2,3,4的信息
select * from test1 where id between 1 and 4;–查询id为1-4 的信息
select * from test1 where name like ‘%三’;–第二个字为‘三’
select * from test1 where name like ‘张%’;–第一个字为‘张’
select * from test1 where name like ‘%水%’;–中间有‘水’

select * from test1 where salary>any(1000,2000,3000);–大于其中一个就可以
select * from test1 where salary>all(1000,1200);–大于全部

select salary,nvl2(salary,’有奖金’,’无’) from test1;–如果salary不为空,返回’有奖金‘,否则返回’无‘

alter table test1 add address varchar(20);–增加一列
alter table test1 drop column address;–删除一列

–删除表中的数据
delete from test1;
truncate table test1;

–聚集函数
select count(salary) from test1;–统计行数
select sum(salary) from test1;–求和
select avg(salary) from test1;–求平均
select max(salary) from test1;–求最大
select min(salary) from test1;–求最小

–数值计算
select abs(10),abs(-10) from dual;–返回绝对值
select ceil(5.4),ceil(-5.2) from dual;–返回大于等于value的最小整数
select floor(5.8),floor(-5.2) from dual;–返回小于等于value的最大整数
select power(2,1),power(2,3) from dual;–返回2的n次幂
select mod(8,3),mod(8,4) from dual;–返回m和n取余的结果
select sqrt(25),sqrt(5) from dual;–返回开方结果
select trunc(5.75),trunc(5.75,1),trunc(5.75,-1) from dual;–对于value进行截断,如果n>0,保留n位小数;n<0,保留-n位整数位,n=0(默认),去掉小数位
select round(5.75),round(5.75,1),round(5.75,-1) from dual;–四舍五入,第二个字段是保留位数

你可能感兴趣的:(oracle)