644 CHAPTER 28 FROM DATABASES TO DATATYPES (Top web site)
Wednesday, April 30th, 2008644 CHAPTER 28 FROM DATABASES TO DATATYPES related tables can be left alone. We can also create foreign keys between tables based on a group of columns between the two tables. We will re-create our staff table again to show the syntax: CREATE TABLE staff ( staffid SERIAL NOT NULL PRIMARY KEY, email TEXT NOT NULL, lname TEXT, nickname TEXT, FOREIGN KEY (lname,nickname) REFERENCES friends(lname,nickname) ); This syntax sets up the relationship just like our previous example; any entry in staff must now have a corresponding entry, based on both the lname and fname columns, in the friends table. UNIQUE A column assigned the UNIQUE attribute ensures that all values possess distinct values, except that NULL values are repeatable. You typically designate a column as UNIQUE to ensure that all fields within that column are distinct for example, to prevent the same e-mail address from being inserted into a newsletter subscriber table multiple times, while at the same time acknowledging that the field could potentially be empty (NULL). An example of a column designated as UNIQUE follows: email TEXT UNIQUE Composite Datatypes A composite datatype defines the structure of a row or record. In simple terms, it is a list of field names and their datatypes. Once a composite type is created, it can be used much like any other datatype, such as when defining a column in a table or declaring a return type for a function. This can prove very useful when you want to tightly couple related information together into a single logical piece. Creating Composite Types You can use the CREATETYPEcommand to create composite types. As shown next, the syntax is similar to that of the CREATETABLE command, though only field names and datatypes can be specified. No constraints or default values can be included. CREATE TYPE im_accounts AS ( jabber text, aim text, irc text ); Let s run through a quick example so that you can see exactly how this works. First, we create a table in which to use our new composite type:
You need excellent and relaible webhost company to host your web applications? Then pay a visit to Inexpensive Web Hosting services.