Date attribute in Db schema - database

A question I got in my last interview:
You are given a Database of Employee details containing a id and date of joining column. Is the table in 1NF?
I was wondering if with date attribute, whether the schema can be in 1NF or not. How would the existence of date change the answer?

The whole Idea of the question was to trick you into thinking Date is not a 1NF column.
1NF specifies that all attributes store atomic values. Codd defines an atomic value as one that "cannot be decomposed into smaller pieces by the DBMS"[7] meaning a column should not be divided into parts with more than one kind of data in it such that what one part means to the DBMS depends on another part of the same column.
Simply put there should not be 2 values in the attribute of a single tuple.
This table does not satisfy 1NF. Because there are multiple phone numbers in a column.
Customer ID First Name Surname Telephone Number
------------------------------------------------------------------------------
123 Pooja Singh 555-861-2025, 192-122-1111
456 San Zhang (555) 403-1659 Ext. 53; 182-929-2929
789 John Doe 555-808-9633
To make the given table in 1NF we seperate the telephone number column into 2 seperate columns.
Customer ID First Name Surname Telephone Number1 Telephone Number2
---------------------------------------------------------------------------------
123 Pooja Singh 555-861-2025 192-122-1111
456 San Zhang (555) 403-1659 Ext. 53 182-929-2929
789 John Doe 555-808-9633
Unlike the 1st table as long as you store only 1 date, in a single column, your table will be in 1NF.

Related

How to create database table based on the type of the users

I have two types of users. For example:
Male
2 Female
Now I have created a table of 'users' that holds all the common information and
'males' and 'females' will hold specific information related to those.
Now my question is how to create the table for males and females.
Do they will hold the user_id column.
If so, then hypothetically, a user can have multiple male or female accounts.
How to solve this issue?
Why don’t you just add a column, ‘gender’, in the users table? It will take just 1 character to describe if a person is male (‘M’) or female (‘F’).
If you insist on creating a new table anyway, you can define the user_id column as unique in the new table and it should be a foreign key that refers to the user_id column of the users table.

Database ER Diagram

Please explain to me the type of relationship portrayed using this flowchart:
Also, explain the different notation for a different type of relationship i.e
One-to-one, One-to-many, Many-to-one, Many-to-many. And what about the participation i.e Total or partial
A record in table 1 maps to exactly 1 record in table 2. but a record in table 2 can map to 1 or many records in table 1.
for example if if table 1 is records of people, and table 2 is records of places of birth. each person in table 1 can only have one place of birth, but a place of birth can have many people

SQL Database Relationship

I have a DB design question,
I'm trying to design a DB-relationship for companies that have users and need to pay a monthly fee according to a formula. A formula has a name and fee but are not linked to an user but to a company.
Example:
Name: Formula 1
Fee: 5,00
Name: Formula 2
Fee: 10,00
Company XYZ has 2 users with Formula 1 and 2 users with Formula 2. They would need to pay 30,00 .
My initial solution was to link Formula's to users:
But the problem I have discovered is that Formula's need to be linked to Companies and not to users. This because of the fact that different companies can have different fee's for the same formula.
Example:
Users from company A with Formula 1 need to pay 5 but users from company B with Formula 1 need to pay 10.
This is where I met an obstacle because I didn't seem to have fully trust in my database design, in which I attempted to link Formula_Type with a company (and seperate fee...).
My attempt was to use groups:
But I faced a problem here:
How or/and where would I split the fee of the Formula? Since they are depending on the company. Also, what foreign key would I use in the USER-table to link it to an Formula, or wouldn't this be possible in my case?
Is what I'm achieving even possible?
Try this on for size:
Company
ID -- PK
Name
etc.
User
ID -- PK
Company_ID -- FK to Company
Last_Name
First_Name
etc.
Formula
ID -- PK
Name
etc.
Formula_Company -- this allows company-specific fees for different formulae
ID -- PK
Formula_ID -- FK to Formula
Company_ID -- FK to Company
Fee
User_Formula_Company -- this identifies which fee a given user is charged
User_ID -- FK to User
Formula_Company_ID -- FK to Formula_Company

Storing phone numbers country code

I split the phone number in 2 parts:
country prefix(e.g. +49)
phone number without leading 0
My question is, which is the best approach to store the country code As it is (+49) or a Foreign key to a countries table?
You shoud use The Normal Forms for Databases.
https://en.wikipedia.org/wiki/Database_normalization
there are rules to roll with such a problem.
/M
The choice is dependent upon:
No. or records
The database used
No. of relationships with other tables
As, country code would be a repeating column it could be placed in a varchar type column as it is e.g. +91-9654637268 .This will allow different formats of the phone number but no validation at database level that the entered value must be a number that you will need to validate at code level .Using a varchar must be the first choice for storing the phone numbers with their country codes as it will be faster by avoiding joins.
But if you need good amount of manipulation use a bigint which will store the number as e.g. 9764536377443 where first two digits are country code and rest of the digits are the phone number part.
Or you could have a separate column for the country code which would add an unnecessary join but could be helpful if the country code is needed at several places and must be well validated and constrained whihc could also be achieved by using any of the above techniques.
Hope it is helpful.
Transactional Database
If this is a transactional database (lots of updating), or a general purpose database (querying and updating) then use Database normalisation as Jonathan says. So have a table called Country with structure
| ID | CountyCode | CountryName |
| 1 | +49 | Germany |
| 2 | +1 | USA |
This way you can keep the country code and the describing information related to it away from the data about the telephone number. So say a country changes its name or country code, rather than having to update each effected row in the telephone number table, you just update the one row in the Country table.
Then a table(s) for the rest of the telephone number (depending on whether you want to split up area code etc..) with a column that references as a foreign key the CountyCode ID
| ID | CountyCodeID | TelNumber |
| 1 | 1 | 12345 |
However bear in mind that this is a general purpose way of doing things, in query heavy situations with larger amounts of data (dataMart, datawarehouse) then a different approach is best see Star Schemas

do these tables fit the database First Normal Form?

table users
userId name company company_address url
1 Joe ABC Work Lane abc.com
2 Jake ABC Work Lane xyz.com
3 tom XYZ Job Street abcde.com
4 jim XYZ Job Street fexyz.com
the second table
id name favourite_food_1 favourite_food_2
1 Sam Curry Steak
2 Lucy Chicken Burgers
if the table don't fit for the 1NF,why? thank you.
The first table fits 1NF. The second does not - there's a repeating group with the two favorite food fields. Not everyone necessarily has two favorite foods (or any favorite foods at all, or has 3+ favorite foods), so those fields are nullable, and therefore causes the table to fail 1NF.
Doesn't 1NF only mean each value has to be atomic? In other words, every relational database table is in 1NF, since sets of values aren't allowed.
1NF sets the very basic rules for an organized database:
1: Eliminate duplicative columns from the same table.
2: Create separate tables for each group of related data and identify each row with a unique column (the primary key).
The problem with your Database tables is "Name"(duplicate column).
Every relational table always satisfies 1NF. A SQL table is in 1NF if it accurately represents a relation, i.e. it has unique column names and doesn't permit nulls or duplicate rows.

Resources