ebb4a9a76e570a1243b483a795fa309fbb027aa8
public/sql.md
| ... | ... | @@ -38,23 +38,46 @@ div .venn { |
| 38 | 38 | |
| 39 | 39 | ### [2] Creating a database and connecting (from shell) |
| 40 | 40 | |
| 41 | + # PostgreSQL |
|
| 42 | + |
|
| 41 | 43 | dropdb example |
| 42 | 44 | createdb example |
| 43 | 45 | psql example |
| 44 | - |
|
| 46 | + |
|
| 47 | + # sqlite3 |
|
| 48 | + rm example.db # drop a sqlite3 database (just remove the file) |
|
| 49 | + sqlite3 example.db # if the db doesn't exist, creates it; starts the command-line interface |
|
| 50 | + |
|
| 45 | 51 | ### [3] Dropping and creating a table |
| 46 | 52 | |
| 53 | +The column with type `serial` for PostgreSQL, or 'integer primary key autoincrement` for sqlite3 |
|
| 54 | +is a primary key that gets a value automatically when you insert a new row. By this means, you don't |
|
| 55 | +have to find out what the highest id is; the database system sets the key value appropriately |
|
| 56 | +automatically. |
|
| 57 | + |
|
| 58 | +The `--` indicates a comment. |
|
| 59 | + |
|
| 60 | + # PostgreSQL |
|
| 61 | + |
|
| 47 | 62 | drop table names; |
| 48 | 63 | create table names ( |
| 49 | 64 | id serial, -- "auto increment" |
| 50 | 65 | first_name text |
| 51 | 66 | ); |
| 52 | 67 | |
| 68 | + # sqlite3 |
|
| 69 | + |
|
| 70 | + drop table names; |
|
| 71 | + create table names ( |
|
| 72 | + id integer primary key autoincrement, |
|
| 73 | + first_name text |
|
| 74 | + ); |
|
| 75 | + |
|
| 53 | 76 | ### [4] Another |
| 54 | 77 | |
| 55 | 78 | drop table books; |
| 56 | 79 | create table books ( |
| 57 | - id serial, -- "auto increment" |
|
| 80 | + id integer primary key autoincrement, -- remember, for PostgreSQL, use: serial |
|
| 58 | 81 | name_id int, |
| 59 | 82 | title text |
| 60 | 83 | ); |