一、Oracle表字段的增加、删除、修改和重命名
增加字段语法:alter table tablename add (column datatype [default value][null/not null],….);
说明:alter table 表名 add (字段名 字段类型 默认值 是否为空);
例:alter table sf_users add (HeadPIC blob);
例:alter table sf_users add (userName varchar2(30) default '空' not null);
修改字段的语法:alter table tablename modify (column datatype [default value][null/not null],….);
说明:alter table 表名 modify (字段名 字段类型 默认值 是否为空);
例:alter table sf_InvoiceApply modify (BILLCODE number(4));
删除字段的语法:alter table tablename drop (column);
说明:alter table 表名 drop column 字段名;
例:alter table sf_users drop column HeadPIC;
字段的重命名:
说明:alter table 表名 rename column 列名 to 新列名 (其中:column是关键字)
例:alter table sf_InvoiceApply rename column PIC to NEWPIC;
表的重命名:
说明:alter table 表名 rename to 新表名
例:alter table sf_InvoiceApply rename to sf_New_InvoiceApply;
二、oracle与SQL SERVER实现获取前几条数据的区别:
1、SQL SERVER/MYSQL :SELECT TOP N * FROM
2、oracle:
SELECT 列名1...列名n FROM
(SELECT 列名1...列名n
FROM 表名ORDER BY 列名1...列名n ORDER BY )
WHERE ROWNUM <= N(抽出记录数)
ORDER BY ROWNUM ASC
三、Oracle中判断某字段不为空及为空的SQL语句:
sql中判断非空不能用等号,因为null在sql中被看作特殊符号,必须使用关键字 is和not应该如此使用:
select * from A where b1 is null -- 为空
或者:
select * from A where b1 is not null -- 不为空
四、oracle查询指定表中的所有字段名:
select name from syscolumns where id=Object_Id('table_name');
五、select into from 和 insert into select 的用法和区别
select into from 和 insert into select都是用来复制表,两者的主要区别为: select into from 要求目标表不存在,因为在插入时会自动创建。insert into select from 要求目标表存在
六、Oracle sql查询时如何保留两位小数
SELECT * FROM A WHERE ROUND(AVG(A.ITME1,2)); //保留2位小數,round會四捨五入
SELECT * FROM B WHERE TRUNC(0.3333,3); //保留3位小數,trunc 不會四捨五入、
七、DECODE的语法:
可用在select decode()from,也可以用在select* from table where table.a = decode()
DECODE(value,if1,then1,if2,then2,if3,then3,...,else)
相当于:
if a=... then
......
else
....
end if;
八、oracle 去重
①row_number() over(partition by )
例:select id ,name from
(select row_number() over(partition by id order by name ) t1 , t2.id,t2.name from b t2)
where t1 = 1 ) t3 on t3.id = t5.id
②distinct
例:select distinct(id) from a
九、oracle 分组查询最新的数据:
SELECT * FROM (select cc.carrier_id,cc.carrier_name,cc.location,cc.high_thres_hold,cc.safety_thres_hold,cc.low_thres_hold,cc.latest_data,cc.qty,
cc.comments,cc.user_name,cc.carrier_family_key,cc.trx_date,MAX(cc.trx_date) over(partition by cc.carrier_id)
as atime from MBDM_CARRIER_CHECK cc) x where carrier_family_key = '{0}' and trx_date = atime
十、关系型数据库的几种事务隔离级别
隔离级别 | 脏读(Dirty Read) | 不可重复读(NonRepeatable Read) | 幻读(Phantom Read) |
读未提交 | 可能 | 可能 | 可能 |
读已提交 | 不可能 | 可能 | 可能 |
可重复读 | 不可能 | 不可能 | 可能 |
可串行化 | 不可能 | 不可能 | 不可能 |
十一、悲观锁和乐观锁
悲观锁:假定数据会发生竞争,因此在数据获取的时候会主动加锁。常见的实现方式就是用SQL语句来显示地加锁,比如select from ... for update用来锁定记录。悲观锁通常在事务中使用,确保事务在执行过程中不会有其他事务修改数据。
乐观锁:假定数据不会冲突,只有在提交更新的时候,才会检查是否有其他事务修改了数据。如果检测到冲突了,则会回滚当前事务。乐观锁通常使用版本号或时间戳来实现。
十二、创建索引
1.先查询现有索引:select index_name,table_name,column_name from user_ind_columns;
2.create INDEX myindex on mytable(column1,column2......);
十三、內、外连接和交叉连接详解:
交叉连接(cross join)、内连接(inner join)、外连接(outer join)
1.cross join:返回两表乘积,也称为笛卡尔积。 例子:select a.id,b.name from a cross join b on a.id = b.id where
2.inner join : 查询连接表中符合连接条件和查询条件的数据行。 例子:select a.id,b.name from a inner join b on a.id = b.id where
3.outer join(包括left outer join、right outer join、 full outer join): 不但返回连接表中符合连接条件和查询条件的数据行,还返回不符合条件的一些行。
(1)left outer join:还返回左表中不符合连接条件单符合查询条件的数据行。
(2)right outer join:还返回右表中不符合连接条件单符合查询条件的数据行。
(3)full outer join:还返回左表中不符合连接条件单符合查询条件的数据行,并且还返回右表中不符合连接条件单符合查询条件的数据行。全外连接实际是上左外连接和右外连接的数学合集(去掉重复),即“全外=左外 UNION 右外”。
十四、kill 正在执行的session
1.SELECT s.sid, s.serial# FROM v$locked_object lo, dba_objects ao, v$session s WHERE ao.object_id = lo.object_id AND lo.session_id = s.sid;
2.ALTER system KILL session 'SID,serial#'