sQl表建立

使用T-sql指令完成以下操作
1、用sql指令创建数据库xsbook,该数据库包含1个数
据文件和1个日志文件,其属性分别如下:
数据文件属性:
a 逻辑名按规定方式命名
b 物理名按规定方式命名
c 初始值2MB
d 最大值20MB
e 增长方式:10%
日志文件属性:
a 逻辑名按规定方式命名
b 物理名按规定方式命名
c 初始值1MB
d 最大值不限制大小
e 增长方式:1MB
2 在xsBook数据库中创建xs表,学生表由5个字段组

字段名 含义 类型 宽度 null情况
xsID 学号 char 6 not null 主

xsName 姓名 char 8 not null
xsSex 性别 bit not null
xsBirth 出生日期 datetime null
xsAddress 籍贯 nchar(60) null
说明:约束名大家自己取名,最好符合系统的命名规

3 对xs表作以下修改
(1)将xsName的宽度更改为char(16)
(2)对xs表增加一个新的字段xs_photo (照片) 类型
为image 可以为Null
(3)删除xs表的字段xs_photo。
(4)在xs表的xsID列上添加主键约束PK_xs。
(5)在xs表的xsName列上添加唯一约束UQ_xs_xsName

(6)删除第(5)题的唯一约束。
4 向学生表中插入5条记录,并用select查询记录情

5 在xsbook数据库中创建book表,图书表由以下字段
组成
字段名 含义 类型 宽度 null情
况 约束情况 说明
bookID 图书编号 char 6 not null

bookName 课程名称 nchar 50 not null

Author 作者 nchar 20 not null

Press 出版社 nvarchar 30 not
null
Price 价格 int null

6 对book表做以下修改
(1)对book表添加主键约束,约束对应的字段为
bookID.
(2)修改book表的kcAuthor的宽度为nchar(30)
(3)修改book表的kcPrice为not null
(4)在book表中添加默认值约束,默认值为"人民邮电
出版社",对应的字段为Press。
(5)在book表中添加Check约束,要求价格在5--100之
间,对应的字段为price。
7 向kc表中添加5条记录,并查询相关记录

--创建数据库xsbook

CREATE DATABASE xsbook

ON  PRIMARY 

(NAME = N'xsbook_data',

FILENAME = N'D:\xsbook_data.mdf' ,

SIZE = 2MB,

MAXSIZE = 20MB,

FILEGROWTH = 10%

)

LOG ON 

(NAME = N'xsbook_log',

FILENAME = N'D:\xsbook_log.ldf' ,

SIZE = 1MB,

MAXSIZE = UNLIMITED,

FILEGROWTH = 1MB

)

GO


--创建表

create table xs表

(

 xsID char(6) not null CONSTRAINT FK_xsid primary key,

 xsName char(8) not null,

 xsSex bit not null, 

 xsBirth datetime null,

 xsAddress nchar(60) null 

)


--将xsName的宽度更改为char(16)

alter table xs表

alter column xsName char(16) not null;


--对xs表增加一个新的字段xs_photo (照片) 类型为image 可以为Null

alter table xs表

add xs_photo image null;


--删除xs表的字段xs_photo

alter table xs表

drop column xs_photo;


--在xs表的xsID列上添加主键约束PK_xs

alter table xs表

drop constraint FK_xsid;

alter table xs表

add constraint PK_xs primary key(xsID);


--在xs表的xsName列上添加唯一约束UQ_xs_xsName

alter table xs表

add constraint UQ_xs_xsName unique(xsName);


--删除第(5)题的唯一约束

alter table xs表

drop constraint UQ_xs_xsName;


--向学生表中插入5条记录

insert into xs表

values('101','张一',1,'1990-09-01','江苏1');

insert into xs表

values('102','张二',0,'1990-09-02','江苏2')

insert into xs表

values('103','张三',1,'1990-09-03','江苏3')

insert into xs表

values('104','张四',0,'1990-09-04','江苏4')

insert into xs表

values('105','张五',1,'1990-09-05','江苏5');


--用select查询记录

select * from xs表;


--***********************************

--创建表

create table book表

(

bookID char(6) not null,

bookName nchar(50) not null,

Author nchar(20) not null,

Press nvarchar(30) not null , 

Price int null   

)

--对book表添加主键约束,约束对应的字段为bookID.

alter table book表

add constraint FK_bookid primary key(bookID);


--修改book表的kcAuthor的宽度为nchar(30)

alter table book表

alter column Author nchar(30) not null;


--修改book表的kcPrice为not null

alter table book表

alter column Price int not null;


--在book表中添加默认值约束,默认值为"人民邮电出版社",对应的字段为Press

alter table book表

add constraint DF_press default('人民邮电出版社') for Press;


--在book表中添加Check约束,要求价格在5--100之间,对应的字段为price

alter table book表

add constraint CK_price check(Price between 5 and 100);


--向kc表中添加5条记录

insert into book表

values('001','xiyouji1','wuchengen','beijingchubanshe1',60);

insert into book表

values('002','xiyouji2','wuchengen','beijingchubanshe2',61);

insert into book表

values('003','xiyouji3','wuchengen','beijingchubanshe3',62);

insert into book表

values('004','xiyouji4','wuchengen','beijingchubanshe4',63);

insert into book表

values('005','xiyouji5','wuchengen','beijingchubanshe5',64);


--查询相关记录

select * from book表


温馨提示:答案为网友推荐,仅供参考
相似回答