| Author |
Topic |
|
lkytmr
Starting Member
14 Posts |
Posted - 2002-04-02 : 13:46:22
|
| I am writing a flash card program that drills spanish language students. I create a table ( StudentName ) for each student that contains the words that each student has completed drill. In order to drill the current student, I want to select words from the table ( dictionary ) that do not exist in the completed words ( StudentName ) table.For example:Table 1 ( dictionary)Spanish, English, Type ( Type can be verb, adjective, etc.) plus other fields are also included in this table.Table 2 ( Sam )Spanish ( No other fields exist in this table. )Now I want to select all fields of all rows from the dictionary table that are verbs and that do not exist in table Sam.I have tried every type of join and just can't seem to hit on the right combination.I hope I have explained myself clearly.Thanks for your thoughts and timeTom Ryan |
|
|
LarsG
Constraint Violating Yak Guru
284 Posts |
Posted - 2002-04-02 : 13:59:39
|
| select d.spanish from dictionary as d left join sam as s on d.spanish = s.spanish where d.type = 'verb' and s.spanish is nullI don't think that your design with one table for each student is that good though. It should not be any problem putting the data for all students into one table. |
 |
|
|
yakoo
Constraint Violating Yak Guru
312 Posts |
Posted - 2002-04-02 : 14:01:44
|
Sniped - we really should have a sniped imageFirst off, why do you create a table for each user? You should be able to utilize a better design to take care of this.[Dictionary]WordID, Spanish, English, Type[Students]StudentID, StudentName[CompletedWords]StudentID, WordIDWith this design you will only have 3 tables for an unlimited amount of users.For your first design here is what you query should look likeSELECT d.* FROM DictionaryLEFT OUTER JOIN Sam s ON (d.Spanish = s.Spanish)WHERE D.Type = 'Verb' AND s.Spanish IS NULL Edited by - yakoo on 04/02/2002 14:04:58 |
 |
|
|
lkytmr
Starting Member
14 Posts |
Posted - 2002-04-02 : 15:09:41
|
| Thanks to lars and yakoo for your responses. Your sql statements did the job and I am grateful. Although I am clearly not as proficient at SQL as you two, I am curious as to why the method you suggested would be better?Although it is true that I would have fewer tables by including a student ID with each record, each record would be bigger. With thousands and possibly tens of thousands of words per student, it seems to me that a single table per student would be less overhead.It also seems that as the number of students gets larger, the overhead on the search engine to separate the student's words from all the others would be a much bigger burden than simply grabbing a table filled with the individual's practice set.Is the overhead of extra tables that big of a burden, or is the number of allowed tables limited in a way that will come back to haunt me?Again many thanks for taking time to answer what must seem to you like trivial questions. I really do appreciate it.ThanksTom Ryan |
 |
|
|
Jay99
468 Posts |
Posted - 2002-04-02 : 15:35:31
|
| SQL Server won't mind ten's of thousand's of records. You would have to get up into the hundred's of million's of records inorder to justify this design. Proper indexing will handle your student/word pairings just fine.Put simply, what happens when you get a new student? Would you rewrite all your queries to join to your newly created table? What happens when you decide to quiz your kids on the gerundio form seperate from los verbos . . . You would have to make that change in each of the queries associated with each of the students individual tables . . .Jay<O> |
 |
|
|
lkytmr
Starting Member
14 Posts |
Posted - 2002-04-02 : 15:50:25
|
| Jay,I understand and accept the premise that SQLServer won't mind tens of thousands of records. For the sake of argument or discussion however, 1. What if there were to be thousands, or tens of thousands of students, with each student having thousands of records. Would the overhead of carting around and sifting through the data then justify the extra table? Would SQLServer choke on a database with thousands of tables?2. My program actually generates the various forms of the conjugated verb by storing an index with the verb that indicates how that verb is to be conjugated. So all forms of conjugations are handled by retrieving the verb and method_of_conjugation columns.Thanks again,Tom Ryan |
 |
|
|
yakoo
Constraint Violating Yak Guru
312 Posts |
Posted - 2002-04-02 : 16:20:24
|
From the design I gave you earlier you should easily be able to create a good application around it without ever having to worry about adding a new student.You could then make stored procedures that will do exactly what you need -> in this case return a recordset from dictionary of words that are left to be 'read' by a specified student.Therefore you could create a stored procedure that accepts a parameter of @StudentID (or studentName) and that will return to you your result set.Then you could doSELECT d.* FROM DictionaryLEFT OUTER JOIN (SELECT Spanish FROM CompleteWords WHERE StudentID = 1) C ON (d.Spanish = c.Spanish)WHERE d.Type = 'verb' AND c.Spanish IS NULL |
 |
|
|
yakoo
Constraint Violating Yak Guru
312 Posts |
Posted - 2002-04-02 : 16:21:59
|
here is the code to generate the tables and stored procedure for youif exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[FK_CompletedWords_Dictionary]') and OBJECTPROPERTY(id, N'IsForeignKey') = 1)ALTER TABLE [dbo].[CompletedWords] DROP CONSTRAINT FK_CompletedWords_DictionaryGOif exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[FK_CompletedWords_Students]') and OBJECTPROPERTY(id, N'IsForeignKey') = 1)ALTER TABLE [dbo].[CompletedWords] DROP CONSTRAINT FK_CompletedWords_StudentsGOif exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[sp_GetWords]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)drop procedure [dbo].[sp_GetWords]GOif exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[CompletedWords]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)drop table [dbo].[CompletedWords]GOif exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[Dictionary]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)drop table [dbo].[Dictionary]GOif exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[Students]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)drop table [dbo].[Students]GOCREATE TABLE [dbo].[CompletedWords] ( [StudentID] [int] NOT NULL , [Spanish] [varchar] (50) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL ) ON [PRIMARY]GOCREATE TABLE [dbo].[Dictionary] ( [Spanish] [varchar] (50) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL , [English] [varchar] (50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL , [Type] [varchar] (50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ) ON [PRIMARY]GOCREATE TABLE [dbo].[Students] ( [StudentID] [int] IDENTITY (1, 1) NOT FOR REPLICATION NOT NULL , [StudentName] [varchar] (50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ) ON [PRIMARY]GOALTER TABLE [dbo].[CompletedWords] WITH NOCHECK ADD CONSTRAINT [PK_CompletedWords] PRIMARY KEY CLUSTERED ( [StudentID], [Spanish] ) ON [PRIMARY] GOALTER TABLE [dbo].[Dictionary] WITH NOCHECK ADD CONSTRAINT [PK_Dictionary] PRIMARY KEY CLUSTERED ( [Spanish] ) ON [PRIMARY] GOALTER TABLE [dbo].[Students] WITH NOCHECK ADD CONSTRAINT [PK_Students] PRIMARY KEY CLUSTERED ( [StudentID] ) ON [PRIMARY] GO CREATE INDEX [IX_Dictionary] ON [dbo].[Dictionary]([Spanish]) ON [PRIMARY]GOALTER TABLE [dbo].[CompletedWords] ADD CONSTRAINT [FK_CompletedWords_Dictionary] FOREIGN KEY ( [Spanish] ) REFERENCES [dbo].[Dictionary] ( [Spanish] ), CONSTRAINT [FK_CompletedWords_Students] FOREIGN KEY ( [StudentID] ) REFERENCES [dbo].[Students] ( [StudentID] )GOSET QUOTED_IDENTIFIER ON GOSET ANSI_NULLS OFF GOCREATE PROCEDURE sp_GetWords (@StudentID integer) ASSELECT d.* FROM Dictionary dLEFT OUTER JOIN (SELECT Spanish FROM CompletedWords WHERE StudentID = 1) c ON d.Spanish = c.SpanishWHERE d.Type = 'verb' AND c.Spanish IS NULLGOSET QUOTED_IDENTIFIER OFF GOSET ANSI_NULLS ON GO |
 |
|
|
Jay99
468 Posts |
Posted - 2002-04-02 : 16:27:23
|
First, if your design is working for you and you are getting the performance you need, then it doesn't matter what a bunch on goons on the internet tell you. A design/database/program/computer is a tool, if it does the job then it does the job.quote: 1. What if there were to be thousands, or tens of thousands of students, with each student having thousands of records. Would the overhead of carting around and sifting through the data then justify the extra table? Would SQLServer choke on a database with thousands of tables?
Here is the bottom line; creating a new table for each student will most likly create maintenance and enhancement NIGHTMARES down the road and may cause you performance problems. Using something like Yakoo's schema will greatly reduce the maintenance and enhancement problems and will probably not cause performance problems. (Notice my wishy-washy-ness? That's the beauty of database: there is hardly ever a difinitive answer . . . )If you think about how a clustered index works in the context of a query against the [CompletedWords] table vs a query against the [Sam] table, you'll see there isn't much difference. The 1st level node on the index will put you at all the 'Sams' and from there your node navigation will act just like any index you have on the [Sam] table.Now think about your system tables. Instead of 3 tables you will have 1 + N (N=number of students). Instead of X stored procedure you will have NX, which means NX query plans to store. Instead of running your maintenance routines (reindexing for example) on 3 tables, you will need to run them on 1 + N tables. I could go on. Will SQL choke on a database with 10K tables? I dunno, in my professional experience I have never seen a SQL Server database with more than say 150 tables.quote: 2. My program actually generates the various forms of the conjugated verb by storing an index with the verb that indicates how that verb is to be conjugated. So all forms of conjugations are handled by retrieving the verb and method_of_conjugation columns.
My comment was more to point out a potential enhancement that your design would make difficult. If you have already though of every possible enhancement that you will ever want and have prepared for it like this . . . great!Jay<O> |
 |
|
|
lkytmr
Starting Member
14 Posts |
Posted - 2002-04-02 : 16:43:43
|
| Thanks to everyone.I am truly astonished at the level of knowledge you guys operate from. I get the same feeling when I am playing chess with people several levels above me. I feel like a small boy in a room full of adults.If any of you guys ever plan on coming to Louisville KY, drop me an email and I'll take you out for pizza.Tom Ryan |
 |
|
|
yakoo
Constraint Violating Yak Guru
312 Posts |
Posted - 2002-04-02 : 16:59:34
|
What about beer.Beer and SQLTeam.com go hand in hand |
 |
|
|
|