数据类型
标量类型(SCALAR,或称基本数据类型):用于保存单个值,例如:字符串、数字、日期、布尔
复合类型(COMPOSITE):复合类型可以在内部存放多种数值,类似于多个变量的集合,例如:记录类型、嵌套表、索引表、可变数组等都称为符合类型
引用类型 (REFERENCE):用于指向另一不同的对象,例如:REF CURSOR、REF
LOB类型:大数据类型,最多可以存储4G的信息,主要用来处理二进制数据;
常用标量类型
数值型:NUMBER([数据总长度,小数位总长度) BINARY_INTEGER
字符型;VARCHAR2(长度) ROWID
日期型:DATE 、TIMESTAMP
数值型数据可以保存整数、浮点数
数据类型--范例
观察CHAR和VARCHAR2的区别
DECLARE
v_info_char CHAR(10);
v_info_varchar VARCHAR2(10);
BEGIN
v_info_char:='VDATA';
v_info_varchar:='VDATA';
DBMS_OUTPUT.put_line('v_info_char:'||LENGTH(v_info_char));
DBMS_OUTPUT.put_line('v_info_varchar:'||LENGTH(v_info_varchar));
END;
v_info_char:10
v_info_varchar:5
验证NCHAR和NVARCHAR2
DECLARE
v_info_nchar NCHAR(20);
v_info_nvarchar NVARCHAR2(20);
BEGIN
v_info_nchar:='MR.HONG';
v_info_nvarchar:='13820305037';
DBMS_OUTPUT.put_line('v_info_nchar:'||LENGTH(v_info_nchar));
DBMS_OUTPUT.put_line('v_info_nvarchar:'||LENGTH(v_info_nvarchar));
END;
/
v_info_nchar:20
v_info_nvarchar:11
日期型
DATE型
定义DATE型变量
DECLARE
v_date1 DATE:=SYSDATE;
v_date2 DATE:=SYSTIMESTAMP;
v_date3 DATE:='2018-05-01'; --TO_DATE(‘2018-05-01 23:59:59’,‘yyyy-mm-dd hh24:mi:ss’
BEGIN
DBMS_OUTPUT.put_line(TO_CHAR(v_date1,'yyyy-mm-dd hh24:mi:ss'));
DBMS_OUTPUT.put_line(TO_CHAR(v_date2,'yyyy-mm-dd hh24:mi:ss'));
DBMS_OUTPUT.put_line(TO_CHAR(v_date3,'yyyy-mm-dd hh24:mi:ss'));
END;
/
2018-05-02 15:28:39
2018-05-02 15:28:39
2018-05-01 00:00:00
TIMESTAMP比DATE类型更精确
定义TIMESTAMP型变量
DECLARE
v_timestamp1 DATE:=SYSDATE;
v_timestamp2 TIMESTAMP:=SYSDATE;
v_timestamp3 TIMESTAMP:=SYSTIMESTAMP;
BEGIN
DBMS_OUTPUT.put_line(v_timestamp1);
DBMS_OUTPUT.put_line(v_timestamp2);
DBMS_OUTPUT.put_line(v_timestamp3);
END;
/
2018-05-02 15:49:46
02-MAY-18 03.49.46.000000 PM
02-MAY-18 03.49.46.541681 PM
v_timestamp2 TIMESTAMP WITH TIME ZONE:=SYSTIMESTAMP;
v_timestamp3 TIMESTAMP WITH LOCAL TIME ZONE:=SYSTIMESTAMP;
02-MAY-18 04.10.24.412348 PM +08:00
02-MAY-18 04.10.24.412353 PM
定义布尔型变量
DECLARE
v_flag BOOLEAN;
BEGIN
v_flag:=true;
IF v_flag THEN
DBMS_OUTPUT.put_line('fullfill');
END IF;
END;
/
子类型
DECLARE
SUBTYPE score_subtype IS NUMBER(5,2) NOT NULL;
v_score score_subtype:=99.35;
BEGIN
DBMS_OUTPUT.put_line('score:'||v_score);
END;
/
DECLARE
SUBTYPE string_subtype IS VARCHAR2(200);
v_company string_subtype;
BEGIN
v_company:='www.vdedu.com';
DBMS_OUTPUT.put_line(v_company);
END;
/