|
|
|
Grasshopper
      
Group: General Forum Members
Last Login: Thursday, March 03, 2011 1:24 PM
Points: 10,
Visits: 26
|
|
I'm trying to help a friend but it's been ages since I last took up my DB normalization course. Have I done the right thing by designing the tables as such, or can you point out if I've created less or more than enough?
Thanks
--student table create table xStudent (xID INT identity(1,1), studentNo char(7) null, studentName char(100) null) insert xStudent Values ('0315288','Sam Asiata') insert xStudent Values ('0315289','John Doe') insert xStudent Values ('0515111','Mike Jordan')
--class table create table xClass (xClassID INT identity(1,1), ClassCode char(10) null, ClassName char(17) null) Insert xClass values ('123456/12','Research') Insert xClass values ('678910/13','Technology') Insert xClass values ('123456/13','Crime') Insert xClass values ('123456/14','Business') Insert xClass values ('123456/15','Accounting')
--Tutorial table (1 to 12) create table xTutorial (xTutId INT identity(1,1), TutorialCd char(10) null, TutorialName char(17) null) Insert xTutorial values ('Tutor1','Tutorial 1') Insert xTutorial values ('Tutor2','Tutorial 2') Insert xTutorial values ('Tutor3','Tutorial 3') Insert xTutorial values ('Tutor4','Tutorial 4') Insert xTutorial values ('Tutor5','Tutorial 5')
--Student - Class link, to get the classes belonging to a particular student create table xStudClass (xTutId INT identity(1,1), StudentNo char(7) null, ClassCode char(10) null)
Insert xStudClass values ('0315288','123456/12') Insert xStudClass values ('0315288','123456/13') Insert xStudClass values ('0315288','123456/14') Insert xStudClass values ('0315289','123456/14') Insert xStudClass values ('0315289','123456/12')
--Link between Tutorial and Classes, to count the number of tutorial for each class/student create table xTutorClass (xTutClassId INT identity(1,1), TutorialCd char(10) null, ClassCode char(10) null, xID INT null) Insert xTutorClass Values ('Tutor2','123456/12',1) Insert xTutorClass Values ('Tutor3','123456/12',1) Insert xTutorClass Values ('Tutor5','123456/12',1) Insert xTutorClass Values ('Tutor5','123456/12',2) Insert xTutorClass Values ('Tutor2','123456/12',2) Insert xTutorClass Values ('Tutor5','123456/14',2) Insert xTutorClass Values ('Tutor5','123456/13',3)
select * from xClass select * from xTutorial select * from xStudent select * from xStudClass select * from xTutorClass order by 3
--This is to generate count select b.studentNo, b.StudentName, a.classCode, c.ClassName, count(*) as TutorCount from xTutorClass a INNER JOIN xStudent b ON a.xID = b.xID INNER JOIN xClass c ON a.ClassCode = c.ClassCode group by b.StudentNo, b.StudentName, a.ClassCode, c.ClassName
|
|
|
|
|
SSC Rookie
      
Group: General Forum Members
Last Login: Thursday, May 17, 2012 7:56 AM
Points: 40,
Visits: 178
|
|
| It seems to be OK. Only need is to generate links
|
|
|
|
|
SSCrazy
      
Group: General Forum Members
Last Login: Today @ 10:07 AM
Points: 2,802,
Visits: 7,107
|
|
| You seem to have a perfectly good natural key in the Student Table of StudentNo, but you have added a column xId to act as the identity column. Is this because StudentNo is not unique?
|
|
|
|
|
SSChampion
        
Group: General Forum Members
Last Login: Friday, May 17, 2013 12:22 PM
Points: 10,571,
Visits: 11,871
|
|
|
|
|
|
Hall of Fame
       
Group: General Forum Members
Last Login: Tuesday, January 29, 2013 10:54 AM
Points: 3,837,
Visits: 3,821
|
|
In addition to what everyone else has said, I would also tighten up your use of NULLs. Setting your columns up as NOT NULL by default is a good practice to have. Not that using NOT NULL moves your tables into a new normal form, but it does help identify possible normalization areas. I've seen, too often, developers using NULLable columns in tables because not all rows in the table use that column. This type of use of NULLs usually means that the entity is not normalized because you've got a number of columns that don't depend on the key.
It does not look like this is the case for your tables, but a good practice is to always start off with columns defined as NOT NULL and only change it if there is a good business reason to allow NULL values in that column.
John Rowan
====================================================== ====================================================== Forum Etiquette: How to post data/code on a forum to get the best help - by Jeff Moden
|
|
|
|
|
SSCrazy
      
Group: General Forum Members
Last Login: 2 days ago @ 2:32 PM
Points: 2,224,
Visits: 4,083
|
|
Noticed that the ClassCode values have a slash in the same position which may indicate that the column can be decomposed, such as the values before the slash indicate the department and after the slash, a unique value within the department. If so, then there should be two columns. Another ways of determining if this is a compound column is to search for a query that has "where ClassCode like '123456/%' ". Also look for another table that has a candidate key that matches the values before the slash.
Insert xClass values ('123456/12','Research') Insert xClass values ('678910/13','Technology') Insert xClass values ('123456/13','Crime') Insert xClass values ('123456/14','Business') Insert xClass values ('123456/15','Accounting')
SQL = Scarcely Qualifies as a Language
|
|
|
|
|
Grasshopper
      
Group: General Forum Members
Last Login: Thursday, March 03, 2011 1:24 PM
Points: 10,
Visits: 26
|
|
steveb (7/29/2009) You seem to have a perfectly good natural key in the Student Table of StudentNo, but you have added a column xId to act as the identity column. Is this because StudentNo is not unique?
Yeah, I guess I might as well drop the xID because it defeats the purpose of the StudentNo being unique. Thanks
|
|
|
|
|
Grasshopper
      
Group: General Forum Members
Last Login: Thursday, March 03, 2011 1:24 PM
Points: 10,
Visits: 26
|
|
| thanks everyone for your very helpful tips and advice. I guess my basic normalization training is still alive and kicking. :)
|
|
|
|