2023年6月21日发(作者:)
sql数据库基础实例操作环境 win10+sql2016⽬的 加深对SQL语句的理解和⽅便以后的查询、复习例1 创建数据库在D盘的data⽂件夹下建⽴名为⾼校图书管理的数据库,主数据⽂件为⾼校图书管理 _data,初始容量为10MB,最⼤容量为50MB,增幅为10MB。⽇志⽂件为⾼校图书管理 _log,初始容量为5MB,最⼤容量为20MB,增幅为5MB。create database ⾼校图书管理on primary( name=⾼校图书管理_data, filename="D:data⾼校图书管理_", size=10MB, maxsize=50MB, filegrowth=10%)log on( name=⾼校图书管理_log, filename="D:data⾼校图书管理_", size=5MB, maxsize=20MB, filegrowth=5%)GO例2 创建基本表为⾼校图书管理数据库,创建读者类别表、读者表、图书表、借阅表。要求如下:读者类别包含的字段有类别编号、类别名称、可借阅天数、可借阅数量、超期罚款额,其中类别编号为主键,类别名称、可借阅天数、可借阅数量、超期罚款额不能为空。读者表包含的字段有读者卡号、姓名、性别、单位、办卡⽇期、卡状态、类别编号,其中卡号为主键,姓名、性别、单位、办卡⽇期、卡状态、类别编号不能为空。图书表包含的字段有图书编号、书名、类名、作者、出版社、出版⽇期、单价、库存数量,其中图书编号为主键,书名、类名、作者、出版社、出版⽇期、单价、库存数量不能为空。借阅表包含的字段有读者卡号、图书编号、借书⽇期、还书⽇期,其中读者卡号和图书编号为主键,且分别参照读者表的读者卡号和图书表的图书编号,借书⽇期、还书⽇期不能为空。use ⾼校图书管理gocreate table 读者类别( 类别编号 char(2) primary key, 类别名称 char(10) not null, 可借阅天数 tinyint not null, 可借阅数量 tinyint not null, 超期罚款额 smallmoney not null)gocreate table 读者( 读者卡号 char(10) primary key, 姓名 char(16) not null, 性别 char(1) not null default '男', 单位 char(30) not null, 办卡⽇期 date not null, 卡状态 char(5) not null, 类别编号 char(2), constraint c1 check(性别 in('男','⼥')), constraint c2 foreign key(类别编号) references 读者类别(类别编号))gocreate table 图书( 图书编号 char(8) primary key, 书名 char(40) not null, 类名 char(16) not null, 作者 char(16) not null, 出版社 char(20) not null, 出版⽇期 date, 单价 smallmoney not null, 库存数量 tinyint not null)gocreate table 借阅( 读者卡号 char(10), 图书编号 char(8), 借书⽇期 date not null, 还书⽇期 date, constraint c3 primary key(读者卡号,图书编号), constraint c4 foreign key(读者卡号) references 读者(读者卡号), constraint c5 foreign key(图书编号) references 图书(图书编号))go例3 修改基本表为⾼校图书管理数据库的读者表增加新的整型id,该列作为每⾏数据的标识,并且⾃动增加,初始值为1.在读者姓名⼀列增加唯⼀性约束约束名为unique_dzxm。Use ⾼校图书管理goalter table 读者add id int identity(1,1)goalter table 读者with nocheckadd constraint unique_dzxm unique(姓名)go假设读者类别表中未定义主键,将读者类别中的类型编号定义为主键alter table 读者类别 add primary key(类别编号)将读者表新增的id列删除alter table 读者drop column idgo若⼀列上有约束或默认值,则⽆法删除,必须先删除约束 ,例如姓名alter table 读者drop constraint unique_dzxmgoalter table 读者drop column 姓名go例4 索引在图书表的书名列上,创建⼀个名称为idx_sm的唯⼀索引,并依据书名字段进⾏升序排序Use ⾼校图书管理goalter table 读者add id int identity(1,1)goalter table 读者with nocheckadd constraint unique_dzxm unique(姓名)go在读者表的姓名和性别上,创建⼀个idx_xmxb的唯⼀⾮聚集复合索引,并根据姓名升序,性别降序进⾏排序create unique nonclustered index idx_xmxb on 读者(姓名,性别desc)
2023年6月21日发(作者:)
sql数据库基础实例操作环境 win10+sql2016⽬的 加深对SQL语句的理解和⽅便以后的查询、复习例1 创建数据库在D盘的data⽂件夹下建⽴名为⾼校图书管理的数据库,主数据⽂件为⾼校图书管理 _data,初始容量为10MB,最⼤容量为50MB,增幅为10MB。⽇志⽂件为⾼校图书管理 _log,初始容量为5MB,最⼤容量为20MB,增幅为5MB。create database ⾼校图书管理on primary( name=⾼校图书管理_data, filename="D:data⾼校图书管理_", size=10MB, maxsize=50MB, filegrowth=10%)log on( name=⾼校图书管理_log, filename="D:data⾼校图书管理_", size=5MB, maxsize=20MB, filegrowth=5%)GO例2 创建基本表为⾼校图书管理数据库,创建读者类别表、读者表、图书表、借阅表。要求如下:读者类别包含的字段有类别编号、类别名称、可借阅天数、可借阅数量、超期罚款额,其中类别编号为主键,类别名称、可借阅天数、可借阅数量、超期罚款额不能为空。读者表包含的字段有读者卡号、姓名、性别、单位、办卡⽇期、卡状态、类别编号,其中卡号为主键,姓名、性别、单位、办卡⽇期、卡状态、类别编号不能为空。图书表包含的字段有图书编号、书名、类名、作者、出版社、出版⽇期、单价、库存数量,其中图书编号为主键,书名、类名、作者、出版社、出版⽇期、单价、库存数量不能为空。借阅表包含的字段有读者卡号、图书编号、借书⽇期、还书⽇期,其中读者卡号和图书编号为主键,且分别参照读者表的读者卡号和图书表的图书编号,借书⽇期、还书⽇期不能为空。use ⾼校图书管理gocreate table 读者类别( 类别编号 char(2) primary key, 类别名称 char(10) not null, 可借阅天数 tinyint not null, 可借阅数量 tinyint not null, 超期罚款额 smallmoney not null)gocreate table 读者( 读者卡号 char(10) primary key, 姓名 char(16) not null, 性别 char(1) not null default '男', 单位 char(30) not null, 办卡⽇期 date not null, 卡状态 char(5) not null, 类别编号 char(2), constraint c1 check(性别 in('男','⼥')), constraint c2 foreign key(类别编号) references 读者类别(类别编号))gocreate table 图书( 图书编号 char(8) primary key, 书名 char(40) not null, 类名 char(16) not null, 作者 char(16) not null, 出版社 char(20) not null, 出版⽇期 date, 单价 smallmoney not null, 库存数量 tinyint not null)gocreate table 借阅( 读者卡号 char(10), 图书编号 char(8), 借书⽇期 date not null, 还书⽇期 date, constraint c3 primary key(读者卡号,图书编号), constraint c4 foreign key(读者卡号) references 读者(读者卡号), constraint c5 foreign key(图书编号) references 图书(图书编号))go例3 修改基本表为⾼校图书管理数据库的读者表增加新的整型id,该列作为每⾏数据的标识,并且⾃动增加,初始值为1.在读者姓名⼀列增加唯⼀性约束约束名为unique_dzxm。Use ⾼校图书管理goalter table 读者add id int identity(1,1)goalter table 读者with nocheckadd constraint unique_dzxm unique(姓名)go假设读者类别表中未定义主键,将读者类别中的类型编号定义为主键alter table 读者类别 add primary key(类别编号)将读者表新增的id列删除alter table 读者drop column idgo若⼀列上有约束或默认值,则⽆法删除,必须先删除约束 ,例如姓名alter table 读者drop constraint unique_dzxmgoalter table 读者drop column 姓名go例4 索引在图书表的书名列上,创建⼀个名称为idx_sm的唯⼀索引,并依据书名字段进⾏升序排序Use ⾼校图书管理goalter table 读者add id int identity(1,1)goalter table 读者with nocheckadd constraint unique_dzxm unique(姓名)go在读者表的姓名和性别上,创建⼀个idx_xmxb的唯⼀⾮聚集复合索引,并根据姓名升序,性别降序进⾏排序create unique nonclustered index idx_xmxb on 读者(姓名,性别desc)
发布评论