SQL server 添加主外鍵約束

---添加主鍵約束            

 

alter table 表名      

add constraint 約束名 primary key (主鍵)          -

 

--添加唯一約束           

 

alter table 表名              

add constraint 約束名 unique (字段)       

 

---添加默認約束          

 

alter table 表名         

add constraint 約束名 default ('默認內容') for 字段             

 

--添加檢查check約束,要求字段只能在1到100之間             

 

alter table 表名 

add constraint 約束名 check (字段 between 1 and 100 )        

      

---添加外鍵約束(主表stuInfo和從表stuMarks建立關係,關聯字段爲stuNo)    

        

alter table 從表              

add constraint 約束名 <br>   foreign key(關聯字段) references 主表(關聯字段)      

     

sql server中刪除約束的語句是:          

 

alter table 表名 drop constraint 約束名             

sp_helpconstraint 表名 找到數據表中的所有列的約束           

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章