Link to home
Start Free TrialLog in
Avatar of F-J-K
F-J-KFlag for Canada

asked on

How Well My Table is Normalized? - Relational Database

I posted two table DDLs - Motel and MotelRoom. These tables are intended to be normalized up to 3rd normal form.

Regarding Motel:

I had Country, City and Province each seperated in its own table and then
i just put the foreign key CountryID, ProvinceID and CityID in the Motel's table. However,
an expert long time ago didn't recommend to do it that way because it just adds an overhead when you want to query the data. I'm not convinced yet since his recommendation doesn't fulfill the normalization process. I don't know i might be wrong. Any idea?

Regarding MotelRoom:

I had roomType and bedType each in its own table and then i just added
the foreign key roomTypeID and bedTypeID in MotelRoom. However, one
of the dba's suggest me to put rid of roomType and bedType tables and
put these two as columns with CHECK constraints. I'm kind of confused
now, does this fullfill normalization process?

Thanks
CREATE TABLE Motel
(
        motelID     NUMBER(7)    ,  CONSTRAINT motel_motelid_pk     PRIMARY KEY(motelID),
        branchname  VARCHAR2(50)    CONSTRAINT motel_branchname_nn  NOT NULL            ,
        numofrooms  NUMBER(10)      CONSTRAINT motel_numofrooms_nn  NOT NULL            ,
        address1    VARCHAR2(100)   CONSTRAINT motel_address1_nn    NOT NULL            ,
        address2    VARCHAR2(100)                                                       ,
        city        VARCHAR2(50)    CONSTRAINT motel_city_nn        NOT NULL            ,
        province    VARCHAR2(50)    CONSTRAINT motel_province_nn    NOT NULL            ,
        postalcode  VARCHAR2(50)    CONSTRAINT motel_postalcode_nn  NOT NULL            ,
        country     VARCHAR2(50)    CONSTRAINT motel_country_nn     NOT NULL
);

-------------------------------------------------------------------------------

CREATE TABLE MotelRoom
(
        roomID       NUMBER(7),     CONSTRAINT motelroom_roomid_pk      PRIMARY KEY(roomID)                                                      ,
        motelID      NUMBER(7),     CONSTRAINT motelRoom_motelid_fk     FOREIGN KEY(motelID) REFERENCES Motel(motelID)                            ,
        roomtype     CHAR(3)        CONSTRAINT motelroom_roomtype_nn    NOT NULL                                                                  ,
                                    CONSTRAINT motelroom_roomtype_ck    CHECK(roomtype IN('STD', 'MOD', 'SUP', 'DLX', 'ROH', 'JST', 'STE', 'STU')),
        bedtype      CHAR(1)        CONSTRAINT motelroom_bedtype_nn     NOT NULL                                                                  ,
                                    CONSTRAINT motelroom_bedtype_ck     CHECK(bedtype  IN ('K', 'C', 'Q', 'D', 'T'))                              ,
        price        NUMBER(6,2)    CONSTRAINT motelroom_price_nn       NOT NULL                                                                  ,
        notes        VARCHAR2(500)                                                                                                                ,
        isavailable  CHAR(1)        CONSTRAINT motelroom_isavailable_nn NOT NULL                                                                  ,
                                    CONSTRAINT motelroom_isavailable_ck CHECK(isavailable IN ('Y', 'N'))
);

Open in new window

SOLUTION
Avatar of Bill Bach
Bill Bach
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of F-J-K

ASKER

Thanks for your responses!

@mark_wills:

"if you find yourself repeating the same data (ie content of a field) over multiple rows then that should be your "warning bell" and a key indicator that a new data relationship is required."

This is why i was thinking to make City and Country each in its own table because if i have million records and i wanted to query e.g. all records that is in US then i will only get records that has US. USA won't be returned. Thus, during records insertions there might be some tiny inconsistency which will cost alot in the future. I might be wrong, i don't know. Anything would you like to say?
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
lot of comments..
Here is one file for self help.
Normalization.pdf
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of dportas
dportas

>> Creating a city table is directly connected with the normalization.

I don't see why you would think that. Your example is incomplete but I assume MotelID is the only key and the relevant dependencies are MotelID->Name and MotelID->City. I would say therefore that the Motels table is already in 5NF. Creating another table for City won't change that and has nothing to do with normalization that I can see.
Hi dportas,
You're right but some normal forms are also concerned by dependecies between non-key attributes. Motel table is not 5NF because of the (supposed) dependency City -> Country, and maybe the City -> Postalcode as well.
Regards,
Franck
Avatar of F-J-K

ASKER

Your replies are detailed and well explained. They helped me make up my decision! I'm grateful of your tremendous support.

To close up,

@mark_wills,

"I noticed VARCHAR2 datatypes, and also notice SQL2008 - are you doing Oracle or SQL ?
Not that it makes too much difference, just curious..."

The syntax i posted is written in Oracle. I was into the design aspect rather than the syntax. I don't think
the syntax has an impact of the design i make.

Best Regards,
F-J-K
Avatar of F-J-K

ASKER

Well Done! A+