|
| 1 | + |
| 2 | +> CS258 and this guide is based on PostgreSQL other versions may be different |
| 3 | +
|
| 4 | +> This page shows limited select commands |
| 5 | +
|
| 6 | +> This page provides links in the titles to the PostgreSQL documentation It is recommenced you visit for a deeper understanding of the commands |
| 7 | +
|
| 8 | +## Create |
| 9 | + |
| 10 | +### [CREATE SCHEMA](https://www.postgresql.org/files/documentation/pdf/14/postgresql-14-A4.pdf#page=1722) |
| 11 | + |
| 12 | +<pre>CREATE SCHEMA <i>schema_name</i> [AUTHORIZATION <i>user_name</i>]</pre> |
| 13 | + |
| 14 | +enters a new schema into the database, a schema contains tables if no schema is specified the public schema is used. |
| 15 | + |
| 16 | +tables within a schema can be referenced with the dot operator |
| 17 | + |
| 18 | +CREATE TABLE _schema.table_ .... |
| 19 | + |
| 20 | +if no schema is specified then `public` is used |
| 21 | + |
| 22 | +<pre> |
| 23 | +CREATE TABLE <i>table_name</i> (...) == CREATE TABLE <i>public.table_name</i> (...) |
| 24 | +</pre> |
| 25 | + |
| 26 | +**catalogue** named collection of schemas |
| 27 | + |
| 28 | +### [CREATE TABLE](https://www.postgresql.org/files/documentation/pdf/14/postgresql-14-A4.pdf#page=1738) |
| 29 | + |
| 30 | +<pre> |
| 31 | +CREATE TABLE <i>table_name</i> ( |
| 32 | + <i>column_name</i> <i>data_type</i> <i>constraints</i> |
| 33 | + <i>column_name</i> <i>data_type</i> <i>constraints</i> |
| 34 | + <i>column_name</i> <i>data_type</i> <i>constraints</i> |
| 35 | + ... |
| 36 | +) |
| 37 | +</pre> |
| 38 | + |
| 39 | +#### [CREATE VIEW](https://www.postgresql.org/files/documentation/pdf/14/postgresql-14-A4.pdf#page=1792) |
| 40 | + |
| 41 | +Can be used to create a virtual table that is the result of a query , the view is virtual and is run on every request |
| 42 | + |
| 43 | +## [Constraints](https://www.postgresql.org/files/documentation/pdf/14/postgresql-14-A4.pdf#page=98) |
| 44 | + |
| 45 | +### [NOT NULL](https://www.postgresql.org/files/documentation/pdf/14/postgresql-14-A4.pdf#page=101) |
| 46 | + |
| 47 | +Specifies that data in a particular column must not be null |
| 48 | + |
| 49 | +``` |
| 50 | +CREATE TABLE products( |
| 51 | + name text NOT NULL |
| 52 | +) |
| 53 | +``` |
| 54 | + |
| 55 | +### [CHECK](https://www.postgresql.org/files/documentation/pdf/14/postgresql-14-A4.pdf#page=99) |
| 56 | + |
| 57 | +A check consists of a boolean expression that must evaluate to true for the data to be valid |
| 58 | + |
| 59 | +``` |
| 60 | +CREATE TABLE products( |
| 61 | + price numeric CHECK (price > 0) |
| 62 | +) |
| 63 | +``` |
| 64 | +the constraint can also be given a name this can help with error messages as the mae will be provided |
| 65 | +``` |
| 66 | +CREATE TABLE products( |
| 67 | + price numeric CONSTRAINT positive_price CHECK (price > 0) |
| 68 | +) |
| 69 | +``` |
| 70 | + |
| 71 | +A check can also refer to multiple columns by adding it to the end of the table |
| 72 | + |
| 73 | +``` |
| 74 | +CREATE TABLE products( |
| 75 | + price numeric CONSTRAINT positive_price CHECK (price > 0) |
| 76 | + discount_price numeric CONSTRAINT positive_price CHECK (discountPrice > 0) |
| 77 | + check( discount_price < price) |
| 78 | +) |
| 79 | +``` |
| 80 | + |
| 81 | +Check can oly be assumed to evaluate on update or delete so should only refer to items in the current row not other elements or table |
| 82 | + |
| 83 | +### [PRIMARY KEY](https://www.postgresql.org/files/documentation/pdf/14/postgresql-14-A4.pdf#page=103) |
| 84 | + |
| 85 | +Assignees a column as a key. multiple can be used with `PRIMARY KEY (a,b)` |
| 86 | + |
| 87 | +### [UNIQUE](https://www.postgresql.org/files/documentation/pdf/14/postgresql-14-A4.pdf#page=101) |
| 88 | + |
| 89 | +Ensures that data is unique among all the rows in the table. multiple can be used with `UNIQUE (a,b)` |
| 90 | + |
| 91 | +### Foreign Key [REFERENCES](https://www.postgresql.org/files/documentation/pdf/14/postgresql-14-A4.pdf#page=103) |
| 92 | + |
| 93 | +Ensures that referential integrity is maintained between tables |
| 94 | + |
| 95 | +``` |
| 96 | +CREATE TABLE orders( |
| 97 | + product_id integer REFERENCES products |
| 98 | +) |
| 99 | +``` |
| 100 | + |
| 101 | +can use multiple foreign keys with `FOREIGN KEY (b, c) REFERENCES other_table (c1, c2)` |
| 102 | + |
| 103 | +a foreign key must reference a UNIQUE column or a primary KEY if no column is specified the primary key will be chosen |
| 104 | + |
| 105 | +#### Referential Triggered Actions |
| 106 | + |
| 107 | +When a value that a foreign key is referencing is updated or deleted |
| 108 | + |
| 109 | +``` |
| 110 | +CREATE TABLE orders( |
| 111 | + product_id integer REFERENCES products ON DELETE CASCADE |
| 112 | +) |
| 113 | +``` |
| 114 | + |
| 115 | + |
| 116 | +Action | ON DELETE | ON UPDATE |
| 117 | + - |- |- |
| 118 | +RESTRICT | prevents the deletion | prevents the update |
| 119 | +CASCADE | any row(s) referencing deleted rows are also deleted | any row(s) referencing updated rows are changed to the new reference |
| 120 | +SET NULL | sets the reference to null | sets the reference to null |
| 121 | +SET DEFAULT | Sets the reference to the default value | Sets the reference to the default value |
| 122 | + |
| 123 | +## DROP |
| 124 | + |
| 125 | +**!! WARNING these commands can destroy large amounts of data** |
| 126 | + |
| 127 | +## [DROP SCHEMA](https://www.postgresql.org/files/documentation/pdf/14/postgresql-14-A4.pdf#page=1843) |
| 128 | + |
| 129 | +Removes a schema from the database |
| 130 | + |
| 131 | +``` |
| 132 | +DROP SCHEMA mystuff |
| 133 | +``` |
| 134 | + |
| 135 | +## [DROP TABLE](https://www.postgresql.org/files/documentation/pdf/14/postgresql-14-A4.pdf#page=1849) |
| 136 | + |
| 137 | +Removes a table from the database |
| 138 | + |
| 139 | +``` |
| 140 | +DROP TABLE mytable |
| 141 | +``` |
| 142 | + |
| 143 | + |
| 144 | +## Other |
| 145 | + |
| 146 | +### Default |
| 147 | + |
| 148 | +used to define a default value if none is provided |
| 149 | + |
| 150 | +``` |
| 151 | +CREATE TABLE meal( |
| 152 | + drink text DEFAULT "water" |
| 153 | +) |
| 154 | +``` |
0 commit comments