While building and querying a relational database are key components of managing data, expanding the database functionality plays an equally vital role. This includes ways we can modify the database to adapt to changing needs, like adding and remove datasets, or updating existing information.
Consider our Python dictionary that represents a simple relational database. Suppose we want to include data for the key 'finance'. Adding a new relation or dataset can be achieved by simply defining a new key-value pair in the dictionary. As Python dictionaries are mutable, changes take effect immediately in the dictionary object itself.
In the context of software development, this could mean addition of new features or data fields as per project requirements. For example, in finance, data fields could include topics like 'investment_banking', 'corporate_finance', 'asset_management', difficulty level as 'medium', and a boolean indicating whether it's fun.
Deletion of relations or datasets is also straightforward. Assume a situation where we no longer need data for 'mathematics'. We can easily remove the key-value pair from our Python dictionary using the del
keyword.
Here, we also handle the search to demonstrate the addition and deletion. After adding 'finance', a search for 'finance' yields the data we added. Similarly, after deleting 'mathematics', a search for it returns 'No data found for key: mathematics'.
This encapsulates a basic but crucial aspect of managing relational databases - the ability to modify the database structure as per changing needs, again reflecting the principle of how real-world database systems are managed.
xxxxxxxxxx
if __name__ == "__main__":
database = {'mathematics': {'topics': ['calculus', 'combinatorics', 'algebra'], 'difficulty': 'hard', 'is_fun': True},
'AI': {'topics': ['machine_learning', 'deep_learning', 'neural_networks'], 'difficulty': 'hard', 'is_fun': True}}
def search(database, key):
if key in database:
return database[key]
else:
return f'No data found for key: {key}'
# Adding a new key-value pair to the database
database['finance'] = {'topics': ['investment_banking', 'corporate_finance', 'asset_management'], 'difficulty': 'medium', 'is_fun': False}
print(search(database, 'finance'))
# Deleting a key-value pair from the database
del database['mathematics']
print(search(database, 'mathematics'))