I want to set up two tables in sql command.
I have a Customer
table and a User Account
table.
A Customer
has none or at most 1 User_account
(0...1) relationship.
But a USER_ACCOUNT
will always have a CUSTOMER
.
How can I do it via SQL command?
EDIT Here's what I have tried so far:
ALTER TABLE UserAccount DROP CONSTRAINT FKUserAccountToCustomer
GO
DROP TABLE Customer;
DROP TABLE UserAccount;
GO
CREATE TABLE Customer
(
CustomerID INT NOT NULL IDENTITY,
(...)
);
GO
CREATE TABLE UserAccount
(
UserAccountID INT NOT NULL IDENTITY,
CustomerID INT NOT NULL,
(...)
);
GO
ALTER TABLE Customer ADD PRIMARY KEY (CustomerID);
GO
ALTER TABLE UserAccount ADD PRIMARY KEY(UserAccountID);
GO
IF NOT EXISTS (SELECT * FROM sysdiagrams WHERE name = 'FKUserAccountToCustomer')
BEGIN
ALTER TABLE UserAccount
ADD CONSTRAINT FKUserAccountToCustomer
FOREIGN KEY(CustomerID)
REFERENCES Customer(CustomerID)
END;