Web hosting solutions - 642 CHAPTER 28 FROM DATABASES TO DATATYPES
April 28th, 2008642 CHAPTER 28 FROM DATABASES TO DATATYPES You can view the results of these entries easily enough: rob=# SELECT * FROM default_now_example; attempt | insert_time ———+——————————a | 2005-10-16 15:41:39.382608-05 b | 1492-01-13 21:12:00-05 | 2005-10-16 15:42:17.860467-05 rows) As you can see, in our first INSERT statement, the default time was entered because we passed in the DEFAULT keyword. In the second, the time we specified was entered. In the third, an autogenerated time was inserted because we did not specify a value; this is the same behavior as using the DEFAULT keyword. NOT NULL Defining a column as NOT NULL disallows any attempt to insert a NULL value into the column. Using the NOT NULL attribute, where relevant, is always suggested, because it results in at least baseline verification that all necessary values have been passed to the query. An example of a NOT NULL column assignment follows: zipcode VARCHAR(10) NOT NULL NULL Simply stated, the NULL attribute means that NULL values are acceptable for the given field. This is also the default value for the field if no data is given and there is no DEFAULT attribute specified. This is the default characteristic for columns in PostgreSQL, so you will not often see it stated explicitly. PRIMARY KEY The PRIMARY KEY attribute is used to guarantee uniqueness for a given row. No values residing in a column designated as PRIMARY KEY are repeatable or nullable within that column. It s quite common to see SERIAL columns designated as a primary key, because this column doesn t necessarily have to bear any relation to the row data, other than acting as its unique identifier. However, there are two other ways for ensuring a record s uniqueness: Single-field primary keys: Typically used when a pre-existing, nonmodifiable unique identifier exists for each row entered into the database, such as a part number or social security number. Note that this key should never change once it is set. Multiple-field primary keys: Can be useful when it is not possible to guarantee uniqueness from any single field within a record. Thus, multiple fields are conjoined to ensure uniqueness. If the number of columns required to ensure uniqueness grows cumbersome, it is common practice to simply designate a SERIALinteger as the primary key, to alleviate the need to somehow generate unique identifiers with every insertion. The following three examples demonstrate creation of the auto-increment, single-field, and multiple-field primary key fields, respectively.
Please visit our professional web hosting services to find out about cheap and reliable webhost service that will surely answer all your demands.