mirror of
https://github.com/jbranchaud/til
synced 2026-01-03 23:28:02 +00:00
Add Create Table Adds A Data Type as a postgres til
This commit is contained in:
@@ -7,7 +7,7 @@ variety of languages and technologies. These are things that don't really
|
|||||||
warrant a full blog post. These are mostly things I learn by pairing with
|
warrant a full blog post. These are mostly things I learn by pairing with
|
||||||
smart people at [Hashrocket](http://hashrocket.com/).
|
smart people at [Hashrocket](http://hashrocket.com/).
|
||||||
|
|
||||||
_401 TILs and counting..._
|
_402 TILs and counting..._
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
@@ -180,6 +180,7 @@ _401 TILs and counting..._
|
|||||||
- [Count Records By Type](postgres/count-records-by-type.md)
|
- [Count Records By Type](postgres/count-records-by-type.md)
|
||||||
- [Create A Composite Primary Key](postgres/create-a-composite-primary-key.md)
|
- [Create A Composite Primary Key](postgres/create-a-composite-primary-key.md)
|
||||||
- [Create hstore From Two Arrays](postgres/create-hstore-from-two-arrays.md)
|
- [Create hstore From Two Arrays](postgres/create-hstore-from-two-arrays.md)
|
||||||
|
- [Create Table Adds A Data Type](postgres/create-table-adds-a-data-type.md)
|
||||||
- [Creating Conditional Constraints](postgres/creating-conditional-constraints.md)
|
- [Creating Conditional Constraints](postgres/creating-conditional-constraints.md)
|
||||||
- [Creating Custom Types](postgres/creating-custom-types.md)
|
- [Creating Custom Types](postgres/creating-custom-types.md)
|
||||||
- [Day Of Week By Name For A Date](postgres/day-of-week-by-name-for-a-date.md)
|
- [Day Of Week By Name For A Date](postgres/day-of-week-by-name-for-a-date.md)
|
||||||
|
|||||||
32
postgres/create-table-adds-a-data-type.md
Normal file
32
postgres/create-table-adds-a-data-type.md
Normal file
@@ -0,0 +1,32 @@
|
|||||||
|
# Create Table Adds A Data Type
|
||||||
|
|
||||||
|
Each time you create a table in PostgreSQL, a new data type represented by
|
||||||
|
that table is created and added to the `pg_type` table. According to the
|
||||||
|
Postgres docs:
|
||||||
|
|
||||||
|
> CREATE TABLE also automatically creates a data type that represents the
|
||||||
|
> composite type corresponding to one row of the table. Therefore, tables
|
||||||
|
> cannot have the same name as any existing data type in the same schema.
|
||||||
|
|
||||||
|
For instance, if you create a `users` table like so:
|
||||||
|
|
||||||
|
```sql
|
||||||
|
create table users (
|
||||||
|
id serial primary key,
|
||||||
|
first_name varchar not null,
|
||||||
|
last_name varchar not null
|
||||||
|
);
|
||||||
|
```
|
||||||
|
|
||||||
|
then the `pg_type` will now contain an entry with a `typname` of `users`.
|
||||||
|
|
||||||
|
```sql
|
||||||
|
select * from pg_type where typname = 'users';
|
||||||
|
-[ RECORD 1 ]--+------------
|
||||||
|
typname | users
|
||||||
|
typnamespace | 2200
|
||||||
|
typowner | 16384
|
||||||
|
...
|
||||||
|
```
|
||||||
|
|
||||||
|
h/t Bruce Momjian
|
||||||
Reference in New Issue
Block a user