趣味SQL——创建指定的数据类型

原创作品,出自 “深蓝的blog” 博客,深蓝的blog:http://blog.csdn.net/huangyanlong/article/details/46908843 

 

趣味SQL——创建指定的数据类型

 

在一篇文章上看到“提出过能够创建指定的数据类型”。于是想尝试着创建一下看看。

可是没有按预想的那样成功~~

 

 create type  MyName as object (first varchar2(20),second varchar2(20) );

 

create table  newtype_test(

ID varchar2(32),

newname  MyName

);

 

看一下创建的表结构:有两个列。

在查询数据看看效果:有三个列!

select  * from   newtype_test;

 

 

以下就是实验插入数据看看了,以命令行的方式插入,不成功,几次尝试例如以下:

SQL> insert into newtype_test(id,newname.first,newname.second) values(2,'shen','lan');

insert into newtype_test(id,newname.first,newname.second) values(2,'shen','lan')

ORA-00904: "NEWNAME"."SECOND":标识符无效

SQL> insert into newtype_test(id,newname.first,newname.second) values(2,shen,lan);

insert into newtype_test(id,newname.first,newname.second) values(2,shen,lan)

ORA-00984:列在此处不同意

SQL> insert into newtype_test(id,newname.first,newname.second) values(2,(shen),(lan));

insert into newtype_test(id,newname.first,newname.second) values(2,(shen),(lan))

ORA-00984:列在此处不同意

SQL> insert into newtype_test(id,newname) values(2,'shenlan');

insert into newtype_test(id,newname) values(2,'shenlan')

ORA-00932:数据类型不一致:应为 HYL.MYNAME,但却获得 CHAR

SQL> insert into newtype_test(id,newname) values(2,shenlan);

insert into newtype_test(id,newname) values(2,shenlan)

ORA-00984:列在此处不同意

SQL> insert into newtype_test(id,newname) values(2,(shen,lan));

insert into newtype_test(id,newname) values(2,(shen,lan))

ORA-00907:缺失右括号

SQL> insert into newtype_test(id,newname) values(2,(shen),(lan));

insert into newtype_test(id,newname) values(2,(shen),(lan))

ORA-00913:值过多

SQL> insert into newtype_test(id,newname) values(2,(shen));

insert into newtype_test(id,newname) values(2,(shen))

ORA-00984:列在此处不同意

SQL> insert into newtype_test(id,newname(first,second)) values(2,('shen','lan'));

insert into newtype_test(id,newname(first,second)) values(2,('shen','lan'))

ORA-00917:缺失逗号

SQL> insert into newtype_test(id,newname,(first,second)) values(2,('shen','lan'));

insert into newtype_test(id,newname,(first,second)) values(2,('shen','lan'))

ORA-01747: user.table.column, table.column或列说明无效

SQL> insert into newtype_test(id,newname.(first,second)) values(2,('shen','lan'));

insert into newtype_test(id,newname.(first,second)) values(2,('shen','lan'))

ORA-01747: user.table.column, table.column或列说明无效

SQL> insert into newtype_test.newname.first values('shen');

insert into newtype_test.newname.first values('shen')

ORA-00926:缺失 VALUESkeyword

SQL> insert into newtype_test.newname values (shen);

insert into newtype_test.newname values (shen)

ORA-00942:表或视图不存在

SQL> insert into newtype_test.newname values (shen,lan);

insert into newtype_test.newname values (shen,lan)

ORA-00942:表或视图不存在

SQL> insert into newtype_test.newname(first,second) values(shen,lan);

insert into newtype_test.newname(first,second) values(shen,lan)

ORA-00942:表或视图不存在

SQL> insert into newtype_test(hyl.tname.first,hyl.tname.second) values(1,2);

insert into newtype_test(hyl.tname.first,hyl.tname.second) values(1,2)

ORA-00904: "HYL"."TNAME"."SECOND":标识符无效

尝试了半天,也没弄明确,怎么插入,于是尝试了一下用工具插入数据:

select   * from   newtype_test for  update;

 

居然成功插入了,查询有值。例如以下:

 

于是,尝试用工具逆向导出插入语句来看一看。例如以下:

 

粘贴后。例如以下所看到的:

prompt  Importing  table newtype_test...

set feedback off

set   define off

insertinto newtype_test (ID,  NEWNAME.FIRST,  NEWNAME.SECOND)

values ('1','huang','yanlong');

prompt  Done.

验证,按上面的书写方式插入数据(这个方式之前事实上是试过的啊!

),果然。依然是不成功。

这是怎么回事呢?于是封建迷信的把这个变成脚本,运行一下看看。例如以下:还是报错!

至此。重复尝试几次,通过PL/SQL Developer工具手工插入数据是可行的。但为什么用指令运行就出问题了?是语法不正确嘛~~

(吐血中。

。。)

这就是SQL的趣味~~~~

最后,看一下,查询结果:

能够看到,在查询newname这个字段时,显示出的的确是两个拆分的列。

 

补充时间:2015716日星期四

过来纠个错。上面的实验有点臆想了。我们查看一下官方文档,看看正统的解释是如何的:

Object Tables

     An Oracle object type is a user-defined type with a name, attributes, and methods. Object types make it possible to model real-world entities such as customers and purchase orders as objects in the database.

An object type defines a logical structure, but does not create storage.Example 2-5 creates an object type named department_typ.

Example 2-5 Object Type

CREATE TYPE department_typ AS OBJECT

   ( d_name     VARCHAR2(100),

     d_address  VARCHAR2(200) );

/

An object table is a special kind of table in which each row represents an object. TheCREATE TABLE statement inExample 2-6 creates an object table named departments_obj_t of the object typedepartment_typ. The attributes (columns) of this table are derived from the definition of the object type. TheINSERT statement inserts a row into this table.

Example 2-6 Object Table

CREATE TABLE departments_obj_t OF department_typ;

INSERT INTO departments_obj_t

  VALUES ('hr', '10 Main St, Sometown, CA');

Like a relational column, an object table can contain rows of just one kind of thing, namely, object instances of the same declared type as the table. By default, every row object in an object table has an associated logical object identifier (OID) that uniquely identifies it in an object table. The OID column of an object table is a hidden column.

oracle的对象表。

假设上面的实验我们又一次来做一下,例如以下就能够实现了。

create table  myname_test   OF MyName;

SELECT *  FROM  myname_test;

insert into   myname_test  VALUES   ('huang',  'yanlong');

--这次插入是能够的

commit;

SELECT  *   FROM  myname_test;

小结:

        Oracle对象类型是具有名称、属性、和方法的用户定义类型。对象类型使得对现实世界中的实体(比如产品经理和项目名称等),作为对象在数据库中进行建模成为可能。对象表是一种特殊的表,当中每一行表示一个对象。

 

补充时间:2015716日星期四

过来纠个错,上面的实验有点臆想了,我们查看一下官方文档,看看正统的解释是如何的:

Object Tables

      An Oracle object type is a user-defined type with a name, attributes, and methods. Object types make it possible to model real-world entities such as customers and purchase orders as objects in the database.

     An object type defines a logical structure, but does not create storage. Example 2-5 creates an object type named department_typ.

     Example 2-5 Object Type

CREATE   TYPE   department_typ   AS   OBJECT

   ( d_name     VARCHAR2(100),

   d_address  VARCHAR2(200) );

/

    An object table is a special kind of table in which each row represents an object. The CREATE TABLE statement in Example 2-6 creates an object table named departments_obj_t of the object type department_typ. The attributes (columns) of this table are derived from the definition of the object type. The INSERT statement inserts a row into this table.

   Example 2-6 Object Table

CREATE   TABLE   departments_obj_t   OF   department_typ;

INSERT    INTO    departments_obj_t

VALUES  ('hr',  '10 Main St,  Sometown, CA');

        Like a relational column, an object table can contain rows of just one kind of thing, namely, object instances of the same declared type as the table. By default, every row object in an object table has an associated logical object identifier (OID) that uniquely identifies it in an object table. The OID column of an object table is a hidden column.

        oracle的对象表。假设上面的实验我们又一次来做一下。例如以下就能够实现了。

create    table    myname_test    OF   MyName;

SELECT   *    FROM   myname_test;

insert   into   myname_test    VALUES   ('huang', 'yanlong');

--这次插入是能够的

commit;

SELECT   *   FROM   myname_test;

 

小结:

       Oracle对象类型是具有名称、属性、和方法的用户定义类型。对象类型使得对现实世界中的实体(比如产品经理和项目名称等),作为对象在数据库中进行建模成为可能。对象表是一种特殊的表,当中每一行表示一个对象。

 

*******************************************蓝的成长记系列****************************************************

原创作品。出自 “深蓝的blog” 博客,欢迎转载,转载时请务必注明出处(http://blog.csdn.net/huangyanlong)。

蓝的成长记——追逐DBA(1):奔波于路上,挺进山东

蓝的成长记——追逐DBA(2):安装!安装!久违的记忆。引起我对DBA的又一次认知

蓝的成长记——追逐DBA(3):古董上操作。数据导入导出成了问题

蓝的成长记——追逐DBA(4):追忆少年情愁,再探oracle安装(Linux下10g、11g)

蓝的成长记——追逐DBA(5):不谈技术谈业务,恼人的应用系统

蓝的成长记——追逐DBA(6): 做事与做人:小技术,大为人

蓝的成长记——追逐DBA(7):基础命令,地基之石

蓝的成长记——追逐DBA(8):重拾SP报告,回顾oracle的STATSPACK实验

蓝的成长记——追逐DBA(9):国庆渐去。追逐DBA,新规划。新启程

蓝的成长记——追逐DBA(10):飞刀防身。熟络而非专长:摆弄中间件Websphere

蓝的成长记——追逐DBA(11):回家后的安逸,晕晕乎乎醒了过来

蓝的成长记——追逐DBA(12):七天七收获的SQL

蓝的成长记——追逐DBA(13):协调硬件厂商,六个故事:所见所感的“server、存储、交换机......”

蓝的成长记——追逐DBA(14):难忘的“云”端,起步的hadoop部署

蓝的成长记——追逐DBA(15):以为FTP非常“简单”。谁成想一波三折

蓝的成长记——追逐DBA(16):DBA也喝酒。被捭阖了

蓝的成长记——追逐DBA(17):是分享。还是消费,在后IOE时代学会成长

******************************************************************************************************************

********************************************足球与oracle系列*************************************************

原创作品,出自 “深蓝的blog” 博客。欢迎转载,转载时请务必注明出处(http://blog.csdn.net/huangyanlong)。

足球与oracle系列(1):32路诸侯点兵。oracle32进程联盟 之A组巴西SMON进程的大局观

足球与oracle系列(2):巴西揭幕战预演。oracle体系结构杂谈

足球与oracle系列(3):oracle进程排名,世界杯次回合即将战罢!

足球与oracle系列(4):从巴西慘败于德国,想到,差异的RAC拓扑对照! 

足球与oracle系列(5):fifa14游戏缺失的directX库类比于oracle的rpm包!

足球与oracle系列(6):伴随建库的亚洲杯——加油中国队

******************************************************************************************************************

原文地址:https://www.cnblogs.com/lytwajue/p/6755251.html