Main content
Course: Unlisted resources area > Unit 2
Lesson 3: SQL documentation- SQL: CREATE table with multiple data types
- SQL: CREATE table with a primary key
- SQL: Specifying DEFAULT values in CREATE TABLE
- SQL: CREATE TABLE with foreign key reference
- SQL: INSERTing values in tables
- SQL: SELECTing rows
- SQL: SELECTing with WHERE conditions
- SQL: Filter with IN
- SQL: SELECT with ORDER BY
- SQL: Transform SELECT results with CASE
- SQL: Filtering with LIKE
- SQL: Using SELECT with LIMIT
- SQL: ROUND and other core functions
- SQL: SELECT with aggregate functions
- SQL: Grouping SELECT results with GROUP BY
- SQL: Using HAVING with GROUP BY
- SQL: JOIN on tables
- SQL: UPDATE and DELETE
- SQL: ALTER TABLE
© 2024 Khan AcademyTerms of usePrivacy PolicyCookie Notice
SQL: CREATE table with a primary key
When you create a table in SQL, you usually want to specify an id column to be the primary key. That will make that value the unique identifier for the row in the table. You can write
PRIMARY KEY
after a column to make it the primary key.If you attempt to insert a row with the same primary key as a previous row, you will get a SQL error (try it in the commented out code below). If you insert a row without specifying the primary key, then SQL will automatically pick one for you that's different from other values.
You can also specify
AUTOINCREMENT
after PRIMARY KEY
. When you do that, the SQL engine makes sure that it picks a completely new id, one that wasn't even used by deleted rows. This might be necessary if you are using ids in a user-facing way, and want to guarantee lifetime uniqueness of them. It does take more processing power, so only use AUTOINCREMENT
if you need it.For more details, see the SQLite reference for Data constraints and
AUTOINCREMENT
.