当前位置:Gxlcms > 数据库问题 > SQL Server 【附】创建"商品管理数据库"、"学生选课数据库"的SQL语句

SQL Server 【附】创建"商品管理数据库"、"学生选课数据库"的SQL语句

时间:2021-07-01 10:21:17 帮助过:8人阅读

建立"商品管理数据库"数据库-- create database 商品管理数据库 on(name=商品管理数据库_m, filename=D:\商品管理系统\商品管理数据库_m.mdf, size=6mb,filegrowth=1mb,maxsize=unlimited) log on(name=商品管理数据库_l, filename=D:\商品管理系统\商品管理数据库_l.ldf, size=1,filegrowth=10%,maxsize=3) go use 商品管理数据库 --建立"客户信息表"数据表-- create table 客户信息表 (客户编号 nchar(8)not null primary key, 客户姓名 nvarchar(5)not null, 联系电话 nvarchar(11)not null, 地址 nvarchar(30)not null, 邮箱 nvarchar(20)null) --为"客户信息表"的"联系电话"设置唯一约束-- alter table 客户信息表 add constraint UN_客户信息表_联系电话 unique (联系电话) --为"客户信息表"的"地址"设置默认约束-- alter table 客户信息表 add constraint DF_客户信息表_地址 default 辽宁沈阳for 地址 --为"客户信息表"的"邮箱"设置检查约束-- alter table 客户信息表 add constraint CK_客户信息表_邮箱 check (邮箱 like _%@_%._%) --建立"商品类型表"数据表-- create table 商品类型表 (商品类型编号 nchar(6)not null primary key, 商品类型名 nvarchar(10)not null) --建立"商品信息表"数据表-- create table 商品信息表 (商品编号 nchar(8)not null primary key, 商品类型编号 nchar(6)not null, 商品名称 nvarchar(20)not null, 商品单位 nchar(2)not null, 产地 nvarchar(30)not null) --为"商品信息表"的"商品类型编号"设置外键约束-- alter table 商品信息表 add constraint FK_商品类型表_商品信息表_商品类型编号 foreign key (商品类型编号) references 商品类型表(商品类型编号) --为"商品信息表"的"商品单位"设置默认约束-- alter table 商品信息表 add constraint DF_商品信息表_商品单位 default for 商品单位 --为"商品信息表"的"产地"设置默认约束-- alter table 商品信息表 add constraint DF_商品信息表_产地 default 辽宁沈阳for 产地 --建立"进货信息表"数据表-- create table 进货信息表 (进货编号 int not null primary key, 商品编号 nchar(8)not null, 进货单价 decimal(6,2)not null, 进货数量 int not null, 进货金额 decimal, 进货日期 date not null) --为"进货信息表"的"商品编号"设置外键约束-- alter table 进货信息表 add constraint FK_商品信息表_进货信息表_商品编号 foreign key (商品编号) references 商品信息表(商品编号) --为"进货信息表"的"进货单价"设置检查约束-- alter table 进货信息表 add constraint CK_进货信息表_进货单价 check (进货单价>=0) --为"进货信息表"的"进货数量"设置检查约束-- alter table 进货信息表 add constraint CK_进货信息表_进货数量 check (进货数量>0) --为"进货信息表"的"进货金额"设置??约束-- --为"进货信息表"的"进货日期"设置默认约束-- alter table 进货信息表 add constraint DF_进货信息表_进货日期 default getdate()for 进货日期 --建立"销售信息表"数据表-- create table 销售信息表 (销售编号 int not null primary key, 商品编号 nchar(8)not null, 销售单价 decimal(6,2)not null, 销售数量 int not null, 销售金额 decimal, 销售日期 date not null, 客户编号 nchar(8)not null) --为"销售信息表"的"商品编号"设置外键约束-- alter table 销售信息表 add constraint FK_商品信息表_销售信息表_商品编号 foreign key (商品编号) references 商品信息表(商品编号) --为"销售信息表"的"销售单价"设置检查约束-- alter table 销售信息表 add constraint CK_销售信息表_销售单价 check (销售单价>=0) --为"销售信息表"的"销售数量"设置检查约束-- alter table 销售信息表 add constraint CK_销售信息表_销售数量 check (销售数量>0) --为"销售信息表"的"销售金额"设置??约束-- --为"销售信息表"的"销售日期"设置默认约束-- alter table 销售信息表 add constraint DF_销售信息表_销售日期 default getdate()for 销售日期 --为"销售信息表"的"客户编号"设置外键约束-- alter table 销售信息表 add constraint FK_客户信息表_销售信息表_客户编号 foreign key (客户编号) references 客户信息表(客户编号) --建立"库存信息表"数据表-- create table 库存信息表 (库存编号 int not null, 商品编号 nchar(8)not null, 库存数量 int not null) --为"库存信息表"的"商品编号"设置外键约束-- alter table 库存信息表 add constraint FK_商品信息表_库存信息表_商品编号 foreign key (商品编号) references 商品信息表(商品编号) --为"库存信息表"的"库存数量"设置检查约束-- alter table 库存信息表 add constraint CK_库存信息表_库存数量 check (库存数量>=0) --添加表数据-- --添加"客户信息表"数据-- insert 客户信息表 values(20130001,张峰,13600001111,辽宁沈阳,zhf@163.com) insert 客户信息表 values(20130002,赵小天,13700002222,辽宁大连,zxt@163.com) insert 客户信息表 values(20130003,钱成,13800003333,辽宁锦州,qc@163.com) insert 客户信息表 values(20130004,孙飞,13900004444,辽宁沈阳,sf@163.com) insert 客户信息表 values(20130005,李小明,13100005555,辽宁盘锦,lxm@163.com) insert 客户信息表 values(20130006,周笑,13300006666,辽宁大连,zx@163.com) --把查询到"客户信息表"的表数据生成(添加到)一张新数据表"客户信息表01"-- select * into 客户信息表01 from 客户信息表 --把查询到"客户信息表"的表数据添加到一张已存在的数据表"客户信息表01"中-- insert into 客户信息表01 select*from 客户信息表 --添加"商品类型表"数据-- insert 商品类型表 values(RZL001,日杂类) insert 商品类型表 values(SPL001,饮料类) insert 商品类型表 values(SPL002,食品类) insert 商品类型表 values(WJL001,文具类) --添加"商品信息表"数据-- insert 商品信息表 values(11110001,SPL001,可口可乐,,辽宁沈阳) insert 商品信息表 values(11110002,SPL001,矿泉水,,辽宁沈阳) insert 商品信息表 values(11110003,SPL001,雪花啤酒,,辽宁沈阳) insert 商品信息表 values(11110004,SPL001,青岛啤酒,,辽宁沈阳) insert 商品信息表 values(22220001,SPL002,德芙巧克力(牛奶),,辽宁沈阳) insert 商品信息表 values(22220002,SPL002,士力架,,辽宁沈阳) insert 商品信息表 values(22220003,SPL002,彩虹糖,,辽宁沈阳) insert 商品信息表 values(33330001,WJL001,中华铅笔,,辽宁沈阳) insert 商品信息表 values(33330002,WJL001,大演算,,辽宁沈阳) insert 商品信息表 values(33330003,WJL001,便签本,,辽宁沈阳) insert 商品信息表 values(33330004,WJL001,曲别针,,辽宁沈阳) insert 商品信息表 values(33330005,WJL001,真彩签字笔(黑),,辽宁沈阳) insert 商品信息表 values(33330006,WJL001,真彩签字笔(红),,辽宁沈阳) insert 商品信息表 values(33330007,WJL001,鸵鸟钢笔水(深蓝),,辽宁沈阳) insert 商品信息表 values(33330008,WJL001,鸵鸟钢笔水(纯蓝),,辽宁沈阳) insert 商品信息表 values(44440001,RZL001,雕牌透明皂,,辽宁沈阳) insert 商品信息表 values(44440002,RZL001,中华牙膏,,辽宁沈阳) insert 商品信息表 values(44440003,RZL001,心相印纸巾(无味*10),,辽宁沈阳) insert 商品信息表 values(44440004,RZL001,飘柔洗发(240ml),,辽宁沈阳) insert 商品信息表 values(44440005,RZL001,枪手杀虫剂(200ml),,辽宁沈阳) insert 商品信息表 values(44440006,RZL001,金鸡鞋油,,辽宁沈阳) --添加"进货信息表"数据-- insert 进货信息表 values(1,11110001,2.00,100,200.00,2012-11-01) insert 进货信息表 values(2,11110002,1.00,200,200.00,2012-11-01) insert 进货信息表 values(3,11110003,2.00,200,400.00,2012-11-01) insert 进货信息表 values(4,22220001,6.00,10,60.00,2012-11-01) insert 进货信息表 values(5,22220002,2.00,300,600.00,2012-11-01) insert 进货信息表 values(6,22220003,2.00,150,30.00,2012-11-01) insert 进货信息表 values(7,33330002,0.40,150,60.00,2012-11-01) insert 进货信息表 values(8,22220001,6.00,10,60.00,2012-11-02) insert 进货信息表 values(9,22220002,2.00,300,600.00,2012-11-02) insert 进货信息表 values(10,22220003,2.00,150,300.00,2012-11-02) insert 进货信息表 values(11,33330002,0.40,150,60.00,2012-11-02) insert 进货信息表 values(12,22220001,6.00,100,600.00,2012-11-02) insert 进货信息表 values(13,33330002,0.80,150,120.00,2012-11-02) insert 进货信息表 values(14,33330001,0.90,200,180.00,2012-11-02) insert 进货信息表 values(15,33330003,0.60,350,210.00,2012-11-02) insert 进货信息表 values(16,33330004,2.00,150,300.00,2012-11-02) insert 进货信息表 values(17,33330005,1.00,200,200.00,2012-11-02) insert 进货信息表 values(18,33330006,1.00,110,110.00,2012-11-03) insert 进货信息表 values(19,33330007,3.00,100,300.00,2012-11-03) insert 进货信息表 values(20,33330007,3.00,150,450.00,2012-11-03) insert 进货信息表 values(21,44440001,2.00,200,400.00,2012-11-03) insert 进货信息表 values(22,44440002,3.00,100,300.00,2012-11-03) insert 进货信息表 values(23,44440003,5.00,150,750.00,2012-11-03) insert 进货信息表 values(24,44440004,23.00,100,2300.00,2012-11-04) insert 进货信息表 values(25,44440005,16.00,100,1600.00,2012-11-04) insert 进货信息表 values(26,44440006,2.00,200,400.00,2012-11-04) insert 进货信息表 values(27,33330004,2.00,150,300.00,2012-11-04) insert 进货信息表 values(28,22220001,6.00,100,600.00,2012-11-04) insert 进货信息表 values(29,22220002,2.00,300,600.00,2012-11-04) insert 进货信息表 values(30,22220003,2.00,150,300.00,2012-11-04) insert 进货信息表 values(31,22220002,0.70,150,05.00,2012-11-04) --添加"销售信息表"数据-- insert 销售信息表 values(1,11110001,3.00,50,1

人气教程排行