T-SQL
DDL:
–进入某个数据库
use TestBase
go
–创建表
create table ProductInfos
(
Id int identity(1001,1) primary key not null,
ProNo varchar(50) unique not null,
ProName nvarchar(20) not null,
TypleId int not null,foreign key references ProductType(TypeId),
Price decimal(18,2) check (Price<10000) default(0.00) not null,
ProCount int default (0) null,
)
go
–删除表
drop table ProductInfos
go
–在建表完成后,创建约束
Create table ProductInfos
(
Id int identity(1001,1) not null, --标识种子,
–主键 Id
alter table ProductInfos add constraint
PK_ProductInfos primary key(Id)
–外键 TypeId
alter table ProductInfos add constraint
FK_ProductInfos foreign key (TypeId) references
ProductType(TypeId)
–unique约束 ProNo
Alter table ProductInfos add constraint
IX_ProductInfos_ProNo unique(ProNo)
–unique ProNo+ProName
Alter table ProductInfos add constraint
IX_ProductInfos_ProNo unique (ProNo,ProName)
–check 约束
alter table ProductInfos add constraint
CK_ProductInfos_Price check(Price<10000)
–default 约束
alter table ProductInfos add constraint DF_ProCount
default (0) for ProCount<