| 112 | '' |
| 113 | Another possibility is to create VIEWs: |
| 114 | '' |
| 115 | {{{ |
| 116 | #!sql |
| 117 | CREATE TABLE "myapp_place" ( |
| 118 | "id" integer NOT NULL PRIMARY KEY, |
| 119 | "name" varchar(50) NOT NULL |
| 120 | ); |
| 121 | |
| 122 | CREATE TABLE "myapp_restaurant_base" ( |
| 123 | "id" integer NOT NULL PRIMARY KEY REFERENCES "myapp_places" ("id"), |
| 124 | "description" text NOT NULL |
| 125 | ); |
| 126 | |
| 127 | CREATE VIEW "myapp_restaurant" AS |
| 128 | SELECT |
| 129 | a."id", a."name", |
| 130 | b."description" |
| 131 | FROM "myapp_place" AS a |
| 132 | LEFT JOIN "myapp_restaurant_base" AS b ON a."id"=b."id" |
| 133 | ; |
| 134 | |
| 135 | CREATE TABLE "myapp_italianrestaurant_base" ( |
| 136 | "id" integer NOT NULL PRIMARY KEY REFERENCES "myapp_restaurant" ("id"), |
| 137 | "has_decent_gnocchi" bool NOT NULL |
| 138 | ); |
| 139 | |
| 140 | CREATE VIEW "myapp_italianrestaurant" AS |
| 141 | SELECT |
| 142 | a."id", a."name", |
| 143 | b."description", |
| 144 | c."has_decent_gnocchi" |
| 145 | FROM "myapp_place" AS a |
| 146 | LEFT JOIN "myapp_restaurant_base" AS b ON a."id"=b."id" |
| 147 | LEFT JOIN "myapp_italianrestaurant_base" AS c on a."id"=c."id" |
| 148 | ; |
| 149 | }}} |
| 150 | |
| 151 | In MySQL-5.0, most of the theoretically-updatable VIEWs are updatable, but currently, "You cannot use UPDATE to update more than one underlying table of a view that is defined as a join." so unfortunately this doesn't work (yet): |
| 152 | |
| 153 | {{{ |
| 154 | mysql> INSERT INTO "myapp_italianrestaurant" ("name","description","has_decent_gnocchi") |
| 155 | -> VALUES ('DePalma\'s', 'Fine Italian Dining', 1); |
| 156 | ERROR 1288 (HY000): The target table myapp_italianrestaurant of the INSERT is not updatable |
| 157 | }}} |
| 158 | '' |
| 159 | However, it is possible to do this: |
| 160 | '' |
| 161 | {{{ |
| 162 | mysql> INSERT INTO "myapp_place" ("id","name") VALUES (1,'DePalma\'s'); |
| 163 | Query OK, 1 row affected (0.00 sec) |
| 164 | |
| 165 | mysql> INSERT INTO "myapp_restaurant_base" ("id","description") VALUES (1,'Fine Italian Dining'); |
| 166 | Query OK, 1 row affected (0.01 sec) |
| 167 | |
| 168 | mysql> INSERT INTO "myapp_italianrestaurant_base" ("id","has_decent_gnocchi") VALUES (1,1); |
| 169 | Query OK, 1 row affected (0.00 sec) |
| 170 | |
| 171 | mysql> UPDATE "myapp_italianrestaurant" SET "description"='Exquisite Italian Dining'; |
| 172 | Query OK, 1 row affected (0.00 sec) |
| 173 | Rows matched: 1 Changed: 1 Warnings: 0 |
| 174 | |
| 175 | mysql> SELECT * FROM myapp_italianrestaurant; |
| 176 | +----+-----------+--------------------------+--------------------+ |
| 177 | | id | name | description | has_decent_gnocchi | |
| 178 | +----+-----------+--------------------------+--------------------+ |
| 179 | | 1 | DePalma's | Exquisite Italian Dining | 1 | |
| 180 | +----+-----------+--------------------------+--------------------+ |
| 181 | 1 rows in set (0.03 sec) |
| 182 | }}} |
| 183 | '' |
| 184 | Unfortunately if you try to update name and description at the same time, this fails. The VIEW scheme |
| 185 | would still be useful for SELECT, but I think the base tables will have to be updated directly. |
| 186 | At least VIEWs buy you an easier SELECT statement without JOINs. It's possible to handle some |
| 187 | of the VIEW updating with INSTEAD OF triggers with Oracle, IBM DB2, and MS SQL (PostgreSQL uses |
| 188 | CREATE RULE ... AS ON (INSERT|UPDATE) ... DO INSTEAD), but that kind is not in MySQL yet. |
| 189 | I'm not sure how much effort it's worth. -- Andy Dustman |
| 190 | '' |
| 191 | |