PostgreSQL - Tutorialspoint

 PostgreSQL

About the Tutorial

PostgreSQL is a powerful, open source object-relational database system. It has more than 15 years of active development and a proven architecture that has earned it a strong reputation for reliability, data integrity, and correctness. PostgreSQL runs on all major operating systems, including Linux, UNIX (AIX, BSD, HP-UX, SGI IRIX, Mac OS X, Solaris, Tru64), and Windows. This tutorial will give you quick start with PostgreSQL and make you comfortable with PostgreSQL programming.

Audience

This tutorial has been prepared for the beginners to help them understand the basic to advanced concepts related to PostgreSQL Database.

Prerequisites

Before you start practicing with various types of examples given in this reference, I'm making an assumption that you are already aware about what is database, especially RDBMS and what is a computer programming language.

Copyright & Disclaimer

Copyright 2017 by Tutorials Point (I) Pvt. Ltd. All the content and graphics published in this e-book are the property of Tutorials Point (I) Pvt. Ltd. The user of this e-book is prohibited to reuse, retain, copy, distribute or republish any contents or a part of contents of this e-book in any manner without written consent of the publisher. We strive to update the contents of our website and tutorials as timely and as precisely as possible, however, the contents may contain inaccuracies or errors. Tutorials Point (I) Pvt. Ltd. provides no guarantee regarding the accuracy, timeliness or completeness of our website or its contents including this tutorial. If you discover any errors on our website or in this tutorial, please notify us at contact@

i

PostgreSQL

Table of Contents

About the Tutorial ............................................................................................................................................ i Audience........................................................................................................................................................... i Prerequisites..................................................................................................................................................... i Copyright & Disclaimer ..................................................................................................................................... i Table of Contents ............................................................................................................................................ ii

1. PostgreSQL ? Overview .............................................................................................................................1 Brief History..................................................................................................................................................... 1 Key Features of PostgreSQL............................................................................................................................. 2 Procedural Languages Support........................................................................................................................ 2

2. PostgreSQL ? Environment Setup..............................................................................................................3 Installing PostgreSQL on Linux/Unix................................................................................................................ 3 Installing PostgreSQL on Windows .................................................................................................................. 4 Installing PostgreSQL on Mac .......................................................................................................................... 7

3. PostgreSQL ? Syntax ...............................................................................................................................11 The SQL Statement ........................................................................................................................................ 11 PostgreSQL SQL commands........................................................................................................................... 11

4. PostgreSQL ? Data Type ..........................................................................................................................35 Numeric Types ............................................................................................................................................... 35 Monetary Types............................................................................................................................................. 36 Character Types ............................................................................................................................................. 36 Binary Data Types .......................................................................................................................................... 37 Date/Time Types ........................................................................................................................................... 37 Boolean Type ................................................................................................................................................. 37 Enumerated Type .......................................................................................................................................... 38 Geometric Type ............................................................................................................................................. 38 Network Address Type .................................................................................................................................. 38 Bit String Type ............................................................................................................................................... 39 Text Search Type ........................................................................................................................................... 39 UUID Type...................................................................................................................................................... 39 XML Type ....................................................................................................................................................... 39 JSON Type ...................................................................................................................................................... 40 Array Type ..................................................................................................................................................... 40 Composite Types ........................................................................................................................................... 41 Range Types................................................................................................................................................... 42 Object Identifier Types .................................................................................................................................. 43 Pseudo Types................................................................................................................................................. 43

5. PostgreSQL ? CREATE Database ..............................................................................................................45 Using createdb Command ............................................................................................................................. 45

6. PostgreSQL ? SELECT Database ...............................................................................................................48 Database SQL Prompt.................................................................................................................................... 48 OS Command Prompt .................................................................................................................................... 49

7. PostgreSQL ? DROP Database .................................................................................................................50 Using dropdb Command................................................................................................................................ 51

ii

PostgreSQL

8. PostgreSQL ? CREATE Table ....................................................................................................................53

9. PostgreSQL ? DROP Table .......................................................................................................................55

10. PostgreSQL ? Schema..............................................................................................................................56 Syntax to Create Table in Schema ................................................................................................................. 56 Syntax to Drop Schema ................................................................................................................................. 57

11. PostgreSQL ? INSERT Query ....................................................................................................................58

12. PostgreSQL ? SELECT Query ....................................................................................................................60

13. PostgreSQL ? Operators ..........................................................................................................................62 PostgreSQL Arithmetic Operators ................................................................................................................. 62 PostgreSQL Comparison Operators ............................................................................................................... 64 PostgreSQL Logical Operators ....................................................................................................................... 66 PostgreSQL Bit String Operators.................................................................................................................... 69

14. PostgreSQL ? Expressions........................................................................................................................71 PostgreSQL ? Boolean Expressions................................................................................................................ 71 PostgreSQL ? Numeric Expression................................................................................................................. 72 PostgreSQL ? Date Expressions ..................................................................................................................... 73

15. PostgreSQL ? WHERE Clause ...................................................................................................................74

16. PostgreSQL ? AND & OR Conjunctive Operators .....................................................................................79 The AND Operator ......................................................................................................................................... 79 The OR Operator ........................................................................................................................................... 80

17. PostgreSQL ? UPDATE Query ..................................................................................................................82

18. PostgreSQL ? DELETE Query ....................................................................................................................84

19. PostgreSQL ? LIKE Clause ........................................................................................................................86

20. PostgreSQL ? LIMIT Clause ......................................................................................................................89

21. PostgreSQL ? ORDER BY Clause...............................................................................................................91

22. PostgreSQL ? GROUP BY .........................................................................................................................94

23. PostgreSQL ? WITH Clause ......................................................................................................................97 Recursive WITH ............................................................................................................................................. 97

24. PostgreSQL ? HAVING Clause ................................................................................................................101

25. PostgreSQL ? DISTINCT Keyword...........................................................................................................104

ADVANCED POSTGRESQL ........................................................................................................ 107

26. PostgreSQL ? CONSTRAINTS..................................................................................................................108 NOT NULL Constraint................................................................................................................................... 108 UNIQUE Constraint ...................................................................................................................................... 109

iii

PostgreSQL

PRIMARY KEY Constraint ............................................................................................................................. 109 FOREIGN KEY Constraint.............................................................................................................................. 110 CHECK Constraint ........................................................................................................................................ 111 EXCLUSION Constraint................................................................................................................................. 111 Dropping Constraints................................................................................................................................... 112

27. PostgreSQL ? JOINS...............................................................................................................................113 The CROSS JOIN ........................................................................................................................................... 114 The INNER JOIN ........................................................................................................................................... 115 The LEFT OUTER JOIN .................................................................................................................................. 116 The RIGHT OUTER JOIN ............................................................................................................................... 117 The FULL OUTER JOIN .................................................................................................................................. 117

28. PostgreSQL ? UNIONS Clause ................................................................................................................119 The UNION ALL Clause................................................................................................................................. 121

29. PostgreSQL ? NULL Values ....................................................................................................................123

30. PostgreSQL ? ALIAS Syntax....................................................................................................................126

31. PostgreSQL ? TRIGGERS ........................................................................................................................129 Listing TRIGGERS.......................................................................................................................................... 132 Dropping TRIGGERS ..................................................................................................................................... 132

32. PostgreSQL ? INDEXES ..........................................................................................................................133 Index Types.................................................................................................................................................. 133 The DROP INDEX Command ........................................................................................................................ 135 When Should Indexes be Avoided? ............................................................................................................. 135

33. PostgreSQL ? ALTER TABLE Command...................................................................................................136

34. PostgreSQL ? TRUNCATE TABLE Command ...........................................................................................139

35. PostgreSQL ? VIEWS..............................................................................................................................140 Creating Views ............................................................................................................................................. 140 Dropping Views ........................................................................................................................................... 142

36. PostgreSQL ? TRANSACTIONS ...............................................................................................................143 Transaction Control ..................................................................................................................................... 143 The COMMIT Command .............................................................................................................................. 144 The ROLLBACK Command............................................................................................................................ 144

37. PostgreSQL ? LOCKS ..............................................................................................................................146 DeadLocks.................................................................................................................................................... 146 Advisory Locks ............................................................................................................................................. 147

38. PostgreSQL ? Sub Queries .....................................................................................................................148 Subqueries with the SELECT Statement ...................................................................................................... 148 Subqueries with the INSERT Statement ...................................................................................................... 149 Subqueries with the UPDATE Statement..................................................................................................... 150 Subqueries with the DELETE Statement ...................................................................................................... 151

39. PostgreSQL ? AUTO INCREMENT...........................................................................................................153

iv

................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download