讲到number数据类型在查询中的默认的显示格式与长度,我的印象中模糊想起在2014年的那个夏天有看过相应的书讲到,
说number数据类型在查询中默认显示是10位数的长度,超出10位之后,会以科学计数法格式显示,设计不到10位长度的
前面会以空格显示。其实这篇文章的最初想法不是这个,原来要探讨的是手机号码适合用哪种数据类型存储。
至于number类型在查询中显示格式的话题是在测试当中遇到的,顺便一起拿来测试。如果谁有更好想法的,可以在评论
分享一下,可以两个问题一起。
测试过程如下:
----最初的想法:探讨手机号码存储的数据类型:
--设计表格3个:
--T1:
create table t1(
id number(5)
constraint t1_pk primary key,
name varchar2(10) not null,
tel number(11),
created date);
--T2:
create table t2(
id number(5)
constraint t2_pk primary key,
name varchar2(10) not null,
tel number(12),
created date);
--T3: #该表的Tel字段采取字符类型
create table t3(
id number(5)
constraint t3_pk primary key,
name varchar2(10) not null,
tel varchar2(11),
created date);
--查看3个表得表结构:
SQL> desc t1
Name Null? Type
----------------------------------------- -------- ----------------------------
ID NOT NULL NUMBER(5)
NAME NOT NULL VARCHAR2(10)
TEL NUMBER(11)
CREATED DATE
SQL> desc t2
Name Null? Type
----------------------------------------- -------- ----------------------------
ID NOT NULL NUMBER(5)
NAME NOT NULL VARCHAR2(10)
TEL NUMBER(12)
CREATED DATE
SQL> desc t3
Name Null? Type
----------------------------------------- -------- ----------------------------
ID NOT NULL NUMBER(5)
NAME NOT NULL VARCHAR2(10)
TEL VARCHAR2(11)
CREATED DATE
--调整会话时间的显示格式:
alter session set nls_date_format = 'yyyy-mm-dd hh24:mi:ss';
--分别往3个表T1、T2与T3插入数据:
insert into t1 values(12546,'enmo',13645789452,sysdate);
insert into t1 values(12547,'ocmu',13645789453,sysdate);
insert into t2 values(12548,'ppqq',13645789454,sysdate);
insert into t2 values(12549,'qqpp',13645789456,sysdate);
insert into t3 values(12550,'yyww','13645789456',sysdate);
insert into t3 values(12551,'wwyy','13645789457',sysdate);
--分别查看3个表的内容:
SQL> select * from t1;
ID NAME TEL CREATED
---------- ---------- ---------- -------------------
12546 enmo 1.3646E+10 2016-10-21 16:29:35
12547 ocmu 1.3646E+10 2016-10-21 16:29:38
SQL> select * from t2;
ID NAME TEL CREATED
---------- ---------- ---------- -------------------
12548 ppqq 1.3646E+10 2016-10-21 16:29:45
12549 qqpp 1.3646E+10 2016-10-21 16:29:47
SQL> select * from t3;
ID NAME TEL CREATED
---------- ---------- ----------- -------------------
12550 yyww 13645789456 2016-10-21 16:30:12
12551 wwyy 13645789457 2016-10-21 16:30:14
从以上的查询数据看,t1,t2没有达到我们想看到的设计效果,因为Tel字段存储的是11位的手机号码,只有t3是完整显示出来11位。
--创建t5表: #设计了tel字段长度为13,两位小数,纯属实验添加对照组需求,实际没必要
SQL> create table t5(
2 id number(5)
3 constraint t5_pk primary key,
4 name varchar2(10) not null,
5 tel number(13,2),
6 created date);
--查看表结构:
Table created.
SQL> desc t5;
Name Null? Type
----------------------------------------- -------- ----------------------------
ID NOT NULL NUMBER(5)
NAME NOT NULL VARCHAR2(10)
TEL NUMBER(13,2)
CREATED DATE
--插入数据:
insert into t5 values(12555,'hhss',13945789455,sysdate);
insert into t5 values(12556,'hhll',13945789456,sysdate);
--查看表中的数据:
SQL> select * from t5;
ID NAME TEL CREATED
---------- ---------- ---------- -------------------
12555 hhss 1.3946E+10 2016-10-21 16:51:33
12556 hhll 1.3946E+10 2016-10-21 16:51:40
--查看两个字段的实际长度:
SQL> select length(id),length(tel) from t1;
LENGTH(ID) LENGTH(TEL)
---------- -----------
5 11
5 11
--指定tel字段显示的长度:
SQL> col tel for 99999999999;
SQL> select * from t5;
ID NAME TEL CREATED
---------- ---------- ------------ -------------------
12555 hhss 13945789455 2016-10-21 16:51:33
12556 hhll 13945789456 2016-10-21 16:51:40
SQL> select * from t1;
ID NAME TEL CREATED
---------- ---------- ------------ -------------------
12546 enmo 13645789452 2016-10-21 16:29:35
12547 ocmu 13645789453 2016-10-21 16:29:38
有更多想法的朋友,可以在评论了分享。谢谢观赏!
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/31392094/viewspace-2126923/
,如需转载,请注明出处,否则将追究法律责任。