1

Lately i found myself designing a database. The database is consisted of several tables (InnoDB) :

Table 1: Country (id , country_name)

Table 2: City (id, city_name , countryid)

Table 3: Users (id , cityid , A , B, C, D, E)

On the Users table, A , B ,C , D and E are some characteristics of the user, where characteristic A if you combine it with cityid must be unique, that is why i created a unique index for these 2 columns:

CREATE UNIQUE INDEX idx_user ON Users(cityid , A);

The rest columns B,C,D and E are other user characteristics (for example hair color, height, weight, etc.), that as you understand, will be repeated on the table ( hair color = black, or weight = 75 kg).

At the same time countryid and cityid are configured as foreign keys on UPDATE and DELETE CASCADE.

Search will be based on cityid and A columns. A drop down menu to select the city (hence cityid) and a text box to insert the characteristic A and then hit SEARCH button.

My questions are:

  1. On Users table, i have repeating data in the same column (columns B, C ,D and E). This is against 2NF. Do i have to create a separate table for each of these columns and then assign a foreign key of each of these tables to Users table in order to achieve 2NF?

    Table B (id, Bchar)

    Table C (id, Cchar)

    Table D (id, Dchar)

    Table E (id, Echar)

    Users (id, cityid, A, Bid, Cid, Did, Eid)

  2. For the time i will not use columns B,C,D and E as search data, only display them after searching using cityid and A search. If (in the future) i decide that i need to display all results of Users that live in cityid and have black hair, what do i have to keep in mind now while designing the database?

  3. In one hand we have DML(INSERT, UPDATE, DELETE) and on the other hand quering (SELECT). DML will work faster on normalized DBs and quering on denormalized DBs. Is there a middle solution?

  4. Will UNIQUE INDEX created above , be enough to ensure uniqueness for the combination of the data in columns cityid and A? Do i need to further restrict it using JavaScript or better PHP?

  5. Multiple Queries VS Joins: Normalizing the database will require multiple queries or a single query with joins. In the case where "The user searches for a user from Madrid with characteristic A":

    a) Multiple queries:

    i) Go to City table and find the id of Madrid (for example, id = 2 )

    ii) Given the Madrid id and the input for characteristic A, go to Users table and SELECT * FROM Users WHERE cityid="2" AND A="characteristic";

    b) INNER JOIN:

    i) SELECT City.city_name, Users.B, Users.C FROM City INNER JOIN Users ON Users.cityid = City.id;
    

    Which one should i prefer?

Thanks in advance.

user1613360
  • 1,280
  • 3
  • 16
  • 42
christostsang
  • 1,701
  • 3
  • 27
  • 46
  • Check this to get info on choosing primary key or unique index http://stackoverflow.com/questions/487314/primary-key-or-unique-index – user1613360 Jul 20 '14 at 10:35
  • You need to give 1. all column sets that are unique but contain no smaller such set (keys) and 2. all column sets that could be put in their own table and joined back to the originals (JDs). Eg "repeating data in the same column (columns B, C ,D and E)" is unclear. – philipxy Jul 20 '14 at 21:54

1 Answers1

0

Your tables are already in 2NF.The condition for 2NF is there should be no partial dependency.For example lets take your users table and user-id is the primary key and another primary key more appropriate to call candidate key is (cityid,A) with which you can uniquely represent a row in the table.Your table is not in 2NF if cityid or A alone is enough to uniquely retrieve B,C,D or E but in your case one needs both (cityid,A) to retrieve a unique record and hence it's already normalized.

Note:

Your tables are not in 3NF.The condition for 3NF is no transitive dependency.Let's take the users table here userid is the primary key and you can get a unique (cityid,A) pair with that and in turn you can get a unique (B,C,D,E) record with (cityid,A) obtained from userid.In short if A->B and B->C indirectly A->C which is called transitive dependency and it's present in your user table and hence it's not a suitable candidate for 3NF.

user1613360
  • 1,280
  • 3
  • 16
  • 42
  • So its better for the database to follow what i described in the first question? Or if not, how do i go to 3rd NF on my example? – christostsang Jul 20 '14 at 13:28
  • No you need not go to 3NF 2NF is fine.Anyways to convert to 3NF you should split users table as table1(id,cityid) and table2(id,A,B,C,D,E). – user1613360 Jul 20 '14 at 13:50
  • But if i split the table i cannot have the UNIQUE INDEX (cityid, A).. Anyway, the reason i am worried is because i dont want to have the same data repeated in the B,C,D,E columns.. Breaking them into separate tables and link them with the Users table using a FK, it maybe a solution, but 5 FK (4 from B,C,D,E tables and 1 from City table) on the same table seems to many.. I dont know if this will cause problems to the database – christostsang Jul 20 '14 at 13:57
  • Don't worry too much about normalization.The only way to make it 3NF is to implement what I said and yeah you would loose the unique index.What you have now is perfect and I told you about 3NF just for information. – user1613360 Jul 20 '14 at 14:07
  • OK! Thanks for the answer! Do you have any comments on the questions 2-5? If yes, i would be more than happy to read them! :) – christostsang Jul 20 '14 at 14:10
  • 1
    For question two nothing is necessary you already have a proper design to implement the query.For question three the basic job of normalization is to ensure data integrity and in most cases normalization decreases data redundancy and improves the performance and the speed depends on the amount of data if you have millions of records then you should have a complete different approach.For question four check the link which I commented and yeah it's enough to have an unique index and at last your join query is fine and it wont be slow at all. – user1613360 Jul 20 '14 at 19:30
  • @user1613360 & christostsang: For the given unique column sets (keys) and reasonable guesses for functional dependencies, these tables are in BCNF and absent any other JDs they are in 5NF. Read the definitions of the NFs. (2NF & 3NF are unimportant, and you should normalize a design to 5NF initially.) – philipxy Jul 21 '14 at 07:39
  • @philipxy The users table is not in 3NF there is transitive dependency and how can u tell 2NF and 3NF are unimportant without 2NF and 3NF we can't go to 5NF because initial condition for each NF is to satisfy the previous NF's.In real world people hardly implement 5NF in their designs and the best they do is 3NF and I know because I have been working in this field for the past 4 years.Anyways thanks for the input and down vote.No one cares. – user1613360 Jul 21 '14 at 11:24
  • 1.The user table table is not in 3NF I can challenge you to prove otherwise. 2.Everyone starts with 1NF and proceeds further and no one starts with 5NF and your suggestion to start with 5NF is stupid.3.The condition for 3NF is no transitive dependency should be there and it should satisfy 2NF you please read the definition."Transitive dependency don't matter to 3NF" What a joke? 3.My answer is not wrong I have answered the user's query adequately and I don't know what's wrong with you posting inappropriate comments. – user1613360 Jul 21 '14 at 12:57
  • @christostsang "[Codd's definition states that a table is in 3NF if and only if both of the following conditions hold: The relation R (table) is in second normal form (2NF) Every non-prime attribute of R is non-transitively dependent on every superkey of R.](https://en.wikipedia.org/wiki/Third_normal_form)" (Not that Wikipedia is a good source of relational database info.) Note that "non-transitively" does not mean "not transitively". [Also.](https://dba.stackexchange.com/a/2394) – philipxy Jul 21 '14 at 14:10
  • @philipxy What's wrong with you why are you quoting inappropriate articles.I asked you to prove it's already in 3NF which you didn't do.Now all of a sudden you are sending wiki articles.Have you gone Nuts?Please stop this can't waste more time on your stupidity.If you think you can add more or have an more appropriate info post it as an answer. – user1613360 Jul 21 '14 at 14:21