- Local time
- Today, 17:57
- Joined
- Feb 19, 2013
- Messages
- 17,411
see post #19Question is how to relate them after creating? It is possble by SQL or only VBA?
see post #19Question is how to relate them after creating? It is possble by SQL or only VBA?
Use [DrawingLog]
GO
CREATE TABLE dbo.[tblTransmittal] ([TransmittalID] INT IDENTITY,
[JobID] INT ,
[StageID] INT ,
[TransmittalSeq] INT ,
[TimberlineTransmittalSeq] INT ,
[ReviewerID] INT ,
[SentDT] DATETIME ,
[SentViaID] INT ,
[AllApprovedInd] SMALLINT ,
[Comments] NTEXT ,
[ContactID] INT ,
[Subject] VARCHAR(255) ,
[AdditionalText] VARCHAR(255) ,
[SendShop] SMALLINT ,
[SendCopy] SMALLINT ,
[SendPrints] SMALLINT ,
[SendChange] SMALLINT ,
[SendPlans] SMALLINT ,
[SendSpecifications] SMALLINT ,
[SendSamples] SMALLINT ,
[SendDrawing] SMALLINT ,
[SendCalculations] SMALLINT ,
[SendDocuments] SMALLINT ,
[SendSketches] SMALLINT ,
[TransmitReasonID] INT ,
[CCBox] VARCHAR(255) ,
[UpdateDT] DATETIME ,
[UpdateBy] VARCHAR(20) ,
[upsize_ts] [timestamp] Null);
CREATE INDEX [lkpSentViatblTransmittal] On [tblTransmittal] ([SentViaID]);
CREATE INDEX [lkpTransmitReasontblTransmittal] On [tblTransmittal] ([TransmitReasonID]);
ALTER TABLE [tblTransmittal] ADD CONSTRAINT [PK_tblTransmittal] PRIMARY KEY ([TransmittalID]);
CREATE INDEX [tblContactstblTransmittal] On [tblTransmittal] ([ContactID]);
CREATE INDEX [tblJobtblTransmittal] On [tblTransmittal] ([JobID]);
CREATE INDEX [tblReviewerstblBatch] On [tblTransmittal] ([ReviewerID]);
CREATE INDEX [tblStagestblSubmital] On [tblTransmittal] ([StageID]);
CREATE UNIQUE INDEX [UniqueID] On [tblTransmittal] ([JobID], [TransmittalSeq]);
Totally agreeThe request from your boss is crazy.
thank you, it is nice way!It would be far easier for him to create the tables using the GUI than to type out the necessary DDL. If what he really wants is to send you a file with fields in columns, that is a whole different process. You will have to link to the csv file and loop through the records to build the DDL yourself. Each row will be a field with a data type. It will need an additional column to mark the PK. The file will be the table and should include the name of the table you need to create. The file format for relationships will be different. If you need indexes, that will be a third type of input file.
CREATE TABLE tblCustomers (CustomerID INTEGER NOT NULL, [Last Name] TEXT(50) NOT NULL, [First Name] TEXT(50) NOT NULL, Phone TEXT(10), Email TEXT(50))
ALTER TABLE tblCustomers
ALTER COLUMN [Last Name] INTEGER REFERENCES tblInvoices ([Last Name])
ALTER TABLE Sales.TempSalesReason
ADD CONSTRAINT FK_TempSales_SalesReason FOREIGN KEY (TempID)
REFERENCES Sales.SalesReason (SalesReasonID)
ON DELETE CASCADE
ON UPDATE CASCADE
;
The error message tells you where the problem is. tblCustomers ([Last Name]) is not unique but part of a composite PK. So you cannot create a relationship. Make lastname a unique field and it should then work - but then you can only have one customer with a last name of 'Smith'.
ALTER TABLE tblInvoices
ADD CONSTRAINT FK_Test FOREIGN KEY ([PK]) REFERENCES tblCustomers ([PK])