关于数据库的部分知识点

来源:互联网 发布:depthmap软件下载 编辑:程序博客网 时间:2024/05/18 02:29
数据库
create table (表名) ((列名) 数据类型 ); 创建一张表
insert into (表名) values( 与没一列相对应的数据类型); 向表里面添加数据
create table (表名)((列名)数据类型 default ) 表示给列添加缺省值
create table (表名)( c1 number ,
c2 number(6),该列只能填6为整数 a 999999
c3 number(4,3),包括小数后面三位,总共能添加4位数(整数的位数 :逗号前面的数减去逗号后面的数)a 9.999 
c4 number(3,-3),-3在这里表示小数点前三位数字采取四舍五入的方式舍去,总共 6位整数 a 999000
c5 number(2,4))2-4=-2, 表名整数位没有,有4位小数,而且有效数字是2 a 0.0099
create table ()(c1 varchar2(9),c2 char()) vachar2 必须定义长度,存储数据时对空格不敏感,按实际长度存储 
char 可以不定义长度,缺省长度为1,按照定义长度存数据,长度不够自动添加空格补齐
create table () (c1 date) date类型后面不设定长度,oracle是用固定长度来存储日期,7个字节 21世纪 13年 09月 04日 15时30分 30秒
select sysdate from dual; 缺省日期格式 : DD MON YEAR
select (250-base_duration)*unix_cost+base_cost "cost_250" from cost cost_250 这里表示列名,家了双引号显示原有的内容 不加则大写

null 值的处理:
null 不能用不等于 或者等于跟任何值比较,包括其本身,要测试某一列是否有空值只能用is null
null在输入数据时,该字段没有指定值,并没有缺省值,空值不等于0。空值不等于空格,算数表达式中包含空值的结果都为空
在算数表达式中,包含空值 可用 nvl(p1,p2) 空值转换函数
if p1 is null then return p2; else return p1,end if
select 子句后面可跟 列名,表达式,函数:实际上完成的是对数据的处理。
select real_name||idcard_no from account ||是拼接 ,将两个列名拼接到一起,并将列的内容也拼接到一起
a'''' 如果要在字符串中显示单引号的话 要写一对单引号

去重:
distinct 对行去重
select distinct unix_host from service 对service 表中的 unix_host 列中 的 行去重。
select 12*base_duration yearduration,12*base_cost yearcost from cost where yearduration = 70.8 会报错
selete语句的执行顺序为:from where select ,所以where 后面不可以跟列别名
select unix_host,os_username from service where os_username = 'huangr'
字符串的比较:当比较的内容为字符串时,要加单引号,字符串比较中一定要注意大小写敏感,对于varchar2 对大小写以及空格敏感。
在字符串比较中,char会将短字符串补齐后,再比较,过长的字符串会将后面的字符串省略再比较
upper(p1)将字符串变成大写 lower(p2)所有字母变成小写 initcap(p3)将首字母大写
select base_cost,name from cost where base_cost>=5 and base_cost<=10
select base_cost,name from cost where base_cost between 5 and 10 这两个表达式是等价的 
select base_cost,name from cost where base_cost in(5.9,8.5,10.5) 三个里面的其中之一
select base_cost,name from cost where base_cost = any(5.9,10.5,8.5) 
select base_cost,name from cost where base_cost=5.9 or base_cost=8.5 or base_cost=10.5 这三个表达式是等价的
语法顺序: select from where 执行顺序 from where select

order by 语句:
语法顺序 select from order by
执行顺序 from select order by 


% 在数据库里面 表示 0个或则多个字符 ‘h%’ 表示 h+后面0个或者多个字符 “_”表示一个字符 若需要查找 % _ 本身 则需要 escape 转义‘S\_%’ escape '\'
单行函数:
字符函数 upper lower initcap length select
数值函数 round 四舍五入 trunc 截取 
日期函数 month_between 两个日期之间相差多少个月。
add_month 对当前日期添加或者减少月。
next_day根据参数出现下一个的日期
组函数 sum()求和 ]
max()求最大值
min()最小值
avg()平均值
count()处理数据不为空值的次数
trunc(45.998,2) 截取到小数点后两位 trunc(45.6856,-1) 截取到小数点前一位
转换函数:
to_char to_char(1.1,'00.00')01.10 to_char(base_cost,'99.99')表示 小数点前两位和小时点后两位 如果超出的范围就会显示乱码
to_date to_date('09-09-1990','dd-mm-yyyy'); 按照格式转换成日期类型
to_number char类型专函成number类型再进行运算 
sql里面的时间可以相互加减,返回值是以天为单位。
case when end 语句 于decode 语句
decode语句只能用于等值的条件选择 decode
case when end 功能比decode功能强大

group by语句
select unix_host,count(os_username) from service where unix_host='192.168.0.26' group by unix_host;
select unix_host,count(os_username) from service group by unix_host,trunc(create_date);
就是将相同的行分到一组中 然后在处理数据
原创粉丝点击