Here's the modified Python script that prints the relationships between the tables in a concise and clean way:
import sqlite3
# Connect to the SQLite database
connection = sqlite3.connect('city_database.db')
# Create a cursor object
cursor = connection.cursor()
# Create CITY table
cursor.execute('''
CREATE TABLE IF NOT EXISTS CITY (
id INTEGER PRIMARY KEY,
name TEXT,
description TEXT
)
''')
# Create DISTRICT table
cursor.execute('''
CREATE TABLE IF NOT EXISTS DISTRICT (
id INTEGER PRIMARY KEY,
name TEXT,
population INTEGER,
city_id INTEGER,
FOREIGN KEY(city_id) REFERENCES CITY(id)
)
''')
# Create LANDMARK table
cursor.execute('''
CREATE TABLE IF NOT EXISTS LANDMARK (
id INTEGER PRIMARY KEY,
name TEXT,
type TEXT,
description TEXT,
city_id INTEGER,
FOREIGN KEY(city_id) REFERENCES CITY(id)
)
''')
# Create STREET table
cursor.execute('''
CREATE TABLE IF NOT EXISTS STREET (
id INTEGER PRIMARY KEY,
name TEXT,
description TEXT,
district_id INTEGER,
FOREIGN KEY(district_id) REFERENCES DISTRICT(id)
)
''')
# Create TOURIST table
cursor.execute('''
CREATE TABLE IF NOT EXISTS TOURIST (
id INTEGER PRIMARY KEY,
name TEXT,
nationality TEXT,
landmark_id INTEGER,
FOREIGN KEY(landmark_id) REFERENCES LANDMARK(id)
)
''')
# Create VENDOR table
cursor.execute('''
CREATE TABLE IF NOT EXISTS VENDOR (
id INTEGER PRIMARY KEY,
name TEXT,
goodsSold TEXT,
street_id INTEGER,
FOREIGN KEY(street_id) REFERENCES STREET(id)
)
''')
# Create RIVER table
cursor.execute('''
CREATE TABLE IF NOT EXISTS RIVER (
id INTEGER PRIMARY KEY,
name TEXT,
length FLOAT
)
''')
# Create BRIDGE table
cursor.execute('''
CREATE TABLE IF NOT EXISTS BRIDGE (
id INTEGER PRIMARY KEY,
name TEXT,
yearBuilt INTEGER,
river_id INTEGER,
FOREIGN KEY(river_id) REFERENCES RIVER(id)
)
''')
# Print the relationships between the tables
print("Relationships:")
print("- CITY.id to DISTRICT.city_id")
print("- CITY.id to LANDMARK.city_id")
print("- DISTRICT.id to STREET.district_id")
print("- LANDMARK.id to TOURIST.landmark_id")
print("- STREET.id to VENDOR.street_id")
print("- RIVER.id to BRIDGE.river_id")
# Commit the changes and close the connection
connection.commit()
connection.close()
This script will create the tables and then print the relationships between them in a concise and clean format.