加入Oracle Xpress Edition中的查询

时间:2016-04-08 07:10:22

标签: inner-join oracle-sqldeveloper oracle-xe

您好我正在创建一个名为Category的表,插入两行。

当我加入此表时,输出打印多行。

我的表格是: - 类别

CREATE TABLE Category(
       ID int NOT NULL Primary key, 
       Code varchar(50), 
       Name varchar(200), 
       Notes varchar(500),
       Image1 varchar(250),
       Image2 varchar(250), 
       Image3 varchar(250), 
       CreatedBy int NOT NULL, 
       PageTitle varchar(100), 
       MetaName varchar(200),
       MetaDescription varchar(500), 
       MetaKeywords varchar(500),
       ERPReferencesSysteID int, 
       ERPReferencesID int,
       AssignedTo int,
       Status int );

插入查询: -

insert into Category values (902,'Code2', 'NAME2','Note2', 'Image12', 
  'Image22','Image32', 2016, 'PageTitle2','MetaName2', 'MetaDescription2', 
  'MetaKeyword2',02, 08, 2,1);

我的加入查询: -

SELECT DISTINCT info.COLUMN_NAME AS NAME, info.OWNER AS TABLESCHEMA,
 info.TABLE_NAME,UIC.COLUMN_POSITION AS POSITION,info.NULLABLE AS ISNULLABLE,
 info.DATA_TYPE AS DATATYPE,tc.CONSTRAINT_TYPE AS CONSTRAINTTYPE, 
 tc.R_OWNER AS REFERENCESCHEMA FROM all_tab_columns  
 info join  all_constraints  tc on info.TABLE_NAME = tc.TABLE_NAME 
 join user_cons_columns rk on info.TABLE_NAME = rk.TABLE_NAME 
 join USER_IND_COLUMNS UIC on UIC.TABLE_NAME = info.TABLE_NAME 
 where info.TABLE_NAME= 'CATEGORY' AND info.owner ='SAN' ORDER BY 
 UIC.COLUMN_POSITION ASC;

当我执行此连接时,查询行会多次显示。

我在这个查询中做错了什么?

2 个答案:

答案 0 :(得分:0)

使用INNER JOIN来加入你的桌子。

SELECT DISTINCT info.COLUMN_NAME AS NAME, info.OWNER AS TABLESCHEMA,
info.TABLE_NAME,UIC.COLUMN_POSITION AS POSITION,info.NULLABLE AS ISNULLABLE,
info.DATA_TYPE AS DATATYPE,tc.CONSTRAINT_TYPE AS CONSTRAINTTYPE, 
tc.R_OWNER AS REFERENCESCHEMA 
FROM all_tab_columns info 
INNER JOIN  all_constraints  tc ON info.TABLE_NAME = tc.TABLE_NAME 
INNER JOIN user_cons_columns rk ON info.TABLE_NAME = rk.TABLE_NAME 
INNER JOIN USER_IND_COLUMNS UIC ON UIC.TABLE_NAME = info.TABLE_NAME 
WHERE info.TABLE_NAME = 'CATEGORY' 
AND info.owner ='SAN' 
ORDER BY UIC.COLUMN_POSITION ASC;

答案 1 :(得分:0)

表格中有三个限制条件;主键和两个非空约束。其中一个是冗余的,因为主键列无论如何都隐含地不为空。

因此user_cons_columns中有三行。其中两个是id

SELECT uc.constraint_name, uc.constraint_type, ucc.column_name
FROM user_constraints uc
JOIN user_cons_columns ucc ON ucc.constraint_name = uc.constraint_name
WHERE uc.table_name = 'CATEGORY';

CONSTRAINT_NAME                C COLUMN_NAME        
------------------------------ - --------------------
SYS_C00110620                  C ID                  
SYS_C00110621                  C CREATEDBY           
SYS_C00110622                  P ID                  

user_ind_columns的{​​{1}}中还有一行,但id中没有一行,这是另一个非空约束。

createdby

SELECT ui.index_name, uic.column_name FROM user_indexes ui JOIN user_ind_columns uic ON uic.index_name = ui.index_name WHERE ui.table_name = 'CATEGORY'; INDEX_NAME COLUMN_NAME ------------------------------ -------------------- SYS_C00110622 ID 中的id行与user_cons_columns中的user_ind_columns行匹配,因此您在结果集中获得两行。 distinct没有帮助,因为您在结果集中包含约束类型,因此您会看到P和C的单独行。

您也未在联接中包含column_name;所以all_tab_columns中的所有16行都加入了所有三个约束及其列。当您使用内部联接时,您似乎只想显示主键及其索引,因此这似乎是一种冗长的方式:

SELECT distinct atc.column_name AS NAME, atc.owner AS tableschema,
 atc.table_name, aic.column_position AS position, atc.nullable AS isnullable,
 atc.data_type AS datatype, ac.constraint_type AS constrainttype, 
 ac.r_owner AS referenceschema
FROM all_tab_columns atc
JOIN all_constraints ac
ON ac.owner = atc.owner
AND ac.table_name = atc.table_name 
JOIN all_cons_columns acc
ON acc.table_name = atc.table_name 
AND acc.column_name = atc.column_name
JOIN all_ind_columns aic
ON aic.table_owner = atc.owner
AND aic.table_name = atc.table_name 
AND aic.column_name = atc.column_name
WHERE atc.table_name= 'CATEGORY'
AND atc.owner = 'SAN'
AND ac.constraint_type = 'P'
ORDER BY aic.column_position ASC;

为P约束ID列获取单行。如果您打算显示所有列,则需要外连接。