Re: New to SQL server
- From: "onedaywhen" <jamiecollins@xxxxxxxxxx>
- Date: 6 Dec 2006 02:34:09 -0800
dbahooker@xxxxxxxxxxx wrote:
DRI and CHECK constraints?
SQL SERVER WORKS LIKE A CHARM, PLEASE EXPLAIN YOUR SO CALLED BUGS
I didn't call them bugs; rather, they are omissions by design.
Both Cascade referential actions in DRI and table-level CHECK
constraints are both features of _full_ SQL-92 and Microsoft only
claims _entry level_ SQL-92 compliance for SQL Server (and not even
that for Access/Jet). Still, I think it's a poor show that these
features have been in Access/Jet for so long yet still missing from SQL
Server.
Didn't I already provide examples? Oh yeah, you didn't like
self-referencing FKs and inline constraints. OK, try this:
CREATE TABLE Employees (
employee_ID INTEGER NOT NULL
)
;
ALTER TABLE Employees ADD
CONSTRAINT pk__Employees
PRIMARY KEY (employee_ID)
;
CREATE TABLE OrgChart
employee_ID INTEGER NOT NULL,
manager_employee_ID INTEGER NOT NULL
)
;
ALTER TABLE OrgChart ADD
CONSTRAINT pk__OrgChart
PRIMARY KEY (employee_ID)
;
ALTER TABLE OrgChart ADD
CONSTRAINT fk__OrgChart__employee_ID
FOREIGN KEY (employee_ID)
REFERENCES Employees (employee_ID)
ON DELETE CASCADE
ON UPDATE NO ACTION
;
ALTER TABLE OrgChart ADD
CONSTRAINT fk__OrgChart__manager_employee_ID
FOREIGN KEY (manager_employee_ID)
REFERENCES Employees (employee_ID)
ON DELETE CASCADE
ON UPDATE NO ACTION
;
The above is SQL-92 code so it can used in Jet (e.g. ADO or, if you
prefer the GUI, in 'ANSI-92 Query Mode').
It works fine in Jet. For SQL Server the last statement generates and
error, 'Introducing FOREIGN KEY constraint
'fk__OrgChart__manager_employee_ID' on table 'OrgChart' may cause
cycles or multiple cascade paths. Specify ON DELETE NO ACTION or ON
UPDATE NO ACTION, or modify other FOREIGN KEY constraints.'
You see, Jet has been programmed to resolve most cascade paths whereas
SQL Server is programmed to count the number of paths and choke when it
gets to 2; the fact the paths can be resolved easily is of no
consequence because SQL Server simply does not have the ability to do
it. I figure they were too busy trying to get CLR into the product
rather than get TSQL up to standard ;-)
Of course, you could write a trigger to perform the cascade action for
the second one but you have to resolve the paths yourself, do error
handling, transaction management, etc. Don't you think it a bit of a
weird table design that one cascade would be done with a FK and other
done with a trigger? Or would you write them both as triggers? All your
FKs as triggers? And you thought it was only Access/Jet where you had
to write your own tools to do basic operations <vbg>!
As for CHECK constraints:
ALTER TABLE OrgChart ADD
CONSTRAINT manager_for_maximum_three_employees
CHECK (NOT EXISTS (
SELECT T2.manager_employee_ID, COUNT(*)
FROM OrgChart AS T2
GROUP BY T2.manager_employee_ID
HAVING COUNT(*) > 3))
;
Works fine in Jet but in SQL Server it generates an error, 'Subqueries
are not allowed in this context. Only scalar expressions are allowed.'
Table-level CHECK constraints must be implemented in SQL Server with a
trigger. Don't get me wrong: it's great that SQL Server has a trigger
language to be able to plug these gaps; my point is, DRI and CHECK
constraints are the basic fundamentals and I have better things to do
with my time that to code around Microsoft's omissions.
Jamie.
--
.
- Follow-Ups:
- Re: New to SQL server
- From: dbahooker@xxxxxxxxxxx
- Re: New to SQL server
- References:
- Re: New to SQL server
- From: Bill Edwards
- Re: New to SQL server
- From: aaron.kempf@xxxxxxxxx
- Re: New to SQL server
- From: aaron.kempf@xxxxxxxxx
- Re: New to SQL server
- From: onedaywhen
- Re: New to SQL server
- From: dbahooker@xxxxxxxxxxx
- Re: New to SQL server
- From: onedaywhen
- Re: New to SQL server
- From: dbahooker@xxxxxxxxxxx
- Re: New to SQL server
- From: onedaywhen
- Re: New to SQL server
- From: dbahooker@xxxxxxxxxxx
- Re: New to SQL server
- Prev by Date: Re: New to SQL server
- Next by Date: Re: New to SQL server
- Previous by thread: Re: New to SQL server
- Next by thread: Re: New to SQL server
- Index(es):
Relevant Pages
|