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