Databases Flashcards
How to join two databases together?
SELECT *
FROM student
JOIN enroll ON student.SID = enroll.SID
How to join three databases together?
SELECT *
FROM Investigation
JOIN Cases ON Cases.CID = Investigation.CID
JOIN Officers ON Officers.OID = Investigation.OID
What are the main features of SQLite?
- Serverless
- Zero configuration
- Cross-platform
- Small Runtime Footprint
- Highly Reliable
- Self-contained
SQLite syntax:
How to create database
sqlite3 <filename></filename>
SQLite syntax:
List names of tables
sqlite3 <filename><br></br>.tables</filename>
SQLite syntax:
Show structure of a table
sqlite3 <filename><br></br>.schema <table name>
</table></filename>
What import is needed for SQLite dbs?
import sqlite3
How to create a connection and run a query?
import sqlite3
conn = sqlite3.connect("dbfile.db") curs = conn.cursor() results = curs.execute("SELECT \* FROM table;")
for row in results:
print(row[0])
conn.close()
Create a table with Python
curs = conn.cursor()
curs.execute(“CREATE TABLE addressbook (\
ID INTEGER PRIMARY KEY AUTOINCREMENT,\
NAME TEXT);”)
How to split SQL on multiple lines?
result = cur.execute(“SELECT *\
FROM visits;”)
or
sql_query = “”” SELECT *
FROM visits
“”“
result = cur.execute(sql_query)
How to insert data into a table?
INSERT INTO lecturers VALUES (NULL,
‘Rian Bloggs’, ‘Dr’, ‘Head of School’,
’1990-12-10’);
How to update an entry in SQL?
UPDATE lecturers
SET Position=”Senior Lecturer’
WHERE Name=’Yves’;
How to delete an entry in SQL?
DELETE FROM lecturers
WHERE Name = ‘Yves’