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 can I use PostgreSQL and ZFS snapshots together?
- How can I use PostgreSQL with YAML?
- How can I extract the year from a PostgreSQL timestamp?
- How can I extract the year from a date in PostgreSQL?
- How do I use PostgreSQL's XMLTABLE to parse XML data?
- How do I convert a string to lowercase in PostgreSQL?
- How can I convert XML data to a PostgreSQL table?
- How can I set a PostgreSQL interval to zero?
- How can I troubleshoot zero damaged pages in PostgreSQL?
- How do I use PostgreSQL ZonedDateTime to store date and time information?
See more codes...