Is this normalized?

  • 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

  • It seems to be OK. Only need is to generate links

  • 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?

  • 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).

  • 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[/url] - by Jeff Moden

  • 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

  • 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

  • thanks everyone for your very helpful tips and advice. I guess my basic normalization training is still alive and kicking. 🙂

Viewing 8 posts - 1 through 7 (of 7 total)

You must be logged in to reply to this topic. Login to reply