{"id":46,"date":"2006-05-19T20:48:21","date_gmt":"2006-05-19T19:48:21","guid":{"rendered":"http:\/\/bar-solutions.com\/wordpress\/?p=46"},"modified":"2006-05-20T08:29:13","modified_gmt":"2006-05-20T07:29:13","slug":"defaults","status":"publish","type":"post","link":"https:\/\/blog.bar-solutions.com\/?p=46","title":{"rendered":"Defaults"},"content":{"rendered":"<p>Oracle 9i and up provide you with a new keyword: <strong>DEFAULT<\/strong>. This means you can get to the default values of a column when doing an insert or update.<\/p>\n<p>I have tried some things with this on my 10G XE database and I have come up with the following script to test it all (a bit).<\/p>\n<p> <font><\/font><font><\/font><font color=\"#0000f0\">DROP<\/font> <font color=\"#0000f0\">TABLE<\/font> customer<font color=\"#ff0000\">;<\/font><br \/> <em><font color=\"#339960\">&#8212; Create table<\/font><\/em><br \/> <font color=\"#0000f0\">create<\/font> <font color=\"#0000f0\">table<\/font> customer<br \/> <font color=\"#ff0000\">(<\/font><br \/> <font color=\"#0000f0\">id<\/font> <font color=\"#0000f0\">number<\/font><font color=\"#ff0000\">(8),<\/font><br \/> <font color=\"#0000f0\">name<\/font> <font color=\"#0000f0\">varchar2<\/font><font color=\"#ff0000\">(50)<\/font><br \/> <font color=\"#ff0000\">)<\/font><br \/> <font color=\"#ff0000\">;<\/font><br \/> <em><font color=\"#339960\">&#8212; Add comments to the columns <\/font><\/em><br \/> <font color=\"#0000f0\">comment<\/font> <font color=\"#0000f0\">on<\/font> <font color=\"#0000f0\">column<\/font> customer.<font color=\"#0000f0\">id<\/font><br \/> <font color=\"#0000f0\">is<\/font> <font color=\"#ff0000\">&#39;Customer ID&#39;;<\/font><br \/> <font color=\"#0000f0\">comment<\/font> <font color=\"#0000f0\">on<\/font> <font color=\"#0000f0\">column<\/font> customer.<font color=\"#0000f0\">name<\/font><br \/> <font color=\"#0000f0\">is<\/font> <font color=\"#ff0000\">&#39;Customer Name&#39;;<\/font><br \/> <em><font color=\"#339960\">&#8212; Create\/Recreate primary, unique and foreign key constraints <\/font><\/em><br \/> <font color=\"#0000f0\">alter<\/font> <font color=\"#0000f0\">table<\/font> customer<br \/> <font color=\"#0000f0\">add<\/font> <font color=\"#0000f0\">constraint<\/font> pk_customer <font color=\"#0000f0\">primary<\/font> <font color=\"#0000f0\">key<\/font> <font color=\"#ff0000\">(<\/font><font color=\"#0000f0\">ID<\/font><font color=\"#ff0000\">);<\/font><br \/> <em><font color=\"#339960\">&#8212; create the sequence<\/font><\/em><br \/> <font color=\"#0000f0\">CREATE<\/font> <font color=\"#0000f0\">SEQUENCE<\/font> customer_id <font color=\"#0000f0\">START<\/font> <font color=\"#0000f0\">WITH<\/font> <font color=\"#ff0000\">1<\/font> <font color=\"#0000f0\">NOCACHE<\/font><font color=\"#ff0000\">;<\/font><br \/> <em><font color=\"#339960\">&#8212; add a couple of customers<\/font><\/em><br \/> <font color=\"#0000f0\">INSERT<\/font> <font color=\"#0000f0\">INTO<\/font> customer<font color=\"#ff0000\">(<\/font><font color=\"#0000f0\">ID<\/font><font color=\"#ff0000\">,<\/font> <font color=\"#0000f0\">name<\/font><font color=\"#ff0000\">)<\/font> <font color=\"#0000f0\">VALUES<\/font> <font color=\"#ff0000\">(<\/font>customer_id.<font color=\"#0000f0\">NEXTVAL<\/font><font color=\"#ff0000\">,<\/font> <font color=\"#ff0000\">&#39;Patrick&#39;);<\/font><br \/> <font color=\"#0000f0\">INSERT<\/font> <font color=\"#0000f0\">INTO<\/font> customer<font color=\"#ff0000\">(<\/font><font color=\"#0000f0\">ID<\/font><font color=\"#ff0000\">,<\/font> <font color=\"#0000f0\">name<\/font><font color=\"#ff0000\">)<\/font> <font color=\"#0000f0\">VALUES<\/font> <font color=\"#ff0000\">(<\/font>customer_id.<font color=\"#0000f0\">NEXTVAL<\/font><font color=\"#ff0000\">,<\/font> <font color=\"#ff0000\">&#39;Dana&#39;);<\/font><br \/> <font color=\"#0000f0\">INSERT<\/font> <font color=\"#0000f0\">INTO<\/font> customer<font color=\"#ff0000\">(<\/font><font color=\"#0000f0\">ID<\/font><font color=\"#ff0000\">,<\/font> <font color=\"#0000f0\">name<\/font><font color=\"#ff0000\">)<\/font> <font color=\"#0000f0\">VALUES<\/font> <font color=\"#ff0000\">(<\/font>customer_id.<font color=\"#0000f0\">NEXTVAL<\/font><font color=\"#ff0000\">,<\/font> <font color=\"#ff0000\">&#39;Quinty&#39;);<\/font><br \/> <font color=\"#0000f0\">INSERT<\/font> <font color=\"#0000f0\">INTO<\/font> customer<font color=\"#ff0000\">(<\/font><font color=\"#0000f0\">ID<\/font><font color=\"#ff0000\">,<\/font> <font color=\"#0000f0\">name<\/font><font color=\"#ff0000\">)<\/font> <font color=\"#0000f0\">VALUES<\/font> <font color=\"#ff0000\">(<\/font>customer_id.<font color=\"#0000f0\">NEXTVAL<\/font><font color=\"#ff0000\">,<\/font> <font color=\"#ff0000\">&#39;Kayleigh&#39;);<\/font><br \/> <font color=\"#0000f0\">INSERT<\/font> <font color=\"#0000f0\">INTO<\/font> customer<font color=\"#ff0000\">(<\/font><font color=\"#0000f0\">ID<\/font><font color=\"#ff0000\">,<\/font> <font color=\"#0000f0\">name<\/font><font color=\"#ff0000\">)<\/font> <font color=\"#0000f0\">VALUES<\/font> <font color=\"#ff0000\">(<\/font>customer_id.<font color=\"#0000f0\">NEXTVAL<\/font><font color=\"#ff0000\">,<\/font> <font color=\"#ff0000\">&#39;Mitchell&#39;);<\/font><br \/> <font color=\"#0000f0\">INSERT<\/font> <font color=\"#0000f0\">INTO<\/font> customer<font color=\"#ff0000\">(<\/font><font color=\"#0000f0\">ID<\/font><font color=\"#ff0000\">,<\/font> <font color=\"#0000f0\">name<\/font><font color=\"#ff0000\">)<\/font> <font color=\"#0000f0\">VALUES<\/font> <font color=\"#ff0000\">(<\/font>customer_id.<font color=\"#0000f0\">NEXTVAL<\/font><font color=\"#ff0000\">,<\/font> <font color=\"#ff0000\">&#39;Steven&#39;);<\/font><br \/> <font color=\"#0000f0\">SELECT<\/font> <font color=\"#ff0000\">*<\/font> <font color=\"#0000f0\">FROM<\/font> customer<font color=\"#ff0000\">;<\/font><br \/> <em><font color=\"#339960\">&#8212; now add a new column with a default<\/font><\/em><br \/> <font color=\"#0000f0\">alter<\/font> <font color=\"#0000f0\">table<\/font> customer <font color=\"#0000f0\">add<\/font> <font color=\"#0000f0\">location<\/font> <font color=\"#0000f0\">varchar2<\/font><font color=\"#ff0000\">(50);<\/font><br \/> <font color=\"#0000f0\">SELECT<\/font> <font color=\"#ff0000\">*<\/font> <font color=\"#0000f0\">FROM<\/font> customer<font color=\"#ff0000\">;<\/font><br \/> <em><font color=\"#339960\">&#8212; Add\/modify columns <\/font><\/em><br \/> <font color=\"#0000f0\">alter<\/font> <font color=\"#0000f0\">table<\/font> CUSTOMER <font color=\"#0000f0\">modify<\/font> <font color=\"#0000f0\">LOCATION<\/font> <font color=\"#0000f0\">default<\/font> <font color=\"#ff0000\">&#39;Almere&#39;;<\/font><br \/> <em><font color=\"#339960\">&#8212; Add comments to the columns <\/font><\/em><br \/> <font color=\"#0000f0\">comment<\/font> <font color=\"#0000f0\">on<\/font> <font color=\"#0000f0\">column<\/font> customer.<font color=\"#0000f0\">location<\/font><br \/> <font color=\"#0000f0\">is<\/font> <font color=\"#ff0000\">&#39;Customer Location&#39;;<\/font><br \/> <em><font color=\"#339960\">&#8212; now check the values<\/font><\/em><br \/> <font color=\"#0000f0\">SELECT<\/font> <font color=\"#ff0000\">*<\/font> <font color=\"#0000f0\">FROM<\/font> customer<font color=\"#ff0000\">;<\/font><br \/> <em><font color=\"#339960\">&#8212; the column has *NOT* been filled with the default.<\/font><\/em><br \/> <em><font color=\"#339960\">&#8212; Now update Steven<\/font><\/em><br \/> <font color=\"#0000f0\">UPDATE<\/font> customer <font color=\"#0000f0\">SET<\/font> customer.<font color=\"#0000f0\">LOCATION<\/font> <font color=\"#ff0000\">=<\/font> <font color=\"#ff0000\">&#39;Chicago&#39;<\/font> <font color=\"#0000f0\">WHERE<\/font> <font color=\"#0000f0\">NAME<\/font> <font color=\"#ff0000\">=<\/font> <font color=\"#ff0000\">&#39;Steven&#39;;<\/font><br \/> <font color=\"#0000f0\">SELECT<\/font> <font color=\"#ff0000\">*<\/font> <font color=\"#0000f0\">FROM<\/font> customer<font color=\"#ff0000\">;<\/font><br \/> <em><font color=\"#339960\">&#8212; update the empty columns with the default value<\/font><\/em><br \/> <font color=\"#0000f0\">UPDATE<\/font> customer <font color=\"#0000f0\">SET<\/font> customer.<font color=\"#0000f0\">LOCATION<\/font> <font color=\"#ff0000\">=<\/font> <font color=\"#0000f0\">DEFAULT<\/font><br \/> <font color=\"#0000f0\">WHERE<\/font> customer.<font color=\"#0000f0\">LOCATION<\/font> <font color=\"#0000f0\">IS<\/font> <font color=\"#0000f0\">NULL<\/font><font color=\"#ff0000\">;<\/font><br \/> <font color=\"#0000f0\">SELECT<\/font> <font color=\"#ff0000\">*<\/font> <font color=\"#0000f0\">FROM<\/font> customer<font color=\"#ff0000\">;<\/font> <\/p>\n<p>When you run this script, you can see what the <strong>DEFAULT<\/strong> keyword does.<\/p>\n<p>I have found that the default keyword cannot be used in the nvl function. At least not on my instance ;-). I still have to try how it works in PL\/SQL code, like in triggers, but usually, stuff that doesn&#39;t work in SQL, doesn&#39;t work in PL\/SQL either.<\/p>\n<p>I can see use for this keyword in update scripts, after you alter a column to have a default, while there is already data in the table. But it might also help, to prevent NULL values in the table, even without a NOT NULL constraint.<\/p>\n<p>All in all, it seems like a nice addition to the language, which I need to get in the back of my head when doing PL\/SQL development. I know I am still much of an Oracle 7 developer, because that&#39;s where my PL\/SQL development &#39;career&#39; started.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Oracle 9i and up provide you with a new keyword: DEFAULT. This means you can get to the default values of a column when doing an insert or update. I have tried some things with this on my 10G XE database and I have come up with the following script to test it all (a [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2,5,9],"tags":[],"class_list":["post-46","post","type-post","status-publish","format-standard","hentry","category-oracle","category-plsql","category-sql"],"_links":{"self":[{"href":"https:\/\/blog.bar-solutions.com\/index.php?rest_route=\/wp\/v2\/posts\/46","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.bar-solutions.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.bar-solutions.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.bar-solutions.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.bar-solutions.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=46"}],"version-history":[{"count":0,"href":"https:\/\/blog.bar-solutions.com\/index.php?rest_route=\/wp\/v2\/posts\/46\/revisions"}],"wp:attachment":[{"href":"https:\/\/blog.bar-solutions.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=46"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.bar-solutions.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=46"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.bar-solutions.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=46"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}