Common Beginner Questions, Answered Forum-Style
This site does not run a discussion forum. Instead, this page collects the questions beginners ask most often in classes and online communities, with short answers and a link for each. The questions are typical ones, not posts by real users.
Getting started
Which database should I install first?
If nobody has told you to use a particular one, start with SQLite. It is one file, needs no server, and Python already includes it. See SQLite, PostgreSQL or MySQL for your first project?
Do I need to learn SQL if I use a framework?
Yes, at least the basics. Frameworks write SQL for you, but when something is slow or wrong, you need to read what they sent. Knowing SELECT, WHERE, JOIN and keys pays off quickly.
Tables and keys
What is the difference between a primary key and a foreign key?
A primary key identifies each row in its own table. A foreign key is a column in another table that points to that primary key. Our complete read on keys has examples.
My ids have gaps after I deleted rows. Did I break something?
No. Databases do not reuse or renumber ids, on purpose. See what happens to auto-increment ids.
Queries
Why does WHERE phone = NULL find nothing?
NULL means "unknown", and comparing anything with unknown is not true. Use IS NULL instead. NULL explained covers this.
Why did my LEFT JOIN lose rows?
Usually because a WHERE condition on the right-hand table removed the rows where it had no match. Move that condition into the ON clause. See why did my LEFT JOIN lose rows?
What does GROUP BY actually do?
It makes one result row per group, such as one per artist, so you can count or sum within each group. Quick answers on GROUP BY and COUNT.
Connecting from code
My program inserts rows but they disappear when I restart. Why?
You probably did not commit the transaction. Call commit(), or use a block that commits for you. See a first look at transactions.
Where should I put my database password?
Not in your code. Use an environment variable. How to keep a connection string in an environment variable.
What is SQL injection, and am I at risk?
If your code glues user input into SQL text, yes. Use placeholders for every value. SQL injection: short questions, plain answers.
Errors
"No such table", but I just created it!
You may be connected to a different database file, often because of a relative path. Print the full path your program opens. How to read your first database error message.
"Connection refused". What now?
The database server is not running, or the host or port is wrong. Reader questions about connection errors walks through the checks.
Asking your own question elsewhere
Public communities such as the database sections of Stack Exchange, and the official mailing lists or forums of each database project, welcome beginner questions. Include your database and version, the exact query, the exact error and what you expected. Those four things get you a useful answer much faster.
A free learning page for beginners. Practise on a test database, never on real customer data, and check the official documentation for your database version.