mirror of
https://github.com/jbranchaud/til
synced 2026-01-03 15:18:01 +00:00
Add Create A Composite Primary Key as a postgres til.
This commit is contained in:
@@ -105,6 +105,7 @@ smart people at [Hashrocket](http://hashrocket.com/).
|
||||
- [Checking The Type Of A Value](postgres/checking-the-type-of-a-value.md)
|
||||
- [Configure The Timezone](postgres/configure-the-timezone.md)
|
||||
- [Count Records By Type](postgres/count-records-by-type.md)
|
||||
- [Create A Composite Primary Key](postgres/create-a-composite-primary-key.md)
|
||||
- [Default Schema](postgres/default-schema.md)
|
||||
- [Defining Arrays](postgres/defining-arrays.md)
|
||||
- [Edit Existing Functions](postgres/edit-existing-functions.md)
|
||||
|
||||
19
postgres/create-a-composite-primary-key.md
Normal file
19
postgres/create-a-composite-primary-key.md
Normal file
@@ -0,0 +1,19 @@
|
||||
# Create A Composite Primary Key
|
||||
|
||||
The unique identifier for a given row in a table is the *primary key*.
|
||||
Generally, a row can be uniquely identified by a single data point (such as
|
||||
an id), so the primary key is simply that single data point. In some cases,
|
||||
your data can be more appropriately uniquely identified by multiple values.
|
||||
This is where composite primary keys can lend a hand. Consider an example
|
||||
`plane_tickets` table where each ticket can be uniquely identified by the
|
||||
passenger and flight it is associated with:
|
||||
|
||||
```sql
|
||||
create table plane_tickets (
|
||||
passenger_id integer references passengers not null,
|
||||
flight_id integer references flights not null,
|
||||
confirmation_number varchar(6) not null,
|
||||
seat_assignment varchar not null,
|
||||
primary key (passenger_id, flight_id)
|
||||
);
|
||||
```
|
||||
Reference in New Issue
Block a user