Oracle数据库的数据类型有:1、字符char,固定长度1-2000字节;2、字符VARCHAR2,可变长度1-4000字节;3、长字符LONG,可变长度,最大2GB;4、数字NUMBER[(精度,[小数点位数])];5、日期DATE,存储日期和时间部分,精确到秒;6、日期TIMESTAMP 存储日期、时间和时区,秒值精确到小数点后面6位;7、二进制BLOB字段,存放较大的二进制数据。等等注意:数值类型的值不需要加单引号,时间和字符串类型需要加单引号。
主键:一个表中能唯一区分每一条记录的字段,可以一个也可以多个,可以作为其它表的外键;字段:数据库表网格中的列,记作column;记录:数据库表网格中的行,记作row;外键:一个表中的某个字段的取值是另一表中的主键;唯一键:一个表中能唯一区分每一条记录的字段,不能做外键。
增加一列:alter table wtemp add key_num int;设置列的值:update wtemp set key_num = 1;删除一列:alter table wtemp drop column key_num;
表中有十条一样的记录,删除其中九条,保留一条:1、create table temp as select * from wtemp where rownum = 1//创建一个临时表,保存第一条记录2、delete from wtemp where 1=1;//删除原表中所有的记录3、insert into wtemp select * from temp;//然后将临时表中的记录插入到原表中4、drop table temp;//删除临时表。
对于向oracle中同时插入多条数据的时候,只能同时写对个Insert into语句,例如:insert into data values(1,to_date('2007-5-1','yyyy-mm-dd'),2,46);insert into data values(1,to_date('2007-5-1','yyyy-mm-dd'),2,46);
select stuidfrom Personwhere Sex='女' //where用于限定所有数据的条件group by stuidhaving count(*)>=1//having是用来限定分组内的条件,一般都只是聚合函数
--内连接查询-----多表查询时建议使用inner join(只查询出两个表stuID相等的记录)select * from Personinner join Student on Person.StuID=Student.StuIDwhere Sex='女'
--左连接查询(没有关联的部分为null)select * from Personleft join Student on Person.StuID=Student.StuID --右连接查询(没有与右表关联的部分为null)select * from Personright join Student on Student.StuID=Person.StuID --全连接查询(没有关联的部分为null)select * from Personfull join Student on Student.StuID=Person.StuID
--查询结果用作查询条件进行比较运算的时候,结果必须是一行一列的,可以0行一列(一一对应)select * from Person where Salary=(select MAX(salary) from person)select * from Person where Salary=(select salary from person where ID=1)
--查询结果用作查询条件进行in查询的时候,结果只能是多行一列的,可以是0行一列(一对多)select * from Person where StuID in (select StuID from Student)
select * from Personinner join(select StuID,AVG(salary) avgsalaryfrom Person group by StuID) PersonAvg on Person.StuID=PersonAvg.StuIDwhere Person.Salary<PersonAvg.avgsalary --工资小于平均值的人的信息
--any表示取其中的任意一条记录select * from Personwhere StuID=2 and Salary>any(select Salary from Person where StuID=1)--大于其中最小的 --all表示取其中的所有的记录select * from Personwhere StuID=2 and Salary>all(select Salary from Person where StuID=1)--大于其中最大的值
--exists表示数据存在select * from Personwhere exists(select * from Student)--exists判断表中是否有数据存在 一般用 exists关联查询时比其它关联查询效率要高select * from Person pwhere exists(select * from Student s where p.StuID=s.StuID)
--union是将两个查询结果进行合并,并且消除了重复行--上下两个数据集的字段必须一致,而且数据类型也必须一致select * from Person where StuID=1unionselect * from Person where StuID=2
--条件语句,利用case when可以进行条件匹配select id,revtime,max(case channel when 1 then val end) as channel1Val,max(case channel when 2 then val end) as channel1Val,max(case channel when 3 then val end) as channel1Valfrom data group by id, revtime
case channel when 1 then ....; when 2 then ....; else ....;end case;
新闻热点
疑难解答