Programming/PostgreSQL/Table Queries: Difference between revisions
< Programming | PostgreSQL
Jump to navigation
Jump to search
Brodriguez (talk | contribs) (Update page) |
Brodriguez (talk | contribs) (Minor clean up) |
||
(5 intermediate revisions by the same user not shown) | |||
Line 1: | Line 1: | ||
All of the following assume you have first [[Programming/PostgreSQL/Databases#Load_a_Database | loaded a database]]. | All of the following assume you have first [[Programming/PostgreSQL/Databases#Load_a_Database | loaded a database]]. | ||
These also require replacing {{ ic |<table_name>}} with the actual name of the table to query. | |||
Line 22: | Line 24: | ||
== Create Table == | == Create Table == | ||
CREATE TABLE | CREATE TABLE <table_name> ( | ||
<columns> | <columns> | ||
); | ); | ||
Where {{ ic |<columns>}} is replaced by the actual columns desired for the table. | Where {{ ic |<columns>}} is replaced by the actual columns desired for the table. | ||
For example, to create a table with columns of {{ ic |id, name, description}}, we can do: | For example, to create a table with columns of {{ ic |id, name, description}}, we can do: | ||
CREATE TABLE | CREATE TABLE <table_name> ( | ||
id serial PRIMARY KEY, | id serial PRIMARY KEY, | ||
name VARCHAR(100), | name VARCHAR(100), | ||
Line 38: | Line 41: | ||
== Delete Table == | == Delete Table == | ||
DROP TABLE <table_name>; | DROP TABLE <table_name>; | ||
== Display Number of Records in Table == | |||
SELECT COUNT(*) FROM <table_name>; |
Latest revision as of 03:06, 27 January 2023
All of the following assume you have first loaded a database.
These also require replacing <table_name>
with the actual name of the table to query.
Show Tables
\dt
Or for expanded data:
\dt+
Alternatively:
SELECT table_name FROM information_schema.tables WHERE table_schema = 'public';
Show Extra Relational Data About Table
\d <table_name>
Show Table Indexes
SELECT indexname, indexdef FROM pg_indexes WHERE tablename = '<table_name>';
Create Table
CREATE TABLE <table_name> ( <columns> );
Where <columns>
is replaced by the actual columns desired for the table.
For example, to create a table with columns of id, name, description
, we can do:
CREATE TABLE <table_name> ( id serial PRIMARY KEY, name VARCHAR(100), description VARCHAR(100) );
Delete Table
DROP TABLE <table_name>;
Display Number of Records in Table
SELECT COUNT(*) FROM <table_name>;