Re: Newbie: Help Excluding Records from a Table using multiple rows from another table
From: Joe Celko (jcelko212_at_earthlink.net)
Date: 04/30/04
- Next message: Jon Feutz: "Re: Newbie: Help Excluding Records from a Table using multiple rows from another table"
- Previous message: Dan Guzman: "Re: DDL and Transaction"
- In reply to: Jon: "Newbie: Help Excluding Records from a Table using multiple rows from another table"
- Next in thread: Jon Feutz: "Re: Newbie: Help Excluding Records from a Table using multiple rows from another table"
- Messages sorted by: [ date ] [ thread ]
Date: Thu, 29 Apr 2004 19:22:11 -0700
1) Please post DDL, so that people do not have to guess what the keys,
constraints, Declarative Referential Integrity, datatypes, etc. in your
schema are. What you did post was actually vry good for a newbie!
2) Never use IDENTITY for a key. You can look up my rants about that.
You needed a key for Application_Servers and had the brains not to use
IDENTITY which is a typical newbie error! 10 points!
3) Rows are not records; fields are not columns; tables are not files;
there is no sequential access or ordering in an RDBMS, so "first",
"next" and "last" are totally meaningless. It really helps to have the
right mental model.
4) We don't use loops in SQL; this is a set-oriented language. We are
back to that mental model thing again.
5) That nvarchar(50) tells me that you did absolutely no design on this
schema and that you deserve to have all the garbage that is going to
accumlate in those columns. I'll be happy to add am emtire sutra in
Chinese myself!
6) Remember to add DRI actions; that is one of many reasons that rows
are not records; fields are not columns; tables are not files.
CREATE TABLE Applications
(app_id INTEGER NOT NULL PRIMARY KEY,
app_name CHAR(30) NOT NULL);
CREATE TABLE Servers
(svr_id INTEGER NOT NULL PRIMARY KEY,
svr_name CHAR(30) NOT NULL);
CREATE TABLE Application_Servers,
(app_id INTEGER NOT NULL
REFERENCES Applications (app_id)
ON DELETE CASCADE
ON UPDATE CASCADE,
svr_id INTEGER NOT NULL
REFERENCES TABLE Servers (svr_id)
ON DELETE CASCADE
ON UPDATE CASCADE,
PRIMARY KEY (app_id, svr_id));
>> I want to Return All of the Servers not Assigned to a given
application. <<
SELECT @my_app_id,
svr_id AS missing_id, svr_name AS missing_name
FROM Servers
WHERE svr_id
NOT IN (SELECT svr_id
FROM Application_Servers
WHERE app_id = @my_app_id);
--CELKO--
*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
- Next message: Jon Feutz: "Re: Newbie: Help Excluding Records from a Table using multiple rows from another table"
- Previous message: Dan Guzman: "Re: DDL and Transaction"
- In reply to: Jon: "Newbie: Help Excluding Records from a Table using multiple rows from another table"
- Next in thread: Jon Feutz: "Re: Newbie: Help Excluding Records from a Table using multiple rows from another table"
- Messages sorted by: [ date ] [ thread ]
Relevant Pages
|