use master
go
使用默认数据库
if exists(select * from sys.databases where name="A")
drop database A
go
检查是否存在数据库A,如果存在则删除
create database A
go
use A
go
创建数据库A,使用数据库A
create table ProductCategory--产品类别表
(
id int identity(1,1) primary key,--主键Id,自增
Name nvarchar(16) not null--类别名称,非空
)
go
创建表ProductCategory
create table Product--产品表
(
id int identity(1,1) primary key,--主键ID,自增
ProductName nvarchar(32) not null,--产品名称,非空
MarketPrice decimal(16,2) not null,--市场价,非空
SellingPrice decimal(16,2) not null,--售价,非空
CategoryId int references ProductCategory(id) not null,--类别ID,非空,外键
Introduction nvarchar(128) not null,--产品介绍,非空
IsOnSale bit not null,--是否上架 1上架,0下架
Addtime datetime default(getdate()) not null--添加时间,默认当前时间
)
go
创建表Product
insert into ProductCategory values("手机");
insert into ProductCategory values("笔记本");
insert into ProductCategory values("平板电脑");
insert into ProductCategory values("台式机");
给表ProductCategory插入数据
insert into Product values("iPhone XR","6099","7200",1,"",1,default);
insert into Product values("拯救者","8099","8500",2,"",1,default);
insert into Product values("华为平板M5","2099","3100",3,"",1,default);
insert into Product values("天翼510Pro","5200","4988",4,"",0,default);
给表Product插入数据
select * from ProductCategory
select * from Product
查询表