韩顺平oracle视频笔记二(字段说明、表结构操作、日期插入、常用函数、主键ID)

一、字段说明

 

char 最大2000字符
varchar2 变长, 最大字符4000
clob 字符型,大对象 位4G
number -10的38次方到10的38次方,可以表示整数和小数
number(5,2) 表示一个小数,范围是-999.99到999.99
number(5) 表示一个整数,范围-99999到99999
date

年月日时分秒

datestamp 是oracle9i对date数据类型的扩展
blob 二进制数据,可以存放图片/声音

二、创建、修改、删除表

create table student(
    xh number(4),
    xm varchar(20),
    sex char(2),
    birthday date,
    sal number(7,2)
);

 

1、添加一个字段

alter table student add(classid number(2));

 

2、修改一个字段

alter table student modify(xm varchar2(30));

 

3、删除一个字段

alter table student drop column sal;

 

4、修改表的名字

rename student to stu;

 

5、删除表

drop table student;

 

三、插入数据

1、Oracle默认的日期格式是“DD-MON-YY”

insert into student values(1,'xiaoming','男','12-3月-2012',2345.6,2);

 

更改日期的格式

alter session set nls_date_format = 'yyyy-mm-dd';

insert into student values(2,'xiaohong','男','1988-11-30',2345.6,2);

 

2、插入空值

insert into student(xh,xm,sex,birthday)values(3,'aa','女',null);

select * from student where birthday is null;

 

3、删除数据,保留日志

delete from student;

 

4、删除数据,不写日志,无法找回删除记录,速度快

 

truncate table student;

 

四、ORACLE函数学习

字符函数

    1、lower(char)转换为小写格式

    SQL> select lower(job) from scott.emp ;

    

    2、upper(char)转换为大写格式

    SQL> select upper(job) from scott.emp ;

    

    3、length(char)字符串的长度

    SQL> select length(job) from scott.emp ;

    select * from scott.emp where length(ename)=5;

    

    4、substr(char,m,n)取字符串的子串

    SQL> select substr(name,2,3) from student;

    

    SUBSTR

    ------

    uan

    ium

    备注:查询name是从第2个字母开始取,长度为3个字符,第一个数从1开始,例子是huangbiao

    

    5、以首字母大写的方式显示所有员工的姓名?

    SQL> select upper(substr(ename,1,1))||lower(substr(ename,2,(length(ename)-1))) from scott.emp;

    

    6、replace(char1,search_string,replace_string)

    显示所有员工的姓名,用“我是A”替换“A”

    select replace(ename,'A','我是A') from scott.emp;

数学函数

    格式:round(n,[m])用户执行四舍五入

    n表示要被作用的数据

    m表示保留小数位

    

    trunc(55.66,1)==55.7

    trunc(n,[m])用于截取数字

    n表示要被作用的数据

    m表示保留小数位

    trunc(55.66,1)==55.6

    trunc(55.66,-1)==50

    trunc(55.66)==55

    

    mod(m,n)取模

    floor(n)小于或者等于n的最大整数

    ceil(n)大于或者是等于n的最小整数

    

    查询已经入职超过300个月的员工信息

    SQL> select * from scott.emp where sysdate>add_months(scott.emp.hiredate,300);

    备注:sysdate为当前日期

    add_months(scott.emp.hiredate,300)表示hiredate加上300个月

    SQL> select sysdate-scott.emp.hiredate  from scott.emp;

    package hb.com;

 

 

五、自动添加主键函数

    自动添加唯一的主键,使用sys_guid()属性,这里字符串的长度至少为36位

create table stu_user(
    id varchar(36) DEFAULT sys_guid() not null primary key ,
    name varchar2(20),
    password varchar2(20),
    dept_id varchar2(36) 
);
insert into stu_user (name,password,dept_id)values('huangbiao','hb','001');

 

 

猜你喜欢

转载自hbiao68.iteye.com/blog/1471942