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_ProductInf

本文详细介绍了T-SQL中的DDL(数据定义语言)和DML(数据操纵语言)操作,包括创建、删除表,添加约束,以及插入、更新、删除数据的方法。同时,还讲解了数据查询的基本语法,如选择特定列、排序、模糊查询和范围查询。此外,还涵盖了分组查询、聚合函数、子查询、连接查询(内连接、外连接、全连接和交叉连接)的应用,是学习T-SQL的实用教程。
最低0.47元/天 解锁文章
1907





