SQL常用语句.sql 927 B

123456789101112131415161718192021222324
  1. ---添加主键约束
  2. AFTER TABLE [表名] ADD CONSTRAINT [PK_约束名] PRIMARY KEY([字段1],[字段2],[..])
  3. --示例:
  4. Alter Table stuInfo Add Constraint PK_stuNO primary Key(stuNo)
  5. ---添加唯一约束
  6. ALTER TABLE [表名] ADD CONSTRAINT [UK_约束名] UNIQUE([字段1],[字段2],[..])
  7. --示例:
  8. Alter Table stuInfo Add Constraint UQ_stuID unique(stuID)
  9. ---添加默认值约束
  10. ALTER TABLE [表名] ADD CONSTRAINT [DF_约束名] DEFAULT(默认值) FOR [字段]
  11. --示例:
  12. Alter Table stuInfo Add Constraint DF_stuAddress default('地址不详') for stuAddress
  13. ---添加条件检查约束
  14. ALTER TABLE [表名] ADD CONSTRAINT [CK_约束名] CHECK(条件表达式)
  15. --示例:
  16. Alter Table stuInfo Add Constraint CK_stuAge check(stuAge between 15 and 40)
  17. ---添加外键约束
  18. ALTER TABLE [表名] ADD CONSTRAINT [FK_约束名] FOREIGN KEY[字段名] REFERENCES [外键所在表名].[字段名]
  19. --示例:
  20. Alter Table stuMarks Add Constraint FK_stuNo foreign key(stuNo) references stuInfo(stuNo)