首页 > 技术文章 > 数据库创建&数据表创建

kuugachen 2013-10-29 16:56 原文

--第2_1题创建数据库
create database Student201038897041
on primary 
(name='student1',
filename='F:\coures\SQL Server数据库应用\student\data\student1.mdf',
size=10MB,
maxsize=100MB,
filegrowth=10%),
filegroup[group1]
(name=N'student2',
filename=N'F:\coures\SQL Server数据库应用\student\mydb\Student2.ndf',
size=20MB,
maxsize=100MB,
filegrowth=1MB
)
LOG ON 
(name=N'Studentlog1',
filename='F:\coures\SQL Server数据库应用\student\log\Studentlog1.ldf',
size=10MB,
maxsize=50MB,
filegrowth=1MB
)
alter database student201038897041
add log file(name=N'studentlog2',
filename='F:\coures\SQL Server数据库应用\student\log\Studentlog2.ldf',
size=10MB,
filegrowth=1MB,
maxsize=50MB
)

--第2_2题修改Student数据库,
--增加一个数据文件Student3,把它存放到file1
--文件组中,初始大小5MB,最大尺寸20MB,
--增长速度15%。

alter database student201038897041
add file 
(name='student3',
filename='F:\coures\SQL Server数据库应用\student\mydb\student3.ndf',
size=5MB,
maxsize=20MB,
filegrowth=15%
)to filegroup group1

--删除数据文件Student3.ndf.




--第2_3题在Student数据库中,创建“学生表”,
--包括如下字段:学号(char(6))、
--姓名(Char(8))、年龄(int not null和
--性别(Char(2))).主键为学号,
--年龄在16到30岁之间,性别默认值为“女”.
create table studentTable 
(studentNum char(6)not null primary key,
name char(8) ,age int not null,check(age>=16 and age<=30),
sex char(2) check(sex=''or sex='') default(''),
)

--第2_4题修改数据表“学生表”,  在学生表中增加宇
--段:家庭地址(varchar(30))和学生所在系(Char(20)).
alter table studentTable
add  studentAddress varchar(30),
studentCollege char(20)

----第2_5题修改数据表“学生表’,设置年龄的默认值为20
alter table studentTable
add constraint age_s default (20)for age

--第2_6题向“学生表”插入4条记录
insert into studentTable(studentNum,name,age,sex,studentAddress
,studentCollege)
values('021101','王英',20,'','绍兴路','交通工程系')
insert into studentTable(studentNum,name,age,sex,
studentAddress,studentCollege)
values('022111','吴波',18,'','延安路','汽车系')

insert into studentTable(studentNum,name,age,sex,
studentAddress,studentCollege)
values('034320','李霞',19,'','南山路','管理信息系')
insert into studentTable(studentNum,name,age,sex,
studentAddress,studentCollege)
values('031202','张兵',20,'','北山路','汽车系')

--第2_7题修改表中的数据。
--(1)在学生信息表,学生王英从交通工程系转到管理信息系,请修改此记录。

update studentTable set studentCollege='管理信息系' 
where studentNum='021101'

----(2)吴波同学的家搬到了解放路。
update studentTable set studentAddress='解放路' 
where studentNum='022111';

----(3)在学生信息表中,管理信息系的学生都毕业了,把他们的记录都删除。

delete from studentTable where
 studentCollege='管理信息系';

--第2_8.关于学生表的“姓名”字段建立唯一非聚集索引IX_XM,按姓名降序排列。
create unique  index  IX_XM on 
studentTable(name desc)

--第2_9为学生表创建一个基于年龄和
--学号的索引IX_年龄,其中年龄按降序排列,
--当年龄相同时,按学号升序排列.
create index Ix_age on studentTable
(age desc,studentNum asc)

--第2_10关于家庭地址建立非簇索引,要求填充因子为80,
--索引名为address.使用SQL命令查看索引address的空间使用情况.
create nonclustered index [address]on studentTable
(studentAddress)with fillfactor=80

--第2_11修改索引address,要求填充因子为90.
--删除索引address和“IX_年龄”。
drop index [address]on studentTable
go
drop index [IX_age]on studentTable
go
sp_helpindex studentTable

go
--第2_13将Student数据库中的“学生表”删除
drop table studentTable
go
drop database [student2010388979041]
go
--2_15设计规划创建数据库studentcourse.
--向studentcourse添加三张数据表学生基本信息表S、
--课程数据表C和学生选课数据表sc,
--数据结构如表3.6~表3.8所示:完整性约束如表3.9~表3.11所示:记录信息如表3.3~表3.5所示
create database studentcourse

go
create table s (
学号 char(6)not null primary key default('J0400'),
姓名 char(8)not null,
性别 char(2)not null,
出生日期 datetime not null default('1980-01-01'),
系 varchar(20)not null,
电话 char(8),
CONSTRAINT CK_Num CHECK (学号 Like'[A-Z][0-9][0-9][0-9][0-9]'),
CONSTRAINT CK_Sex CHECK (性别 ='' OR 性别 =''),
CONSTRAINT CK_tel CHECK (电话 Like'[0-9][0-9][0-9]-[0-9][0-9][0-9][0-9]')
)

go

create table c
(课程号 char(3)not null primary key,
课程名 varchar(20) not null,
学分 smallint ,预选课程号 char(3),
教师 char(8),
CONSTRAINT CK_Cno CHECK (课程号 Like 'C[0-9][0-9]'),
)

 go
 CREATE TABLE     SC (
学号   Char (6) NOT NULL,
课程号   Char (3) NOT NULL,
成绩   Smallint  NULL,
FOREIGN KEY(课程号) REFERENCES  C (课程号),
FOREIGN KEY( 学号 ) REFERENCES  S (学号),
CHECK (成绩>=(0) AND 成绩<=(100) OR 成绩 IS NULL),
PRIMARY KEY CLUSTERED (学号  ASC,课程号 ASC)) 


 --查询李丽同学选修课程信息
 select s.姓名,s.学号,SC.课程号,c.课程名,c.学分,sc.成绩 from s,sc,c 
 where s.学号=SC.学号 and SC.课程号=C.课程号 and s.姓名='李丽'
 
 --查询陈弄清教师所讲授的课程信息
 select s.学号,s.姓名,c.课程号,c.课程名,c.教师,SC.成绩 from SC,s,c
 where s.学号=SC.学号 and SC.课程号=c.课程号 and c.教师='陈弄清'
 

 

推荐阅读