Join Bill Weinman for an in-depth discussion in this video Understanding data types, part of PostgreSQL 9 with PHP Essential Training.
Postgres has a rich set of data types, and while you may never use most of them, it's a good idea to have an overview of what's available. Numeric types include three sizes of integers--2-byte, 4-byte, and 8-byte sizes-- decimal or numeric for fixed precision applications, like money, and note that while Postgres has a money type, this is depreciated so you should avoid using it. You should use decimal or numeric instead. Decimal and numeric are the same thing. It has real and double types for a floating point precision, and it has serial types for automatically incrementing unique identifiers.
Character types include the standard VARCHAR, or Character Variant for Variable Length Strings; CHAR for fixed length strings; and TEXT, which is actually equivalent to VARCHAR with no maximum length specified. It's useful to note that the fixed- length strings are actually less efficient in Postgres than the VARCHARs. The VARCHARs and the text are more efficient than the fixed-length character strings, although the fixed-length character strings can be more efficient with storage depending on the application.
So it's important to look at your application and understand what you are looking for: efficiency and speed, or storage space. And I think most of the time you will pick the text type or the VARCHAR type. It's also worth noting that with VARCHAR you can specify a maximum length, and this may be important in some applications, because that constraint gets imposed by the database, although in most cases where you are running your database from an application it may actually be more efficient for you to implement that constraint in your host language than in the database.
So again this is something to look at in terms of performance and convenience, but I think most of the time of you look at all these trade-offs you are going to select the text type for almost all text. Postgres also supports the standard date and time types: timestamp that stores both date and time, date for dates alone and time for time alone, and an interval type for a time intervals. Just a few other data types that are supported. There are binary types, and these are packed binary so you get one bit per binary value.
It's extremely efficient in storage space. Boolean types, now this is different than binary types. Boolean types are for logical Booleans, and this is basically true or false, and these take up one byte of storage per Boolean value. So they're not efficient on storage, but they are convenient for use. And there is enumerated types for enumerations. So Postgres has a rich set of data types available, and while you may never use most of them, it's a good idea to have an overview of what's available.
Author
Released
11/10/2011- Installing PostgreSQL
- Understanding database architecture
- Inserting, updating, and deleting data in a table
- Creating a database library
- Indexing ID fields
- Storing numbers, text, and Boolean values
- Reading data
- Using casts to force type
- Using mathematical functions
- Concatenating strings
- Working with date and time functions and operators
- Defining CRUD
- Using PHP to insert, read, update, and delete rows in a database
Skill Level Beginner
Duration
Views
Related Courses
-
PHP with MySQL Beyond the Basics (2009)
with Kevin Skoglund10h 26m Intermediate
-
Introduction
-
Welcome54s
-
Prerequisites3m 19s
-
Installing PostgreSQL on a Mac14m 49s
-
Installing PostgreSQL on a PC11m 16s
-
Installing XAMPP and SID on a PC13m 34s
-
-
1. Quick Start
-
Creating a database5m 28s
-
Creating a table7m 1s
-
Inserting data into a table8m 25s
-
Getting data from a table4m 37s
-
Updating data in a table4m 29s
-
Creating a database library7m 20s
-
2. Creating a Database
-
Creating a database7m 14s
-
Defining a table in SQL5m 35s
-
Creating a table in PHP6m 41s
-
Creating indexes5m 20s
-
Indexing ID fields3m 26s
-
3. Data Types
-
Understanding data types2m 53s
-
Storing text3m 17s
-
Storing Boolean values2m 11s
-
Storing dates and times4m 49s
-
-
4. Storing and Reading Data
-
Reading data with SELECT5m 24s
-
Reading with JOIN4m 16s
-
5. SQL Expressions
-
Matching patterns with LIKE2m 51s
-
Using casts to force type2m 26s
-
6. Mathematical Functions and Operators
-
Using math functions3m 2s
-
7. String Functions and Operators
-
Concatenating strings1m 34s
-
Trimming and padding strings2m 49s
-
Converting numbers1m 48s
-
8. Date and Time Functions and Operators
-
9. PHP Interfaces
-
Choosing an interface1m 39s
-
Using the PDO interface9m 53s
-
Creating a library11m 19s
-
-
10. Web Applications
-
Defining CRUD4m 18s
-
Managing the database in PHP3m 29s
-
-
Conclusion
-
Goodbye31s
-
- Mark as unwatched
- Mark all as unwatched
Are you sure you want to mark all the videos in this course as unwatched?
This will not affect your course history, your reports, or your certificates of completion for this course.
CancelTake notes with your new membership!
Type in the entry box, then click Enter to save your note.
1:30Press on any video thumbnail to jump immediately to the timecode shown.
Notes are saved with you account but can also be exported as plain text, MS Word, PDF, Google Doc, or Evernote.
Share this video
Embed this video
Video: Understanding data types