Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

Is this normalized? Expand / Collapse
Author
Message
Posted Wednesday, July 29, 2009 5:33 AM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Thursday, March 3, 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


Post #761400
Posted Wednesday, July 29, 2009 5:40 AM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Monday, April 7, 2014 1:56 AM
Points: 41, Visits: 181
It seems to be OK. Only need is to generate links
Post #761407
Posted Wednesday, July 29, 2009 6:35 AM
SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Friday, May 30, 2014 6:27 PM
Points: 2,808, Visits: 7,175
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?
Post #761459
Posted Wednesday, July 29, 2009 9:54 AM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: 2 days ago @ 12:10 PM
Points: 10,191, Visits: 13,115
I would say that it is normalized, although I would have FirstName and LastName columns instead of just a StudentName.

I would also echo SteveB's response about the identity columns. If you aren't using them as foreign keys and if the other columns (StudentNo, ClassCd, etc...) are unique, which they have to be to be used a foreign keys, I would make them the primary key and eliminate the identity column(s).




Jack Corbett

Applications Developer

Don't let the good be the enemy of the best. -- Paul Fleming

Check out these links on how to get faster and more accurate answers:
Forum Etiquette: How to post data/code on a forum to get the best help
Need an Answer? Actually, No ... You Need a Question
How to Post Performance Problems
Crosstabs and Pivots or How to turn rows into columns Part 1
Crosstabs and Pivots or How to turn rows into columns Part 2
Post #761735
Posted Wednesday, July 29, 2009 11:26 AM
Hall of Fame

Hall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of Fame

Group: General Forum Members
Last Login: Tuesday, August 19, 2014 10:01 AM
Points: 3,840, Visits: 3,841
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
Post #761828
Posted Wednesday, July 29, 2009 11:38 AM
SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Monday, May 12, 2014 12:57 PM
Points: 2,281, Visits: 4,225
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
Post #761837
Posted Wednesday, July 29, 2009 3:47 PM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Thursday, March 3, 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
Post #762009
Posted Wednesday, July 29, 2009 4:33 PM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Thursday, March 3, 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. :)
Post #762023
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse