Database first experiment

La la la la la, the database is finally going to be tested. I am very worried that it will not be done well. What should I do if it fails? It
is just my homework, and there may be problems. Welcome to come and correct me


1. Topic (100 points)

1. Create the 6 tables given later (20 points)
2. Create constraints in different ways; (5 points)
3. View and delete constraints; (5 points)
4. Create and delete default and rules (5 points) )
5. Master the characteristics and usage of primary key constraints; (5 points)
6. Master the usage of unique constraints; (5 points)
7. Master the usage of default constraints and default objects; (5 points)
8. Master CHECK constraints and rules Usage of objects; (5 points)
9. Master the method of using primary key and foreign key constraints to achieve referential integrity (5 points)
10. Add a field; (5 points)
11. Delete a field; (5 points)
12 , Add a constraint; (5 points)
Thirteen, modify the data type of the field (5 points)
Fourteen, create an index; (5 points)
fifteen, rebuild the index (5 points)
sixteen, create a view; (5 points)
Seventeen, delete the view (5 points)

insert image description here
insert image description here

insert image description here

insert image description here
insert image description hereinsert image description here

insert image description here

2. Some basic knowledge

2.1 Five constraints in the database and how to add them

Five constraints

1. Primary key constraint (Primay Key Coustraint) uniqueness, non-nullness
2 Unique constraint (Unique Counstraint) uniqueness, can be empty, but there can only be one
3. Check constraint (Check Counstraint) The scope and format of the column data Restrictions (such as: age, gender, etc.)
4. Default constraints (Default Counstraint) The default value of the data
5. Foreign key constraints (Foreign Key Counstraint) need to establish a relationship between the two tables and refer to the columns of the main table

Syntax Example for Big Five Constraints

1.添加主键约束(将stuNo作为主键)
alter table stuInfo 
add constraint PK_stuNo primary key (stuNo) 
2.添加唯一约束(身份证号唯一,因为每个人的都不一样) 
alter table stuInfo 
add constraint UQ_stuID unique(stuID) 
3.添加默认约束(如果地址不填 默认为“地址不详”) 
alter table stuInfo 
add constraint DF_stuAddress default (‘地址不详’) for stuAddress 
4.添加检查约束 (对年龄加以限定 15-40岁之间) 
alter table stuInfo 
add constraint CK_stuAge check (stuAge between 15 and 40)  
alter table stuInfo 
add constraint CK_stuSex check (stuSex=’男’ or stuSex=’女′)  
5.添加外键约束 (主表stuInfo和从表stuMarks建立关系,关联字段stuNo)  
alter table stuInfo 
add constraint FK_stuNo foreign key(stuNo)references stuinfo(stuNo)  

2.2 View and delete constraints

--查看约束

1. 查看user表的约束信息

select * from user_constraints WHERE TABLE_NAME='USERS';
2. 查看约束列上的信息

select * from WHERE user_cons_columns TABLE_NAME='USERS';


--删除约束
ALTER TABLE 表名 DROP CONSTRAINT 约束名

2.3 Add a new field

alter  TABLE  t_Execution(表名)  
add  FIsModifyQuote(列名)  int(1)(字段的属性和长度)  not  NULL(设置非空)  DEFAULT  0(默认值设置为0)

2.4 Delete a field

alter table [表名] drop 字段名

2.5 Modify the data type of a field

alter table <表名> alter column <列名> type <type>;

Sometimes a coercion is used

alter table <table name> alter column <column name> type <type> using <column name> :: <type>;

2.6 Create an index

Classification of Index

Unique index (UNIQUE): The index value of each row is unique (create a unique constraint, the system will automatically create a unique index)
Primary key index: When the primary key column specified when creating the table, the primary key index will be automatically created and has a unique characteristic.
Clustered index (CLUSTERED): The clustered index is equivalent to the pinyin lookup using the dictionary, because the clustered index storage records are physically continuous, that is, the pinyin a must be followed by b.
Non-clustered index (NONCLUSTERED): The non-clustered index is equivalent to the radical lookup using the dictionary. The non-clustered index is logically continuous, and the physical storage is not continuous.
PS: There can only be one clustered index for a table, but multiple non-clustered indexes can exist for a table.

UNIQUE: Create a unique index for a table or view. A unique index does not allow two rows to have the same index key value. The view's clustered index must be unique. If the column to be uniquely indexed has duplicate values, the duplicate values ​​must be deleted first.
CLUSTERED: Indicates that the specified created index is a clustered index. When an index is created, the logical order of the key values ​​determines the physical order of the corresponding rows in the table. The bottom layer (or leaf level) of the clustered index contains the actual data rows for the table.
NONCLUSTERED: Indicates that the specified created index is a non-clustered index. Creates an index specifying a logical ordering of the table. For nonclustered indexes, the physical ordering of data rows is independent of the index ordering.

CREATE INDEX 索引名 ON 表名(列名);

joint index

CREATE INDEX 索引名 ON 表名(列名1, 列名2, 列名3, ...);

2.7 Delete index

DROP INDEX 索引名;

View all indexes of a table

SELECT * FROM ALL_INDEXES WHERE TABLE_NAME = '表名'

2.8 Rebuild Index

DBCC DBREINDEX('Table'')

2.9 Creating Views

CREATE VIEW 视图名称
 AS
    SELECT column_name(s) FROM table_name 
WHERE condition

2.10 View view

select * from 视图名

2.11 Delete view

drop view 视图名

3. My junk code (for reference only)

--参照完整性:若属性(或属性组)F是基本关系R的外码,它与基本关系S的主码Ks相对应,
--则对于R中每个元组在F上的值必须:或者取空值(F的每个属性组均为空值),或者等于S中某个元组的主码值。

--提示:定义约束格式为
--constraint  约束名称  约束类型  (列)
--如constraint  pk_id  primary  key  clustered  (id))


--ALTER TABLE 表名 DROP PRIMARY KEY; -- 这种方式才对,不用加主键名字是因为一张表中只有一个主键
--ALTER TABLE stu DROP INDEX phone_number; -- 删除唯一约束的真正语法
--later table 表名 add constraint 外键名 foreign key (外键字段列) referenes 主表名称(主表列名称);
---- 删除外键约束    ALTER TABLE employee DROP FOREIGN KEY emp_dep_fk;


--SQLServer 中有五种约束,* Primary Key 约束、 *Foreign Key 约束、* Unique 约束、 Default 约束和* Check 约束,






--为了可以现场演示,这里写一个可以删掉刚创建的【读者信息表】,然后就可以重复演示了
drop table 读者信息表



--创建第一个表:读者信息表

create table 读者信息表
(
	借书证号	int			not null unique,		--主码,不能为空
	姓名		char(10)	not null,				--名字给到10个字符,不能为空
	性别		char(2)		not null,				--性别给到2个字符,表中没有空
	出生日期	date		not null,				--这是数据库特有的变量,不能为空
	借书量		smallint,							--看起来都很小,虽然应该不能为空,但表中有空
	工作单位	char(30)	not null,				--工作单位可能比较长
	电话		char(10)	not null,				
	E_mail		char(20)	not null				--给多一点,不能直接写E-mail,只好写E_mail了

	 --指定借书证号为主键值,并且创建一个聚簇索引
	constraint reader_rno primary key clustered (借书证号) , 
	 --输入性别字段值时,只能接受“男”或者“女”
	constraint chkoo_rsex check(性别 in ('男','女')), 
	--为rphone字段创建检查约束,限制只能输入类似85860126之类的数据,而不能随意输入其他数据
	constraint check_phone check
	(电话 like '[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]')  , 

)



insert into 读者信息表 values ('29307142','张晓露','女','1989-02-1','2','管理信息系','85860126','[email protected]');
insert into 读者信息表 values ('36405216','李阳','男','1988-12-26', '1','航海系','85860729','[email protected]');
insert into 读者信息表 values ('28308208','王新全','男','1988-04-25','1','人文艺术系','85860618','[email protected]');
insert into 读者信息表 values ('16406236','张继刚','男','1989-08-18','1','轮机工程系','85860913','[email protected]');
insert into 读者信息表(借书证号,姓名,性别,出生日期,工作单位,电话,E_mail) values ('16406247','顾一帆','男','1981-12-30', '轮机工程系','85860916','[email protected]');

--检查一下表的内容
select * from 读者信息表

--但是奇怪的是,插入后,没有按照我插入的顺序,而是按照了借书证号的首字符排序










--为了可以现场演示,这里写一个可以删掉刚创建的【借还明细表】,然后就可以重复演示了
drop table 借还明细表



--创建第二个表:借还明细表
create table 借还明细表
(
	借书证号	int			not null,
	图书编号	char(10)	not null,	--可以看到有0开头的数据,所以用char
	借还		char(2)		not null,	--借或者还
	借书日期	date		not null,	--同理
	还书日期	date,					--可以为空
	数量		smallint	not null,	--很小的数字
	工号		char(10)	not null	--带着0开头的数据
	foreign key(借书证号) references 读者信息表(借书证号),   --外码
	constraint chk_bor check(借还 in ('借','还')), 
)



--插入数据
insert into 借还明细表 values ('29307142','07108667','还','2008-03-28','2008-04-14','1','002016');
insert into 借还明细表 (借书证号,图书编号,借还,借书日期,数量,工号) values ('29307142','99011818','借','2008-04-27','1','002016');
insert into 借还明细表 (借书证号,图书编号,借还,借书日期,数量,工号) values ('36405216','07410802','借','2008-04-27','1','002018');
insert into 借还明细表 (借书证号,图书编号,借还,借书日期,数量,工号) values ('29307142','07410298','借','2008-04-28','1','002018');
insert into 借还明细表 values ('36405216','00000746','还','2008-04-29','2008-05-09','1','002016');
insert into 借还明细表 (借书证号,图书编号,借还,借书日期,数量,工号) values ('28308208','07410139','借','2008-05-10','1','002019');
insert into 借还明细表 (借书证号,图书编号,借还,借书日期,数量,工号) values ('16406236','07410139','借','2008-05-11','1','002017');



--确认数据
select *from 借还明细表



--更怪了,这次的顺序是对的,没有按照借书证号排序












--为了可以现场演示,这里写一个可以删掉刚创建的【图书类别】,然后就可以重复演示了
drop table 图书类别


--创建第三个表:图书类别
create table 图书类别
(
	类别号		char(10)	not null,	--不确定有多少,就先给10吧
	图书类别	char(20)	not null	--名字可能会很长
)

--插入数据
insert into 图书类别 values('H31','英语');
insert into 图书类别 values('I267','当代作品');
insert into 图书类别 values('TP312','程序语言');
insert into 图书类别 values('TP393','计算机网络');
insert into 图书类别 values('U66','船舶工程');



--确认数据
select * from 图书类别

--这次的数据也对了,难道是因为主码的原因吗












--为了可以现场演示,这里写一个可以删掉刚创建的【图书借阅明细表】,然后就可以重复演示了
drop table 图书借阅明细表


--创建第四个表
create table 图书借阅明细表
(
	图书编号	char(10)	not null,	--可以看到有0开头的数据,所以用char
	图书名称	char(30)	not null,	--可能很长的名字
	借书证号	char(20)	not null,
	借出日期	date		not null,		--不能为空
	归还日期	date,		--可能为空
	库存数		int			--不知道有多少
)


--插入数据

insert into 图书借阅明细表 (图书编号,图书名称,借书证号,借出日期,库存数) values('99011818','文化苦旅','29307142','2008-04-27','14');
insert into 图书借阅明细表 (图书编号,图书名称,借书证号,借出日期,库存数) values('07410802','航海英语','36405216','2008-04-27','24');
insert into 图书借阅明细表 (图书编号,图书名称,借书证号,借出日期,库存数) values('07410298','C++程序设计语言','29307142','2008-04-28','14');
insert into 图书借阅明细表 (图书编号,图书名称,借书证号,借出日期,库存数) values('07410139','艺海潮音','28308208','2008-05-10','18');
insert into 图书借阅明细表 (图书编号,图书名称,借书证号,借出日期,库存数) values('07410139','艺海潮音','16406236','2008-05-11','17');


--查看数据
select * from 图书借阅明细表


--这次也对的,我迷茫了







--为了可以现场演示,这里写一个可以删掉刚创建的【工作人员】,然后就可以重复演示了
drop table 工作人员


--创建第五个表:工作人员

create table 工作人员
(
	工号		char(10)	not null,	--带着0开头的数据
	姓名		char(10)	not null,	--给10个字符
	性别		char(2)		not null,	--性别为两个字符
	出生日期	date		not null,	--不能为0
	联系电话	char(10)	not null,	--感觉用int也可以
	E_mail		char(20)	not null,	--给多一点,不能直接写E-mail,只好写E_mail了
  	--为rphone字段创建检查约束,限制只能输入类似85860126之类的数据,而不能随意输入其他数据
	constraint choo_wphone check
	(联系电话 like '[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]') 

)


--插入数据
insert into 工作人员 values('002016','周学飞','男','1971-05-03','85860715','[email protected]');
insert into 工作人员 values('002017','李晓静','女','1979-09-15','85860716','[email protected]');
insert into 工作人员 values('002018','顾彬','男','1972-04-25','85860717','[email protected]');
insert into 工作人员 values('002019','陈欣','女','1968-11-03','85860718','[email protected]');


--查看数据
select * from 工作人员

--现在顺序是对的










--为了可以现场演示,这里写一个可以删掉刚创建的【图书明细表】,然后就可以重复演示了
drop table 图书明细表

--创建第六个表:图书明细表

create table 图书明细表
(
	类别号		char(10)	not null,
	图书编号	char(10)	not null,
	图书名称	char(30)	not null,
	作者		char(10),
	出版社		char(30),
	定价		char(10),
	购进日期	date,
	购入数		int,
	复本数		int,
	库存数		int
)


--插入数据
insert into 图书明细表 values('I267','99011818','文化苦旅','余秋雨','知识出版社','16','2000-03-19',8,15,14);
insert into 图书明细表 values('TP312','00000476','Delphi高级开发指南','坎图','电子工业出版社','80','2000-03-19',15,15,15);
insert into 图书明细表 values('U66','01058589','船舶制造基础','杨敏','国防工业出版社','19','2001-07-15',20,20,20);
insert into 图书明细表 values('I267','07410139','艺海潮音','李叔','江苏文艺出版社','19','2007-04-12',15,20,18);
insert into 图书明细表 values('TP312','07410298','C++程序设计','成颖','东南大学出版社','38','2007-05-08',10,15,14);
insert into 图书明细表 values('H31','07410802','航海英语','陈宏权','武汉工业大学出版社','42','2007-10-20',25,25,24);
insert into 图书明细表 values('H31','07108667','大学英语学习辅导','姜丽蓉','北京理工大学出版社','23.5','2008-02-06',25,25,25);
insert into 图书明细表 values('TP393','07410810','网络工程实用教程','汪新民','北京大学出版社','34.8','2008-08-21',10,15,15);


--查看数据
select * from 图书明细表




--增加一个字段
alter table 读者信息表 add rage smallint;   --增加“年龄”列,smallint
--删除一个字段
alter table 读者信息表 drop column rage ;   --删除“年龄”列
--删除约束
alter table 读者信息表 drop CONSTRAINT reader_rno;  --删除rno的unique约束
--alter table reader drop CONSTRAINT rname
--修改字段的数据类型
alter table 读者信息表 ALTER COLUMN 借书量 int;      --将借书量修改为int

--创建索引
create unique index Rearno on 读者信息表(借书证号);  --创建唯一索引
create clustered index Boobname on 图书明细表(图书名称);  --创建聚簇索引

drop index Rearon;  --删除索引
drop index Boobname;  --删除索引


--创建视图
go
create view readerview
as
select 姓名,出生日期 from 读者信息表;
go

--查看当前视图
select * from readerview

--删除视图
drop view readerview;   

Guess you like

Origin blog.csdn.net/weixin_62529383/article/details/130142455