2014-11-23 113 views
-1

我已創建我的表並填充它,但我不確定如何編寫查詢以驗證所有表的參照完整性。SQL:驗證所有表的參照完整性的查詢

這就是我需要:

  • SELECT列表中的所有主鍵
  • 從通訊錄中的所有表
  • WHERE顯示所有關係

這是我到目前爲止有:

SELECT 
    categId, zipCode, suppId, acctId, prodId, orderId 
From 
    CATEGORY, ZIP, SUPPLIER, ACCOUNT, SUPPLIER_REGION, PRODUCT, PROD_DETAIL, ORDERX, ORDER_LINE_ITEM 

我是n ot確定要放在哪裏。

我的教授想要命令來驗證參照完整性。他還有很多其他的東西,但這是我不知道該怎麼做的唯一的事情。

我添加了我的表格。

create table CATEGORY (
    categId varchar (8), 
    categIdParent varchar (8), 
    constraint CATEGORY_PK primary key (categId), 
    constraint CATEGORY_FK foreign key (categIdParent) references CATEGORY (categId) 
); 

create table ZIP(
    zipCode int (5), 
    city varChar (16), 
    state varChar (16), 
    constraint ZIP_PK primary key (zipCode) 
); 

create table SUPPLIER (
    supplId int (9), 
    supplName varChar (24), 
    supplStatus varChar (16), 
    supplYearEstabl date, 
    zipCode int (5), 
    constraint SUPPLIER_PK primary key (supplId), 
    constraint SUPPLIER_FK Foreign Key (zipCode) references ZIP (zipCode) 
); 

CREATE TABLE SUPPLIER_REGION (
    supplId int (9), 
    region varChar (24), 
    constraint SUPPLIER_REGION_PK primary key (supplId, region), 
    constraint SUPPLIER_REGION_FK foreign key (supplId) references SUPPLIER (supplId) 
); 

create table PRODUCT (
    prodId int (11), 
    prodDescr varChar (256), 
    prodType varChar (12), 
    prodModel Char (16), 
    prodPlaceOrigin Char (16), 
    prodPrice decimal (10,2), 
    prodMinQty int (8), 
    supplId int (9), 
    categId varchar (8), 
    constraint PRODUCT_PK primary key (prodId), 
    constraint PRODUCT_FK1 foreign key (supplId) references SUPPLIER (supplId), 
    constraint PRODUCT_FK2 foreign key (categId) references CATEGORY (categId) 
); 

create table PROD_DETAIL (
    prodId int (11), 
    prodDetailNo int (8), 
    prodDetailName varChar (32), 
    prodDetailValue decimal (10,2), 
    constraint PROD_DETAIL_PK primary key (prodID, prodDetailNo), 
    constraint PROD_DETAIL_FK foreign key (prodID) references PRODUCT (prodID) 
); 

create table ACCOUNT (
    acctId varchar(50), 
    acctName varChar (24), 
    acctDept varChar (16), 
    acctTitle varChar (16), 
    acctGender Char (1), 
    acctEmail varChar (24), 
    acctAddr varChar (24), 
    zipCode int (5), 
    acctPhone int (11), 
    constraint ACCOUNT_PK primary key (acctId), 
    constraint ACCOUNT_FK foreign key (zipCode) references ZIP (zipCode) 
); 

create table ORDERX (
    orderId int (8), 
    orderPayMethod varChar (16), 
    orderShipDate date, 
    acctId varchar (50), 
    constraint ORDER_PK primary key (orderId), 
    constraint ORDER_FK foreign key (acctId) references ACCOUNT (acctId) 
); 

create table ORDER_LINE_ITEM (
    orderId int (8), 
    orderLineNo int (8), 
    orderLineQty int (8), 
    orderLineUnit int (8), 
    orderLinePrice decimal (10,2), 
    prodId int (11), 
    constraint ORDER_LINE_ITEM_PK primary key (orderId, orderLineNo), 
    constraint ORDER_LINE_ITEM_FK1 foreign key (orderId) references ORDERX (orderId), 
    constraint ORDER_LINE_ITEM_FK2 foreign key (prodId) references PRODUCT (prodId) 
); 
+1

那麼,哪個RDBMS? – Strawberry 2014-11-23 22:39:26

+2

爲什麼你需要「驗證」參考完整性?你忘了定義外鍵約束嗎? – 2014-11-23 22:41:56

回答

0

對於SQL Server來說,一個好方法是在SSMS中創建一個圖表。當您將每個表放入圖表中時,FK關係將呈現爲表和主鍵之間的線將顯示爲「關鍵」圖標。