postgresqlHow can I use PostgreSQL types to create a database?
PostgreSQL provides a wide range of data types that can be used to create a database. The following example code creates a table called "users" with three columns:
CREATE TABLE users (
id SERIAL PRIMARY KEY,
name VARCHAR(50) NOT NULL,
email VARCHAR(50) NOT NULL
);
The id
column is of type SERIAL
, which is an auto-incrementing integer. The name
and email
columns are of type VARCHAR
, which is a variable-length character type.
The following query inserts a row into the users
table:
INSERT INTO users (name, email)
VALUES ('John Doe', '[email protected]');
The output of this query is:
INSERT 0 1
The following query retrieves the newly inserted row:
SELECT * FROM users;
The output of this query is:
id | name | email
----+--------+---------------------
1 | John Doe | [email protected]
(1 row)
Code explanation
CREATE TABLE
- used to create a table in the databaseSERIAL
- an auto-incrementing integer typeVARCHAR
- a variable-length character typeINSERT INTO
- used to insert a row into a tableSELECT
- used to retrieve data from a table
Helpful links
More of Postgresql
- How do I use PostgreSQL ZonedDateTime to store date and time information?
- How do I parse XML data using PostgreSQL?
- How can I use PostgreSQL and ZFS snapshots together?
- How can I troubleshoot zero damaged pages in PostgreSQL?
- How can Zalando use PostgreSQL to improve its software development?
- How can I retrieve data from PostgreSQL for yesterday's date?
- How can I use PostgreSQL XOR to compare two values?
- How do I set the PostgreSQL work_mem parameter?
- How can I use PostgreSQL with YAML?
- How can I use PostgreSQL XML functions to manipulate XML data?
See more codes...