fredag 24 augusti 2012

SQL good practies

SQL good practies, from SO

Column order does matter so if (and only if) the column orders match you can for example:

insert into items_verselect * from items where item_id=2;

Or if they don't match you could for example:

insert into items_ver(item_id, item_group, name)
select * from items where item_id=2;

but relying on column order is a bug waiting to happen (it can change, as can the number of columns) - it also makes your SQL harder to read
There is no good 'shortcut' - you should explicitly list columns for both the table you are inserting into and the query you are using for the source data, eg:
insert into items_ver (item_id, name, item_group)
select item_id, name, item_group from items where item_id=2;

Inga kommentarer:

Skicka en kommentar