数据库原理

实验内容:
在以下实验中,通过在事先创建的studb数据库,用SQL语句创建描述学生的基本信息、课程的基本信息及学生选修课程的信息的三张表,表结构如表1—表3所示。

  1. 表1 student

在这里插入图片描述

  1. 表2 cours
    在这里插入图片描述
  2. 表3 sc
    在这里插入图片描述
  3. 创建数据库和相应表
    ​​
​
use studb
create table student2
(sno char(5) primary key,
sname nvarchar(20) not null,
sex char(2) check(sex='男'or sex='女'),
sdept varchar(10),
age int default 20,
birthday smalldatetime,
livedorm bit default 1
)
create table course2
(cno char(4)primary key,
cname nvarchar(20)not null,
classhour tinyint,
credit tinyint
)
create table sc1
(sno char(5),
cno char(4),
grade decimal(3,0) check(grade>=0 and grade<=100),
constraint pk_snocno primary key (sno,cno),
constraint fk_sno foreign key (sno)references student2(sno),
constraint fk_cno foreign key (cno)references course2(cno)
)

​


2.向student表中增加“address”和“tel”。

use studb alter table student2 add address varchar(20); alter table student2 add tel varchar(12);

3.在student表中删除“address”和“tel”。

use studb alter table student2 drop column address; alter table student2 drop column tel;

4.将student表中“age”属性的数据类型由int修改为smallint。

use studb alter table student2 alter column age smallint ;

5.将student表中“age”属性的default约束删掉,然后为“age”属性设置数据范围为15至30岁。

use studb alter table student2 drop DF__student__age__117F9D94 ; alter table student2 add constraint c_age check ( age>=15 and age<=30 );

6.将sc表的“grade”属性数据类型修改为decimal并设置恰当的数据宽度,设置其域完整性为0.0至100.0。

use studb alter table sc alter column grade decimal(4,1); alter table sc add constraint c_grade check ( grade<=100.0 and grade>=0.0 );

7.Course表“hours”属性的域和现实情况不符,结合自身实际情况,请你为它设置恰当的域。

use studb alter table course2 drop constraint CK__course__hours__164452B1 alter table course2 add constraint CK_course_hours check ( hours>=0 and hours<=70 );

8.在student表中删除“sex”属性上的约束,然后删除“sex”属性。

use studb alter table student2 drop CK__student__sex__108B795B; alter table student2 drop column sex;

9.改变student表中“sdept”的宽度。如果改为宽度比原来大或者宽度比原来小,在两种情况下均使用insert语句尝试为student表添加记录并观察结果。

use studb alter table student2 alter column sdept varchar(25); insert into student2 (sdept) values('xingong'); alter table student2 alter column sdept varchar(3); insert into student2 (sdept) values('xingong');drop table student2;

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值