å¹æçãªããŒã¿ããŒã¹ã®ã€ã³ããã¯ã¹äœæ
æ£èŠåãããããŒã¿ããŒã¹ãšã¯äœã§ããïŒ
çŽ äººã®èšèã§èšãã°ãæ£èŠåãšã¯ãããŒã¿ãåå²ããŠæŽæ°å¯èœãªããŒã¿ã®å°ããªãã£ã³ã¯ã«ãªã³ã¯ããããšã«ãããããŒã¿ã®åé·æ§ãæžããæ¹æ³ã§ãªã¬ãŒã·ã§ãã«ããŒã¿ããŒã¹ãæ§é åããããã»ã¹ã§ãã
ãã®èšäºã§ã¯ãäž»ã«æ£èŠåãããæ§é ã§æ©èœããããŒã¿ããŒã¹ã«çŠç¹ãåœãŠãã»ãšãã©ã®äººã粟éããŠããïŒãŸãã¯æ³åã§ããïŒé åã§ããéèååŒãã¯ã©ã€ã¢ã³ããããã³é£çµ¡å ã«ã€ããŠèª¬æããŸãã
ãªãæ£èŠåãããã®ã§ããïŒ
ããçšåºŠã®ã¬ãã«ãŸãã¯æ£èŠåã¯ãã»ãšãã©ã®ããŒã¿ã»ããã«å€§å¹ ãªæ¡åŒµãããããå¯èœæ§ããããŸããããŒã¿ã¬ã€ã¯ãšæ£èŠåãããŠããªãããŒã¿åŠçã¯ãããžãã¹äœ¿çšã®ããã€ãã®åŽé¢ã§æ³šç®ãéããŠããŸãããã»ãšãã©ã®äŒæ¥ã¯ãã¡ã€ã³ããŒã¿ãããçš®ã®éåžžã®åœ¢åŒã§ä¿åããããšã§ããããæ©æµãåããã§ããããã§ããéã;
- æŽæ°ãã¹ããŒãã¢ããããŸãïŒä»¥äžãåç §ïŒ
- ããŒã¿ã®åãåããã容æã«ããŸã
- éåžžãããŒã¿ãããããªã³ãã¯å°ãããªããŸã
- æ¥çã®åºæºã«æºæ
ç§ãã¡ã®ã¢ãããŒã
ç§ãã¡ã®æšæºçãªã¢ãããŒãã¯ãããŒã¿ã3ã€ã®ç°ãªãæ¹æ³ã§æ ŒçŽãããŠãããã®ããã«èŠãããšã§ãããæ°ããSQL ServerããŒã¹ã®ã·ã¹ãã ãæ§ç¯ãããšãã¯ãããããç°ãªãã¹ããŒãã«ä¿æããããšããŸãã
ãã®ã¢ãããŒãã¯ã以åã®ã¯ã©ã€ã¢ã³ããšé£æºããŠãããã·ã¹ãã ãããã€ããŒã®é床ã倧å¹
ã«åäžããŠããŸãã
ãããŠãã»ã¯ã·ã§ã³ããšã«åå¥ã®ãµãèšäºãè¿œå ããè€æ°ã®ããŒã¿ããŒã¹éã®ã·ã¹ãã ãã¥ãŒãã©ã«ã¬ããŒãã«é¢ããæŠå¿µãæ¢æ±ããã»ã¯ã·ã§ã³ãè¿œå ããããšãç®æããŸãã
ã€ã³ããã¯ã¹ã®æŠèŠ
SQL Serverã«çŠç¹ãåœãŠãŠããŸãããåãååãå€ãã®ç°ãªãã·ã¹ãã ã«é©çšãããŸããã€ã³ããã¯ã¹ã®æ°ãšã¿ã€ãã¯ãèªã¿åããšæžã蟌ã¿ã®ããã©ãŒãã³ã¹ãåå¥ã«æ¹åãŸãã¯äœäžãããå¯èœæ§ããããŸãã
ã¯ã©ã¹ã¿ãŒå
ããŒãã«ããšã«1ã€ã«å¶éãããŠãããããã«ãããããŒã¿ããã£ã¹ã¯ã«ä¿åãããæ¹æ³ãå®çŸ©ãããŸãã
ãã®ã¿ã€ãã®ã€ã³ããã¯ã¹ãæã€ããŒãã«ã¯ã¯ã©ã¹ã¿ãŒåããŒãã«ãšåŒã°ãããªãããŒãã«ã¯ããŒããšåŒã°ããŸãã
éã¯ã©ã¹ã¿ãŒå
ããã¯ãåè¡ãåç §ããåå¥ã®ããŒãã«ãšã»ãŒèããããšãã§ããŸãããSQL Serverã§ã¯ãå®éã®ã¹ãã¬ãŒãžã¯ããŒãã«ã®çš®é¡ïŒã¯ã©ã¹ã¿ãŒå/ããŒãïŒã«å¿ããŠå€åããŸãã
ç¬èªæ§
ãããã®ã€ã³ããã¯ã¹ã¯äž¡æ¹ãšãäžæã§ããå¯èœæ§ããããé©åã«äœ¿çšãããšãããŒã¿ã®ä¿åæ¹æ³ã«å®éã®æ©èœåŒ·åãããããããšãã§ããŸãã
è€åã€ã³ããã¯ã¹
ãã¹ãŠã®ã€ã³ããã¯ã¹ã¯1ã€ä»¥äžã®åã䜿çšã§ããŸãããã¯ã©ã¹ã¿ãŒåã€ã³ããã¯ã¹ã¯900ãã€ãæªæºã§ããå¿ èŠããããŸãã
ã¡ãã£ãšåŸ ã£ãŠãã ãããäž»ããŒã¯ã©ãã§ããïŒ
人ã ããäž»ããŒãã«ã€ããŠèšåããŠãããšãã圌ãã¯ãäžæã®ã¯ã©ã¹ã¿ãŒåã€ã³ããã¯ã¹ãã«ã€ããŠè©±ããŠããããšããããããããªãã®æ°ã®äººã ããããæŽæ°ããŒã¹ã®IDãã£ãŒã«ãå ã®ããŒãã«ã«èªåçã«æ ŒçŽããŸããã¬ã³ãŒããäœæããããšãå€éšããŒã䜿çšããŠå¥ã®ããŒãã«ããåç §ã§ããŸãã
å€éšããŒã¯ãå®éã«ã¯ä»»æã®äžæã®ã€ã³ããã¯ã¹ãåç §ã§ããè€æ°ã®åãåç §ããããšãã§ããŸãã
åç §ããŒã¿
ãã®é åã«ã¯ãã¢ã«ãŠã³ãã¿ã€ããæ¯æãã¿ã€ããªã©ããã§ãŒã³ã®ããã«äžæµã«ããå¥ã®ããŒãã«ã«ãã£ãŠåç
§ããããã¹ãŠã®ãããã¬ãã«æ
å ±ãå«ããå¿
èŠããããŸããããã§ã®å©ç¹ã¯ãåäžã®æŽæ°ã䜿çšããŠæ£èŠåãããããŒã¿ããŒã¹ã®è€æ°ã®è¡ãå€æŽã§ããäžæ¹ã§ãæ£èŠåãããŠããªãå Žåã¯ãã¹ãŠã®è¡ãæŽæ°ããå¿
èŠãããããšã§ãã
æšæºçãªäœ¿çšæ³
äžè¬ã«ãIDåãäžæã®ã¯ã©ã¹ã¿ãŒåã€ã³ããã¯ã¹ãšããŠäœ¿çšããã®ãçæ³çã§ãã以äžã«4ã€ã®ããŒãã«ãšã¹ããŒããäœæããŸãã
Reference Tables
CREATE SCHEMA RefGOCREATE TABLE Ref.AddressType(AddressTypeID INT IDENTITY(1,1) CONSTRAINT PK_Ref_AddressType PRIMARY KEY CLUSTERED,AddressTypeName NVARCHAR(100))CREATE TABLE Ref.ClientType(ClientTypeID INT IDENTITY(1,1) CONSTRAINT PK_Ref_Client PRIMARY KEY CLUSTERED,ClientTypeName NVARCHAR(100))CREATE TABLE Ref.ContactType(ContactTypeID INT IDENTITY(1,1) CONSTRAINT PK_Ref_ContactType PRIMARY KEY CLUSTERED,ContactTypeName NVARCHAR(100))CREATE TABLE Ref.TransactionType(TransactionTypeID INT IDENTITY(1,1) CONSTRAINT PK_Ref_TransactionType PRIMARY KEY CLUSTERED,TransactionTypeName NVARCHAR(100))
ããžãã¹ããŒã¿
ãã®äžéã¬ãã«ã®é åã«ã¯ãã¢ã«ãŠã³ããã¯ã©ã€ã¢ã³ããé£çµ¡å ããŸãã¯ä»ã®äœãã«ãã£ãŠåç §ãããå¯èœæ§ã®ãããã®ä»ã®é åãå«ãŸããã¿ã€ãæ å ±ãåç §ãããŸãã
ãã®ã¬ãã«ã¯ãããŸããŸãªã¢ãããŒããæ··åšããŠããå¯èœæ§ããããããéåžžãã¡ã€ã³ã€ã³ããã¯ã¹ãé 眮ããå Žæã決å®ãããšããç¹ã§æãæ±ãã«ããã¬ãã«ã§ãã
以äžã¯ãã¢ãã¬ã¹ãã¯ã©ã€ã¢ã³ããããã³é£çµ¡å ããŒãã«ãäœæããããã®ããŒãã«ã§ãããã®ã³ãŒãã«ã¯ãClientãAddressãããã³Addressã¿ã€ãã®ãã£ãŒã«ããçµåããè¿œå ã®ïŒçµåïŒããŒãã«ããããããã§ã¯ãä»ã®ããŒãã«ãšã¯ç°ãªãæ¹æ³ã§å®è¡ãããã¯ã©ã¹ã¿ãŒåã€ã³ããã¯ã¹ãäœæããŸãããããã¯ãã»ãšãã©ã®ã¢ããªã±ãŒã·ã§ã³ã§ãããã¯èªã¿åããå€çšããããŒãã«ã§ãããæ¿å ¥ããã©ãŒãã³ã¹ã®æå°éã®åäžãåãå ¥ããããšãã§ããããã§ãããããç§ãã¡ã«ãã£ãŠæ§ç¯ãããã¢ããªã±ãŒã·ã§ã³ã§ããå Žåãããããåæ§ã®æ¹æ³ã§ã¯ã©ã€ã¢ã³ãã®é£çµ¡å ã®è©³çŽ°ãåé¢ããŸãã
Business Tables
CREATE SCHEMA BusGOCREATE TABLE Bus.[Address](AddressID INT CONSTRAINT PK_Bus_Address PRIMARY KEY CLUSTERED,AddressName NVARCHAR(100),AddressTypeID INT CONSTRAINT FK_Bus_Client_AddressTypeID FOREIGN KEY REFERENCES Ref.AddressType(AddressTypeID),AddressLine1 NVARCHAR(MAX)--Use more detail as required...)CREATE TABLE Bus.Client(ClientID INT IDENTITY(1,1) PRIMARY KEY CLUSTERED,ClientName NVARCHAR(100),ClientType INT CONSTRAINT FK_Bus_Client_ClientType FOREIGN KEY REFERENCES Ref.ClientType(ClientTypeID))--Use one table to handle all client addressesCREATE TABLE Bus.ClientAddress(ClientAddressID INT IDENTITY(1,1) CONSTRAINT PK_Bus_ClientAddressID PRIMARY KEY NONCLUSTERED,AddressTypeID INT,ClientID INT,AddressID INT,CONSTRAINT UQ_Bus_ClientAddress UNIQUE NONCLUSTERED (ClientID,AddressTypeID)--This ensures one type per client, can slow down inserts slightly)CREATE UNIQUE CLUSTERED INDEX CDX_Bus_ClientAddress ON Bus.ClientAddress(ClientID,AddressTypeID,AddressID)CREATE TABLE Bus.Contact(ContactID INT IDENTITY(1,1) CONSTRAINT PK_Bus_Contact PRIMARY KEY CLUSTERED,ContactName NVARCHAR(100),ContactTypeID INT CONSTRAINT FK_Bus_Contact_ContactTypeID FOREIGN KEY REFERENCES Ref.ContactType(ContactTypeID)--Could be broken out into a joining table if desired--Use more detail as required...)
ãã©ã³ã¶ã¯ã·ã§ã³ããŒã¿
ãã®é åã«ã¯ãã¡ã¢ãæ¯æãã泚æãªã©ãå«ãŸããéåžžãããžãã¹é åãšåç §é åã®äž¡æ¹ãæããŸãã
äžæã®ããŒã¯èå¥ã«é©ããŠããŸãããèªã¿åãæéã圱é¿ãåãããããäžè¬çãªäœ¿çšæ³ã§ã¯ããã£ã¹ã¯äžã®ããŒã¿ã䞊ã¹æ¿ããæ¹æ³ã§ã¯ãªãå¯èœæ§ããããŸãã以äžã«äœæãããããŒãã«ã¯1ã€ã ãã§ãããããã¯ããªãã«ã¢ã€ãã¢ãäžããã¯ãã§ããTransactional Tables
CREATE SCHEMA TraGOCREATE TABLE Tra.[Transaction](TransactionID INT IDENTITY(1,1) CONSTRAINT PK_Tra_TransactionID PRIMARY KEY NONCLUSTERED,TransactionDate DATETIME CONSTRAINT DF_Tra_Transaction_TransactionDate DEFAULT GETUTCDATE(),--Use GETDATE() for local time.TransactionTypeID INT CONSTRAINT FK_Tra_Transaction_TransactionTypeID FOREIGN KEY REFERENCES Ref.TransactionType(TransactionTypeID),ClientID INT CONSTRAINT FK_Tra_Transaction_ClientID FOREIGN KEY REFERENCES Bus.Client(ClientID),ContactID INT CONSTRAINT FK_Tra_Transaction_ContactID FOREIGN KEY REFERENCES Bus.Contact(ContactID),TransactionAmount DECIMAL(18,2)--Use more detail as required...)CREATE CLUSTERED INDEX CDX_Tra_Transaction ON Tra.[Transaction](TransactionDate,TransactionTypeID,ClientID,ContactID)
åå ãšã¬ããŒã
äžèšã®æ¶ç©ºã®ããŒã¿ããŒã¹ã§ã¯ãçŸå®ã®ç掻ãå¯èœãªéãå¿ å®ã«è¡šçŸããããšããŠããŸããããã¯æ±ºããŠåããªããã°ãªããªãã¢ãããŒãã§ã¯ãªããããªãã¯äžèšã®æ å ±ãã©ã®ããã«äœ¿çšãããã«ã€ããŠæçµçã«è²¬ä»»ããããŸãã
ããŒã¿ã第3å±€ã«å ¥ããšãã€ã³ããã¯ã¹äœæã®çŠç¹ã¯ãã¢ããªã±ãŒã·ã§ã³ãŸãã¯ã¬ããŒãããããŒã¿ãèªã¿åãæ¹æ³ã«ç§»ããŸãããããã«ã¯ãããŒãã«éã®çµåãããã³ããŒãã«ã«å«ãŸããå¯èœæ§ã®ããããŸãã¯å«ãŸããå¯èœæ§ã®ãããã€ã³ããå¿ ãå«ãŸããŸãã WHEREå¥ã