I am been mucking around with declarative constraints lately and have come across a few weird ones..
This first schema is the only possible way to implement a true one-to-one relationship in SQL Server declaratively
It is such a pity it is impossible to actual add any rows, without issuing a NOCHECK CONSTRAINT.
I will post a solution on how to implement a true one-to-one using security and instead of triggers later
The second is a Self Reference to the same column!
--True One-to-One
CREATE SCHEMA AUTHORIZATION DBO
CREATE TABLE A (Letter CHAR(1) NOT NULL PRIMARY KEY,
CONSTRAINT FK_A FOREIGN KEY (Letter) REFERENCES B(Letter))
CREATE TABLE B (Letter CHAR(1) NOT NULL PRIMARY KEY,
CONSTRAINT FK_B FOREIGN KEY (Letter) REFERENCES A(Letter))
Go
--Clean up
ALTER TABLE A DROP CONSTRAINT FK_A
DROP TABLE B,A
GO
--Self Reference to same column
CREATE SCHEMA AUTHORIZATION DBO
CREATE TABLE A (Letter CHAR(1) NOT NULL PRIMARY KEY,
CONSTRAINT FK_A FOREIGN KEY (Letter) REFERENCES A(Letter))
GO
--Clean up
DROP TABLE A
Can anybody think of others?
Print | posted on Wednesday, November 26, 2003 9:49 AM