轉:PostgreSQL Cheat Sheet

來源:互聯網
上載者:User

標籤:statement   upd   dealloc   ace   http   int   form   insert   back   

PostgreSQL Cheat SheetCREATE DATABASE
CREATE DATABASE dbName;
CREATE TABLE (with auto numbering integer id)
CREATE TABLE tableName ( id serial PRIMARY KEY, name varchar(50) UNIQUE NOT NULL, dateCreated timestamp DEFAULT current_timestamp);
Add a primary key
ALTER TABLE tableName ADD PRIMARY KEY (id);
Create an INDEX
CREATE UNIQUE INDEX indexName ON tableName (columnNames);
Backup a database  (command line)
pg_dump dbName > dbName.sql
Backup all databases  (command line)
pg_dumpall > pgbackup.sql
Run a SQL script  (command line)
psql -f script.sql databaseName
Search using a regular expression
SELECT column FROM table WHERE column ~ ‘foo.*‘;
The first  N records
SELECT columns FROM table LIMIT 10;
Pagination
SELECT cols FROM table LIMIT 10 OFFSET 30;
Prepared Statements
PREPARE preparedInsert (int, varchar) AS  INSERT INTO tableName (intColumn, charColumn) VALUES ($1, $2);EXECUTE preparedInsert (1,‘a‘);EXECUTE preparedInsert (2,‘b‘);DEALLOCATE preparedInsert;
Create a Function
CREATE OR REPLACE FUNCTION month (timestamp) RETURNS integer  AS ‘SELECT date_part(‘‘month‘‘, $1)::integer;‘LANGUAGE ‘sql‘;
Table Maintenance
VACUUM ANALYZE table;
Reindex a database, table or index
REINDEX DATABASE dbName;
Show query plan
EXPLAIN SELECT * FROM table;
Import from a file
COPY destTable FROM ‘/tmp/somefile‘;
Show all runtime parameters
SHOW ALL;
Grant all permissions to a user
GRANT ALL PRIVILEGES ON table TO username;
Perform a transaction
BEGIN TRANSACTION  UPDATE accounts SET balance += 50 WHERE id = 1;COMMIT;

Basic SQL

Get all columns and rows from a table
SELECT * FROM table;
Add a new row
INSERT INTO table (column1,column2)VALUES (1, ‘one‘);
Update a row
UPDATE table SET foo = ‘bar‘ WHERE id = 1;
Delete a row
DELETE FROM table WHERE id = 1;

 

http://www.petefreitag.com/cheatsheets/postgresql/

 

轉:PostgreSQL Cheat Sheet

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.