Fremdschlüssel in SQLite von Python [Hinweis]

Ich werde den externen Schlüssel in SQLite von Python verwenden

Erstellen Sie zunächst eine Personentabelle.

db = "./exsample.db"
con = sqlite3.connect(db)
cur = con.cursor()
table = "Person" #Tabellenname
sql = f"""create table {table}(
    id integer primary key autoincrement,
    name text,
    age integer,
)"""
cur.execute(sql) #SQL-Ausführung
self.con.commit() #sparen

"id" wird als Primärschlüssel mit "Primärschlüssel" verwendet und automatisch mit "Autoincrement" zugewiesen.

Erstellen Sie eine externe Tabelle.

table = "Memo" #Tabellenname
sql = f"""create table {table}(
    id integer primary key autoincrement,
    title text,
    content text,
    writer_id integer,
    foreign key(writer_id) references Person(id)
)"""
cur.execute(sql) #SQL-Ausführung
self.con.commit() #sparen

Beachten Sie, dass der Fremdschlüssel (writer_id) auf Person (id) verweist. Erstellen Sie hier einmal mit "writer_id integer" ein Element von "integer" und darunter Fremdschlüssel (<Elementname>) verweist auf <Verbindungstabellenname> (<Verbindungstabellenelementname>),.

Recommended Posts

Fremdschlüssel in SQLite von Python [Hinweis]
SQLite in Python
Tastenanschlag in Python
Tastenanschlag in Python
Python-Eingabehinweis in AtCoder
Implementierte List und Bool in Python und SQLite3 (persönliche Notiz)
[Hinweis] Project Euler in Python (Problem 1-22)
Verwendung von SQLite in Python
Schreiben Sie externe Schlüsseleinschränkungen in Django
Python-Notiz
In Python + SQLite3 "OperationalError: keine solche Spalte:"
Versuch, SQLite3 mit Python zu handhaben [Hinweis]
Quadtree in Python --2
Python in der Optimierung
CURL in Python
Python lernen note_002
Metaprogrammierung mit Python
Python 3.3 mit Anaconda
Python-Programmierhinweis
Geokodierung in Python
SendKeys in Python
[Python] Lernnotiz 1
Metaanalyse in Python
Unittest in Python
Python lernen note_004
Epoche in Python
Zwietracht in Python
Deutsch in Python
DCI in Python
Quicksort in Python
N-Gramm in Python
Programmieren mit Python
Plink in Python
Konstante in Python
FizzBuzz in Python
Schritt AIC in Python
[Hinweis] openCV + Python
LINE-Bot [0] in Python
CSV in Python
Reverse Assembler mit Python
Reflexion in Python
Konstante in Python
nCr in Python.
Format in Python
Scons in Python 3
Puyopuyo in Python
Python in Virtualenv
PPAP in Python
Quad-Tree in Python
Python-Anfängernotiz
Reflexion in Python
Chemie mit Python
Hashbar in Python
DirectLiNGAM in Python
LiNGAM in Python
In Python reduzieren
In Python flach drücken
3 Möglichkeiten, Zeitzeichenfolgen mit Python zu analysieren [Hinweis]