数据库练习题2---创建与管理架构和基本表及数据操纵

请使用T-SQL完成以下内容:
1、 请在指定数据库(首先在电脑最后一个磁盘上建立文件夹,并建立数据文件保存在该文件夹里)内完成以下内容:
1) 依据数据表的结构创建相对应的数据表,表结构如下所示;
学生信息表(student)
                       字段名称 字段类型及长度 说明 备注
                       Sno Char(9) 学生学号 主关键字
                       Sname Char(6) 学生姓名 非空
                       Ssex Char(2) 学生性别 可为空
                       Sage Int 学生年龄 可为空
                       Sdept VarChar(8) 学生所在院系 可为空
课程信息表(course)
                       字段名称 字段类型及长度 说明 备注
                       Cno Char(4) 课程编号 主关键字
                       Cname VarChar(20) 课程名称 非空
                       Cpno Char(4) 先行课 可为空
                       Ccredit Int 学分 可为空
选课信息表(sc)
                       字段名称 字段类型及长度 说明 备注
                       Sno Char(9) 学生学号 主关键字
                       Cno Char(4) 课程编号 主关键字
                       Grade Int 成绩 可为空

create table Student (
	Sno   char(9)  primary key,
	Sname char(6)  not null,
	Ssex  char(2)  ,
	Sage  Int      ,
	Sdept VarChar(8))
go	
create table course (
	Cno   char(4)  primary key,
	Cname VarChar(20)  not null,
	Cpno  char(4)  ,
	Ccredit  Int    )
go
create table sc (
	Sno   char(9)  not null,
	Cno   char(4)  not null,
	Grade  Int     
	primary key (Sno,Cno)
	foreign key (Sno) references Student (Sno),
	foreign key (Cno) references course (Cno)	)
go	
 

2) 在表student中增加新字段 “班级名称(sclass)“;

Alter table student add sclass VarChar(20)
 

3) 在表student中删除字段“班级名称(sclass)”;

Alter table student drop column sclass
 

4) 修改表student中字段名为“sname”的字段长度由原来的6改为8;

Alter table student alter column Sname char(8)

5) 修改表student中字段“sdept”名称为“dept”,长度为20;

Alter table student drop column Sdept
Alter table student add dept VarChar(20)
 

6) 修改表student中sage字段名称为sbirth,类型为smalldatetime;

Alter table student drop column Sage
Alter table student add sbirth smalldatetime

7) 修改表student新名称为stu_info;

exec sp_rename'student','stu_info'

8) 删除数据表student。

drop table student
 

2、创建教师授课管理数据库JSSK,并完成以下内容;

	

1) 在数据库JSSK中创建下列三张表;
表名:teachers
列名 数据类型 说明 描述
Tno 字符型,长度7 主键 教师号
Tname 字符型,长度10 非空 姓名
Tsex 字符型,长度2 默认取值为“男” 性别
Birthday 小日期时间型 允许空 出生日期
Dept 字符型,长度20 允许空 所在部门
Sid 字符型,长度18 身份证号
表名: lessons
列名 数据类型 说明 描述
Cno 字符型,长度10 主键 课程号
Cname 字符型,长度20 非空 课程名
Credit 短整型 学分
property 字符型,长度为10 课程性质
表名: shouke
列名 数据类型 说明 描述
Tno 字符型,长度7 主键 教师号
Cno 字符型,长度10 主键 课程名
Hours 整数 课时


create database jssk
create login User3 with password='123456',
default_database=jssk
go
use jssk
go
create user user3
go
create schema mzx authorization user3
go

create table teachers (
	Tno   char(7)  primary key,
	Tname char(10)  not null,
	Tsex  char(2)	default'男'	,
	Birthday  smalldatetime     ,
	Dept  char(20)				,
	Sid   char(18))
go	
create table  lessons (
	Cno   char(10)  primary key,
	Cname char(20)  not null,
	Credit	smallint	,
	property char(10))
go	
create table  shouke (
	Tno   char(7)  not null,
	Cno   char(10) not null,
	Hours int
	primary key (Tno,Cno)
	foreign key (Tno) references teachers (Tno),
	foreign key (Cno) references lessons (Cno)	)
go	
 

2) 在shouke表里添加一个授课类别字段,列名为Type,类型为Char,长度为4;

Alter table shouke add Type char(4)

3) 将shouke表的Hours的类型改为smallint;

Alter table shouke alter column Hours smallint

4) 删除lessons表中的property列。

Alter table lessons drop column property

3、创建产品销售数据库CPXS,数据文件的逻辑文件名为cpxs_data,物理文件名为D:\sql\cpxs.mdf;文件初始大小为2MB,自动增长,每次增长1MB;日志文件逻辑文件名为cpxs_log,物理文件为D:\sql\cpxs.ldf;文件初始大小2MB,自动增长,每次增长15%;
1) 在数据库CPXS中创建下列三张表;
表名:产品表(cp)表结构
列名 数据类型 说明 描述
Cpbh 字符型,长度6 主键 产品编号
Cpmc 字符型,长度30 非空 产品名称
Jg 浮点型,长度8 允许空 价格
Kcl 整型,长度4 允许空 库存量

表名:销售商(xss)表结构
列名 数据类型 说明 描述
Xsbh 字符型,长度6 主键 销售商编号
Xsmc 字符型,长度30 非空 销售商名称
Dq 字符型,长度10 允许空 地区
Fzr 字符型,长度8 允许空 负责人
Dh 字符型,长度12 允许空 电话
Bz 文本,长度16 允许空 备注

表名: 产品销售(xss)表结构
列名 数据类型 说明 描述
Cpbh 字符型,长度6 主键 产品编号
Xsbh 字符型,长度6 主键 销售商编号
Xssj Datetime,长度8 非空 销售时间
Sl 整型,长度4 非空 数量
Je 浮点型,长度8 非空 金额

create database cpxs
on primary
(name=cpxs_data,
filename='D:\sql\cpxs.mdf',
size=3,
filegrowth=1)
log on
(name=cpxs_log,
filename='D:\sql\cpxs.ldf',
size=2,
filegrowth=15%)

create login User4 with password='123456',
default_database=cpxs
go
use cpxs
go
create user user4
go
create schema FPX authorization user4
go

create table cp (
	Cpbh   char(6)  primary key,
	Cpmc   char(30)  not null,
	Jg     float(8)		,
	Kcl    int      check( Kcl>=0 AND Kcl<=9999)  
	)
go	
create table xss (
	Xsbh  char(6)  primary key,
	Xsmc  char(30)  not null,
	Dq    char(10)	,
	Fzr   char(8)   ,
	Dh    char(12)	,
	Bz    char(16))
go	
create table Cxss (
	Cpbh   char(6)  not null,
	Xsbh   char(6)  not null,
	Xssj   Datetime ,	
	Sl     int  check(Sl>=0 AND Sl<=9999)   ,
	Je     float(8)
	primary key (Cpbh,Xsbh)
	foreign key (Cpbh) references cp (Cpbh),
	foreign key (Xsbh) references xss (Xsbh)	)
go	

猜你喜欢

转载自blog.csdn.net/ssdssa/article/details/108954689