Insert into client values('001','sup1') Insert into client values('002','sup2') Insert into shopinfo values(1,'name1',18) Insert into shopinfo values(2,'name2',28)
4) 在供应商信息表中新增一列,供应商性别,类型为char(1),设默认值‘0’,并创
建检查约束‘0’代表女,‘1’代表男。
Alter table client add gys_sex char(1) default ?0? check (gys_sex=?0? or gys_sex=?1?) 5) 在商品信息表中,以供应商id建立非聚集索引。
Create nonclustered index ix_shopinfo on shopinfo(Cl_id) 6) 从数据库中,删除商品信息表(shopinfo)。
Drop table shopinfo
4、 现有车辆信息管理系统,有如下两个实体表结构设计如下:(中) 部门信息表(deptinfo): 字段名 中文含义 类型 Dept_id Dept_name 部门编号 部门名称 Int Varchar(200) 是否主外键 pk 是否主外键 pk Fk 是否为空 N Y 是否为空 N N Y Y 备注 备注 与部门信息表关联 车辆信息表(carinfo): 字段名 中文含义 类型 car_ID Dept_id car_num car_style Per_time 要求学生用T-SQL完成如下内容:
车辆编号 部门编号 车牌号码 车辆类型 购买日期 Int Int Varchar(12) Varchar(32) datetime 1)创建车辆信息管理系统数据库—数据库名称为学生自己姓名的拼音全称。
create database mingzi1 go
2)按上面表设计要求,分别创建部门信息表(deptinfo)、车辆信息表(carinfo); create table deptinfo (
dept_id int primary key, dept_name varchar(200) ) go
create table carinfo (car_id int primary key, dept_id int,
car_num varchar(12) not null,
car_style varchar(32), per_time datetime
constraint fk_dept foreign key(dept_id) references deptinfo(dept_id) )
3)分别部门信息表、车辆信息表中,各插入两条数据,
insert into deptinfo values(1,'生产部') insert into deptinfo values(2,'技术部')
insert into carinfo values(1,1,'渝ABD123','轿车','2013-12-1') insert into carinfo values(2,2,'渝CLV332','货车','2014-3-5') 4)删除车辆信息表中所有数据。
delete from carinfo
5)在车辆信息表中,新增一列购买日期,数据类型为DATETIME,默认时间为“-06-01”。 alter table carinfo add purdate datetime default '2015-06-01' 6)在车辆信息表中,以车牌号码列建立唯一性非聚集索引。
create unique nonclustered index id_number on carinfo(car_id)
5、 现有仓库管理系统,有如下两个实体表结构设计如下:(中)
仓库信息表(storage) 字段名 st_id st_name st_addr 字段名 gs_id st_id gs_name gs_add 要求学生用T-SQL完成如下内容:
1)创建仓库管理系统数据库—数据库名称为学生自己姓名的拼音全称。 create database mingzi2
2)按上面表设计要求,分别创建仓库信息表(storage)、货物信息表(goodsinfo); create table storage
(st_id int identity(1,1) primary key, st_name varchar(36), st_addr varchar(200) )
中文含义 仓库编号 仓库名称 仓库地址 中文含义 货物ID 仓库编号 货物名称 货物位置 类型 Int Varchar(36) Varchar(200) 类型 Int Int Varchar(36) Varchar(200) 是否主外键 pk 是否主外键 pk fk 是否为空 N Y Y 是否为空 N N Y Y 备注 标识列,初始为1,增长值为1 备注 标识列,初始为1,增长值为1 与storagege表关联 货物信息表goodsinfo: create table goodsinfo
( gs_id int identity(1,1) primary key,
st_id int foreign key references storage(st_id), gs_name varchar(36), gs_add varchar(200) )
3)分别仓库信息表、货物信息表中,各插入两条数据,
insert into storage values('宏远仓库','渝中区中山三路号') insert into storage values('志连仓库','江北区红叶路号')
insert into goodsinfo values(2,'电线','C区-24') insert into goodsinfo values(1,'管道','F区-12')
4)在货物信息表中,以货物名称建立非聚集索引
create nonclustered index index_name on goodsinfo(gs_name) 5)在仓库信息表中,新增一列联系电话
alter table storage add phone char(11) 6)从数据库中,删除货物信息表goodsinfo。
drop table goosinfo
6、现在商品销售系统,有如下三个表结构设计如下:(较难)
(1)Members(用户表) 字段名 数据类型 M_account Char(9) M_name Char(8) M_date datetime M_sex Char(2) 是否为空 Not Null Not Null Yes Yes 主键? Pk_eshop No No No 外键约束 No No No No 缺省值 ‘男’ 备注 会员号 会员名 出生年月 性别 (2)Products(产品信息表)
字段名 数据类型 是否为空 主键? 外键约束 缺省值 P_no Int Not Null Pk_ Products No P_name Varchar(50) Not Null No No (3)Orders(订单表) 字段名 数据类型 是否为空 主键 约束 P_no Int Not Null Pk_Orders Fk_P_no,参照Products中P_no M_acount Char(9) Not Null Pk_Orders FK_M_account,参照Membe中M_account O_count Int Null 要求学生用T-SQL完成如下内容:
1)创建商品销售系统—数据库名称为学生自己姓名的拼音全称。 create database mingzi3
2)按上面表设计要求,分别创建以上三张表; create table members
备注 产品号 产品名 备注 产品号 会员号 数量 (M_account char(9) constraint pk_eshop primary key, M_name char(8) not null, M_date datetime ,
M_sex char(2) default '男' )
create table products
( P_no int constraint Pk_ products primary key, P_name varchar(50) not null )
create table orders (
P_no int,
M_account char(9) not null, O_count int,
constraint Pk_Orders primary key(P_no,M_account),
constraint fk_p_no foreign key(P_no) references products(P_no), constraint fk_M_account foreign key(M_account) references members(M_account) )
3)为订单表的O_count列添加CHECK约束,要求数量在0到100之间 alter table orders
add constraint ck_oc check(O_count>0 and O_count<100)
alter table orders add constraint ck_oc check(O_count between 0 and 100))
4)在产品信息表中,新增一列产品价格p_price,类型为money。 alter table products add p_price money
5)分别在用户信息表、产品信息表和订单表三张表中,各插入两条数据,
insert into members values('014072101','张于','1979-03-12',default) insert into members values('014072102','李晓妮','1986-02-18','女')
insert into products values(1,'轩尼XO') insert into products values(2,'迪奥粉饼')
insert into orders values(1,'014072101',2) insert into orders values(2,'014072101',6)
二、数据查询统计(以上课的scmdb数据库)
单表查询、排序(易)
1. 查询Studentinfo表中每个学生的所有信息。
Select * From Studentinfo 2. 查询Courseinfo表中所有课程情况。
Select *From CourseInfo
3. 查询“系部信息表”DeptInfo中的所有数据表中的所有数据。
Select *From DeptInfo
4. 查询“学生信息表”StudentInfo中的数据,只显示学号、姓名、性别、固定电话。
SELECT Student_Code, Student_Name,Student_Sex,Student_Phone FROM StudentInfo
5. 查询Courseinfo表中所有课程的课程编号,课程名。
SELECT Course_Code,Course_Name FROM Courseinfo
6. 查询“学生信息表”StudentInfo中所有女同学的学 号、姓 名、班级ID,要求列名以中
文方式显示.
Select Student_Code as '学号','姓名' = Student_Name,Class_ID 班级ID From StudentInfo where Student_Sex='0'
7. 在“学生信息表”StudentInfo中以考生自己的学号查询自己的学号,姓名,手机号码,
结果中各列的标题分别指定为我的学号,我的姓名,我的手机号码
Select Student_Code as '我的学号','我的姓名' = Student_Name,
Student_Mobile '我的手机号码' From StudentInfo
where Student_Code='140011102' //这里与考生学号一致
8. 查询课程表Courseinfo的每个课程的学分,将查询结果中的学分加3分作为新学分,并
显示结果
Select course_credit+3 '新学分' From courseinfo
9. 查询课程表Courseinfo的每个课程的学分、课程编码和课程名称,将学分查询结果提高
10%,并显示结果。
Select course_credit*1.1 '学分' , course_code,course_name From courseinfo
10. 查询“学生信息表”StudentInfo中的学号、姓名、性别、固定电话,将学号和姓名组合
在一起显示为“学号姓名”。
SELECT Student_Code+Student_Name as ?学号姓名?,Student_Sex,Student_Phone FROM StudentInfo 11. 查询Studentinfo表学生的入学时间,不显示重复的行,并注意观察结果。
Select distinct student_indate from studentinfo
12. 查询Studentinfo表中有哪些性别。
select distinct Student_Sex from StudentInfo