Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
LLatinsky
Starting Member
38 Posts |
Posted - 2005-04-28 : 16:53:38
|
| Hello. I have to design a table that will store all course equivalents. There is a Course table that stores all info regarding a particular class. ClassID ClassName........1 Basic 1012 Basic 1233 DBMS 4 Comp 2 and so on...While designing a CourseEquivalent tablewith ClassID, EquivClassID columns - FKs referencing Course table,I am debating how to store the data in it ...If I write out all the entries - like, if ClassID 1 is equivalent to Classid 2 and 2 is the same as 3 - etries in CourseEquivalent tablewill be :1 22 12 33 23 11 3Then selecting an Equivalent for ClassID 1 becomes really easy but adds(updates and deletes) - especially deletes, are a problem,and table, though narrow, could be huge.This table will be used to display course equivalents in multiple reports. If I store everything I am kind of struggling with the delete - how do I know which links to delete if 1 is brokenIf if store only 1 record per link like 1 22 3I cannot get my select to work right. thank youAny input will be really appreciated |
|
|
spirit1
Cybernetic Yak Master
11752 Posts |
Posted - 2005-04-29 : 05:20:54
|
now that's descriptive...you know what would help. create table statments, insert into statements for sample data and desired results based on sample data.Go with the flow & have fun! Else fight the flow |
 |
|
|
LLatinsky
Starting Member
38 Posts |
Posted - 2005-04-29 : 08:48:50
|
| Well, at this point I am debating which way to store data. I was hoping people did something like this before. I just don't want to reinvent the wheelThank you |
 |
|
|
AndrewMurphy
Master Smack Fu Yak Hacker
2916 Posts |
Posted - 2005-04-29 : 09:13:24
|
| " just don't want to reinvent the wheel"....That may be so....but your description is too brief (and repeating 1,2,3 everywhere doesn't help either)..for us to EASILY help you.That's what we need some more help from YOU. And maybe by writing out this larger sample input data....you will actually solve the problem yourself....but clarifying your thinking. |
 |
|
|
LLatinsky
Starting Member
38 Posts |
Posted - 2005-04-29 : 12:38:36
|
| use tempdbgoCREATE TABLE [EducationClass] ( [CourseCodeID] [int] IDENTITY (1, 1) NOT NULL , [ClassCode] [varchar] (20) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL , [Title] [varchar] (64) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL , [Description] [varchar] (5000) COLLATE SQL_Latin1_General_CP1_CI_AS NULL , [vsKeyword] [int] NULL , [vsCategory] [int] NULL , [RecertificationPeriod] [smallint] NULL , [vsRecertificationUnits] [int] NULL , [Duration] [int] NULL , [vsDurationUnits] [int] NULL , [CollegeCreditHours] [decimal](4, 1) NULL , [OtherCreditHours] [decimal](4, 1) NULL , [DateChanged] [datetime] NOT NULL , [ChangeUser] [int] NULL , [InactiveFlag] [bit] NOT NULL CONSTRAINT [DF_ClassInactive] DEFAULT (0) CONSTRAINT [PK_EducationClass] PRIMARY KEY CLUSTERED ( [CourseCodeID] ) ON [PRIMARY] , CONSTRAINT [XAK1_EducationClass] UNIQUE ( [ClassCode] ) ) ON [PRIMARY]GOCREATE TABLE [CourseCodeEquivalent] ( [CourseCodeID] [int] NOT NULL , [EquivCourseCodeID] [int] NOT NULL , CONSTRAINT [PK_CourseCodeEq_EducationClass] PRIMARY KEY CLUSTERED ( [CourseCodeID], [EquivCourseCodeID] ) ON [PRIMARY] , CONSTRAINT [FK_CourseCodeEq_EducationClass1] FOREIGN KEY ( [CourseCodeID] ) REFERENCES [EducationClass] ( [CourseCodeID] ), CONSTRAINT [FK_CourseCodeEq_EducationClass2] FOREIGN KEY ( [EquivCourseCodeID] ) REFERENCES [EducationClass] ( [CourseCodeID] )) ON [PRIMARY]GOINSERT INTO [EducationClass] ( [ClassCode], [Title], [DateChanged] , [ChangeUser] )SELECT 'CIS','Comp Info1', getdate(),1union allSELECT 'BIS','Comp Info2', getdate(),1union allSELECT 'AIS','Comp Info3', getdate(),1union allSELECT 'FA','First Aid', getdate(),1goselect*from [EducationClass] go-- there may be many classes that are equivalent to each othergo-- suppose ClassCodeIDs 1,2,3 are equivalents-- 2 ways to storeWAY # 1INSERT INTO [CourseCodeEquivalent] ( [CourseCodeID] , [EquivCourseCodeID] )values(1,2)goINSERT INTO [CourseCodeEquivalent] ( [CourseCodeID] , [EquivCourseCodeID] )values(3,4)go/*the select from this table should show for classID 1EquivCourseCodeID23- I do not know how to write this select*/ WAY # 2 IF OBJECT_ID('dbo.addCourseCodeEquivalent') IS NOT NULLDROP PROCEDURE dbo.addCourseCodeEquivalentGOCREATE PROCEDURE dbo.addCourseCodeEquivalent @CourseCodeID int , @EquivCourseCodeID int AS SET NOCOUNT ONDECLARE @Return intdeclare @Courses table (CourseCodeID int, EquivCourseCodeID int)INSERT INTO @Courses (CourseCodeID , EquivCourseCodeID)SELECT @CourseCodeID , @EquivCourseCodeIDUNION SELECT @EquivCourseCodeID, @CourseCodeID UNION SELECT CourseCodeID , EquivCourseCodeIDFROM CourseCodeEquivalent WHERECourseCodeID = @CourseCodeID OR EquivCourseCodeID = @CourseCodeID UNION SELECT CourseCodeID , EquivCourseCodeIDFROM CourseCodeEquivalent WHERECourseCodeID = @EquivCourseCodeID OR EquivCourseCodeID = @EquivCourseCodeID set @return = @@errorif @return <> 0 goto errhandler --select*from @coursesINSERT INTO @Courses (CourseCodeID , EquivCourseCodeID)SELECT t.CourseCodeID ,t1.EquivCourseCodeID from @Courses t cross join @Courses t1 where t.CourseCodeID <>t1.EquivCourseCodeID set @return = @@errorif @return <> 0 goto errhandler --select*from @courses INSERT INTO dbo.CourseCodeEquivalent(CourseCodeID, EquivCourseCodeID) SELECT distinct CourseCodeID, EquivCourseCodeID FROM @Courses p WHERE NOT EXISTS(SELECT 1 FROM dbo.CourseCodeEquivalent l WHERE (p.CourseCodeID= l.CourseCodeID AND p.EquivCourseCodeID = l.EquivCourseCodeID) OR (p.CourseCodeID = l.EquivCourseCodeID AND p.EquivCourseCodeID = l.CourseCodeID)) set @return = @@errorif @return <> 0 goto errhandlerRETURN 0ErrHandler:RETURN @Returngo exec dbo.addCourseCodeEquivalent 1,2goexec dbo.addCourseCodeEquivalent 2,3goselect*from coursecodeequivalent where coursecodeid = 1go-- now if a link between 1 and 2 is deleted or 2 is updated to some other class, I don't know what to delete-- because classes may be added directly or by association-- event if I add a flag to CourseCodeEquivalent table for direct add--- I do not know how to write this delete statement-- drop table coursecodeequivalentthank you |
 |
|
|
|
|
|
|
|