我想从表'ProjectTable'创建一个表'Customer':
CREATE TABLE Customer AS
(
SELECT CustCompanyName, CustContactName, CustContactTitle, CustAddress, CustCity, CustRegion, CustPostalCode, CustCountry, CustPhone, CustFax FROM ProjectTable
);
我需要在这里添加更多内容,因为附加列'id'将是主键列,或'customer'将是对另一个表列的引用。怎么做?
答案 0 :(得分:1)
您可以使用相同的表结构创建新表,并使用与ProjectTable相同的数据,然后添加所需的其他字段:
SELECT CustCompanyName, CustContactName, CustContactTitle, CustAddress, CustCity, CustRegion,
CustPostalCode, CustCountry, CustPhone, CustFax INTO Customer FROM ProjectTable
ALTER TABLE Customer ADD ID INT IDENTITY(1,1) PRIMARY KEY
ALTER TABLE Customer ADD customer VARCHAR(10)
ALTER TABLE Customer ADD CONSTRAINT Customer_customer_FK FOREIGN KEY ( customer ) REFERENCES MyOtherTable(PKColumn)