{"id":656,"date":"2018-04-19T20:38:40","date_gmt":"2018-04-19T12:38:40","guid":{"rendered":"https:\/\/blog.freesilo.com\/?p=656"},"modified":"2018-04-19T20:38:40","modified_gmt":"2018-04-19T12:38:40","slug":"%e5%88%9b%e5%bb%ba%e4%b8%8e%e5%88%a0%e9%99%a4sql%e7%ba%a6%e6%9d%9f%e6%88%96%e5%ad%97%e6%ae%b5%e7%ba%a6%e6%9d%9f%e3%80%82","status":"publish","type":"post","link":"https:\/\/freesilo.com\/?p=656","title":{"rendered":"\u521b\u5efa\u4e0e\u5220\u9664SQL\u7ea6\u675f\u6216\u5b57\u6bb5\u7ea6\u675f\u3002"},"content":{"rendered":"<pre class=\"brush: sql; title: ; notranslate\" title=\"\">\r\n1)\u7981\u6b62\u6240\u6709\u8868\u7ea6\u675f\u7684SQL\r\nselect 'alter table '+name+' nocheck constraint all' \r\nfrom sysobjects where type='U'\r\n\r\n2)\u5220\u9664\u6240\u6709\u8868\u6570\u636e\u7684SQL\r\nselect 'TRUNCATE TABLE '+name from sysobjects where type='U'\r\n\r\n3)\u6062\u590d\u6240\u6709\u8868\u7ea6\u675f\u7684SQL\r\nselect 'alter table '+name+' check constraint all' \r\nfrom sysobjects where type='U'\r\n\r\n4)\u5220\u9664\u67d0\u5b57\u6bb5\u7684\u7ea6\u675f\r\ndeclare @name varchar(100)\r\n--DF\u4e3a\u7ea6\u675f\u540d\u79f0\u524d\u7f00\r\nselect @name=b.name from syscolumns a,sysobjects b \r\nwhere a.id=object_id('\u8868\u540d') and b.id=a.cdefault \r\nand a.name='\u5b57\u6bb5\u540d' and b.name like 'DF%'\r\n--\u5220\u9664\u7ea6\u675f\r\nalter table \u8868\u540d drop constraint @name\r\n--\u4e3a\u5b57\u6bb5\u6dfb\u52a0\u65b0\u9ed8\u8ba4\u503c\u548c\u7ea6\u675f\r\nALTER TABLE \u8868\u540d ADD CONSTRAINT @name  DEFAULT (0) FOR &#x5B;\u5b57\u6bb5\u540d]\r\n\r\n--\u5220\u9664\u7ea6\u675f\r\nALTER TABLE tablename\r\nDrop CONSTRAINT \u7ea6\u675f\u540d\r\n--\u4fee\u6539\u8868\u4e2d\u5df2\u7ecf\u5b58\u5728\u7684\u5217\u7684\u5c5e\u6027\uff08\u4e0d\u5305\u62ec\u7ea6\u675f\uff0c\u4f46\u53ef\u4ee5\u4e3a\u4e3b\u952e\u6216\u9012\u589e\u6216\u552f\u4e00\uff09\r\nALTER TABLE tablename \r\nalter column \u5217\u540d int not null\r\n--\u6dfb\u52a0\u5217\u7684\u7ea6\u675f\r\nALTER TABLE tablename\r\nADD CONSTRAINT DF_tablename_\u5217\u540d DEFAULT(0) FOR \u5217\u540d\r\n--\u6dfb\u52a0\u8303\u56f4\u7ea6\u675f\r\nalter table  tablename  add  check(\u6027\u522b in ('M','F'))\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>1)\u7981\u6b62\u6240\u6709\u8868\u7ea6\u675f\u7684SQL select &#8216;alter table &#8216;+name&#8230;<br \/><a class=\"read-more-button\" href=\"https:\/\/freesilo.com\/?p=656\">Read more<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[7],"tags":[],"class_list":["post-656","post","type-post","status-publish","format-standard","hentry","category-sql"],"_links":{"self":[{"href":"https:\/\/freesilo.com\/index.php?rest_route=\/wp\/v2\/posts\/656","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/freesilo.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/freesilo.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/freesilo.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/freesilo.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=656"}],"version-history":[{"count":1,"href":"https:\/\/freesilo.com\/index.php?rest_route=\/wp\/v2\/posts\/656\/revisions"}],"predecessor-version":[{"id":657,"href":"https:\/\/freesilo.com\/index.php?rest_route=\/wp\/v2\/posts\/656\/revisions\/657"}],"wp:attachment":[{"href":"https:\/\/freesilo.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=656"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/freesilo.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=656"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/freesilo.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=656"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}