"Database Principles and Applications" Course Experiment Report Definition and Maintenance of SQL Statement Database

Definition and Maintenance of the Experimental Report Database of the Course "Principle and Application of Database"

1. Experimental content

1. Basic operation experiment

(1) Use SQL Server Management Studio to establish a book lending database.
(2) View the properties of the book lending database in SQL Server Management Studio and modify it to make it meet the requirements.
(3) Through SQL Server Management Studio, create three tables of books, readers and loans in the built book lending database.
Its structure is:
books (book number, category, publisher, author, title, pricing);
readers ( Reader ID, name, unit, gender, phone);
borrowing (book number, reader ID, borrowing date, return date);
required to select the appropriate data type for the attribute, define the primary key of each table, Allow Null (allow empty Value) and
Default Value (default value) and other column-level data constraints.
(4) Establish the table-level constraints of the three tables of books, readers, and borrowing in SQL Server Management Studio: Primary Key (primary key) constraints of each table; the foreign key between the borrowing table and the book table, and between the borrowing table and the reader table (Foreign code) constraint, which requires that the outer code constraint expression is determined according to semantics, and then implemented by operation; the UNIQUE (uniqueness) constraint of the book number and reader number of the borrowing table is realized; the reader gender can only be "male" or " Female "check (check) constraints.
(5) Enter 5 records in each of the three tables: book, reader and loan in the book loan database. The required records not only meet the data constraint requirements, but also have related records between tables. You can also insert, delete, and modify data in the three tables of books, readers, and loans in the book lending database through the T-SQL language.

2. Improve operation experiment

Establish a database operation for students to choose courses, realized in SQL Server Management Studio.
The table structure in the database is:
students (student number, name, age, gender, department);
courses (course number, course name, credits, prior course);
elective courses (student number, course number, grade);
requirements:
(1 ) Build a database, build a table, and establish relationships between tables.
(2) Select the appropriate data type.
(3) Define the necessary column-level constraints and table-level constraints.
(4) The new query through SQL Server Management Studio realizes the data addition, data deletion and data modification operations on the student elective database.
Students, courses and electives are required to have more than 5 records each.

2. Experimental process

experiment one:

(1). Create a database
Insert picture description here
(2). Use sql statement to operate

--建立图书表
create table Book1
(
  B_num  varchar(20) primary key, --主键
  B_sort varchar(20) , 
  B_publish varchar(20),
  B_author varchar(20),
  B_name varchar(20) not null,
  B_price numeric(5,2) --小数  
  )
  drop table  Book1;
--建立读者表
  create table  Reader1
  (
  R_num varchar(20) primary key, --主键
  R_name varchar(5) not null,
  R_unit varchar(10),
  R_sex varchar(4) default '男'   not null check (R_sex='男' or R_sex ='女'),
  R_tel varchar(11) 
  )
 drop table reader1;
 --建立借阅表
 create table Borrow1
 (
   B_num  varchar(20) ,
   R_num varchar(20),
   LonaDate date  not null,
   ReturnDate  date  default '2000-01-01' ,--默认时间 
   primary key (B_num,R_num),
   foreign key(R_num) references Reader1(R_num),
   foreign key(B_num) references Book1(B_num)
 )
 drop table Borrow1 ;

 --查看内容
 select * from book1;
 select * from reader1;
 select * from Borrow1;

  --插入数据
 insert into  book1 values ('00000001','计算机','中国工信集团','谢希任','计算机网络',49.00),
                      ('00000002','计算机','科学出版社','白中英','计算机组成',47.56),
				    ('00000003','小说','广西师范大学出版社','肖恩·白塞尔','书店日记',35.46),
				    ('00000004','小说','湖南科学技术出版社','卡洛·罗韦利','时间的秩序',56.00),
				    ('00000005','小说','南海出版公司','塔拉·韦斯特弗','你当像鸟飞往你的山',49.00)


 insert into  Reader1 values('01','李蛋',' ','男',12345678901),
							('02','翠花','财务 ','男',12345678901),
							('03','狗子','行政','男',12345678901),
							('04','张三','销售','男',12345678901),
							('05','张伟','研发','男',12345678901)

insert into   Borrow1 values('00000004','03','2018-07-03',GETDATE()),
                            ('00000001','03','2018-07-09',GETDATE()),
							('00000003','01','2019-12-03',GETDATE()),
							('00000004','01','2018-06-03',GETDATE()),
							('00000003','05','2019-05-03',GETDATE()),--当前时间
							('00000003','02','2019-05-03',GETDATE())
insert into   Borrow1(B_num,R_num,LonaDate)  values('00000005','02','2018-07-03') --实现默认时间

--删除数据
delete  from  Borrow1  where   B_num='00000004' and R_num='03'; 
--修改数据
update borrow1 set   B_num ='00000001'  where   B_num='00000005' and R_num='02'; 

Experiment two:

--创建学生表
create table student
(
  s_num  int PRIMARYKEY,
  s_name varchar(20)not null,
  s_age int check(s_age between 10 and 35),
  s_sex varchar(10) check(s_sex in('男', '女')) default '男', 
  s_unit varchar(20) not null
)
--插入数据
insert into student values (1815001,'张三',18,'男','机械学院'),
                          (1815002,'李白',30,'女','生工学院'),
						  (1815003,'后裔',11,'男','数计学院'),
						  (1815004,'翠花',25,'女','物理学院'),
						  (1815005,'狗蛋',21,'男','机械学院');
--创建课程表
create table course
(
c_num int PRIMARYKEY,
c_name varchar(20) not null,
c_credit  int check(c_credit in (1,2,3,4,5,6)),
先行课 varchar(20) )
--插入数据
drop table course
insert into  course values (001,'C语言',2,001),
							(002,'程序设计电路',2,001),
							(003,'数字电路',3,001),
							(004,'计算机组成',3,003),
							(005,'计算机网络',4,001);

select * from course
--创建选课表
create table selectCourse
(
 s_num  int,
 c_num int,
 score numeric(3,1)  not null   ,
 primary key (s_num,c_num),
 foreign key ( s_num) references student(s_num),
 foreign key (c_num) references course(c_num),
)
drop table selectCourse
--插入数据
insert into  selectCourse values(1815001,001,2),
                          (1815001,002,2),
						(1815001,003,3),
						(1815002,001,2),
						(1815004,001,2)
select * from  Course
--数据增加
insert into student values (1815006,'二狗',16,'男','体育学院');
--修改数据
update course set c_credit=5 where c_name='C语言'

3. Experimental results

Experiment one:
1. Build the table and insert the data as shown below:
Insert picture description here
Insert picture description here
Insert picture description hereInsert picture description here
Insert picture description here

2. After data modification, as shown below:
Insert picture description here

Experiment two:
1. Build the table and insert the data as shown in Figures 6, 7, and 8 below:

Insert picture description here
Insert picture description here
Insert picture description here
2. After data modification, as shown below:

Insert picture description here
4. Experimental analysis
① Analyze the experimental results; the
experimental results are consistent with the theoretical results
② Explain the problems encountered in the experimental process and how to solve them.
1. Problem: Inserting data in a sentence, if you want to display a custom default value, the method shown in the following figure will not work, and it cannot
Insert picture description here
be achieved. Solution: You can only insert through specific columns of data
Insert picture description here

③Analyze the error;
everything must be operated according to SQL syntax; when inserting data, it must comply with column-level data constraints, otherwise an error will be reported

3. Summary of
the experiment The completion of the experiment is average, and the relevant grammar is not familiar, which leads to the slow speed of the experiment and cannot be completed quickly. In the following study, you must be familiar with the relevant grammar and strengthen your proficiency.

Published 10 original articles · won 12 · visited 1854

Guess you like

Origin blog.csdn.net/qq_44236958/article/details/105518791