1, this title uses the following three relational tables:
CARD(CNO,NAME,CLASS)
Description: card: a library card cno: Card name: Name of class: Class
BOOKS(BNO, BNAME, AUTHOR, PRICE, QUANTITY)
Description: books: books bno: ISBN bname: Title author: Author price: Price quantity: Inventory number of books
BORROW(CNO, BNO, RDATE)
Description: borrow: borrowing record cno: library card number bno: ISBN rdate: due date
NOTE: defining each person can borrow a book; stock with the number of library books, books also changed.
Query data required:
(1) identify the library over five readers, output library card number and the number of books borrowed books.
select cno,count(bno) as 册数 from BORROW group by cno having count(bno)>5
(2) query overdue books, output borrower (card number), ISBN and due date.
SELECT CNO AS card, BNO AS ISBN, the rdate AS due date from BORROW WHERE borrow.rdate < GETDATE ()
(2) query includes the title "Network" keyword books, output ISBN, title, author.
select bno,bname,author from books where bname like '%网络%'
(4) the highest price query existing books in books, output title and author.
select bname,author from books where price=(select MAX(price) from books)
2, there is a [student program] database, the database consists of three tables:
Student Table: Student from the Student ID (Sno), name (Sname), gender (Ssex), Age (Sage), where the Department (Sdept) five attributes, denoted: Student (Sno, Sname, Ssex, Sage, Sdept ), Sno for the keyword.
Curriculum: Course by the course number (Cno), course name (Cname), Prerequisite number (Cpno), credits (Ccredit) four attributes, denoted: Course (Cno, Cname, Cpno, Ccredit) Cno critical word.
Results table: SG by the student number (Sno), course number (Cno), performance (Grade) three attributes, denoted by: SG (Sno, Cno, Grade) (SNO, CNO) as a keyword.
The following functions use the SQL language:
(1) establishing student table [Student], wherein the number of property can not be empty learning, and its value is unique.
create table Student ( Sno char(5) primary key, Sname char(10), Ssex char(2) default '男' check (Ssex in ('男','女')), Sage int, Sdept varchar(10) )
(2) query test scores of students have failed to learn numbers.
select distinct Sno from SG where Grade<60
(3) the number 05001 school student's age was changed to 22 years old.
update T_Student set Sage=22 where Sno='05001'
(4) No. 1 course students calculate grade point average.
select AVG(Grade) from SG group by Cno having Cno=1
(5) Create a stored procedure [getDetailByName], "does not exist for this student" by entering the parameter student's name (such as "Joe Smith"), filter out basic information about the student, the student's name on the value of this input does not exist, print information.
create proc getDetailByName @name char(10) as if exists(select * from Student where Sname=@name) begin select * from Student where Sname=@name end else begin print '不存在此学生' end exec getDetailByName 'xxx'
3, an existing relational database as follows:
Database name: Database teachers
Teacher table (number, name, sex, nationality, job title, ID number)
Curriculum (course number, name)
Classroom table (ID, number of teachers, class number, the number of hours)
The following functions use the SQL language:
(1) create the above three tables, required: a primary key (.. Teachers table number, curriculum lesson number), foreign key (.. Classroom table teacher numbers, classroom table class number), default (nation), non-empty ( nation, name), the only (ID number), check (gender, number of hours), automatic number (ID)
the Create the Table Teacher table ( number char ( 10 ) Primary Key , name char ( 10 ) not null , sex char ( 2 ) default ' male ' the Check (sex in ( ' male ' , ' female ' )), the national char ( 10 ) default ' Han ' not null , Title char ( 10 ), ID number char ( 20 is ) UNIQUE )
the Create the Table curriculum ( class number char ( 10 ) Primary Key , name char ( 20 ) not null )
the Create the Table classroom table ( ID int the Identity ( 1 , 1 ), teacher numbers char ( 10 ) the References teachers table (number), class number char ( 10 ) the References curriculum (course number), number of hours int not null the Check (number of hours BETWEEN 0 and 200 is ) )
(2) Add the following information to the course curriculum of the code
Course No. Course Title
100001 SQL Server database
100002 Data Structure
100003 VB Programming
The SET IDENTITY_INSERT curriculum ON INSERT INTO curriculum (course number, name) values ( 100001 , ' SQL Server database ' ) INSERT INTO curriculum (course number, name) values ( 100002 , ' data structures ' ) INSERT INTO curriculum (lessons number, name) values ( 100003 , ' VB programming ' )
(3) create a classroom table view, teacher inquiry number, name, course number, course name and number of hours information. Creating inline table-valued function to retrieve all the "SQL Server Database" the name of the teacher of this course;
the Create View V_TeacherInfo as the SELECT t. number, t.'s name, course. Lesson number, course. name, class. number of hours from classroom table as class join the curriculum as Course, ON class. Lesson number = Course,. Lesson No. join teachers' table as t ON class. teachers number = t. number
the Create function fn_CourseTeacher ( @course char ( 200 )) returns A the Table AS return ( the SELECT curriculum. name, teacher table name from classroom table join teachers' table on classroom table. Teachers Number = teacher table. Number join a curriculum on the curriculum. Lesson number = classroom table. Lesson number where the curriculum. name = @course ) Go the SELECT * from dbo.fn_CourseTeacher ( ' SQL Server database ' )
(4) create a stored procedure to calculate the total hours of a substitute teacher, and the value returned.
Execution: computing "Guo" total class.
Create proc proc_TeachTime the @name char ( 10 ), @time int Output AS SELECT @time = SUM (classroom table number of hours) from classroom table join teachers table on classroom table. teacher number = the @name Go DECLARE @time int Exec proc_TeachTime ' Guo ' , @time the Output Print @time