Difference between revisions of "Sales.Individual (table)"

From dbscript Online Help
Jump to: navigation, search
Line 12: Line 12:
  
  
 +
=== Columns ===
 
{| border="1" cellpadding="5" cellspacing="0" style="border-collapse:collapse"
 
{| border="1" cellpadding="5" cellspacing="0" style="border-collapse:collapse"
 
|- style="background:silver"
 
|- style="background:silver"
Line 18: Line 19:
 
| '''Nullable
 
| '''Nullable
 
| '''Default
 
| '''Default
| '''Description / PK / Index
+
| '''Description
 
 
 
|- valign="top"
 
|- valign="top"
Line 25: Line 26:
 
| not null
 
| not null
 
|  
 
|  
| Unique customer identification number. Foreign key to Customer.CustomerID.<br />PK_Individual_CustomerID
+
| Unique customer identification number. Foreign key to Customer.CustomerID.
 
|- valign="top"
 
|- valign="top"
 
| ContactID
 
| ContactID
Line 31: Line 32:
 
| not null
 
| not null
 
|  
 
|  
| Identifies the customer in the Contact table. Foreign key to Contact.ContactID.<br />
+
| Identifies the customer in the Contact table. Foreign key to Contact.ContactID.
 
|- valign="top"
 
|- valign="top"
 
| Demographics
 
| Demographics
Line 37: Line 38:
 
| null
 
| null
 
|  
 
|  
| Personal information such as hobbies, and income collected from online shoppers. Used for sales analysis.<br />PXML_Individual_DemographicsXMLPATH_Individual_DemographicsXMLPROPERTY_Individual_DemographicsXMLVALUE_Individual_Demographics
+
| Personal information such as hobbies, and income collected from online shoppers. Used for sales analysis.
 
|- valign="top"
 
|- valign="top"
 
| ModifiedDate
 
| ModifiedDate
Line 43: Line 44:
 
| not null
 
| not null
 
| (getdate())
 
| (getdate())
| Date and time the record was last updated.<br />
+
| Date and time the record was last updated.
 
|}
 
|}
  
 +
=== Primary Key ===
 +
{| border="1" cellpadding="5" cellspacing="0" style="border-collapse:collapse"
 +
|- style="background:silver"
 +
| '''Primary Key
 +
| '''Columns
 +
|-
 +
| PK_Individual_CustomerID
 +
| CustomerID
 +
|}
  
 +
=== Indexes ===
 +
{| border="1" cellpadding="5" cellspacing="0" style="border-collapse:collapse"
 +
|- style="background:silver"
 +
| '''Index
 +
| '''Type
 +
| '''Columns
 +
 +
|-
 +
| PXML_Individual_Demographics
 +
|
 +
| Demographics
 +
|-
 +
| XMLPATH_Individual_Demographics
 +
|
 +
| Demographics
 +
|-
 +
| XMLPROPERTY_Individual_Demographics
 +
|
 +
| Demographics
 +
|-
 +
| XMLVALUE_Individual_Demographics
 +
|
 +
| Demographics
 +
|}
  
 +
=== Foreign Keys ===
 
{| border="1" cellpadding="5" cellspacing="0" style="border-collapse:collapse"
 
{| border="1" cellpadding="5" cellspacing="0" style="border-collapse:collapse"
 
|- style="background:silver"
 
|- style="background:silver"
Line 64: Line 99:
 
|}
 
|}
  
 
+
=== Triggers ===
 
{| border="1" cellpadding="5" cellspacing="0" style="border-collapse:collapse"
 
{| border="1" cellpadding="5" cellspacing="0" style="border-collapse:collapse"
 
|- style="background:silver"
 
|- style="background:silver"
Line 75: Line 110:
 
|}
 
|}
  
 +
Trigger iuIndividual
 +
<pre>
 +
CREATE TRIGGER [Sales].[iuIndividual] ON [Sales].[Individual]
 +
AFTER INSERT, UPDATE NOT FOR REPLICATION AS
 +
BEGIN
 +
    DECLARE @Count int;
 +
 +
    SET @Count = @@ROWCOUNT;
 +
    IF @Count = 0
 +
        RETURN;
 +
 +
    SET NOCOUNT ON;
 +
 +
    -- Only allow the Customer to be a Store OR Individual
 +
    IF EXISTS (SELECT * FROM inserted INNER JOIN [Sales].[Store]
 +
        ON inserted.[CustomerID] = [Sales].[Store].[CustomerID])
 +
    BEGIN
 +
        -- Rollback any active or uncommittable transactions
 +
        IF @@TRANCOUNT > 0
 +
        BEGIN
 +
            ROLLBACK TRANSACTION;
 +
        END
 +
    END;
  
 +
    IF UPDATE([CustomerID]) OR UPDATE([Demographics])
 +
    BEGIN
 +
        UPDATE [Sales].[Individual]
 +
        SET [Sales].[Individual].[Demographics] = N'<IndividualSurvey xmlns="http://schemas.microsoft.com/sqlserver/2004/07/adventure-works/IndividualSurvey">
 +
            <TotalPurchaseYTD>0.00</TotalPurchaseYTD>
 +
            </IndividualSurvey>'
 +
        FROM inserted
 +
        WHERE [Sales].[Individual].[CustomerID] = inserted.[CustomerID]
 +
            AND inserted.[Demographics] IS NULL;
 +
       
 +
        UPDATE [Sales].[Individual]
 +
        SET [Demographics].modify(N'declare default element namespace "http://schemas.microsoft.com/sqlserver/2004/07/adventure-works/IndividualSurvey";
 +
            insert <TotalPurchaseYTD>0.00</TotalPurchaseYTD>
 +
            as first
 +
            into (/IndividualSurvey)[1]')
 +
        FROM inserted
 +
        WHERE [Sales].[Individual].[CustomerID] = inserted.[CustomerID]
 +
            AND inserted.[Demographics] IS NOT NULL
 +
            AND inserted.[Demographics].exist(N'declare default element namespace
 +
                "http://schemas.microsoft.com/sqlserver/2004/07/adventure-works/IndividualSurvey";
 +
                /IndividualSurvey/TotalPurchaseYTD') <> 1;
 +
    END;
 +
END;
 +
</pre>
 +
 +
=== References ===
 
{| border="1" cellpadding="5" cellspacing="0" style="border-collapse:collapse"
 
{| border="1" cellpadding="5" cellspacing="0" style="border-collapse:collapse"
 
|- style="background:silver"
 
|- style="background:silver"
Line 85: Line 169:
 
| '''Child Object
 
| '''Child Object
  
 +
|-
 +
| Schema
 +
| Schema
 +
| [[Sales_(schema)|Sales]]
 +
 +
|
 +
|
 
|-
 
|-
 
| Data Type
 
| Data Type
Line 108: Line 199:
 
|}
 
|}
  
 
+
=== Dependencies ===
 
{| border="1" cellpadding="5" cellspacing="0" style="border-collapse:collapse"
 
{| border="1" cellpadding="5" cellspacing="0" style="border-collapse:collapse"
 
|- style="background:silver"
 
|- style="background:silver"
Line 161: Line 252:
 
| iStore
 
| iStore
 
|}
 
|}
 
  
  

Revision as of 00:07, 3 February 2010

wikibot

Table Sales.Individual
Description Demographic data about customers that purchase Adventure Works products online.


Columns

Column Data Type Nullable Default Description
CustomerID int not null Unique customer identification number. Foreign key to Customer.CustomerID.
ContactID int not null Identifies the customer in the Contact table. Foreign key to Contact.ContactID.
Demographics xml null Personal information such as hobbies, and income collected from online shoppers. Used for sales analysis.
ModifiedDate datetime not null (getdate()) Date and time the record was last updated.

Primary Key

Primary Key Columns
PK_Individual_CustomerID CustomerID

Indexes

Index Type Columns
PXML_Individual_Demographics Demographics
XMLPATH_Individual_Demographics Demographics
XMLPROPERTY_Individual_Demographics Demographics
XMLVALUE_Individual_Demographics Demographics

Foreign Keys

Relation Column Referenced Column
Person.Contact ContactID ContactID
Sales.Customer CustomerID CustomerID

Triggers

Triggers Type
iuIndividual ON INSERT UPDATE

Trigger iuIndividual

CREATE TRIGGER [Sales].[iuIndividual] ON [Sales].[Individual] 
AFTER INSERT, UPDATE NOT FOR REPLICATION AS 
BEGIN
    DECLARE @Count int;

    SET @Count = @@ROWCOUNT;
    IF @Count = 0 
        RETURN;

    SET NOCOUNT ON;

    -- Only allow the Customer to be a Store OR Individual
    IF EXISTS (SELECT * FROM inserted INNER JOIN [Sales].[Store] 
        ON inserted.[CustomerID] = [Sales].[Store].[CustomerID]) 
    BEGIN
        -- Rollback any active or uncommittable transactions
        IF @@TRANCOUNT > 0
        BEGIN
            ROLLBACK TRANSACTION;
        END
    END;

    IF UPDATE([CustomerID]) OR UPDATE([Demographics]) 
    BEGIN
        UPDATE [Sales].[Individual] 
        SET [Sales].[Individual].[Demographics] = N'<IndividualSurvey xmlns="http://schemas.microsoft.com/sqlserver/2004/07/adventure-works/IndividualSurvey"> 
            <TotalPurchaseYTD>0.00</TotalPurchaseYTD> 
            </IndividualSurvey>' 
        FROM inserted 
        WHERE [Sales].[Individual].[CustomerID] = inserted.[CustomerID] 
            AND inserted.[Demographics] IS NULL;
        
        UPDATE [Sales].[Individual] 
        SET [Demographics].modify(N'declare default element namespace "http://schemas.microsoft.com/sqlserver/2004/07/adventure-works/IndividualSurvey"; 
            insert <TotalPurchaseYTD>0.00</TotalPurchaseYTD> 
            as first 
            into (/IndividualSurvey)[1]') 
        FROM inserted 
        WHERE [Sales].[Individual].[CustomerID] = inserted.[CustomerID] 
            AND inserted.[Demographics] IS NOT NULL 
            AND inserted.[Demographics].exist(N'declare default element namespace 
                "http://schemas.microsoft.com/sqlserver/2004/07/adventure-works/IndividualSurvey"; 
                /IndividualSurvey/TotalPurchaseYTD') <> 1;
    END;
END;

References

Dependency Type Object Type Referenced Object Child Type Child Object
Schema Schema Sales
Data Type XML Schema Collection Sales.IndividualSurveySchemaCollection
Update Table Sales.Individual Trigger iuIndividual
Select Table Sales.Store Trigger iuIndividual

Dependencies

Reference Type Object Type Referencing Object Child Type Child Object
Select View Sales.vIndividualCustomer
Select View Sales.vIndividualDemographics
Select SQL table-valued-function dbo.ufnGetContactInformation
Update Table Sales.Individual Trigger iuIndividual
Update Table Sales.SalesOrderDetail Trigger iduSalesOrderDetail
Select Table Sales.Store Trigger iStore


automatically generated

Table Sales.Individual
Description Demographic data about customers that purchase Adventure Works products online.


Column Data Type Nullable Default Description / PK / Index
CustomerID int not null Unique customer identification number. Foreign key to Customer.CustomerID.
PK_Individual_CustomerID
ContactID int not null Identifies the customer in the Contact table. Foreign key to Contact.ContactID.
Demographics XML null Personal information such as hobbies, and income collected from online shoppers. Used for sales analysis.
PXML_Individual_DemographicsXMLPATH_Individual_DemographicsXMLPROPERTY_Individual_DemographicsXMLVALUE_Individual_Demographics
ModifiedDate datetime not null (GETDATE()) Date and time the record was last updated.


Relation Column Referenced Column
Person.Contact ContactID ContactID
Sales.Customer CustomerID CustomerID


Triggers Type
iuIndividual ON INSERT UPDATE


Dependency Type Object Type Referenced Object Child Type Child Object
Schema Schema Sales
Data Type XML Schema Collection Sales.IndividualSurveySchemaCollection
Update Table Sales.Individual Trigger iuIndividual
Select Table Sales.Store Trigger iuIndividual


Reference Type Object Type Referencing Object Child Type Child Object
Select View Sales.vIndividualCustomer
Select View Sales.vIndividualDemographics
Select SQL table-valued-function dbo.ufnGetContactInformation
Update Table Sales.Individual Trigger iuIndividual
Update Table Sales.SalesOrderDetail Trigger iduSalesOrderDetail
Select Table Sales.Store Trigger iStore