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's XMLTABLE to parse XML data?
- How do I use a PostgreSQL XML parser in an example?
- How do I use a PostgreSQL transaction?
- How do I use PostgreSQL's UNION ALL statement?
- How do I use PostgreSQL with Qt?
- How can I monitor PostgreSQL performance using Zabbix?
- How can I use PostgreSQL and ZFS snapshots together?
- How can I set a PostgreSQL interval to zero?
- How can I troubleshoot zero damaged pages in PostgreSQL?
- How do I use PostgreSQL's XMIN and XMAX features?
See more codes...