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 troubleshoot zero damaged pages in PostgreSQL?
- How do I use PostgreSQL's XMIN and XMAX features?
- How can I monitor PostgreSQL performance using Zabbix?
- How can I integrate PostgreSQL with Yii2?
- How can I use PostgreSQL with YAML?
- How do I use PostgreSQL's XMLTABLE to parse XML data?
- How do I use PostgreSQL UNION to combine the results of two queries?
- How do I decide whether to use PostgreSQL VARCHAR or TEXT data types?
- How do I use PostgreSQL with Qt?
- How can I retrieve data from PostgreSQL for yesterday's date?
See more codes...