[{"data":1,"prerenderedAt":151},["ShallowReactive",2],{"post:alter-existing-constraint":3},{"id":4,"title":5,"body":6,"date":138,"description":139,"draft":140,"extension":141,"meta":142,"navigation":143,"path":144,"seo":145,"stem":146,"tags":147,"__hash__":150},"posts\u002Fposts\u002Falter-existing-constraint.md","TIL: Alter Existing Constraint in Transact-SQL",{"type":7,"value":8,"toc":136},"minimark",[9,21,27,47,56,94,103,132],[10,11,12,13,20],"p",{},"First, see this quote from ",[14,15,19],"a",{"href":16,"rel":17},"https:\u002F\u002Fdocs.microsoft.com\u002Fen-us\u002Fsql\u002Frelational-databases\u002Ftables\u002Fmodify-unique-constraints?view=sql-server-2017",[18],"nofollow","Modify Unique Constraints",":",[22,23,24],"blockquote",{},[10,25,26],{},"To modify a UNIQUE constraint using Transact-SQL, you must first delete the existing UNIQUE constraint and then re-create it with the new definition.",[10,28,29,30,34,35,40,41,46],{},"This constraint (totally pun intended) extends to all sorts of constraints ",[31,32,33],"em",{},"and"," all sorts of relational databases, by the way - NOT just unique constraints as the quote suggests, and not just SQL Server. You ",[14,36,39],{"href":37,"rel":38},"https:\u002F\u002Fstackoverflow.com\u002Fa\u002F13245101\u002F1525594",[18],"just can't"," ",[14,42,45],{"href":43,"rel":44},"https:\u002F\u002Fstackoverflow.com\u002Fa\u002F861173\u002F1525594",[18],"modify existing constraints in place",".",[10,48,49,50,55],{},"So, you can ",[14,51,54],{"href":52,"rel":53},"https:\u002F\u002Fdocs.microsoft.com\u002Fen-us\u002Fsql\u002Frelational-databases\u002Ftables\u002Fdelete-unique-constraints?view=sql-server-2017",[18],"drop the constraint"," using this:",[57,58,63],"pre",{"className":59,"code":60,"language":61,"meta":62,"style":62},"language-sql shiki shiki-themes github-light github-dark","ALTER TABLE myTable\nDROP CONSTRAINT myConstraint;\n","sql","",[64,65,66,82],"code",{"__ignoreMap":62},[67,68,71,75,78],"span",{"class":69,"line":70},"line",1,[67,72,74],{"class":73},"szBVR","ALTER",[67,76,77],{"class":73}," TABLE",[67,79,81],{"class":80},"sVt8B"," myTable\n",[67,83,85,88,91],{"class":69,"line":84},2,[67,86,87],{"class":73},"DROP",[67,89,90],{"class":73}," CONSTRAINT",[67,92,93],{"class":80}," myConstraint;\n",[10,95,96,97,102],{},"Once the constraint has been dropped, ",[14,98,101],{"href":99,"rel":100},"https:\u002F\u002Fdocs.microsoft.com\u002Fen-us\u002Fsql\u002Frelational-databases\u002Ftables\u002Fcreate-unique-constraints?view=sql-server-2017",[18],"add the unique constraint"," with the additional column or whatever you wanted to do:",[57,104,106],{"className":59,"code":105,"language":61,"meta":62,"style":62},"ALTER TABLE myTable\nADD CONSTRAINT myConstraint UNIQUE (column1, column2);\n",[64,107,108,116],{"__ignoreMap":62},[67,109,110,112,114],{"class":69,"line":70},[67,111,74],{"class":73},[67,113,77],{"class":73},[67,115,81],{"class":80},[67,117,118,121,123,126,129],{"class":69,"line":84},[67,119,120],{"class":73},"ADD",[67,122,90],{"class":73},[67,124,125],{"class":80}," myConstraint ",[67,127,128],{"class":73},"UNIQUE",[67,130,131],{"class":80}," (column1, column2);\n",[133,134,135],"style",{},"html pre.shiki code .szBVR, html code.shiki .szBVR{--shiki-default:#D73A49;--shiki-dark:#F97583}html pre.shiki code .sVt8B, html code.shiki .sVt8B{--shiki-default:#24292E;--shiki-dark:#E1E4E8}html .default .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .dark .shiki span {color: var(--shiki-dark);background: var(--shiki-dark-bg);font-style: var(--shiki-dark-font-style);font-weight: var(--shiki-dark-font-weight);text-decoration: var(--shiki-dark-text-decoration);}html.dark .shiki span {color: var(--shiki-dark);background: var(--shiki-dark-bg);font-style: var(--shiki-dark-font-style);font-weight: var(--shiki-dark-font-weight);text-decoration: var(--shiki-dark-text-decoration);}",{"title":62,"searchDepth":84,"depth":84,"links":137},[],"2018-12-20T12:43:25-05:00","First, see this quote from Modify Unique Constraints:",false,"md",{},true,"\u002Fposts\u002Falter-existing-constraint",{"title":5,"description":139},"posts\u002Falter-existing-constraint",[148,149],"sql-server","til","gD4ovj0rsh-fpDb4XyPbeDtv6rSd6MV_g2b27HC7t5Q",1790471528057]