数据库实验一

最近在数据库系统概论,做实验时遇到很多问题,最主要的是命令记不知道,现在做个简单的总结

在安装环境时也很头疼,不知道用什么版本的数据库,在网上下载的版本也不全,安装几次都有问题,在网上查也没有查出问题,最后没办法,搜了一个完整的版本进行安装,也不知道哪些时必须的,哪些没有必要安装。

创建表的命令

create table tableName(

    属性1 类型

    属性2 类型

    ...

设置一个属性为键值

alter table tableName add primary key(col);

删除一个属性的主键

alter table tableName drop primary key(col);

 为表创建按columnName降序排列的索引。

Create index indxName on tableName(columnName desc);

为表创建按columnName升序的索引

create index indxName on tableName(columnName)

建表的按columnName升序排列的唯一性索引

create uniquel index indxName on tableName(columnName);

删除索引

drop index indxName;

添加一列

alter table tableName add column columnName type;

删除某一属性

alter table tableName drop columnName;

删除表

drop table tableName;



1 实验内容:

(1) 使用SSMS(SQL Server Management Studio)加入实验数据库。

(2) 使用SSMS可视化建立、修改和删除数据库、表。

(3) 使用SSMS对数据库进行备份和恢复。

(4) 使用SSMS对表进行查询、插入、修改、删除。

2 实验步骤:

(1) 加入School数据库。

(2) 建立Test数据库。

(3) 在数据库中建立人员表PERSON(P#,Pname,Page)。更改表设置P#为主键,增加属性Ptype(类型是CHAR,长度是10)

(4) SSMS的查询功能(新建查询)PERSON表进行查询、插入、修改、删除等操作:首先插入两条记录;修改第二条记录;删除第二条记录。

(5) 备份Test数据库。

(6) 删除表PERSON

(7) 恢复Test数据库。

(8) 删除Test数据库。

--PERSON--

create table PERSON(

P# char(10) primary key,

Pname char(10),

Page int

)

--添加Ptype属性--

alter table PERSON add Ptype char(10);

--插入两条记录--

insert into PERSON values('2365','Tom',22,'male');

insert into PERSON values('2356','Lily',20,'female');

--修改第二条记录--

update PERSON set Pname = 'Kaina', Page =21 where P# = '2356';

--删除第二条记录--

delete from PERSON where P# = '2356';

--删除表--

drop table PERSON

--显示结果--

select * from PERSON

实验1.2 数据定义

实验内容:

(1) 使用CREATE语句创建基本表。

(2) 更改基本表的定义,增加列,删除列,修改列的数据类型。

(3) 创建表的升降序索引。

(4) 取消表、表的索引或表的约束。

实验步骤:

(1) 使用SQL语句创建关系数据库表:人员表PERSON(P#,Pname,Page)、房间表ROOM(R#  Rname,Rarea)、表PR(P#,R#,Date)。其中:P#是表PERSON的主键,具有唯一性的约束;Page具有约束“大于18;R#是表ROOM的主键,具有唯一性约束。表PR中的P# R#是外键。

(2) 更改表PERSON,增加属性Ptype(类型是CHAR,长度是10),取消Page大于18的约束。把表ROOM中的属性Rname数据长度改成40

(3) 删除表ROOM中的一个属性 Rarea

(4) 取消表PR的外键。

(5) 为表ROOM创建按R#降序排列的索引。

(6) 为表PERSON创建按P#升序排列的索引。

(7) 创建表PERSON的按Pname升序排列的唯一性索引。

(8) 取消表PERSONP#升序索引。 

--创建PERSON--

create table PERSON(

P# char(10) primary key,

Pname char(10),

Page int,

constraint age check(Page > 18)

)

 

--创建ROOM

create table ROOM(

R# char(10) primary key,

Rname char(10),

Rarea int

)

--创建PR--

create table PR(

P# char(10),

R# char(10),

DatePR nchar(10),

foreign key (P#) references PERSON(P#),

foreign key (R#) references ROOM(R#)

)

--显示创建的三个表--

select * from PERSON;

select * from ROOM;

select * from PR;

--在表PERSON表中添加Ptype属性--

alter table PERSON add Ptype char(10);

--删除PERSON表中年龄约束--

alter table PERSON drop constraint age;

--修改ROOM表中Rname的长度--

alter table ROOM alter column Rname char(40);

--显示创建的三个表--

select * from PERSON;

select * from ROOM;

select * from PR;

 

--删除ROOM表中的Rarea属性

alter table ROOM drop column Rarea;

--取消PR表的外键--

alter table PR drop constraint FK_PR_P#_3D5E1FD2;

alter table PR drop constraint FK_PR_R#_3E52440B;

--ROOM表创建按R#降序排列--

create index RO on ROOM(R# desc);

--PERSON表创建按P#升序排序的索引--

create index PE on PERSON(P#);

--创建表PERSON的按Pname升序排列的唯一性索引--

create unique index Pn on PERSON (Pname asc);

--取消表PERSON的按Pname升序排列的唯一性索引--

drop index PE ON PERSON;






你可能感兴趣的:(数据库)