Junior — Middle
Can you give an example of a table with normalized data?
sobes.tech AI
Answer from AI
A normalized table in a database is organized to minimize redundancy and ensure data integrity. For example, a "Users" table may contain unique records with fields:
| user_id | username | created_at | |
|---|---|---|---|
| 1 | ivan | ivan@mail.com | 2024-01-10 12:00:00 |
| 2 | maria | maria@mail.com | 2024-02-15 09:30:00 |
Here:
user_idis the primary key, a unique identifier.usernameandemailare unique or have constraints to avoid duplication.- There are no repeating or computed data.
In Python, such a table can be represented using a list of dictionaries or a pandas DataFrame:
import pandas as pd
data = [
{'user_id': 1, 'username': 'ivan', 'email': 'ivan@mail.com', 'created_at': '2024-01-10 12:00:00'},
{'user_id': 2, 'username': 'maria', 'email': 'maria@mail.com', 'created_at': '2024-02-15 09:30:00'}
]
df = pd.DataFrame(data)
print(df)
This approach ensures a clean data structure without redundancy and facilitates its maintenance and updating.