2

Please guide me if I'm on right track.

I'm trying to create database schema for Mobile Bill for a person X and how to define PK, FK for the table Bill_Detail_Lines.

Here are the assumptions:

  1. Every customer will have a unique relationship number.
  2. Bill_no will be unique as it is generated every month.
  3. X can call to the same mobile no every month.
  4. Account_no is associated with every mobile no and it doesn't change.

Schema:

table: Bill_Headers

Relationship_no    - int, NOT NULL , PK
Bill_no            - int, NOT NULL , PK
Bill_date          - varchar(255), NOT NULL
Bill_charges       - int, NOT NULL

table: Bill_Detail_Lines

Account_no    - int, NOT NULL
Bill_no       - int, NOT NULL , FK
Relationship_no - int, NOT NULL, FK
Phone_no      - int, NOT NULL
Total_charges  - int

table: Customers

Relationship_no    - int, NOT NULL, PK
Customer_name      - varchar(255)
Address_line_1     - varchar(255) 
Address_line_2     - varchar(255) 
Address_line_3     - varchar(255) 
City               - varchar(255)
State              - varchar(255)
Country            - varchar(255)
2
  • Are your phone numbers really int? In Germany i.e. mobile numbers start with a 0. Why is your bill_date field varchar?
    – Jacob
    Jul 13, 2011 at 11:36
  • bill_date should be date. I'm really interested in freezing the schema and later work out on the fields. Jul 13, 2011 at 11:45

4 Answers 4

0

I would recommend having a primary key for Bill_Detail_Lines. If each line represents a total of all calls made to a given number, then the natural PK seems to be (Relationship_no, Bill_no, Phone_no), or maybe (Relationship_no, Bill_no, Account_no).

If each line instead represents a single call, then I would probably add a Line_no column and make the PK (Relationship_no, Bill_no, Line_no).

1
  • Perfect. Each line represents total call charge made to a number. I can make us of Relationship_no, Bill_no, Phone_no or Account_no as PK. Jul 14, 2011 at 12:30
0

Yes, as for me, everything looks good.

4
  • bill_detail_lines contains summary of all calls made to different mobiles, but I cannot enter more than one if I make bill_no and relationship_no as a primary key. Jul 13, 2011 at 11:36
  • you can insert multiple entries in bill_detail_lines table because bill_no and relationship_no are FK. In bill_detail_lines table the can be duplicated.
    – Sumit
    Jul 13, 2011 at 12:12
  • does this mean that I don't neeed to declare a PK in bill_detail_lines? Jul 13, 2011 at 12:22
  • declaring a PK totally depends on your need. What i meant earlier was that FK will not pose problems in duplicate entries. You just need to have unique entries in the table from where the FK are derived.
    – Sumit
    Jul 13, 2011 at 12:37
0

I have to disagree, there's a couple of 'standards' which aren't being followed. Yes the design looks ok, but the naming convention isn't appropriate.

Firstly, table names should be singular (many people will disagree with this).

If you have a single int, PK on a table, the standard is to call it 'ID', thus you have "SELECT Customer.ID FROM Customer" - for instance. You also then fully qualify the FK columns, for instance: CustomerID on Bill_Headers instead of Relationship_no which you then have to check in the table definition to remember what it's related to.

Something I also always keep in mind, is to make the column header as clear and short as possible without obfuscating the name. For instance, "Bill_charges" on Bill_Headers could just be "Charges", as you're already on the Bill_Header(s) (<- damn that 's'), same goes for Date, but date could be a bit more descriptive, CreatedDate, LastUpdatedDate, etc...

Lastly, beware of hard-coding multiple columns where one would suffice, same other way around. Specifically I'm talking about:

Address_line_1 - varchar(255) Address_line_2 - varchar(255) Address_line_3 - varchar(255)

This will lead to headaches later. SQL does have the capability to store new line characters in a string, thus combining them to one "Address - varchar(8000)" would be easiest. Ideally this would be in a separate table, call it Customer_Address with int "CustomerID - int PK FK" column where you can enter specific information.

Remember, these are just suggestions as there's no single way of database design that everyone SHOULD follow. These are best practices, at the end of the day it's your decision to make.

0

There are a few mistakes:

  1. Realtionship_no and Bill_no are int. Make sure that the entries are within the range of integer. It is better to take them as varchar() or char()

  2. Bill_date should be in data type Date

  3. In table Bill_Detail_Lines also, it is better to have Account_no as varchar() or char() because of the long account no. And the same goes with Phone_no.

Your Customers table is all fine except that you have taken varchar() size as 255 for City State and Country which is too large. You can work with smaller size also.

3
  • Re 1): having a primary key that consists of two columns is perfectly OK.
    – user330315
    Jul 13, 2011 at 12:14
  • Re 1) I will correct it. Multiple columns make a primary key. Jul 13, 2011 at 12:27
  • @ a_horse_with_no_name...i thought he declared 2 PK. My mistake. I updated that. And yes you can use multiple columns...that might solve your purpose.
    – Sumit
    Jul 13, 2011 at 12:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.